Skip to content

Commit f1942e1

Browse files
author
Kapil Borle
committed
Fix extent of UseIdenticalMandatoryParametersForDSC rule
1 parent 1a15076 commit f1942e1

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
4444
// Expected TargetResource functions in the DSC Resource module
4545
List<string> expectedTargetResourceFunctionNames = new List<string>(new string[] { "Set-TargetResource", "Test-TargetResource", "Get-TargetResource" });
4646

47-
IEnumerable<Ast> functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast);
47+
var functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast).Cast<FunctionDefinitionAst>();
4848

4949
// Dictionary to keep track of Mandatory parameters and their presence in Get/Test/Set TargetResource cmdlets
50-
Dictionary<string, List<string>> mandatoryParameters = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
50+
var mandatoryParameters = new Dictionary<string, List<FunctionDefinitionAst>>(StringComparer.OrdinalIgnoreCase);
5151

5252
// Loop through Set/Test/Get TargetResource DSC cmdlets
5353
foreach (FunctionDefinitionAst functionDefinitionAst in functionDefinitionAsts)
@@ -73,18 +73,18 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
7373
{
7474
// Covers Case - [Parameter(Mandatory)] and [Parameter(Mandatory)=$true]
7575
if (namedArgument.ExpressionOmitted || (!namedArgument.ExpressionOmitted && String.Equals(namedArgument.Argument.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase)))
76-
{
76+
{
7777
if (mandatoryParameters.ContainsKey(paramAst.Name.VariablePath.UserPath))
7878
{
79-
mandatoryParameters[paramAst.Name.VariablePath.UserPath].Add(functionDefinitionAst.Name);
79+
mandatoryParameters[paramAst.Name.VariablePath.UserPath].Add(functionDefinitionAst);
8080
}
8181
else
8282
{
83-
List<string> functionNames = new List<string>();
84-
functionNames.Add(functionDefinitionAst.Name);
83+
var functionNames = new List<FunctionDefinitionAst>();
84+
functionNames.Add(functionDefinitionAst);
8585
mandatoryParameters.Add(paramAst.Name.VariablePath.UserPath, functionNames);
86-
}
87-
}
86+
}
87+
}
8888
}
8989
}
9090
}
@@ -93,20 +93,30 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
9393
}
9494
}
9595

96-
// Get the mandatory parameter names that do not appear in all the DSC Resource cmdlets
97-
IEnumerable<string> paramNames = mandatoryParameters.Where(x => x.Value.Count < expectedTargetResourceFunctionNames.Count).Select(x => x.Key);
98-
96+
// Get the mandatory parameter names that do not appear in all the DSC Resource cmdlets
97+
IEnumerable<string> paramNames = mandatoryParameters.Where(x => x.Value.Count < expectedTargetResourceFunctionNames.Count).Select(x => x.Key);
98+
9999
if (paramNames.Count() > 0)
100-
{
100+
{
101101
foreach (string paramName in paramNames)
102102
{
103-
List<string> functionsNotContainingParam = expectedTargetResourceFunctionNames.Except(mandatoryParameters[paramName]).ToList();
104-
yield return new DiagnosticRecord(string.Format(CultureInfo.InvariantCulture, Strings.UseIdenticalMandatoryParametersDSCError, paramName, string.Join(", ", functionsNotContainingParam.ToArray())),
105-
ast.Extent, GetName(), DiagnosticSeverity.Error, fileName);
106-
}
107-
103+
var functionsNotContainingParam = functionDefinitionAsts.Except(mandatoryParameters[paramName]);
104+
105+
foreach (var funcDefnAst in functionsNotContainingParam)
106+
{
107+
yield return new DiagnosticRecord(
108+
string.Format(
109+
CultureInfo.InvariantCulture,
110+
Strings.UseIdenticalMandatoryParametersDSCError,
111+
paramName,
112+
funcDefnAst.Name),
113+
funcDefnAst.Extent,
114+
GetName(),
115+
DiagnosticSeverity.Error,
116+
fileName);
117+
}
118+
}
108119
}
109-
110120
}
111121

112122
/// <summary>
@@ -117,7 +127,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
117127
/// <returns></returns>
118128
public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
119129
{
120-
// For DSC Class based resource, this rule is N/A, since the Class Properties
130+
// For DSC Class based resource, this rule is N/A, since the Class Properties
121131
// are declared only once and available to Get(), Set(), Test() functions
122132
return Enumerable.Empty<DiagnosticRecord>();
123133
}
@@ -127,7 +137,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
127137
/// </summary>
128138
/// <returns>The name of this rule</returns>
129139
public string GetName()
130-
{
140+
{
131141
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseIdenticalMandatoryParametersDSCName);
132142
}
133143

@@ -173,7 +183,7 @@ public string GetSourceName()
173183
{
174184
return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName);
175185
}
176-
}
186+
}
177187

178188
}
179189

0 commit comments

Comments
 (0)