|
| 1 | +using System; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Management.Automation; |
| 8 | +using System.Management.Automation.Language; |
| 9 | +using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; |
| 10 | +using System.ComponentModel.Composition; |
| 11 | +using System.Resources; |
| 12 | +using System.Globalization; |
| 13 | +using System.Threading; |
| 14 | +using System.Reflection; |
| 15 | + |
| 16 | +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// AvoidShouldContinueWithoutForceParameter: Check that if ShouldContinue is used, |
| 20 | + /// the function should have a boolean force parameter |
| 21 | + /// </summary> |
| 22 | + [Export(typeof(IScriptRule))] |
| 23 | + public class AvoidShouldContinueWithoutForce : IScriptRule |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// AvoidShouldContinueWithoutForceCheck that if ShouldContinue is used, |
| 27 | + /// the function should have a boolean force parameter |
| 28 | + /// </summary> |
| 29 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 30 | + { |
| 31 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 32 | + |
| 33 | + // Finds all ParamAsts. |
| 34 | + IEnumerable<Ast> funcAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true); |
| 35 | + |
| 36 | + // Iterrates all ParamAsts and check if there are any force. |
| 37 | + foreach (FunctionDefinitionAst funcAst in funcAsts) |
| 38 | + { |
| 39 | + IEnumerable<Ast> paramAsts = funcAst.FindAll(testAst => testAst is ParameterAst, true); |
| 40 | + bool hasForce = false; |
| 41 | + |
| 42 | + foreach (ParameterAst paramAst in paramAsts) |
| 43 | + { |
| 44 | + if (String.Equals(paramAst.Name.VariablePath.ToString(), "force", StringComparison.OrdinalIgnoreCase) |
| 45 | + && String.Equals(paramAst.StaticType.FullName, "System.Boolean", StringComparison.OrdinalIgnoreCase)) |
| 46 | + { |
| 47 | + hasForce = true; |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (hasForce) |
| 53 | + { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + IEnumerable<Ast> imeAsts = funcAst.FindAll(testAst => testAst is InvokeMemberExpressionAst, true); |
| 58 | + |
| 59 | + foreach (InvokeMemberExpressionAst imeAst in imeAsts) |
| 60 | + { |
| 61 | + VariableExpressionAst typeAst = imeAst.Expression as VariableExpressionAst; |
| 62 | + if (typeAst == null) continue; |
| 63 | + |
| 64 | + if (String.Equals(typeAst.VariablePath.UserPath, "pscmdlet", StringComparison.OrdinalIgnoreCase) |
| 65 | + && (String.Equals(imeAst.Member.Extent.Text, "shouldcontinue", StringComparison.OrdinalIgnoreCase))) |
| 66 | + { |
| 67 | + yield return new DiagnosticRecord( |
| 68 | + String.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceError, funcAst.Name, System.IO.Path.GetFileName(fileName)), |
| 69 | + imeAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// GetName: Retrieves the name of this rule. |
| 77 | + /// </summary> |
| 78 | + /// <returns>The name of this rule</returns> |
| 79 | + public string GetName() |
| 80 | + { |
| 81 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidShouldContinueWithoutForceName); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// GetCommonName: Retrieves the common name of this rule. |
| 86 | + /// </summary> |
| 87 | + /// <returns>The common name of this rule</returns> |
| 88 | + public string GetCommonName() |
| 89 | + { |
| 90 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceCommonName); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// GetDescription: Retrieves the description of this rule. |
| 95 | + /// </summary> |
| 96 | + /// <returns>The description of this rule</returns> |
| 97 | + public string GetDescription() |
| 98 | + { |
| 99 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceDescription); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. |
| 104 | + /// </summary> |
| 105 | + public SourceType GetSourceType() |
| 106 | + { |
| 107 | + return SourceType.Builtin; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 112 | + /// </summary> |
| 113 | + public string GetSourceName() |
| 114 | + { |
| 115 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments