Skip to content

Commit 4510ff4

Browse files
committed
Check if the function name is 'Function' case insensitively. Add a test for good measure
1 parent 3b56a76 commit 4510ff4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Engine/Helper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ public IScriptExtent GetScriptExtentForFunctionName(FunctionDefinitionAst functi
764764

765765
// If the functions name is 'function' then the first token in the
766766
// list is the function keyword itself, so we need to skip it
767-
if (functionDefinitionAst.Name.Equals("function"))
767+
if (functionDefinitionAst.Name.Equals("function", StringComparison.OrdinalIgnoreCase))
768768
{
769769
var funcNameToken = funcNameTokens.Skip(1).FirstOrDefault() ?? funcNameTokens.FirstOrDefault();
770770
return funcNameToken?.Extent;

Tests/Rules/AvoidReservedWordsAsFunctionNames.tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ Describe 'AvoidReservedWordsAsFunctionNames' {
7878
$violations[0].Extent.Text | Should -Be $_
7979
}
8080

81+
It 'flags the correct extent for a function named Function' {
82+
83+
$scriptDefinition = "Function Function { 'test' }"
84+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
85+
86+
$violations.Count | Should -Be 1
87+
$violations[0].Severity | Should -Be 'Warning'
88+
$violations[0].RuleName | Should -Be $ruleName
89+
# Message text should include the function name as used
90+
$violations[0].Message | Should -Be "The reserved word 'Function' was used as a function name. This should be avoided."
91+
# Extent should ideally capture only the function name
92+
$violations[0].Extent.Text | Should -Be 'Function'
93+
94+
# Make sure the extent is the correct `Function` (not the one at the
95+
# very start)
96+
$violations[0].Extent.StartOffset | Should -not -Be 0
97+
}
98+
8199
# Functions can have scopes. So function global:function {} should still
82100
# alert.
83101
It 'flags reserved word "<Name>" with scope "<Scope>" as a violation' -TestCases $scopedReservedWordCases {

0 commit comments

Comments
 (0)