|
17 | 17 | using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
|
18 | 18 | using System.ComponentModel.Composition;
|
19 | 19 | using System.Globalization;
|
| 20 | +using System.Text; |
20 | 21 |
|
21 | 22 | namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
|
22 | 23 | {
|
@@ -46,16 +47,60 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
|
46 | 47 | {
|
47 | 48 | if (Helper.IsMissingManifestMemberException(errorRecord))
|
48 | 49 | {
|
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)); |
52 | 61 | }
|
53 | 62 |
|
54 | 63 | }
|
55 | 64 | }
|
56 | 65 | }
|
57 | 66 |
|
58 | 67 | }
|
| 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 | + } |
59 | 104 |
|
60 | 105 | /// <summary>
|
61 | 106 | /// GetName: Retrieves the name of this rule.
|
|
0 commit comments