@@ -30,6 +30,20 @@ $randomCasedReservedWords = @(
30
30
' uNtIl' , ' UsInG' , ' VaR' , ' wHiLe' , ' wOrKfLoW'
31
31
)
32
32
33
+ $functionScopes = @ (
34
+ " global" , " local" , " script" , " private"
35
+ )
36
+
37
+ # Generate all combinations of reserved words and function scopes
38
+ $scopedReservedWordCases = foreach ($scope in $functionScopes ) {
39
+ foreach ($word in $reservedWords ) {
40
+ @ {
41
+ Scope = $scope
42
+ Name = $word
43
+ }
44
+ }
45
+ }
46
+
33
47
$substringReservedWords = $reservedWords | ForEach-Object {
34
48
" $ ( $_ ) Func"
35
49
}
@@ -58,6 +72,22 @@ Describe 'AvoidReservedWordsAsFunctionNames' {
58
72
$violations [0 ].Extent.Text | Should - Be $_
59
73
}
60
74
75
+ # Functions can have scopes. So function global:function {} should still
76
+ # alert.
77
+ It ' flags reserved word "<Name>" with scope "<Scope>" as a violation' - TestCases $scopedReservedWordCases {
78
+ param ($Scope , $Name )
79
+
80
+ $scriptDefinition = " function $ ( $Scope ) :$ ( $Name ) { 'test' }"
81
+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ($ruleName )
82
+
83
+ $violations.Count | Should - Be 1
84
+ $violations [0 ].Severity | Should - Be ' Warning'
85
+ $violations [0 ].RuleName | Should - Be $ruleName
86
+ $violations [0 ].Message | Should - Be " The reserved word '$Name ' was used as a function name. This should be avoided."
87
+ $violations [0 ].Extent.Text | Should - Be " $ ( $Scope ) :$ ( $Name ) "
88
+ }
89
+
90
+
61
91
It ' detects case-insensitively for "<_>"' - TestCases $randomCasedReservedWords {
62
92
$scriptDefinition = " function $_ { }"
63
93
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ($ruleName )
0 commit comments