Skip to content

Commit 23af8f1

Browse files
author
Kapil Borle
authored
Merge pull request #658 from PowerShell/kapilmb/FixAvoidGlobalRules
Fix AvoidGlobalAliases and AvoidGlobalFunctions rules
2 parents 4cda851 + 039cfda commit 23af8f1

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

Engine/Helper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,19 @@ public static bool IsPowerShellVersionSupported(Version version)
17751775
return version >= minSupportedPSVersion;
17761776
}
17771777

1778+
/// <summary>
1779+
/// Determines if analyzing a script module.
1780+
/// </summary>
1781+
/// <returns>True is file name ends with ".psm1"</returns>
1782+
public static bool IsModuleScript(string filepath)
1783+
{
1784+
if (filepath == null)
1785+
{
1786+
throw new ArgumentNullException("filepath");
1787+
}
1788+
return filepath.EndsWith(".psm1");
1789+
}
1790+
17781791
/// <summary>
17791792
/// Checks if a given file is a valid PowerShell module manifest
17801793
/// </summary>

Rules/AvoidGlobalAliases.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3333
records = new List<DiagnosticRecord>();
3434
this.fileName = fileName;
3535

36-
if (IsScriptModule())
36+
if (fileName != null && Helper.IsModuleScript(fileName))
3737
{
3838
ast.Visit(this);
3939
}
@@ -95,15 +95,6 @@ private bool IsNewAliasCmdlet(CommandAst commandAst)
9595
return false;
9696
}
9797

98-
/// <summary>
99-
/// Determines if analyzing a script module.
100-
/// </summary>
101-
/// <returns>True is file name ends with ".psm1"</returns>
102-
private bool IsScriptModule()
103-
{
104-
return fileName.EndsWith(".psm1");
105-
}
106-
10798
public string GetCommonName()
10899
{
109100
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesCommonName);
@@ -116,7 +107,11 @@ public string GetDescription()
116107

117108
public string GetName()
118109
{
119-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesName);
110+
return string.Format(
111+
CultureInfo.CurrentCulture,
112+
Strings.NameSpaceFormat,
113+
GetSourceName(),
114+
Strings.AvoidGlobalAliasesName);
120115
}
121116

122117
public RuleSeverity GetSeverity()

Rules/AvoidGlobalFunctions.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3333
records = new List<DiagnosticRecord>();
3434
this.fileName = fileName;
3535

36-
if (IsScriptModule())
36+
if (fileName != null && Helper.IsModuleScript(fileName))
3737
{
3838
ast.Visit(this);
3939
}
@@ -66,15 +66,6 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
6666
}
6767
#endregion
6868

69-
/// <summary>
70-
/// Determines if analyzing a script module.
71-
/// </summary>
72-
/// <returns>True is file name ends with ".psm1"</returns>
73-
private bool IsScriptModule()
74-
{
75-
return fileName.EndsWith(".psm1");
76-
}
77-
7869
public string GetCommonName()
7970
{
8071
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsCommonName);
@@ -87,7 +78,11 @@ public string GetDescription()
8778

8879
public string GetName()
8980
{
90-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsName);
81+
return string.Format(
82+
CultureInfo.CurrentCulture,
83+
Strings.NameSpaceFormat,
84+
GetSourceName(),
85+
Strings.AvoidGlobalFunctionsName);
9186
}
9287

9388
public RuleSeverity GetSeverity()

Tests/Rules/AvoidGlobalAliases.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Import-Module PSScriptAnalyzer
22

33
$AvoidGlobalAliasesError = "Avoid creating aliases with a Global scope."
4-
$violationName = "AvoidGlobalAliases"
4+
$violationName = "PSAvoidGlobalAliases"
55

66
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
77
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalAliases.psm1 | Where-Object {$_.RuleName -eq $violationName}

Tests/Rules/AvoidGlobalFunctions.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Import-Module PSScriptAnalyzer
22

33
$functionErroMessage = "Avoid creating functions with a Global scope."
4-
$violationName = "AvoidGlobalFunctions"
4+
$violationName = "PSAvoidGlobalFunctions"
55

66
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
77
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalFunctions.psm1 | Where-Object {$_.RuleName -eq $violationName}

0 commit comments

Comments
 (0)