|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Management.Automation.Language; |
| 16 | +#if !CORECLR |
| 17 | +using System.ComponentModel.Composition; |
| 18 | +#endif |
| 19 | +using System.Globalization; |
| 20 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 21 | + |
| 22 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 23 | +{ |
| 24 | +#if !CORECLR |
| 25 | + [Export(typeof(IScriptRule))] |
| 26 | +#endif |
| 27 | + class UseLiteralInitializerForHashtable : AstVisitor, IScriptRule |
| 28 | + { |
| 29 | + private List<DiagnosticRecord> diagnosticRecords; |
| 30 | + private HashSet<string> presetTypeNameSet; |
| 31 | + private string fileName; |
| 32 | + |
| 33 | + public UseLiteralInitializerForHashtable() |
| 34 | + { |
| 35 | + var presetTypeNames = new string[] |
| 36 | + { |
| 37 | + "system.collection.hashtable", |
| 38 | + "collection.hashtable", |
| 39 | + "hashtable" |
| 40 | + }; |
| 41 | + presetTypeNameSet = new HashSet<string>(presetTypeNames, StringComparer.OrdinalIgnoreCase); |
| 42 | + diagnosticRecords = new List<DiagnosticRecord>(); |
| 43 | + } |
| 44 | + |
| 45 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 46 | + { |
| 47 | + if (ast == null) |
| 48 | + { |
| 49 | + throw new ArgumentNullException("ast"); |
| 50 | + } |
| 51 | + this.fileName = fileName; |
| 52 | + diagnosticRecords.Clear(); |
| 53 | + ast.Visit(this); |
| 54 | + return diagnosticRecords; |
| 55 | + } |
| 56 | + |
| 57 | + public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 58 | + { |
| 59 | + if (commandAst == null |
| 60 | + || commandAst.CommandElements.Count < 2) |
| 61 | + { |
| 62 | + return AstVisitAction.SkipChildren; |
| 63 | + } |
| 64 | + |
| 65 | + if (!commandAst.GetCommandName().Equals("new-object", StringComparison.OrdinalIgnoreCase)) |
| 66 | + { |
| 67 | + return AstVisitAction.Continue; |
| 68 | + } |
| 69 | + AnalyzeNewObjectCommand(commandAst); |
| 70 | + return AstVisitAction.Continue; |
| 71 | + } |
| 72 | + |
| 73 | + private void AnalyzeNewObjectCommand(CommandAst commandAst) |
| 74 | + { |
| 75 | + //new-object hashtable |
| 76 | + var typeNameAst = commandAst.CommandElements[1] as StringConstantExpressionAst; |
| 77 | + if (typeNameAst != null) |
| 78 | + { |
| 79 | + if (presetTypeNameSet.Contains(typeNameAst.Value)) |
| 80 | + { |
| 81 | + var dr = new DiagnosticRecord( |
| 82 | + Strings.UseLiteralInitilializerForHashtableDescription, |
| 83 | + commandAst.Extent, |
| 84 | + GetName(), |
| 85 | + GetDiagnosticSeverity(), |
| 86 | + fileName, |
| 87 | + ruleId: null, |
| 88 | + suggestedCorrections: null); |
| 89 | + diagnosticRecords.Add(dr); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst) |
| 95 | + { |
| 96 | + if (methodCallAst == null) |
| 97 | + { |
| 98 | + return AstVisitAction.SkipChildren; |
| 99 | + } |
| 100 | + |
| 101 | + var typeExprAst = methodCallAst.Expression as TypeExpressionAst; |
| 102 | + if (typeExprAst == null |
| 103 | + || !presetTypeNameSet.Contains(typeExprAst.TypeName.FullName)) |
| 104 | + { |
| 105 | + return AstVisitAction.Continue; |
| 106 | + } |
| 107 | + |
| 108 | + var memberStringConstantExprAst = methodCallAst.Member as StringConstantExpressionAst; |
| 109 | + if (memberStringConstantExprAst == null |
| 110 | + || !memberStringConstantExprAst.Value.Equals("new", StringComparison.OrdinalIgnoreCase)) |
| 111 | + { |
| 112 | + return AstVisitAction.Continue; |
| 113 | + } |
| 114 | + |
| 115 | + // no arguments provided to new |
| 116 | + if (methodCallAst.Arguments == null) |
| 117 | + { |
| 118 | + |
| 119 | + var dr = new DiagnosticRecord( |
| 120 | + Strings.UseLiteralInitilializerForHashtableDescription, |
| 121 | + methodCallAst.Extent, |
| 122 | + GetName(), |
| 123 | + GetDiagnosticSeverity(), |
| 124 | + fileName, |
| 125 | + ruleId: null, |
| 126 | + suggestedCorrections: null); |
| 127 | + diagnosticRecords.Add(dr); |
| 128 | + } |
| 129 | + |
| 130 | + return AstVisitAction.Continue; |
| 131 | + } |
| 132 | + |
| 133 | + public string GetCommonName() |
| 134 | + { |
| 135 | + return Strings.UseLiteralInitilializerForHashtableCommonName; |
| 136 | + } |
| 137 | + |
| 138 | + public string GetDescription() |
| 139 | + { |
| 140 | + return Strings.UseLiteralInitilializerForHashtableDescription; |
| 141 | + } |
| 142 | + |
| 143 | + public string GetName() |
| 144 | + { |
| 145 | + return Strings.UseLiteralInitilializerForHashtableName; |
| 146 | + } |
| 147 | + |
| 148 | + public RuleSeverity GetSeverity() |
| 149 | + { |
| 150 | + return RuleSeverity.Warning; |
| 151 | + } |
| 152 | + |
| 153 | + private DiagnosticSeverity GetDiagnosticSeverity() |
| 154 | + { |
| 155 | + return DiagnosticSeverity.Warning; |
| 156 | + } |
| 157 | + |
| 158 | + public string GetSourceName() |
| 159 | + { |
| 160 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 161 | + } |
| 162 | + |
| 163 | + public SourceType GetSourceType() |
| 164 | + { |
| 165 | + return SourceType.Builtin; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments