|
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,89 @@ 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 hashTableAst = ast.Find(x => x is HashtableAst, false); |
| 54 | + yield return new DiagnosticRecord( |
| 55 | + errorRecord.Exception.Message, |
| 56 | + hashTableAst.Extent, |
| 57 | + GetName(), |
| 58 | + DiagnosticSeverity.Warning, |
| 59 | + fileName, |
| 60 | + suggestedCorrections:GetCorrectionExtent(hashTableAst as HashtableAst)); |
52 | 61 | }
|
53 | 62 |
|
54 | 63 | }
|
55 | 64 | }
|
56 | 65 | }
|
57 | 66 |
|
58 | 67 | }
|
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the correction extent |
| 71 | + /// </summary> |
| 72 | + /// <param name="ast"></param> |
| 73 | + /// <returns>A List of CorrectionExtent</returns> |
| 74 | + private List<CorrectionExtent> GetCorrectionExtent(HashtableAst ast) |
| 75 | + { |
| 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; |
| 104 | + } |
| 105 | + |
| 106 | + var correctionExtents = new List<CorrectionExtent>(); |
| 107 | + string fieldName = "ModuleVersion"; |
| 108 | + string fieldValue = "1.0.0.0"; |
| 109 | + string description = string.Format( |
| 110 | + CultureInfo.CurrentCulture, |
| 111 | + Strings.MissingModuleManifestFieldCorrectionDescription, |
| 112 | + fieldName, |
| 113 | + fieldValue); |
| 114 | + var correctionTextTemplate = @" |
| 115 | +# Version number of this module. |
| 116 | +{0} = '{1}' |
| 117 | +"; |
| 118 | + var correctionText = string.Format( |
| 119 | + correctionTextTemplate, |
| 120 | + fieldName, |
| 121 | + fieldValue); |
| 122 | + var correctionExtent = new CorrectionExtent( |
| 123 | + startLineNumber, |
| 124 | + startLineNumber, |
| 125 | + startColumnNumber, |
| 126 | + startColumnNumber, |
| 127 | + correctionText, |
| 128 | + ast.Extent.File, |
| 129 | + description); |
| 130 | + correctionExtents.Add(correctionExtent); |
| 131 | + return correctionExtents; |
| 132 | + } |
59 | 133 |
|
60 | 134 | /// <summary>
|
61 | 135 | /// GetName: Retrieves the name of this rule.
|
|
0 commit comments