Skip to content

Commit 5173062

Browse files
author
Kapil Borle
committed
Modifies scriptextent for UseToExporFieldsInModuleManifest
1 parent 025c8de commit 5173062

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

Rules/UseToExportFieldsInManifest.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,39 +101,25 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, string scri
101101
{
102102
var arrayAst = pair.Item2.Find(x => x is ArrayLiteralAst || x is ArrayExpressionAst, true);
103103
if (arrayAst == null)
104-
{
105-
extent = GetScriptExtent(pair, scriptText);
104+
{
105+
extent = pair.Item2.Extent;
106106
return false;
107-
}
107+
}
108108
else
109109
{
110+
var elementWithWildcard = arrayAst.Find(x => x is StringConstantExpressionAst
111+
&& x.Extent.Text.Contains("*"), false);
112+
if (elementWithWildcard != null)
113+
{
114+
extent = elementWithWildcard.Extent;
115+
return false;
116+
}
110117
return true;
111118
}
112119
}
113120
}
114121
return true;
115-
}
116-
117-
/// <summary>
118-
/// Gets the script extent.
119-
/// </summary>
120-
/// <param name="pair"></param>
121-
/// <param name="scriptText"></param>
122-
/// <returns></returns>
123-
private ScriptExtent GetScriptExtent(Tuple<ExpressionAst, StatementAst> pair, string scriptText)
124-
{
125-
string[] scriptLines = Regex.Split(scriptText, "\r\n|\r|\n");
126-
return new ScriptExtent(new ScriptPosition(pair.Item1.Extent.File,
127-
pair.Item1.Extent.StartLineNumber,
128-
pair.Item1.Extent.StartColumnNumber,
129-
scriptLines[pair.Item1.Extent.StartLineNumber - 1]), //line number begins with 1
130-
new ScriptPosition(pair.Item2.Extent.File,
131-
pair.Item2.Extent.EndLineNumber,
132-
pair.Item2.Extent.EndColumnNumber,
133-
scriptLines[pair.Item2.Extent.EndLineNumber - 1])); //line number begins with 1
134-
135-
136-
}
122+
}
137123

138124
public string GetError(string field)
139125
{

Tests/Rules/UseToExportFieldsInManifest.tests.ps1

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Import-Module PSScriptAnalyzer
22
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
33
$testManifestPath = Join-Path $directory "TestManifest"
44
$testManifestBadFunctionsWildcardPath = "ManifestBadFunctionsWildcard.psd1"
5+
$testManifestBadFunctionsWildcardInArrayPath = "ManifestBadFunctionsWildcardInArray.psd1"
56
$testManifestBadFunctionsNullPath = "ManifestBadFunctionsNull.psd1"
67
$testManifestBadCmdletsWildcardPath = "ManifestBadCmdletsWildcard.psd1"
78
$testManifestBadAliasesWildcardPath = "ManifestBadAliasesWildcard.psd1"
@@ -35,31 +36,42 @@ Describe "UseManifestExportFields" {
3536
It "detects FunctionsToExport with wildcard" {
3637
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardPath
3738
$results.Count | Should be 1
38-
$results[0].Extent.Text | Should be "FunctionsToExport = '*'"
39+
$results[0].Extent.Text | Should be "'*'"
3940
}
4041

4142
It "detects FunctionsToExport with null" {
4243
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsNullPath
4344
$results.Count | Should be 1
44-
$results[0].Extent.Text | Should be 'FunctionsToExport = $null'
45+
$results[0].Extent.Text | Should be '$null'
4546
}
4647

48+
It "detects array element containing wildcard" {
49+
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardInArrayPath
50+
$results.Count | Should be 3
51+
$results.Where({$_.Message -match "FunctionsToExport"}).Extent.Text | Should be "'Get-*'"
52+
$results.Where({$_.Message -match "CmdletsToExport"}).Extent.Text | Should be "'Update-*'"
53+
54+
# if more than two elements contain wildcard we can show only the first one as of now.
55+
$results.Where({$_.Message -match "VariablesToExport"}).Extent.Text | Should be "'foo*'"
56+
}
57+
58+
4759
It "detects CmdletsToExport with wildcard" {
4860
$results = Run-PSScriptAnalyzerRule $testManifestBadCmdletsWildcardPath
4961
$results.Count | Should be 1
50-
$results[0].Extent.Text | Should be "CmdletsToExport = '*'"
62+
$results[0].Extent.Text | Should be "'*'"
5163
}
5264

5365
It "detects AliasesToExport with wildcard" {
5466
$results = Run-PSScriptAnalyzerRule $testManifestBadAliasesWildcardPath
5567
$results.Count | Should be 1
56-
$results[0].Extent.Text | Should be "AliasesToExport = '*'"
68+
$results[0].Extent.Text | Should be "'*'"
5769
}
5870

5971
It "detects VariablesToExport with wildcard" {
6072
$results = Run-PSScriptAnalyzerRule $testManifestBadVariablesWildcardPath
6173
$results.Count | Should be 1
62-
$results[0].Extent.Text | Should be "VariablesToExport = '*'"
74+
$results[0].Extent.Text | Should be "'*'"
6375
}
6476

6577
It "detects all the *ToExport violations" {

0 commit comments

Comments
 (0)