Skip to content

Commit 88d9dc5

Browse files
David ReynoldsDavid Reynolds
authored andcommitted
Updated AvoidGlobalFunctions rule to only warn on global function when in a module
1 parent 25dcf51 commit 88d9dc5

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

Rules/AvoidGlobalFunctions.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
22
using System;
33
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;
94
using System.ComponentModel.Composition;
5+
using System.Globalization;
6+
using System.Management.Automation.Language;
107

118
namespace 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;

Tests/Rules/AvoidGlobalFunctions.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ $aliasErrorMessage = "Avoid creating aliases with a Global scope."
55
$violationName = "AvoidGlobalFunctions"
66

77
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
8-
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalFunctions.ps1 | Where-Object {$_.RuleName -eq $violationName}
8+
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalFunctions.psm1 | Where-Object {$_.RuleName -eq $violationName}
99
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidGlobalFunctionsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
1010

1111

1212
Describe "$violationName " {
1313
Context "When there are violations" {
14-
It "has 5 avoid using empty Catch block violations" {
14+
It "Has 5 avoid using empty Catch block violations" {
1515
$violations.Count | Should Be 5
1616
}
1717

18-
It "has the correct description message" {
18+
It "Has the correct description message" {
1919
$violations[0].Message | Should Match $functionErroMessage
2020
$violations[1].Message | Should Match $aliasErrorMessage
2121
$violations[2].Message | Should Match $aliasErrorMessage
@@ -26,7 +26,7 @@ Describe "$violationName " {
2626
}
2727

2828
Context "When there are no violations" {
29-
It "returns no violations" {
29+
It "Returns no violations" {
3030
$noViolations.Count | Should Be 0
3131
}
3232
}

Tests/Rules/AvoidGlobalFunctionsNoViolations.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
function functionName {}
2+
function global:functionName {}
33
{
44
New-Alias -Name Name -Scope `
55
Script -Value Value

0 commit comments

Comments
 (0)