@@ -36,20 +36,34 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
36
36
// Finds all functionAst
37
37
IEnumerable < Ast > functionAsts = ast . FindAll ( testAst => testAst is FunctionDefinitionAst , true ) ;
38
38
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
+
39
45
foreach ( FunctionDefinitionAst funcAst in functionAsts )
40
46
{
41
47
// Finds all ParamAsts.
42
48
IEnumerable < Ast > varAsts = funcAst . FindAll ( testAst => testAst is VariableExpressionAst , true ) ;
43
49
44
50
// Iterrates all ParamAsts and check if their names are on the list.
45
51
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
+ }
47
61
// only raise the rules for variables in the param block.
48
62
if ( funcAst . Body != null && funcAst . Body . ParamBlock != null && funcAst . Body . ParamBlock . Parameters != null )
49
63
{
50
64
foreach ( var paramAst in funcAst . Body . ParamBlock . Parameters )
51
65
{
52
- if ( Helper . Instance . IsUninitialized ( paramAst . Name , funcAst ) )
66
+ if ( Helper . Instance . IsUninitialized ( paramAst . Name , funcAst ) && ! dscVariables . Contains ( paramAst . Name . VariablePath . UserPath ) )
53
67
{
54
68
yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ProvideDefaultParameterValueError , paramAst . Name . VariablePath . UserPath ) ,
55
69
paramAst . Name . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName , paramAst . Name . VariablePath . UserPath ) ;
@@ -61,7 +75,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
61
75
{
62
76
foreach ( var paramAst in funcAst . Parameters )
63
77
{
64
- if ( Helper . Instance . IsUninitialized ( paramAst . Name , funcAst ) )
78
+ if ( Helper . Instance . IsUninitialized ( paramAst . Name , funcAst ) && ! dscVariables . Contains ( paramAst . Name . VariablePath . UserPath ) )
65
79
{
66
80
yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ProvideDefaultParameterValueError , paramAst . Name . VariablePath . UserPath ) ,
67
81
paramAst . Name . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName , paramAst . Name . VariablePath . UserPath ) ;
0 commit comments