11using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
22using System ;
33using 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 ;
94using System . ComponentModel . Composition ;
5+ using System . Globalization ;
6+ using System . Management . Automation . Language ;
107
118namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
129{
@@ -41,7 +38,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
4138
4239 public override AstVisitAction VisitCommandParameter ( CommandParameterAst commandParameterAst )
4340 {
44- if ( isScopeParameterForNewAliasCmdlet ( commandParameterAst ) )
41+ if ( IsScopeParameterForNewAliasCmdlet ( commandParameterAst ) )
4542 {
4643 if ( ( commandParameterAst . Argument != null ) // if the cmdlet looks like -Scope:Global check Parameter.Argument
4744 && ( commandParameterAst . Argument . ToString ( ) . Equals ( "Global" , StringComparison . OrdinalIgnoreCase ) ) )
@@ -77,7 +74,7 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
7774
7875 public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst functionDefinitionAst )
7976 {
80- if ( functionDefinitionAst . Name . StartsWith ( "Global:" , StringComparison . OrdinalIgnoreCase ) )
77+ if ( functionDefinitionAst . Name . StartsWith ( "Global:" , StringComparison . OrdinalIgnoreCase ) && IsModule ( ) )
8178 {
8279 records . Add ( new DiagnosticRecord (
8380 string . Format ( CultureInfo . CurrentCulture , Strings . AvoidGlobalFunctionsError ) ,
@@ -92,6 +89,11 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
9289 }
9390 #endregion
9491
92+ private bool IsModule ( )
93+ {
94+ return fileName . EndsWith ( ".psm1" ) ;
95+ }
96+
9597 private Ast FindNextAst ( Ast ast )
9698 {
9799 IEnumerable < Ast > matchingLevelAsts = ast . Parent . FindAll ( item => item is Ast , true ) ;
@@ -101,14 +103,14 @@ private Ast FindNextAst(Ast ast)
101103 {
102104 if ( currentClosest == null )
103105 {
104- if ( isAstAfter ( ast , matchingLevelAst ) )
106+ if ( IsAstAfter ( ast , matchingLevelAst ) )
105107 {
106108 currentClosest = matchingLevelAst ;
107109 }
108110 }
109111 else
110112 {
111- if ( ( isAstAfter ( ast , matchingLevelAst ) ) && ( isAstAfter ( matchingLevelAst , currentClosest ) ) )
113+ if ( ( IsAstAfter ( ast , matchingLevelAst ) ) && ( IsAstAfter ( matchingLevelAst , currentClosest ) ) )
112114 {
113115 currentClosest = matchingLevelAst ;
114116 }
@@ -118,7 +120,7 @@ private Ast FindNextAst(Ast ast)
118120 return currentClosest ;
119121 }
120122
121- private bool isAstAfter ( Ast ast1 , Ast ast2 )
123+ private bool IsAstAfter ( Ast ast1 , Ast ast2 )
122124 {
123125 if ( ast1 . Extent . EndLineNumber > ast2 . Extent . StartLineNumber ) // ast1 ends on a line after ast2 starts
124126 {
@@ -141,7 +143,7 @@ private bool isAstAfter(Ast ast1, Ast ast2)
141143 }
142144 }
143145
144- private bool isScopeParameterForNewAliasCmdlet ( CommandParameterAst commandParameterAst )
146+ private bool IsScopeParameterForNewAliasCmdlet ( CommandParameterAst commandParameterAst )
145147 {
146148 if ( commandParameterAst == null || commandParameterAst . ParameterName == null )
147149 return false ;
0 commit comments