Skip to content

Commit 276f464

Browse files
committed
Add isDscResourceFile Check to PSProvideDefaultParameterValue
1 parent 2528ca6 commit 276f464

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Rules/ProvideDefaultParameterValue.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,34 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3636
// Finds all functionAst
3737
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
3838

39+
// Checks whether this is a dsc resource file (we don't raise this rule for get, set and test-target resource
40+
bool isDscResourceFile = Helper.Instance.IsDscResourceModule(fileName);
41+
42+
List<string> targetResourcesFunctions = new List<string>(new string[] { "get-targetresource", "set-targetresource", "test-targetresource" });
43+
44+
3945
foreach (FunctionDefinitionAst funcAst in functionAsts)
4046
{
4147
// Finds all ParamAsts.
4248
IEnumerable<Ast> varAsts = funcAst.FindAll(testAst => testAst is VariableExpressionAst, true);
4349

4450
// Iterrates all ParamAsts and check if their names are on the list.
4551

46-
HashSet<string> paramVariables = new HashSet<string>();
52+
HashSet<string> dscVariables = new HashSet<string>();
53+
if (isDscResourceFile && targetResourcesFunctions.Contains(funcAst.Name, StringComparer.OrdinalIgnoreCase))
54+
{
55+
// don't raise the rules for variables in the param block.
56+
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
57+
{
58+
dscVariables.UnionWith(funcAst.Body.ParamBlock.Parameters.Select(paramAst => paramAst.Name.VariablePath.UserPath));
59+
}
60+
}
4761
// only raise the rules for variables in the param block.
4862
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
4963
{
5064
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters)
5165
{
52-
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst))
66+
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst) && !dscVariables.Contains(paramAst.Name.VariablePath.UserPath))
5367
{
5468
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath),
5569
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath);
@@ -61,7 +75,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6175
{
6276
foreach (var paramAst in funcAst.Parameters)
6377
{
64-
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst))
78+
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst) && !dscVariables.Contains(paramAst.Name.VariablePath.UserPath))
6579
{
6680
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath),
6781
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath);

0 commit comments

Comments
 (0)