@@ -56,7 +56,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
56
56
{
57
57
yield return new DiagnosticRecord (
58
58
string . Format ( CultureInfo . CurrentCulture , Strings . UseShouldProcessForStateChangingFunctionsError , funcDefAst . Name ) ,
59
- funcDefAst . Extent ,
59
+ Helper . Instance . GetScriptExtentForFunctionName ( funcDefAst ) ,
60
60
this . GetName ( ) ,
61
61
DiagnosticSeverity . Warning ,
62
62
fileName ) ;
@@ -80,12 +80,9 @@ private bool IsStateChangingFunctionWithNoShouldProcessAttribute(Ast ast)
80
80
elem => funcName . StartsWith (
81
81
elem ,
82
82
StringComparison . OrdinalIgnoreCase ) ) . FirstOrDefault ( ) ;
83
- if ( targetFuncName == null
84
- || funcDefAst . Body == null
83
+ if ( targetFuncName == null
85
84
|| funcDefAst . Body . ParamBlock == null
86
- || funcDefAst . Body . ParamBlock . Attributes == null
87
- || ! funcDefAst . Body . ParamBlock . Attributes . Any (
88
- attr => attr . TypeName . GetReflectionType ( ) == typeof ( CmdletBindingAttribute ) ) )
85
+ || funcDefAst . Body . ParamBlock . Attributes == null )
89
86
{
90
87
return false ;
91
88
}
@@ -99,44 +96,32 @@ private bool IsStateChangingFunctionWithNoShouldProcessAttribute(Ast ast)
99
96
/// <returns>Returns true or false</returns>
100
97
private bool HasShouldProcessTrue ( IEnumerable < AttributeAst > attributeAsts )
101
98
{
102
- if ( attributeAsts == null )
103
- {
104
- return false ;
105
- }
106
99
foreach ( var attributeAst in attributeAsts )
107
100
{
108
101
if ( attributeAst == null || attributeAst . NamedArguments == null )
109
102
{
110
103
continue ;
111
104
}
112
- foreach ( var namedAttributeAst in attributeAst . NamedArguments )
105
+ if ( attributeAst . TypeName . GetReflectionAttributeType ( )
106
+ == typeof ( CmdletBindingAttribute ) )
113
107
{
114
- if ( namedAttributeAst == null )
115
- {
116
- continue ;
117
- }
118
- if ( ! IsShouldProcessAttribute ( namedAttributeAst ) )
108
+ foreach ( var namedAttributeAst in attributeAst . NamedArguments )
119
109
{
120
- continue ;
110
+ if ( namedAttributeAst == null
111
+ || ! namedAttributeAst . ArgumentName . Equals (
112
+ "SupportsShouldProcess" ,
113
+ StringComparison . OrdinalIgnoreCase ) )
114
+ {
115
+ continue ;
116
+ }
117
+ return IsShouldProcessTrue ( namedAttributeAst ) ;
121
118
}
122
- return IsShouldProcessTrue ( namedAttributeAst ) ;
123
119
}
124
120
}
125
121
// Cannot find any SupportShouldProcess attribute
126
122
return false ;
127
123
}
128
124
129
- /// <summary>
130
- /// Checks if the named attribute is SupportShouldProcess
131
- /// </summary>
132
- /// <param name="namedAttributeArgumentAst"></param>
133
- /// <returns>Returns true or false</returns>
134
- private bool IsShouldProcessAttribute ( NamedAttributeArgumentAst namedAttributeArgumentAst )
135
- {
136
- return namedAttributeArgumentAst != null
137
- && namedAttributeArgumentAst . ArgumentName . Equals ( "SupportsShouldProcess" , StringComparison . OrdinalIgnoreCase ) ;
138
- }
139
-
140
125
/// <summary>
141
126
/// Checks if the SupportShouldProcess attribute is true
142
127
/// </summary>
@@ -151,11 +136,25 @@ private bool IsShouldProcessTrue(NamedAttributeArgumentAst namedAttributeArgumen
151
136
else
152
137
{
153
138
var varExpAst = namedAttributeArgumentAst . Argument as VariableExpressionAst ;
154
- if ( varExpAst != null
155
- && varExpAst . VariablePath . UserPath . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) )
139
+ if ( varExpAst == null )
156
140
{
157
- return true ;
141
+ var constExpAst = namedAttributeArgumentAst . Argument as ConstantExpressionAst ;
142
+ if ( constExpAst == null )
143
+ {
144
+ return false ;
145
+ }
146
+ bool constExpVal ;
147
+ if ( LanguagePrimitives . TryConvertTo < bool > ( constExpAst . Value , out constExpVal ) )
148
+ {
149
+ return constExpVal ;
150
+ }
158
151
}
152
+ else if ( varExpAst . VariablePath . UserPath . Equals (
153
+ bool . TrueString ,
154
+ StringComparison . OrdinalIgnoreCase ) )
155
+ {
156
+ return true ;
157
+ }
159
158
}
160
159
return false ;
161
160
}
0 commit comments