9
9
#endif
10
10
using System . Globalization ;
11
11
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
12
+ using System . Linq ;
12
13
13
14
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
14
15
{
@@ -113,7 +114,7 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
113
114
IEnumerable < Ast > varAsts = scriptBlockAst . FindAll ( testAst => testAst is VariableExpressionAst , true ) ;
114
115
IEnumerable < Ast > varsInAssignment ;
115
116
116
- Dictionary < string , AssignmentStatementAst > assignments = new Dictionary < string , AssignmentStatementAst > ( StringComparer . OrdinalIgnoreCase ) ;
117
+ Dictionary < string , AssignmentStatementAst > assignmentsDictionary_OrdinalIgnoreCase = new Dictionary < string , AssignmentStatementAst > ( StringComparer . OrdinalIgnoreCase ) ;
117
118
118
119
string varKey ;
119
120
bool inAssignment ;
@@ -148,9 +149,9 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
148
149
{
149
150
string variableName = Helper . Instance . VariableNameWithoutScope ( assignmentVarAst . VariablePath ) ;
150
151
151
- if ( ! assignments . ContainsKey ( variableName ) )
152
+ if ( ! assignmentsDictionary_OrdinalIgnoreCase . ContainsKey ( variableName ) )
152
153
{
153
- assignments . Add ( variableName , assignmentAst ) ;
154
+ assignmentsDictionary_OrdinalIgnoreCase . Add ( variableName , assignmentAst ) ;
154
155
}
155
156
}
156
157
}
@@ -163,9 +164,9 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
163
164
varKey = Helper . Instance . VariableNameWithoutScope ( varAst . VariablePath ) ;
164
165
inAssignment = false ;
165
166
166
- if ( assignments . ContainsKey ( varKey ) )
167
+ if ( assignmentsDictionary_OrdinalIgnoreCase . ContainsKey ( varKey ) )
167
168
{
168
- varsInAssignment = assignments [ varKey ] . Left . FindAll ( testAst => testAst is VariableExpressionAst , true ) ;
169
+ varsInAssignment = assignmentsDictionary_OrdinalIgnoreCase [ varKey ] . Left . FindAll ( testAst => testAst is VariableExpressionAst , true ) ;
169
170
170
171
// Checks if this variableAst is part of the logged assignment
171
172
foreach ( VariableExpressionAst varInAssignment in varsInAssignment )
@@ -195,23 +196,41 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
195
196
196
197
if ( ! inAssignment )
197
198
{
198
- assignments . Remove ( varKey ) ;
199
+ assignmentsDictionary_OrdinalIgnoreCase . Remove ( varKey ) ;
199
200
}
200
201
201
202
// Check if variable belongs to PowerShell built-in variables
202
203
if ( Helper . Instance . HasSpecialVars ( varKey ) )
203
204
{
204
- assignments . Remove ( varKey ) ;
205
+ assignmentsDictionary_OrdinalIgnoreCase . Remove ( varKey ) ;
205
206
}
206
207
}
207
208
}
208
209
}
209
210
210
- foreach ( string key in assignments . Keys )
211
+ // Detect usages of Get-Variable
212
+ var getVariableCmdletNamesAndAliases = Helper . Instance . CmdletNameAndAliases ( "Get-Variable" ) ;
213
+ IEnumerable < Ast > getVariableCommandAsts = scriptBlockAst . FindAll ( testAst => testAst is CommandAst commandAst &&
214
+ getVariableCmdletNamesAndAliases . Contains ( commandAst . GetCommandName ( ) , StringComparer . OrdinalIgnoreCase ) , true ) ;
215
+ foreach ( CommandAst getVariableCommandAst in getVariableCommandAsts )
216
+ {
217
+ var commandElements = getVariableCommandAst . CommandElements . ToList ( ) ;
218
+ // The following extracts the variable name only in the simplest possibe case 'Get-Variable variableName'
219
+ if ( commandElements . Count == 2 && commandElements [ 1 ] is StringConstantExpressionAst constantExpressionAst )
220
+ {
221
+ var variableName = constantExpressionAst . Value ;
222
+ if ( assignmentsDictionary_OrdinalIgnoreCase . ContainsKey ( variableName ) )
223
+ {
224
+ assignmentsDictionary_OrdinalIgnoreCase . Remove ( variableName ) ;
225
+ }
226
+ }
227
+ }
228
+
229
+ foreach ( string key in assignmentsDictionary_OrdinalIgnoreCase . Keys )
211
230
{
212
231
yield return new DiagnosticRecord (
213
232
string . Format ( CultureInfo . CurrentCulture , Strings . UseDeclaredVarsMoreThanAssignmentsError , key ) ,
214
- assignments [ key ] . Left . Extent ,
233
+ assignmentsDictionary_OrdinalIgnoreCase [ key ] . Left . Extent ,
215
234
GetName ( ) ,
216
235
DiagnosticSeverity . Warning ,
217
236
fileName ,
0 commit comments