Skip to content

Commit ace1be2

Browse files
committed
Simplify the code
Instead of explicitly checking some specific folder and specific indices simply loop backwards from the filename to the create folder and check if one of the paths is a match. Avoid the copy pasting of regex, and constant index out of bounds exceptions being thrown.
1 parent 0fde948 commit ace1be2

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

CodeComplianceTest_Engine/Query/Checks/IsValidCreateMethod.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,23 @@ public static Span IsValidCreateMethod(this MethodDeclarationSyntax node)
4848

4949
string filePath = node.SyntaxTree.FilePath;
5050

51+
if (string.IsNullOrEmpty(filePath))
52+
return node.Identifier.Span.ToSpan();
53+
54+
//Split the path, including the filename.
55+
//Split by dot to get rid of extension in .cs file.
56+
//Reverse to start with file and walk backwards
57+
List<string> pathSplit = filePath.Split(System.IO.Path.DirectorySeparatorChar).Select(x => x.Split('.').First()).Reverse().ToList();
58+
5159
foreach (string returnType in ReturnTypeCandidates(node))
5260
{
53-
string fileName = "";
54-
55-
if (!string.IsNullOrEmpty(filePath))
61+
foreach (string path in pathSplit)
5662
{
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+
if (path == "Create") //Loop until we reach the create folder
64+
break;
6365

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
66+
if (Regex.Match(returnType, $"((List|IEnumerable)<)?I?{path}(<.*>)?>?$").Success)
67+
return null; //Name of file or folder matches return type exactly so this is valid. IsValidCreateMethodName will check if the method name matches the file name
7768
}
7869
}
7970
return node.Identifier.Span.ToSpan(); //Create method file (name/path) and return type do not match as required

0 commit comments

Comments
 (0)