@@ -49,55 +49,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
49
49
/// <returns>AstVisitAction to continue to analyze the ast's children</returns>
50
50
public override AstVisitAction VisitCommand ( CommandAst commandAst )
51
51
{
52
- if ( ! IsNewAliasCmdlet ( commandAst ) )
52
+ if ( IsNewAliasCmdlet ( commandAst ) )
53
53
{
54
- return AstVisitAction . SkipChildren ;
55
- }
56
-
57
- return AstVisitAction . Continue ;
58
- }
59
-
60
- /// <summary>
61
- /// Analyzes a CommandParameterAst for the global scope.
62
- /// </summary>
63
- /// <param name="commandParameterAst">The CommandParameterAst to be analyzed</param>
64
- /// <returns>AstVisitAction to skip child ast processing after creating any diagnostic records</returns>
65
- public override AstVisitAction VisitCommandParameter ( CommandParameterAst commandParameterAst )
66
- {
67
- if ( IsScopeParameterForNewAliasCmdlet ( commandParameterAst ) )
68
- {
69
- // Check the commandParameterAst Argument property if it exist. This covers the case
70
- // of the cmdlet looking like "New-Alias -Scope:Global"
54
+ // check the parameters of the New-Alias cmdlet for scope
55
+ var parameterBindings = StaticParameterBinder . BindCommand ( commandAst ) ;
71
56
72
- if ( ( commandParameterAst . Argument != null )
73
- && ( commandParameterAst . Argument . ToString ( ) . Equals ( "Global" , StringComparison . OrdinalIgnoreCase ) ) )
57
+ if ( parameterBindings . BoundParameters . ContainsKey ( "Scope" ) )
74
58
{
75
- records . Add ( new DiagnosticRecord (
76
- string . Format ( CultureInfo . CurrentCulture , Strings . AvoidGlobalAliasesError ) ,
77
- commandParameterAst . Extent ,
78
- GetName ( ) ,
79
- DiagnosticSeverity . Warning ,
80
- fileName ,
81
- commandParameterAst . ParameterName ) ) ;
82
- }
83
- else
84
- {
85
- // If the commandParameterAst Argument property is null the next ast in the tree
86
- // can still be a string const. This covers the case of the cmdlet looking like
87
- // "New-Alias -Scope Global"
59
+ var scopeValue = parameterBindings . BoundParameters [ "Scope" ] . ConstantValue ;
88
60
89
- var nextAst = FindNextAst ( commandParameterAst ) as StringConstantExpressionAst ;
90
-
91
- if ( ( nextAst != null )
92
- && ( ( nextAst ) . Value . ToString ( ) . Equals ( "Global" , StringComparison . OrdinalIgnoreCase ) ) )
61
+ if ( ( scopeValue != null ) && ( scopeValue . ToString ( ) . Equals ( "Global" , StringComparison . OrdinalIgnoreCase ) ) )
93
62
{
94
63
records . Add ( new DiagnosticRecord (
95
- string . Format ( CultureInfo . CurrentCulture , Strings . AvoidGlobalAliasesError ) ,
96
- ( nextAst ) . Extent ,
97
- GetName ( ) ,
98
- DiagnosticSeverity . Warning ,
99
- fileName ,
100
- ( nextAst ) . Value ) ) ;
64
+ string . Format ( CultureInfo . CurrentCulture , Strings . AvoidGlobalAliasesError ) ,
65
+ commandAst . Extent ,
66
+ GetName ( ) ,
67
+ DiagnosticSeverity . Warning ,
68
+ fileName ) ) ;
101
69
}
102
70
}
103
71
}
@@ -106,86 +74,6 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
106
74
}
107
75
#endregion
108
76
109
- /// <summary>
110
- /// Returns the next ast of the same level in the ast tree.
111
- /// </summary>
112
- /// <param name="ast">Ast used as a base</param>
113
- /// <returns>Next ast of the same level in the ast tree</returns>
114
- private Ast FindNextAst ( Ast ast )
115
- {
116
- IEnumerable < Ast > matchingLevelAsts = ast . Parent . FindAll ( item => item is Ast , true ) ;
117
-
118
- Ast currentClosest = null ;
119
- foreach ( var matchingLevelAst in matchingLevelAsts )
120
- {
121
- if ( currentClosest == null )
122
- {
123
- if ( IsAstAfter ( ast , matchingLevelAst ) )
124
- {
125
- currentClosest = matchingLevelAst ;
126
- }
127
- }
128
- else
129
- {
130
- if ( ( IsAstAfter ( ast , matchingLevelAst ) ) && ( IsAstAfter ( matchingLevelAst , currentClosest ) ) )
131
- {
132
- currentClosest = matchingLevelAst ;
133
- }
134
- }
135
- }
136
-
137
- return currentClosest ;
138
- }
139
-
140
- /// <summary>
141
- /// Determines if ast1 is after ast2 in the ast tree.
142
- /// </summary>
143
- /// <param name="ast1">First ast</param>
144
- /// <param name="ast2">Second ast</param>
145
- /// <returns>True if ast2 is after ast1 in the ast tree</returns>
146
- private bool IsAstAfter ( Ast ast1 , Ast ast2 )
147
- {
148
- if ( ast1 . Extent . EndLineNumber > ast2 . Extent . StartLineNumber ) // ast1 ends on a line after ast2 starts
149
- {
150
- return false ;
151
- }
152
- else if ( ast1 . Extent . EndLineNumber == ast2 . Extent . StartLineNumber )
153
- {
154
- if ( ast2 . Extent . StartColumnNumber > ast1 . Extent . EndColumnNumber )
155
- {
156
- return true ;
157
- }
158
- else
159
- {
160
- return false ;
161
- }
162
- }
163
- else // ast2 starts on a line after ast 1 ends
164
- {
165
- return true ;
166
- }
167
- }
168
-
169
- /// <summary>
170
- /// Determines if CommandParameterAst is for the "Scope" parameter.
171
- /// </summary>
172
- /// <param name="commandParameterAst">CommandParameterAst to validate</param>
173
- /// <returns>True if the CommandParameterAst is for the Scope parameter</returns>
174
- private bool IsScopeParameterForNewAliasCmdlet ( CommandParameterAst commandParameterAst )
175
- {
176
- if ( commandParameterAst == null || commandParameterAst . ParameterName == null )
177
- {
178
- return false ;
179
- }
180
-
181
- if ( commandParameterAst . ParameterName . Equals ( "Scope" , StringComparison . OrdinalIgnoreCase ) )
182
- {
183
- return true ;
184
- }
185
-
186
- return false ;
187
- }
188
-
189
77
/// <summary>
190
78
/// Determines if CommandAst is for the "New-Alias" command, checking aliases.
191
79
/// </summary>
0 commit comments