|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Management.Automation.Language; |
| 8 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 9 | +#if !CORECLR |
| 10 | +using System.ComponentModel.Composition; |
| 11 | +#endif |
| 12 | +using System.Globalization; |
| 13 | + |
| 14 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// AvoidMultipleTypeAttributes: Check that parameter does not be assigned to multiple types. |
| 18 | + /// </summary> |
| 19 | +#if !CORECLR |
| 20 | + [Export(typeof(IScriptRule))] |
| 21 | +#endif |
| 22 | + public sealed class AvoidMultipleTypeAttributes : IScriptRule |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// AvoidMultipleTypeAttributes: Check that parameter does not be assigned to multiple types. |
| 26 | + /// </summary> |
| 27 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 28 | + { |
| 29 | + if (ast is null) |
| 30 | + { |
| 31 | + throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 32 | + } |
| 33 | + |
| 34 | + // Finds all ParamAsts. |
| 35 | + IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true); |
| 36 | + |
| 37 | + // Iterates all ParamAsts and check the number of its types. |
| 38 | + foreach (ParameterAst paramAst in paramAsts) |
| 39 | + { |
| 40 | + if (paramAst.Attributes.Where(typeAst => typeAst is TypeConstraintAst).Count() > 1) |
| 41 | + { |
| 42 | + yield return new DiagnosticRecord( |
| 43 | + String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesError, paramAst.Name), |
| 44 | + paramAst.Name.Extent, |
| 45 | + GetName(), |
| 46 | + DiagnosticSeverity.Warning, |
| 47 | + fileName); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// GetName: Retrieves the name of this rule. |
| 54 | + /// </summary> |
| 55 | + /// <returns>The name of this rule</returns> |
| 56 | + public string GetName() |
| 57 | + { |
| 58 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidMultipleTypeAttributesName); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// GetCommonName: Retrieves the common name of this rule. |
| 63 | + /// </summary> |
| 64 | + /// <returns>The common name of this rule</returns> |
| 65 | + public string GetCommonName() |
| 66 | + { |
| 67 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesCommonName); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// GetDescription: Retrieves the description of this rule. |
| 72 | + /// </summary> |
| 73 | + /// <returns>The description of this rule</returns> |
| 74 | + public string GetDescription() |
| 75 | + { |
| 76 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesDescription); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. |
| 81 | + /// </summary> |
| 82 | + public SourceType GetSourceType() |
| 83 | + { |
| 84 | + return SourceType.Builtin; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// GetSeverity: Retrieves the severity of the rule: error, warning or information. |
| 89 | + /// </summary> |
| 90 | + /// <returns></returns> |
| 91 | + public RuleSeverity GetSeverity() |
| 92 | + { |
| 93 | + return RuleSeverity.Warning; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// GetSourceName: Retrieves the name of the module/assembly the rule is from. |
| 98 | + /// </summary> |
| 99 | + public string GetSourceName() |
| 100 | + { |
| 101 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments