Skip to content

Commit 5cd8a3a

Browse files
author
Kapil Borle
committed
Add suggested correction to MissingModuleManifestField rule
1 parent 5e29444 commit 5cd8a3a

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

Rules/MissingModuleManifestField.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1818
using System.ComponentModel.Composition;
1919
using System.Globalization;
20+
using System.Text;
2021

2122
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2223
{
@@ -46,16 +47,60 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4647
{
4748
if (Helper.IsMissingManifestMemberException(errorRecord))
4849
{
49-
System.Diagnostics.Debug.Assert(errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message), Strings.NullErrorMessage);
50-
yield return
51-
new DiagnosticRecord(errorRecord.Exception.Message, ast.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
50+
System.Diagnostics.Debug.Assert(
51+
errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message),
52+
Strings.NullErrorMessage);
53+
var hashTableAsts = ast.Find(x => x is HashtableAst, false);
54+
yield return new DiagnosticRecord(
55+
errorRecord.Exception.Message,
56+
ast.Extent,
57+
GetName(),
58+
DiagnosticSeverity.Warning,
59+
fileName,
60+
suggestedCorrections:GetCorrectionExtent(hashTableAsts as HashtableAst));
5261
}
5362

5463
}
5564
}
5665
}
5766

5867
}
68+
69+
70+
/// <summary>
71+
/// Gets the correction extent
72+
/// </summary>
73+
/// <param name="ast"></param>
74+
/// <returns>A List of CorrectionExtent</returns>
75+
private List<CorrectionExtent> GetCorrectionExtent(HashtableAst ast)
76+
{
77+
// we assume the extent begins with "@{"
78+
if (ast.Extent.Text.IndexOf("@{") != 0)
79+
{
80+
return null;
81+
}
82+
83+
var correctionExtents = new List<CorrectionExtent>();
84+
string moduleVersionText = @"
85+
# Version number of this module.
86+
ModuleVersion = '1.0.0.0'";
87+
int startLineNumber = ast.Extent.StartLineNumber;
88+
int startColumnNumber = ast.Extent.StartColumnNumber + 2; // 2 for "@{",
89+
var correctionText = new StringBuilder();
90+
correctionText.AppendLine();
91+
correctionText.Append(moduleVersionText);
92+
correctionText.AppendLine();
93+
correctionText.AppendLine();
94+
var correctionExtent = new CorrectionExtent(
95+
startLineNumber,
96+
startLineNumber,
97+
startColumnNumber,
98+
startColumnNumber + 1,
99+
correctionText.ToString(),
100+
ast.Extent.File);
101+
correctionExtents.Add(correctionExtent);
102+
return correctionExtents;
103+
}
59104

60105
/// <summary>
61106
/// GetName: Retrieves the name of this rule.

0 commit comments

Comments
 (0)