Skip to content

Commit 5da7ca2

Browse files
author
Kapil Borle
committed
Modify MissingModuleManifest correction behavior
* Insert ModuleVersion field at the end of the hashtable instead of the beginning * Fix MissingModuleManifest test case
1 parent b943634 commit 5da7ca2

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

Rules/MissingModuleManifestField.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5050
System.Diagnostics.Debug.Assert(
5151
errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message),
5252
Strings.NullErrorMessage);
53-
var hashTableAsts = ast.Find(x => x is HashtableAst, false);
53+
var hashTableAst = ast.Find(x => x is HashtableAst, false);
5454
yield return new DiagnosticRecord(
5555
errorRecord.Exception.Message,
56-
ast.Extent,
56+
hashTableAst.Extent,
5757
GetName(),
5858
DiagnosticSeverity.Warning,
5959
fileName,
60-
suggestedCorrections:GetCorrectionExtent(hashTableAsts as HashtableAst));
60+
suggestedCorrections:GetCorrectionExtent(hashTableAst as HashtableAst));
6161
}
6262

6363
}
@@ -66,33 +66,54 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6666

6767
}
6868

69-
7069
/// <summary>
7170
/// Gets the correction extent
7271
/// </summary>
7372
/// <param name="ast"></param>
7473
/// <returns>A List of CorrectionExtent</returns>
7574
private List<CorrectionExtent> GetCorrectionExtent(HashtableAst ast)
7675
{
77-
// we assume the extent begins with "@{"
78-
if (ast.Extent.Text.IndexOf("@{") != 0)
79-
{
80-
return null;
76+
int startLineNumber;
77+
int startColumnNumber;
78+
79+
// for empty hashtable insert after after "@{"
80+
if (ast.KeyValuePairs.Count == 0)
81+
{
82+
// check if ast starts with "@{"
83+
if (ast.Extent.Text.IndexOf("@{") != 0)
84+
{
85+
return null;
86+
}
87+
startLineNumber = ast.Extent.StartLineNumber;
88+
startColumnNumber = ast.Extent.StartColumnNumber + 2; // 2 for "@{",
89+
}
90+
else // for non-empty hashtable insert after the last element
91+
{
92+
int maxLine = 0;
93+
int lastCol = 0;
94+
foreach (var keyVal in ast.KeyValuePairs)
95+
{
96+
if (keyVal.Item2.Extent.EndLineNumber > maxLine)
97+
{
98+
maxLine = keyVal.Item2.Extent.EndLineNumber;
99+
lastCol = keyVal.Item2.Extent.EndColumnNumber;
100+
}
101+
}
102+
startLineNumber = maxLine;
103+
startColumnNumber = lastCol;
81104
}
82105

83106
var correctionExtents = new List<CorrectionExtent>();
84107
string fieldName = "ModuleVersion";
85108
string fieldValue = "1.0.0.0";
86-
int startLineNumber = ast.Extent.StartLineNumber;
87-
int startColumnNumber = ast.Extent.StartColumnNumber + 2; // 2 for "@{",
88109
string description = string.Format(
89110
CultureInfo.CurrentCulture,
90111
Strings.MissingModuleManifestFieldCorrectionDescription,
91112
fieldName,
92113
fieldValue);
93114
var correctionTextTemplate = @"
94115
# Version number of this module.
95-
{0} = {1}
116+
{0} = '{1}'
96117
";
97118
var correctionText = string.Format(
98119
correctionTextTemplate,
@@ -102,7 +123,7 @@ private List<CorrectionExtent> GetCorrectionExtent(HashtableAst ast)
102123
startLineNumber,
103124
startLineNumber,
104125
startColumnNumber,
105-
startColumnNumber + 1,
126+
startColumnNumber,
106127
correctionText,
107128
ast.Extent.File,
108129
description);

Tests/Rules/AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ Describe "MissingRequiredFieldModuleManifest" {
2424
$violations.Message | Should Match $missingMessage
2525
}
2626

27-
$expectedCorrections = 1
28-
It "has $expectedCorrections suggested corrections" {
29-
$violations.SuggestedCorrections.Count | Should Be 1
27+
$numExpectedCorrections = 1
28+
It "has $numExpectedCorrections suggested corrections" {
29+
$violations.SuggestedCorrections.Count | Should Be $numExpectedCorrections
3030
}
3131

3232

3333
It "has the right suggested correction" {
3434
$expectedText = @'
35-
3635
# Version number of this module.
37-
ModuleVersion = 1.0.0.0
36+
ModuleVersion = '1.0.0.0'
3837
'@
3938
$violations[0].SuggestedCorrections[0].Text | Should Match $expectedText
4039
Get-ExtentText $violations[0].SuggestedCorrections[0] $violationFilepath | Should Match ""

0 commit comments

Comments
 (0)