1
1
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
- using System . Linq ;
5
- using System . Text ;
6
- using System . Threading . Tasks ;
7
- using System . Management . Automation . Language ;
8
- using System . Globalization ;
9
4
using System . ComponentModel . Composition ;
5
+ using System . Globalization ;
6
+ using System . Management . Automation . Language ;
10
7
11
8
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
12
9
{
@@ -41,7 +38,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
41
38
42
39
public override AstVisitAction VisitCommandParameter ( CommandParameterAst commandParameterAst )
43
40
{
44
- if ( isScopeParameterForNewAliasCmdlet ( commandParameterAst ) )
41
+ if ( IsScopeParameterForNewAliasCmdlet ( commandParameterAst ) )
45
42
{
46
43
if ( ( commandParameterAst . Argument != null ) // if the cmdlet looks like -Scope:Global check Parameter.Argument
47
44
&& ( commandParameterAst . Argument . ToString ( ) . Equals ( "Global" , StringComparison . OrdinalIgnoreCase ) ) )
@@ -77,7 +74,7 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
77
74
78
75
public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst functionDefinitionAst )
79
76
{
80
- if ( functionDefinitionAst . Name . StartsWith ( "Global:" , StringComparison . OrdinalIgnoreCase ) )
77
+ if ( functionDefinitionAst . Name . StartsWith ( "Global:" , StringComparison . OrdinalIgnoreCase ) && IsModule ( ) )
81
78
{
82
79
records . Add ( new DiagnosticRecord (
83
80
string . Format ( CultureInfo . CurrentCulture , Strings . AvoidGlobalFunctionsError ) ,
@@ -92,6 +89,11 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
92
89
}
93
90
#endregion
94
91
92
+ private bool IsModule ( )
93
+ {
94
+ return fileName . EndsWith ( ".psm1" ) ;
95
+ }
96
+
95
97
private Ast FindNextAst ( Ast ast )
96
98
{
97
99
IEnumerable < Ast > matchingLevelAsts = ast . Parent . FindAll ( item => item is Ast , true ) ;
@@ -101,14 +103,14 @@ private Ast FindNextAst(Ast ast)
101
103
{
102
104
if ( currentClosest == null )
103
105
{
104
- if ( isAstAfter ( ast , matchingLevelAst ) )
106
+ if ( IsAstAfter ( ast , matchingLevelAst ) )
105
107
{
106
108
currentClosest = matchingLevelAst ;
107
109
}
108
110
}
109
111
else
110
112
{
111
- if ( ( isAstAfter ( ast , matchingLevelAst ) ) && ( isAstAfter ( matchingLevelAst , currentClosest ) ) )
113
+ if ( ( IsAstAfter ( ast , matchingLevelAst ) ) && ( IsAstAfter ( matchingLevelAst , currentClosest ) ) )
112
114
{
113
115
currentClosest = matchingLevelAst ;
114
116
}
@@ -118,7 +120,7 @@ private Ast FindNextAst(Ast ast)
118
120
return currentClosest ;
119
121
}
120
122
121
- private bool isAstAfter ( Ast ast1 , Ast ast2 )
123
+ private bool IsAstAfter ( Ast ast1 , Ast ast2 )
122
124
{
123
125
if ( ast1 . Extent . EndLineNumber > ast2 . Extent . StartLineNumber ) // ast1 ends on a line after ast2 starts
124
126
{
@@ -141,7 +143,7 @@ private bool isAstAfter(Ast ast1, Ast ast2)
141
143
}
142
144
}
143
145
144
- private bool isScopeParameterForNewAliasCmdlet ( CommandParameterAst commandParameterAst )
146
+ private bool IsScopeParameterForNewAliasCmdlet ( CommandParameterAst commandParameterAst )
145
147
{
146
148
if ( commandParameterAst == null || commandParameterAst . ParameterName == null )
147
149
return false ;
0 commit comments