Skip to content

Commit 0fde948

Browse files
committed
Check for more acceptable returnType names for case of generic return on IsValidCreateMethod
1 parent 84ee2fd commit 0fde948

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

CodeComplianceTest_Engine/Query/Checks/IsValidCreateMethod.cs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,72 @@ public static Span IsValidCreateMethod(this MethodDeclarationSyntax node)
4747
return null;
4848

4949
string filePath = node.SyntaxTree.FilePath;
50+
51+
foreach (string returnType in ReturnTypeCandidates(node))
52+
{
53+
string fileName = "";
54+
55+
if (!string.IsNullOrEmpty(filePath))
56+
{
57+
fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
58+
if (Regex.Match(returnType, $"((List|IEnumerable)<)?I?{fileName}(<.*>)?>?$").Success)
59+
return null; //File name matches return type exactly so this is valid. IsValidCreateMethodName will check if the method name matches the file name
60+
}
61+
62+
//If file name does not exactly match the return type then we need to check if the return type is in a sub-folder in create
63+
64+
List<string> pathSplit = filePath.Split(System.IO.Path.DirectorySeparatorChar).ToList();
65+
int createIndex = pathSplit.IndexOf("Create");
66+
if (createIndex == -1)
67+
return node.Identifier.Span.ToSpan(); //Evidently this create method isn't working for some reason - even though it should but this is as a protection/precaution
68+
69+
try
70+
{
71+
if (Regex.Match(returnType, $"((List|IEnumerable)<)?I?{pathSplit[createIndex + 1]}(<.*>)?>?$").Success || Regex.Match(returnType, $"((List|IEnumerable)<)?I?{pathSplit[createIndex + 2]}(<.*>)?>?$").Success)
72+
return null; //The folder path after the 'Create' folder matches the return type so this is valid. IsValidCreateMethodName will check if the method name matches the file name
73+
}
74+
catch
75+
{
76+
//In case createIndex + 1 || createIndex + 2 result in an out of bounds error - it means the check has failed and something isn't compliant so can pass through to returning the span
77+
}
78+
}
79+
return node.Identifier.Span.ToSpan(); //Create method file (name/path) and return type do not match as required
80+
}
81+
82+
private static List<string> ReturnTypeCandidates(this MethodDeclarationSyntax node)
83+
{
84+
List<string> returnTypeNames = new List<string>();
85+
5086
var type = node.ReturnType;
5187
if (type is QualifiedNameSyntax)
5288
type = ((QualifiedNameSyntax)type).Right;
53-
89+
5490
string returnType = type.ToString();
55-
string fileName = "";
5691

57-
if(!string.IsNullOrEmpty(filePath))
92+
returnTypeNames.Add(returnType);
93+
94+
//Handle the case where the method returns a generic type.
95+
//Then check the constraints of the generic type, if any of them matches the file name, then that is also acceptable
96+
if (node.ConstraintClauses.Count != 0)
5897
{
59-
fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
60-
if(Regex.Match(returnType, $"((List|IEnumerable)<)?I?{fileName}(<.*>)?>?$").Success)
61-
return null; //File name matches return type exactly so this is valid. IsValidCreateMethodName will check if the method name matches the file name
62-
}
98+
foreach (var constraintClause in node.ConstraintClauses)
99+
{
100+
string constraintString = constraintClause.ToString(); //where T : XXX, YYY
101+
string[] split = constraintString.Split(':');
102+
if (split.Length != 2)
103+
continue;
63104

64-
//If file name does not exactly match the return type then we need to check if the return type is in a sub-folder in create
105+
string target = split[0].Trim().Split(' ').Last().Trim(); //Split with space to get rid of where
65106

66-
List<string> pathSplit = filePath.Split(System.IO.Path.DirectorySeparatorChar).ToList();
67-
int createIndex = pathSplit.IndexOf("Create");
68-
if (createIndex == -1)
69-
return node.Identifier.Span.ToSpan(); //Evidently this create method isn't working for some reason - even though it should but this is as a protection/precaution
107+
if (target != returnType)
108+
continue;
70109

71-
try
72-
{
73-
if (Regex.Match(returnType, $"((List|IEnumerable)<)?I?{pathSplit[createIndex + 1]}(<.*>)?>?$").Success || Regex.Match(returnType, $"((List|IEnumerable)<)?I?{pathSplit[createIndex + 2]}(<.*>)?>?$").Success)
74-
return null; //The folder path after the 'Create' folder matches the return type so this is valid. IsValidCreateMethodName will check if the method name matches the file name
75-
}
76-
catch
77-
{
78-
//In case createIndex + 1 || createIndex + 2 result in an out of bounds error - it means the check has failed and something isn't compliant so can pass through to returning the span
110+
returnTypeNames.AddRange(split[1].Split(',').Select(x => x.Trim()));
111+
}
79112
}
80113

81-
return node.Identifier.Span.ToSpan(); //Create method file (name/path) and return type do not match as required
114+
115+
return returnTypeNames;
82116
}
83117
}
84118
}

0 commit comments

Comments
 (0)