Skip to content

Commit 6fcf059

Browse files
author
Kapil Borle
committed
Modify UseToExportFieldInManifest rule
* Doesn't check for VariablesToExport * Adds ability to wrap around correction string
1 parent c4065e7 commit 6fcf059

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

Rules/UseToExportFieldsInManifest.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Globalization;
2121
using System.Text.RegularExpressions;
2222
using System.Diagnostics;
23+
using System.Text;
2324

2425
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2526
{
@@ -33,7 +34,6 @@ public class UseToExportFieldsInManifest : IScriptRule
3334

3435
private const string functionsToExport = "FunctionsToExport";
3536
private const string cmdletsToExport = "CmdletsToExport";
36-
private const string variablesToExport = "VariablesToExport";
3737
private const string aliasesToExport = "AliasesToExport";
3838

3939
/// <summary>
@@ -70,7 +70,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7070
yield break;
7171
}
7272

73-
string[] manifestFields = { functionsToExport, cmdletsToExport, variablesToExport, aliasesToExport };
73+
string[] manifestFields = { functionsToExport, cmdletsToExport, aliasesToExport };
7474

7575
foreach(string field in manifestFields)
7676
{
@@ -85,19 +85,43 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8585
fileName,
8686
suggestedCorrections: GetCorrectionExtent(field, extent, psModuleInfo));
8787
}
88+
else
89+
{
90+
91+
}
8892
}
8993

9094
}
9195

9296
private string GetListLiteral<T>(Dictionary<string, T> exportedItems)
9397
{
98+
const int lineWidth = 64;
9499
if (exportedItems == null || exportedItems.Keys == null)
95100
{
96101
return null;
97102
}
98-
var sbuilder = new System.Text.StringBuilder();
103+
var sbuilder = new StringBuilder();
99104
sbuilder.Append("@(");
100-
sbuilder.Append(string.Join(", ", exportedItems.Keys.Select(key => string.Format("'{0}'", key))));
105+
var sbuilderInner = new StringBuilder();
106+
int charLadder = lineWidth;
107+
int keyCount = exportedItems.Keys.Count;
108+
foreach (var key in exportedItems.Keys)
109+
{
110+
sbuilderInner.Append("'");
111+
sbuilderInner.Append(key);
112+
sbuilderInner.Append("'");
113+
if (--keyCount > 0)
114+
{
115+
sbuilderInner.Append(", ");
116+
if (sbuilderInner.Length > charLadder)
117+
{
118+
charLadder += lineWidth;
119+
sbuilderInner.AppendLine();
120+
sbuilderInner.Append('\t', 2);
121+
}
122+
}
123+
}
124+
sbuilder.Append(sbuilderInner);
101125
sbuilder.Append(")");
102126
return sbuilder.ToString();
103127
}
@@ -118,16 +142,16 @@ private List<CorrectionExtent> GetCorrectionExtent(string field, IScriptExtent e
118142
case cmdletsToExport:
119143
correctionText = GetListLiteral(psModuleInfo.ExportedCmdlets);
120144
break;
121-
case variablesToExport:
122-
correctionText = GetListLiteral(psModuleInfo.ExportedVariables);
123-
break;
124145
case aliasesToExport:
125146
correctionText = GetListLiteral(psModuleInfo.ExportedAliases);
126147
break;
127148
default:
128149
throw new NotImplementedException(string.Format("{0} not implemented", field));
129150
}
130-
string description = string.Format("Replace {0} with {1}", extent.Text, correctionText);
151+
string description = string.Format(
152+
Strings.UseToExportFieldsInManifestCorrectionDescription,
153+
extent.Text,
154+
correctionText);
131155
corrections.Add(new CorrectionExtent(
132156
extent.StartLineNumber,
133157
extent.EndLineNumber,

Tests/Rules/TestManifest/ManifestBadFunctionsNull.psm1

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
1-
Function Get-Foo
1+
Function Get-Foo1
22
{
33

44
}
55

6-
Function Get-Bar
6+
Function Get-Foo2
7+
{
8+
9+
}
10+
Function Get-Foo3
11+
{
12+
13+
}
14+
Function Get-Foo4
15+
{
16+
17+
}
18+
Function Get-Foo5
19+
{
20+
21+
}
22+
Function Get-Foo6
23+
{
24+
25+
}
26+
Function Get-Foo7
27+
{
28+
29+
}
30+
Function Get-Foo8
31+
{
32+
33+
}
34+
Function Get-Foo9
35+
{
36+
37+
}
38+
Function Get-Foo10
39+
{
40+
41+
}
42+
Function Get-Foo11
43+
{
44+
45+
}
46+
Function Get-Foo12
747
{
848

949
}
1050

11-
Export-ModuleMember -Function Get-Foo
12-
Export-ModuleMember -Function Get-Bar

Tests/Rules/UseToExportFieldsInManifest.tests.ps1

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,18 @@ Describe "UseManifestExportFields" {
5353
$results[0].Extent.Text | Should be '$null'
5454
}
5555

56-
It "suggests corrections for FunctionsToExport with null" {
56+
It "suggests corrections for FunctionsToExport with null and line wrapping" {
5757
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsNullPath
5858
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsNullPath
59-
Test-CorrectionExtent $violationFilepath $violations[0] 1 '$null' "@('Get-Foo', 'Get-Bar')"
59+
Test-CorrectionExtent $violationFilepath $violations[0] 1 '$null' "@('Get-Foo1', 'Get-Foo2', 'Get-Foo3', 'Get-Foo4', 'Get-Foo5', 'Get-Foo6', `r`n`t`t'Get-Foo7', 'Get-Foo8', 'Get-Foo9', 'Get-Foo10', 'Get-Foo11', `r`n`t`t'Get-Foo12')"
6060
}
6161

6262
It "detects array element containing wildcard" {
63+
# if more than two elements contain wildcard we can show only the first one as of now.
6364
$results = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardInArrayPath
64-
$results.Count | Should be 3
65+
$results.Count | Should be 2
6566
$results.Where({$_.Message -match "FunctionsToExport"}).Extent.Text | Should be "'Get-*'"
6667
$results.Where({$_.Message -match "CmdletsToExport"}).Extent.Text | Should be "'Update-*'"
67-
68-
# if more than two elements contain wildcard we can show only the first one as of now.
69-
$results.Where({$_.Message -match "VariablesToExport"}).Extent.Text | Should be "'foo*'"
7068
}
7169

7270

@@ -88,15 +86,9 @@ Describe "UseManifestExportFields" {
8886
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('gfoo', 'gbar')"
8987
}
9088

91-
It "detects VariablesToExport with wildcard" {
92-
$results = Run-PSScriptAnalyzerRule $testManifestBadVariablesWildcardPath
93-
$results.Count | Should be 1
94-
$results[0].Extent.Text | Should be "'*'"
95-
}
96-
9789
It "detects all the *ToExport violations" {
9890
$results = Run-PSScriptAnalyzerRule $testManifestBadAllPath
99-
$results.Count | Should be 4
91+
$results.Count | Should be 3
10092
}
10193
}
10294

0 commit comments

Comments
 (0)