diff --git a/Engine/Generic/AvoidCmdletGeneric.cs b/Engine/Generic/AvoidCmdletGeneric.cs index 719a35d7a..573fe3b4f 100644 --- a/Engine/Generic/AvoidCmdletGeneric.cs +++ b/Engine/Generic/AvoidCmdletGeneric.cs @@ -77,10 +77,6 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// The source name of the rule. public abstract string GetSourceName(); - /// - /// GetSourceType: Retrieves the source type of the rule. - /// - /// The source type of the rule. public abstract SourceType GetSourceType(); /// diff --git a/Engine/Generic/AvoidParameterGeneric.cs b/Engine/Generic/AvoidParameterGeneric.cs index 5f1725536..61160a146 100644 --- a/Engine/Generic/AvoidParameterGeneric.cs +++ b/Engine/Generic/AvoidParameterGeneric.cs @@ -87,10 +87,6 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// The source name of the rule. public abstract string GetSourceName(); - /// - /// GetSourceType: Retrieves the source type of the rule. - /// - /// The source type of the rule. public abstract SourceType GetSourceType(); /// diff --git a/Engine/Generic/ConfigurableRule.cs b/Engine/Generic/ConfigurableRule.cs index 63dc5cd93..5dec169cd 100644 --- a/Engine/Generic/ConfigurableRule.cs +++ b/Engine/Generic/ConfigurableRule.cs @@ -103,10 +103,6 @@ public virtual void ConfigureRule(IDictionary paramValueMap) /// The source name of the rule. public abstract string GetSourceName(); - /// - /// Retrieves the source type of the rule. - /// - /// The source type of the rule. public abstract SourceType GetSourceType(); private void SetDefaultValues() diff --git a/Engine/Generic/ExternalRule.cs b/Engine/Generic/ExternalRule.cs index 55379c3ba..6ae19fa53 100644 --- a/Engine/Generic/ExternalRule.cs +++ b/Engine/Generic/ExternalRule.cs @@ -42,10 +42,7 @@ public string GetParameter() return this.param; } - public SourceType GetSourceType() - { - return SourceType.Module; - } + public SourceType GetSourceType() => SourceType.Module; public string GetParameterType() { @@ -71,7 +68,7 @@ public string GetFullModulePath() #endregion #region Constructors - + public ExternalRule() { diff --git a/Rules/AlignAssignmentStatement.cs b/Rules/AlignAssignmentStatement.cs index 5b941924a..f74b2192c 100644 --- a/Rules/AlignAssignmentStatement.cs +++ b/Rules/AlignAssignmentStatement.cs @@ -481,7 +481,7 @@ var member in enumTypeDefAst.Members.Where( // Next we need to find the location of the equals sign for this // member. We know the line it should be on. We can // search all of the equals signs on that line. - // + // // Unlike hashtables, we don't have an extent for the LHS and // RHS of the member. We have the extent of the entire // member, the name of the member, and the extent of the @@ -699,12 +699,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index 9efe0e916..1a186230f 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -53,7 +53,7 @@ private void SetProperties() { return; } - // Fallback for object from legacy allowlist argument name + // Fallback for object from legacy allowlist argument name if (obj == null) { obj = objLegacy; } @@ -260,13 +260,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidAssignmentToAutomaticVariable.cs b/Rules/AvoidAssignmentToAutomaticVariable.cs index c1ce88462..e44691230 100644 --- a/Rules/AvoidAssignmentToAutomaticVariable.cs +++ b/Rules/AvoidAssignmentToAutomaticVariable.cs @@ -178,13 +178,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidDefaultTrueValueSwitchParameter.cs b/Rules/AvoidDefaultTrueValueSwitchParameter.cs index 3889959c0..c220300fb 100644 --- a/Rules/AvoidDefaultTrueValueSwitchParameter.cs +++ b/Rules/AvoidDefaultTrueValueSwitchParameter.cs @@ -80,13 +80,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidDefaultValueForMandatoryParameter.cs b/Rules/AvoidDefaultValueForMandatoryParameter.cs index 17925ae97..3bb583594 100644 --- a/Rules/AvoidDefaultValueForMandatoryParameter.cs +++ b/Rules/AvoidDefaultValueForMandatoryParameter.cs @@ -75,9 +75,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// The parameter AST to examine /// String comparer for case-insensitive attribute name matching /// - /// True if the parameter has at least one [Parameter] attribute and ALL of them + /// True if the parameter has at least one [Parameter] attribute and ALL of them /// have the Mandatory named argument set to true (explicitly or implicitly). - /// False if the parameter has no [Parameter] attributes or if any [Parameter] + /// False if the parameter has no [Parameter] attributes or if any [Parameter] /// attribute does not have Mandatory=true. /// private static bool HasMandatoryInAllParameterAttributes(ParameterAst paramAst) @@ -122,13 +122,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidEmptyCatchBlock.cs b/Rules/AvoidEmptyCatchBlock.cs index 116102961..1370fe50c 100644 --- a/Rules/AvoidEmptyCatchBlock.cs +++ b/Rules/AvoidEmptyCatchBlock.cs @@ -70,13 +70,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingEmptyCatchBlockDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidExclaimOperator.cs b/Rules/AvoidExclaimOperator.cs index 5521463ea..b0edc9b0e 100644 --- a/Rules/AvoidExclaimOperator.cs +++ b/Rules/AvoidExclaimOperator.cs @@ -67,12 +67,12 @@ public override IEnumerable AnalyzeScript(Ast ast, string file }; diagnosticRecords.Add(new DiagnosticRecord( string.Format( - CultureInfo.CurrentCulture, + CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorError - ), - unaryExpressionAst.Extent, + ), + unaryExpressionAst.Extent, GetName(), - GetDiagnosticSeverity(), + GetDiagnosticSeverity(), fileName, suggestedCorrections: corrections )); @@ -135,13 +135,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidGlobalAliases.cs b/Rules/AvoidGlobalAliases.cs index 8697ad1ca..15176b962 100644 --- a/Rules/AvoidGlobalAliases.cs +++ b/Rules/AvoidGlobalAliases.cs @@ -130,9 +130,6 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; } } \ No newline at end of file diff --git a/Rules/AvoidGlobalFunctions.cs b/Rules/AvoidGlobalFunctions.cs index da8f36f52..6d7ab0c0d 100644 --- a/Rules/AvoidGlobalFunctions.cs +++ b/Rules/AvoidGlobalFunctions.cs @@ -101,9 +101,6 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidGlobalVars.cs b/Rules/AvoidGlobalVars.cs index 9ce1f076a..d933ade44 100644 --- a/Rules/AvoidGlobalVars.cs +++ b/Rules/AvoidGlobalVars.cs @@ -74,13 +74,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidInvokingEmptyMembers.cs b/Rules/AvoidInvokingEmptyMembers.cs index 3f625e31c..42ed63f16 100644 --- a/Rules/AvoidInvokingEmptyMembers.cs +++ b/Rules/AvoidInvokingEmptyMembers.cs @@ -16,7 +16,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules // Rule name is inconsistent. /// /// AvoidInvokingEmptyMembers: Analyzes the script to check if any non-constant members have been invoked. - /// + /// #if !CORECLR [Export(typeof(IScriptRule))] #endif @@ -55,10 +55,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } } } - + } - + /// /// GetName: Retrieves the name of this rule. /// @@ -86,13 +86,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidInvokingEmptyMembersDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidLongLines.cs b/Rules/AvoidLongLines.cs index ad527bb37..d6ed28de0 100644 --- a/Rules/AvoidLongLines.cs +++ b/Rules/AvoidLongLines.cs @@ -78,7 +78,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file )); var record = new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, + String.Format(CultureInfo.CurrentCulture, String.Format(Strings.AvoidLongLinesError, MaximumLineLength)), violationExtent, GetName(), @@ -145,12 +145,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidMultipleTypeAttributes.cs b/Rules/AvoidMultipleTypeAttributes.cs index 590a058d9..594af6e3c 100644 --- a/Rules/AvoidMultipleTypeAttributes.cs +++ b/Rules/AvoidMultipleTypeAttributes.cs @@ -26,7 +26,7 @@ public sealed class AvoidMultipleTypeAttributes : IScriptRule /// public IEnumerable AnalyzeScript(Ast ast, string fileName) { - if (ast is null) + if (ast is null) { throw new ArgumentNullException(Strings.NullAstErrorMessage); } @@ -42,13 +42,13 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) yield return new DiagnosticRecord( String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesError, paramAst.Name), paramAst.Name.Extent, - GetName(), - DiagnosticSeverity.Warning, + GetName(), + DiagnosticSeverity.Warning, fileName); } } } - + /// /// GetName: Retrieves the name of this rule. /// @@ -76,13 +76,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs b/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs index 3dbe949d9..4c82ebe91 100644 --- a/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs +++ b/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs @@ -19,7 +19,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules [Export(typeof(IScriptRule))] #endif public class AvoidNullOrEmptyHelpMessageAttribute : IScriptRule - { + { /// /// AvoidUsingNullOrEmptyHelpMessageAttribute: Check if the HelpMessage parameter is set to a non-empty string. /// @@ -36,7 +36,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } - + foreach (ParameterAst paramAst in funcAst.Body.ParamBlock.Parameters) { if (paramAst == null) @@ -57,19 +57,19 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } - + foreach (NamedAttributeArgumentAst namedArgument in namedArguments) { if (namedArgument == null || !(namedArgument.ArgumentName.Equals("HelpMessage", StringComparison.OrdinalIgnoreCase))) { continue; } - + string errCondition; if (namedArgument.ExpressionOmitted || HasEmptyStringInExpression(namedArgument.Argument)) { errCondition = "empty"; - } + } else if (HasNullInExpression(namedArgument.Argument)) { errCondition = "null"; @@ -84,16 +84,16 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) Strings.AvoidNullOrEmptyHelpMessageAttributeError, paramAst.Name.VariablePath.UserPath); yield return new DiagnosticRecord(message, - paramAst.Extent, - GetName(), - DiagnosticSeverity.Warning, - fileName, + paramAst.Extent, + GetName(), + DiagnosticSeverity.Warning, + fileName, paramAst.Name.VariablePath.UserPath); - } - } + } + } } } - + } } @@ -148,13 +148,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidOverwritingBuiltInCmdlets.cs b/Rules/AvoidOverwritingBuiltInCmdlets.cs index a390178d5..2413d72be 100644 --- a/Rules/AvoidOverwritingBuiltInCmdlets.cs +++ b/Rules/AvoidOverwritingBuiltInCmdlets.cs @@ -251,12 +251,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidPositionalParameters.cs b/Rules/AvoidPositionalParameters.cs index 2071baebd..d243f7313 100644 --- a/Rules/AvoidPositionalParameters.cs +++ b/Rules/AvoidPositionalParameters.cs @@ -61,7 +61,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file // You can also review the remark section in following document, // MSDN: CommandAst.GetCommandName Method if (cmdAst.GetCommandName() == null) continue; - + if ((Helper.Instance.IsKnownCmdletFunctionOrExternalScript(cmdAst, out CommandInfo commandInfo) || declaredFunctionNames.Contains(cmdAst.GetCommandName())) && (Helper.Instance.PositionalParameterUsed(cmdAst, true))) { @@ -119,13 +119,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidReservedCharInCmdlet.cs b/Rules/AvoidReservedCharInCmdlet.cs index e4ae3de6e..58b0d2726 100644 --- a/Rules/AvoidReservedCharInCmdlet.cs +++ b/Rules/AvoidReservedCharInCmdlet.cs @@ -55,9 +55,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharError, funcAst.Name), funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); - } + } } - } + } } /// @@ -85,13 +85,7 @@ public string GetDescription() { return string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidReservedParams.cs b/Rules/AvoidReservedParams.cs index 4035a9c89..374a5f4ce 100644 --- a/Rules/AvoidReservedParams.cs +++ b/Rules/AvoidReservedParams.cs @@ -93,13 +93,7 @@ public string GetDescription() { return string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidReservedWordsAsFunctionNames.cs b/Rules/AvoidReservedWordsAsFunctionNames.cs index 921909704..380993060 100644 --- a/Rules/AvoidReservedWordsAsFunctionNames.cs +++ b/Rules/AvoidReservedWordsAsFunctionNames.cs @@ -26,7 +26,7 @@ public class AvoidReservedWordsAsFunctionNames : IScriptRule // The list of PowerShell reserved words. // https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_reserved_words - // + // // The Below are omitted as they don't pose an issue being a function // name: // assembly, base, command, hidden, in, inlinescript, interface, module, diff --git a/Rules/AvoidSemicolonsAsLineTerminators.cs b/Rules/AvoidSemicolonsAsLineTerminators.cs index 68a121d77..132b53735 100644 --- a/Rules/AvoidSemicolonsAsLineTerminators.cs +++ b/Rules/AvoidSemicolonsAsLineTerminators.cs @@ -164,12 +164,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule: builtin, managed, or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidShouldContinueWithoutForce.cs b/Rules/AvoidShouldContinueWithoutForce.cs index 28a49facd..b9003cf97 100644 --- a/Rules/AvoidShouldContinueWithoutForce.cs +++ b/Rules/AvoidShouldContinueWithoutForce.cs @@ -41,7 +41,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) foreach (ParameterAst paramAst in paramAsts) { if (String.Equals(paramAst.Name.VariablePath.ToString(), "force", StringComparison.OrdinalIgnoreCase) - && (String.Equals(paramAst.StaticType.FullName, "System.Boolean", StringComparison.OrdinalIgnoreCase) + && (String.Equals(paramAst.StaticType.FullName, "System.Boolean", StringComparison.OrdinalIgnoreCase) || String.Equals(paramAst.StaticType.FullName, "System.Management.Automation.SwitchParameter", StringComparison.OrdinalIgnoreCase))) { hasForce = true; @@ -108,13 +108,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index a7567d6e6..1896bc72b 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -157,12 +157,6 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidUserNameAndPasswordParams.cs b/Rules/AvoidUserNameAndPasswordParams.cs index ced0c8ee1..9c1b33112 100644 --- a/Rules/AvoidUserNameAndPasswordParams.cs +++ b/Rules/AvoidUserNameAndPasswordParams.cs @@ -55,7 +55,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) foreach (ParameterAst paramAst in paramAsts) { var attributes = typeAllowList.Select(x => GetAttributeOfType(paramAst.Attributes, x)); - String paramName = paramAst.Name.VariablePath.ToString(); + String paramName = paramAst.Name.VariablePath.ToString(); foreach (String password in passwords) { if (paramName.IndexOf(password, StringComparison.OrdinalIgnoreCase) != -1) @@ -116,7 +116,7 @@ private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordA var usrExt = usernameAst.Extent; var pwdExt = passwordAst.Extent; IScriptExtent startExt, endExt; - var usrBeforePwd + var usrBeforePwd = (usrExt.StartLineNumber == pwdExt.StartLineNumber && usrExt.StartColumnNumber < pwdExt.StartColumnNumber) || usrExt.StartLineNumber < pwdExt.StartLineNumber; @@ -170,13 +170,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidUsingAllowUnencryptedAuthentication.cs b/Rules/AvoidUsingAllowUnencryptedAuthentication.cs index 955de8113..1eaceed23 100644 --- a/Rules/AvoidUsingAllowUnencryptedAuthentication.cs +++ b/Rules/AvoidUsingAllowUnencryptedAuthentication.cs @@ -80,13 +80,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/AvoidUsingBrokenHashAlgorithms.cs b/Rules/AvoidUsingBrokenHashAlgorithms.cs index b43116020..1322463a6 100644 --- a/Rules/AvoidUsingBrokenHashAlgorithms.cs +++ b/Rules/AvoidUsingBrokenHashAlgorithms.cs @@ -145,13 +145,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/AvoidUsingComputerNameHardcoded.cs b/Rules/AvoidUsingComputerNameHardcoded.cs index 2202abe1d..6e4048340 100644 --- a/Rules/AvoidUsingComputerNameHardcoded.cs +++ b/Rules/AvoidUsingComputerNameHardcoded.cs @@ -145,13 +145,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidComputerNameHardcodedDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs index ff7873a5e..9938c1f23 100644 --- a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs +++ b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs @@ -94,13 +94,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingConvertToSecureStringWithPlainTextDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/AvoidUsingDeprecatedManifestFields.cs b/Rules/AvoidUsingDeprecatedManifestFields.cs index fd3431251..85a78d461 100644 --- a/Rules/AvoidUsingDeprecatedManifestFields.cs +++ b/Rules/AvoidUsingDeprecatedManifestFields.cs @@ -145,13 +145,7 @@ public string GetDescription() return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidUsingDoubleQuotesForConstantString.cs b/Rules/AvoidUsingDoubleQuotesForConstantString.cs index 5b2c232b7..7b525b046 100644 --- a/Rules/AvoidUsingDoubleQuotesForConstantString.cs +++ b/Rules/AvoidUsingDoubleQuotesForConstantString.cs @@ -52,7 +52,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file This because one would then have to escape this single quote, which would make the string less readable. If an interpolated string contains a backtick character, then do not warn as well. This is because we'd have to replace the backtick in some cases like `$ and in others like `a it would not be possible at all. - Therefore to keep it simple, all interpolated strings with a backtick are being excluded. + Therefore to keep it simple, all interpolated strings with a backtick are being excluded. */ case StringConstantType.DoubleQuoted: if (stringConstantExpressionAst.Value.Contains("'") || stringConstantExpressionAst.Extent.Text.Contains("`")) @@ -150,12 +150,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/AvoidUsingInvokeExpression.cs b/Rules/AvoidUsingInvokeExpression.cs index 19a42f21a..cfe649681 100644 --- a/Rules/AvoidUsingInvokeExpression.cs +++ b/Rules/AvoidUsingInvokeExpression.cs @@ -63,13 +63,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionRuleDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity:Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidUsingPlainTextForPassword.cs b/Rules/AvoidUsingPlainTextForPassword.cs index 6d877e400..9f314cd4d 100644 --- a/Rules/AvoidUsingPlainTextForPassword.cs +++ b/Rules/AvoidUsingPlainTextForPassword.cs @@ -146,13 +146,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/AvoidUsingWMICmdlet.cs b/Rules/AvoidUsingWMICmdlet.cs index 09fcca7a7..501ca005d 100644 --- a/Rules/AvoidUsingWMICmdlet.cs +++ b/Rules/AvoidUsingWMICmdlet.cs @@ -37,8 +37,8 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) // Iterate all CommandAsts and check the command name foreach (CommandAst cmdAst in commandAsts) { - if (cmdAst.GetCommandName() != null && - (String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase) + if (cmdAst.GetCommandName() != null && + (String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase) || String.Equals(cmdAst.GetCommandName(), "remove-wmiobject", StringComparison.OrdinalIgnoreCase) || String.Equals(cmdAst.GetCommandName(), "invoke-wmimethod", StringComparison.OrdinalIgnoreCase) || String.Equals(cmdAst.GetCommandName(), "register-wmievent", StringComparison.OrdinalIgnoreCase) @@ -69,7 +69,7 @@ private int GetPSMajorVersion(Ast ast) if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); IEnumerable scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true); - + foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts) { if (null != scriptBlockAst.ScriptRequirements && null != scriptBlockAst.ScriptRequirements.RequiredPSVersion) @@ -109,13 +109,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// diff --git a/Rules/AvoidUsingWriteHost.cs b/Rules/AvoidUsingWriteHost.cs index fdd4feafa..aa7c4e7d3 100644 --- a/Rules/AvoidUsingWriteHost.cs +++ b/Rules/AvoidUsingWriteHost.cs @@ -140,13 +140,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWriteHostDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 8418202e4..85428a9db 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -118,13 +118,7 @@ public override RuleSeverity GetSeverity() return RuleSeverity.Warning; } - /// - /// Gets the source type of this rule. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// Gets the name of the source of this rule. diff --git a/Rules/CompatibilityRules/UseCompatibleSyntax.cs b/Rules/CompatibilityRules/UseCompatibleSyntax.cs index c7d10c19d..99d849ee1 100644 --- a/Rules/CompatibilityRules/UseCompatibleSyntax.cs +++ b/Rules/CompatibilityRules/UseCompatibleSyntax.cs @@ -114,13 +114,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Get the type of the source of this rule. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; private static HashSet GetTargetedVersions(string[] versionSettings) { diff --git a/Rules/DscExamplesPresent.cs b/Rules/DscExamplesPresent.cs index 17cca2a1c..49a206100 100644 --- a/Rules/DscExamplesPresent.cs +++ b/Rules/DscExamplesPresent.cs @@ -18,7 +18,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules /// DscExamplesPresent: Checks that DSC examples for given resource are present. /// Rule expects directory Examples to be present: /// For non-class based resources it should exist at the same folder level as DSCResources folder. - /// For class based resources it should be present at the same folder level as resource psm1 file. + /// For class based resources it should be present at the same folder level as resource psm1 file. /// Examples folder should contain sample configuration for given resource - file name should contain resource's name. /// #if !CORECLR @@ -43,7 +43,7 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName String fileNameOnly = Path.GetFileName(fileName); String resourceName = Path.GetFileNameWithoutExtension(fileNameOnly); String examplesQuery = String.Format("*{0}*", resourceName); - Boolean examplesPresent = false; + Boolean examplesPresent = false; String expectedExamplesPath = Path.Combine(new String[] {fileName, "..", "..", "..", "Examples"}); // Verify examples are present @@ -111,7 +111,7 @@ item is TypeDefinitionAst yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentNoExamplesError, resourceName), dscClass.Extent, GetName(), DiagnosticSeverity.Information, fileName); } - } + } } /// @@ -141,13 +141,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/DscTestsPresent.cs b/Rules/DscTestsPresent.cs index 3a19902d9..66f5b9542 100644 --- a/Rules/DscTestsPresent.cs +++ b/Rules/DscTestsPresent.cs @@ -18,7 +18,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules /// DscTestsPresent: Checks that DSC tests for given resource are present. /// Rule expects directory Tests to be present: /// For non-class based resources it should exist at the same folder level as DSCResources folder. - /// For class based resources it should be present at the same folder level as resource psm1 file. + /// For class based resources it should be present at the same folder level as resource psm1 file. /// Tests folder should contain test script for given resource - file name should contain resource's name. /// #if !CORECLR @@ -141,13 +141,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/MisleadingBacktick.cs b/Rules/MisleadingBacktick.cs index ea709b7af..3cb24083f 100644 --- a/Rules/MisleadingBacktick.cs +++ b/Rules/MisleadingBacktick.cs @@ -111,13 +111,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/MissingModuleManifestField.cs b/Rules/MissingModuleManifestField.cs index 180767766..57b672893 100644 --- a/Rules/MissingModuleManifestField.cs +++ b/Rules/MissingModuleManifestField.cs @@ -48,7 +48,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (Helper.IsMissingManifestMemberException(errorRecord)) { System.Diagnostics.Debug.Assert( - errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message), + errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message), Strings.NullErrorMessage); var hashTableAst = ast.Find(x => x is HashtableAst, false); if (hashTableAst == null) @@ -56,10 +56,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) yield break; } yield return new DiagnosticRecord( - errorRecord.Exception.Message, - hashTableAst.Extent, - GetName(), - DiagnosticSeverity.Warning, + errorRecord.Exception.Message, + hashTableAst.Extent, + GetName(), + DiagnosticSeverity.Warning, fileName, suggestedCorrections:GetCorrectionExtent(hashTableAst as HashtableAst)); } @@ -133,7 +133,7 @@ private List GetCorrectionExtent(HashtableAst ast) correctionExtents.Add(correctionExtent); return correctionExtents; } - + /// /// GetName: Retrieves the name of this rule. /// @@ -161,13 +161,7 @@ public string GetDescription() return String.Format(CultureInfo.CurrentCulture, Strings.MissingModuleManifestFieldDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index 652197afd..611b0b048 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -209,13 +209,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; private DiagnosticRecord GetViolationForBraceShouldNotFollowEmptyLine( Token[] tokens, diff --git a/Rules/PlaceOpenBrace.cs b/Rules/PlaceOpenBrace.cs index 5a74626d9..0596dca6a 100644 --- a/Rules/PlaceOpenBrace.cs +++ b/Rules/PlaceOpenBrace.cs @@ -177,13 +177,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; private static string GetError(string errorString) { diff --git a/Rules/PossibleIncorrectComparisonWithNull.cs b/Rules/PossibleIncorrectComparisonWithNull.cs index 692f49f13..87fd482a5 100644 --- a/Rules/PossibleIncorrectComparisonWithNull.cs +++ b/Rules/PossibleIncorrectComparisonWithNull.cs @@ -32,9 +32,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { IEnumerable binExpressionAsts = ast.FindAll(testAst => testAst is BinaryExpressionAst, false); foreach (BinaryExpressionAst binExpressionAst in binExpressionAsts) { - if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq) + if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq) || binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq)) - && binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) + && binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) { if (IncorrectComparisonWithNull(binExpressionAst, ast)) { @@ -62,9 +62,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { private bool IncorrectComparisonWithNull(BinaryExpressionAst binExpressionAst, Ast ast) { - if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq) + if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq) || binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq)) - && binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) + && binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) { if (binExpressionAst.Left.StaticType.IsArray) { @@ -135,13 +135,7 @@ public string GetDescription() { return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectComparisonWithNullDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs b/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs index e18bf54f9..93b52cae2 100644 --- a/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs +++ b/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs @@ -123,13 +123,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfAssignmentOperatorDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs b/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs index 9e33f19f2..3e997e293 100644 --- a/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs +++ b/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs @@ -71,13 +71,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfRedirectionOperatorDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/ProvideCommentHelp.cs b/Rules/ProvideCommentHelp.cs index 3a46f0ada..ff6250908 100644 --- a/Rules/ProvideCommentHelp.cs +++ b/Rules/ProvideCommentHelp.cs @@ -141,13 +141,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.ProvideCommentHelpDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/ReturnCorrectTypesForDSCFunctions.cs b/Rules/ReturnCorrectTypesForDSCFunctions.cs index 0c750571c..9aa3bab9f 100644 --- a/Rules/ReturnCorrectTypesForDSCFunctions.cs +++ b/Rules/ReturnCorrectTypesForDSCFunctions.cs @@ -175,7 +175,7 @@ item is TypeDefinitionAst /// /// The name of this rule public string GetName() - { + { return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReturnCorrectTypeDSCFunctionsName); } @@ -197,13 +197,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.ReturnCorrectTypesForDSCFunctionsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/ReviewUnusedParameter.cs b/Rules/ReviewUnusedParameter.cs index 9b727e7fc..0dd4c2aac 100644 --- a/Rules/ReviewUnusedParameter.cs +++ b/Rules/ReviewUnusedParameter.cs @@ -219,13 +219,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. @@ -259,7 +253,7 @@ IDictionary GetVariableCount(Ast ast, Dictionary data .Select(variableExpressionAst => ((VariableExpressionAst)variableExpressionAst).VariablePath.UserPath) .GroupBy(variableName => variableName, StringComparer.OrdinalIgnoreCase) .ToDictionary(variableName => variableName.Key, variableName => variableName.Count(), StringComparer.OrdinalIgnoreCase); - + foreach (string key in result.Keys) { if (content.ContainsKey(key)) diff --git a/Rules/UseApprovedVerbs.cs b/Rules/UseApprovedVerbs.cs index f74aead52..897533931 100644 --- a/Rules/UseApprovedVerbs.cs +++ b/Rules/UseApprovedVerbs.cs @@ -96,13 +96,7 @@ public string GetDescription() { return string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseBOMForUnicodeEncodedFile.cs b/Rules/UseBOMForUnicodeEncodedFile.cs index e399b6d0a..4eb8e4997 100644 --- a/Rules/UseBOMForUnicodeEncodedFile.cs +++ b/Rules/UseBOMForUnicodeEncodedFile.cs @@ -41,9 +41,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) // Did not detect the presence of BOM // Make sure there is no byte > 127 (0x7F) to ensure file is ASCII encoded // Else emit rule violation - + if (0 != byteStream.Count(o => o > 0x7F)) - { + { yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileError, System.IO.Path.GetFileName(fileName), null), null, GetName(), DiagnosticSeverity.Warning, fileName); } @@ -58,7 +58,7 @@ private Encoding GetByteStreamEncoding(byte[] byteStream) // Analyze BOM if (byteStream.Length >= 4 && byteStream[0] == 0x00 && byteStream[1] == 0x00 && byteStream[2] == 0xFE && byteStream[3] == 0xFF) { - // UTF-32, big-endian + // UTF-32, big-endian return Encoding.GetEncoding("utf-32BE"); } else if (byteStream.Length >= 4 && byteStream[0] == 0xFF && byteStream[1] == 0xFE && byteStream[2] == 0x00 && byteStream[3] == 0x00) @@ -89,7 +89,7 @@ private Encoding GetByteStreamEncoding(byte[] byteStream) // Did not detect BOM OR Unknown File encoding return null; - + } /// @@ -119,13 +119,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseCmdletCorrectly.cs b/Rules/UseCmdletCorrectly.cs index ccec27e0b..e4961c15e 100644 --- a/Rules/UseCmdletCorrectly.cs +++ b/Rules/UseCmdletCorrectly.cs @@ -227,13 +227,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseCmdletCorrectlyDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseCompatibleCmdlets.cs b/Rules/UseCompatibleCmdlets.cs index c74665e95..bb0ad0d9d 100644 --- a/Rules/UseCompatibleCmdlets.cs +++ b/Rules/UseCompatibleCmdlets.cs @@ -108,13 +108,7 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// Analyzes the given ast to find the [violation] diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 2c77787c6..6e42faa57 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -385,7 +385,7 @@ private static Token NextTokenIgnoringComments(Token[] tokens, int startIndex) { return null; } - + for (int i = startIndex + 1; i < tokens.Length; i++) { switch (tokens[i].Kind) @@ -397,18 +397,18 @@ private static Token NextTokenIgnoringComments(Token[] tokens, int startIndex) return tokens[i]; } } - + // We've run out of tokens return null; } - + private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens, int startIndex) { if (startIndex >= tokens.Length - 1) { return false; } - + Token nextToken = null; for (int i = startIndex + 1; i < tokens.Length; i++) { @@ -427,7 +427,7 @@ private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens return false; } } - + // We've run out of tokens but haven't seen a newline return false; } @@ -599,13 +599,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; private void AddViolation( Token token, diff --git a/Rules/UseConsistentParameterSetName.cs b/Rules/UseConsistentParameterSetName.cs index bf12f37e2..e65bc402e 100644 --- a/Rules/UseConsistentParameterSetName.cs +++ b/Rules/UseConsistentParameterSetName.cs @@ -432,9 +432,6 @@ public override string GetDescription() => string.Format( Strings.UseConsistentParameterSetNameDescription ); - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// public override SourceType GetSourceType() => SourceType.Builtin; /// diff --git a/Rules/UseConsistentParametersKind.cs b/Rules/UseConsistentParametersKind.cs index fd2dfa732..0224a862c 100644 --- a/Rules/UseConsistentParametersKind.cs +++ b/Rules/UseConsistentParametersKind.cs @@ -160,12 +160,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 7f7550ffe..ab555ab27 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -181,13 +181,7 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; private bool IsOperator(Token token) { @@ -243,7 +237,7 @@ private IEnumerable FindOpenBraceViolations(TokenOperations to { continue; } - + yield return new DiagnosticRecord( GetError(ErrorKind.BeforeOpeningBrace), lcurly.Value.Extent, @@ -520,7 +514,7 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode token hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0; return whiteSpaceSize == actualWhitespaceSize; } - + private static bool IsPreviousTokenLParen(LinkedListNode tokenNode) { return tokenNode.Previous.Value.Kind == TokenKind.LParen; diff --git a/Rules/UseConstrainedLanguageMode.cs b/Rules/UseConstrainedLanguageMode.cs index 7d73dd5eb..26956557b 100644 --- a/Rules/UseConstrainedLanguageMode.cs +++ b/Rules/UseConstrainedLanguageMode.cs @@ -85,7 +85,7 @@ public UseConstrainedLanguageMode() { // This rule is disabled by default - users must explicitly enable it Enable = false; - + // IgnoreSignatures defaults to false (respects signatures) IgnoreSignatures = false; } @@ -103,8 +103,8 @@ private bool IsTypeAllowed(string typeName) // Handle array types (e.g., string[], System.String[], int[][]) // Strip array brackets and check the base type string baseTypeName = typeName; - - + + // Handle multi-dimensional or jagged arrays by removing all brackets while (baseTypeName.EndsWith("[]", StringComparison.Ordinal)) { @@ -148,13 +148,13 @@ public override IEnumerable AnalyzeScript(Ast ast, string file // Check if the file is signed (via signature block detection) bool isFileSigned = IgnoreSignatures ? false : IsScriptSigned(fileName); - + // Note: If IgnoreSignatures is true, isFileSigned will always be false, // causing all CLM checks to run regardless of actual signature status // Check if this is a module manifest (.psd1 file) bool isModuleManifest = fileName != null && fileName.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase); - + if (isModuleManifest) { // Perform PSD1-specific checks @@ -167,13 +167,13 @@ public override IEnumerable AnalyzeScript(Ast ast, string file if (isFileSigned) { // Even signed scripts have these restrictions in CLM: - + // 1. Check for dot-sourcing (still restricted in CLM even for signed scripts) CheckDotSourcing(ast, fileName, diagnosticRecords); - + // 2. Check for type constraints on parameters (still need to be validated) CheckParameterTypeConstraints(ast, fileName, diagnosticRecords); - + return diagnosticRecords; } @@ -210,7 +210,7 @@ private bool IsScriptSigned(string fileName) { // Read the file content string content = System.IO.File.ReadAllText(fileName); - + // Check for signature block marker // A signed PowerShell script contains a signature block that starts with: // # SIG # Begin signature block @@ -267,12 +267,12 @@ testAst is CommandAst cmdAst && { // Use StaticParameterBinder to reliably get parameter values var bindingResult = StaticParameterBinder.BindCommand(cmd, true); - + // Check for -ComObject parameter if (bindingResult.BoundParameters.ContainsKey("ComObject")) { string comObjectValue = null; - + // Try to get the value from the AST directly first if (bindingResult.BoundParameters["ComObject"].Value is StringConstantExpressionAst strAst) { @@ -283,14 +283,14 @@ testAst is CommandAst cmdAst && // Fall back to ConstantValue comObjectValue = bindingResult.BoundParameters["ComObject"].ConstantValue as string; } - + // Only flag if COM object name was found AND it's not in the allowed list if (!string.IsNullOrWhiteSpace(comObjectValue) && !AllowedComObjects.Contains(comObjectValue)) { diagnosticRecords.Add( new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, - Strings.UseConstrainedLanguageModeComObjectError, + String.Format(CultureInfo.CurrentCulture, + Strings.UseConstrainedLanguageModeComObjectError, comObjectValue), cmd.Extent, GetName(), @@ -299,25 +299,25 @@ testAst is CommandAst cmdAst && )); } } - + // Check for -TypeName parameter if (bindingResult.BoundParameters.ContainsKey("TypeName")) { var typeNameValue = bindingResult.BoundParameters["TypeName"].ConstantValue as string; - + // If ConstantValue is null, try to extract from the AST Value if (typeNameValue == null && bindingResult.BoundParameters["TypeName"].Value is StringConstantExpressionAst typeStrAst) { typeNameValue = typeStrAst.Value; } - + // Only flag if type name was found AND it's not in the allowed list if (!string.IsNullOrWhiteSpace(typeNameValue) && !IsTypeAllowed(typeNameValue)) { diagnosticRecords.Add( new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, - Strings.UseConstrainedLanguageModeNewObjectError, + String.Format(CultureInfo.CurrentCulture, + Strings.UseConstrainedLanguageModeNewObjectError, typeNameValue), cmd.Extent, GetName(), @@ -380,7 +380,7 @@ testAst is CommandAst cmdAst && { diagnosticRecords.Add( new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, + String.Format(CultureInfo.CurrentCulture, Strings.UseConstrainedLanguageModeClassError, classDef.Name), classDef.Extent, @@ -394,11 +394,11 @@ testAst is CommandAst cmdAst && CheckParameterTypeConstraints(ast, fileName, diagnosticRecords); // Check for disallowed type constraints on variables (e.g., [System.Net.WebClient]$client) - var typeConstraints = ast.FindAll(testAst => - testAst is TypeConstraintAst typeConstraint && + var typeConstraints = ast.FindAll(testAst => + testAst is TypeConstraintAst typeConstraint && !(typeConstraint.Parent is ParameterAst), // Exclude parameters - handled above true); - + foreach (TypeConstraintAst typeConstraint in typeConstraints) { var typeName = typeConstraint.TypeName.FullName; @@ -442,7 +442,7 @@ testAst is TypeConstraintAst typeConstraint && foreach (ConvertExpressionAst convertExpr in convertExpressions) { var typeName = convertExpr.Type.TypeName.FullName; - + // Special case: [PSCustomObject]@{} is not allowed in CLM // Even though PSCustomObject is an allowed type for parameters, // the type cast syntax with hashtable literal is blocked in CLM @@ -460,7 +460,7 @@ testAst is TypeConstraintAst typeConstraint && )); continue; // Already flagged, skip general type check } - + if (!IsTypeAllowed(typeName)) { diagnosticRecords.Add( @@ -478,9 +478,9 @@ testAst is TypeConstraintAst typeConstraint && // Check for member invocations on disallowed types // This includes method calls and property access on variables with type constraints - var memberInvocations = ast.FindAll(testAst => + var memberInvocations = ast.FindAll(testAst => testAst is InvokeMemberExpressionAst || testAst is MemberExpressionAst, true); - + foreach (Ast memberAst in memberInvocations) { // Skip static member access - already handled by TypeExpressionAst check @@ -488,15 +488,15 @@ testAst is TypeConstraintAst typeConstraint && { continue; } - + if (memberAst is MemberExpressionAst memAst && memAst.Static) { continue; } // Get the expression being invoked on (e.g., the variable in $var.Method()) - ExpressionAst targetExpr = memberAst is InvokeMemberExpressionAst invExpr - ? invExpr.Expression + ExpressionAst targetExpr = memberAst is InvokeMemberExpressionAst invExpr + ? invExpr.Expression : ((MemberExpressionAst)memberAst).Expression; // Check if the target has a type constraint @@ -532,7 +532,7 @@ private void CheckDotSourcing(Ast ast, string fileName, List d // Example: . .\script.ps1 // PowerShell doesn't have a specific DotSourceExpressionAst, so we check the command extent var commands = ast.FindAll(testAst => testAst is CommandAst, true); - + foreach (CommandAst cmdAst in commands) { // Check if the command extent starts with a dot followed by whitespace @@ -559,12 +559,12 @@ private void CheckParameterTypeConstraints(Ast ast, string fileName, List testAst is ParameterAst, true); - + foreach (ParameterAst param in parameters) { // Check for type constraints on parameters var typeConstraints = param.Attributes.OfType(); - + foreach (var typeConstraint in typeConstraints) { var typeName = typeConstraint.TypeName.FullName; @@ -613,7 +613,7 @@ private string GetTypeConstraintFromExpression(ExpressionAst expr) var typeConstraint = parameterAst.Attributes .OfType() .FirstOrDefault(); - + if (typeConstraint != null) { return typeConstraint.TypeName.FullName; @@ -631,7 +631,7 @@ private string GetTypeConstraintFromExpression(ExpressionAst expr) // Check if this is a member expression that might have a known return type // For now, we'll be conservative and only check direct type constraints - + return null; } @@ -646,7 +646,7 @@ private ParameterAst FindParameterForVariable(VariableExpressionAst varExpr) } var varName = varExpr.VariablePath.UserPath; - + // Walk up to find the containing function or script block Ast current = varExpr.Parent; while (current != null) @@ -665,7 +665,7 @@ private ParameterAst FindParameterForVariable(VariableExpressionAst varExpr) } } } - + // Check function parameters (for functions with parameters outside param block) if (funcAst.Parameters != null) { @@ -677,10 +677,10 @@ private ParameterAst FindParameterForVariable(VariableExpressionAst varExpr) } } } - + break; // Don't check outer function scopes } - + if (current is ScriptBlockAst scriptAst) { var paramBlock = scriptAst.ParamBlock; @@ -696,10 +696,10 @@ private ParameterAst FindParameterForVariable(VariableExpressionAst varExpr) } break; // Don't check outer script block scopes } - + current = current.Parent; } - + return null; } @@ -734,7 +734,7 @@ private Dictionary GetOrBuildTypedVariableCache(Ast scope) { var varName = assignedVar.VariablePath.UserPath; var typeName = convertExpr.Type.TypeName.FullName; - + // Store in cache (first assignment wins) if (!typedVariables.ContainsKey(varName)) { @@ -759,16 +759,16 @@ private string FindTypedAssignment(VariableExpressionAst varExpr) } var varName = varExpr.VariablePath.UserPath; - + // Walk up to find the containing function or script block Ast searchScope = varExpr.Parent; - while (searchScope != null && - !(searchScope is FunctionDefinitionAst) && + while (searchScope != null && + !(searchScope is FunctionDefinitionAst) && !(searchScope is ScriptBlockAst)) { searchScope = searchScope.Parent; } - + if (searchScope == null) { return null; @@ -776,12 +776,12 @@ private string FindTypedAssignment(VariableExpressionAst varExpr) // Use cached results instead of re-scanning the entire scope var typedVariables = GetOrBuildTypedVariableCache(searchScope); - + if (typedVariables.TryGetValue(varName, out string typeName)) { return typeName; } - + return null; } @@ -792,7 +792,7 @@ private void CheckModuleManifest(Ast ast, string fileName, List x is HashtableAst, false) as HashtableAst; - + if (hashtableAst == null) { return; @@ -818,7 +818,7 @@ private void CheckWildcardExports(HashtableAst hashtableAst, string fileName, Li if (kvp.Item1 is StringConstantExpressionAst keyAst) { string keyName = keyAst.Value; - + if (exportFields.Contains(keyName, StringComparer.OrdinalIgnoreCase)) { // Check if the value contains a wildcard @@ -827,7 +827,7 @@ private void CheckWildcardExports(HashtableAst hashtableAst, string fileName, Li // The value in a hashtable is a StatementAst, need to extract the expression var valueExpr = GetExpressionFromStatement(kvp.Item2); - + if (valueExpr is StringConstantExpressionAst stringValue) { if (stringValue.Value == "*") @@ -877,7 +877,7 @@ private void CheckWildcardExports(HashtableAst hashtableAst, string fileName, Li } if (hasWildcard) break; } - } + } } if (hasWildcard && wildcardExtent != null) @@ -910,7 +910,7 @@ private void CheckScriptModules(HashtableAst hashtableAst, string fileName, List if (kvp.Item1 is StringConstantExpressionAst keyAst) { string keyName = keyAst.Value; - + if (moduleFields.Contains(keyName, StringComparer.OrdinalIgnoreCase)) { var valueExpr = GetExpressionFromStatement(kvp.Item2); @@ -1090,12 +1090,6 @@ public override string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; } } diff --git a/Rules/UseCorrectCasing.cs b/Rules/UseCorrectCasing.cs index f4f2c40b7..0334cb2c5 100644 --- a/Rules/UseCorrectCasing.cs +++ b/Rules/UseCorrectCasing.cs @@ -247,13 +247,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseDeclaredVarsMoreThanAssignments.cs b/Rules/UseDeclaredVarsMoreThanAssignments.cs index b35caafbc..f2e68193d 100644 --- a/Rules/UseDeclaredVarsMoreThanAssignments.cs +++ b/Rules/UseDeclaredVarsMoreThanAssignments.cs @@ -77,13 +77,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 9c453e3a1..34f9421e5 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -168,13 +168,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalMandatoryParametersDSCDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseIdenticalParametersDSC.cs b/Rules/UseIdenticalParametersDSC.cs index 785db5066..003b71aba 100644 --- a/Rules/UseIdenticalParametersDSC.cs +++ b/Rules/UseIdenticalParametersDSC.cs @@ -62,7 +62,7 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName || !CompareParamAsts(paramAst, paramNames[paramAst.Name.VariablePath.UserPath])) { yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalParametersDSCError), - paramAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); + paramAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); } } } @@ -118,14 +118,14 @@ private bool CompareParamAsts(ParameterAst paramAst1, ParameterAst paramAst2) return true; } - + /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule public string GetName() - { + { return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseIdenticalParametersDSCName); } @@ -147,13 +147,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalParametersDSCDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseLiteralInitializerForHashtable.cs b/Rules/UseLiteralInitializerForHashtable.cs index 56a31508f..78b8509fe 100644 --- a/Rules/UseLiteralInitializerForHashtable.cs +++ b/Rules/UseLiteralInitializerForHashtable.cs @@ -110,13 +110,7 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// Visits command ast to check for "new-object" command diff --git a/Rules/UseOutputTypeCorrectly.cs b/Rules/UseOutputTypeCorrectly.cs index ac7ff4a01..e0a722deb 100644 --- a/Rules/UseOutputTypeCorrectly.cs +++ b/Rules/UseOutputTypeCorrectly.cs @@ -152,13 +152,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UsePSCredentialType.cs b/Rules/UsePSCredentialType.cs index de0b2e5a5..1895085fd 100644 --- a/Rules/UsePSCredentialType.cs +++ b/Rules/UsePSCredentialType.cs @@ -191,13 +191,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseProcessBlockForPipelineCommand.cs b/Rules/UseProcessBlockForPipelineCommand.cs index 4b2298855..548cb8dab 100644 --- a/Rules/UseProcessBlockForPipelineCommand.cs +++ b/Rules/UseProcessBlockForPipelineCommand.cs @@ -25,7 +25,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } IEnumerable scriptblockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true); - + foreach (ScriptBlockAst scriptblockAst in scriptblockAsts) { if (scriptblockAst.ProcessBlock != null @@ -34,7 +34,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } - + foreach (ParameterAst paramAst in scriptblockAst.ParamBlock.Parameters) { foreach (AttributeBaseAst paramAstAttribute in paramAst.Attributes) @@ -80,10 +80,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandDescription); } - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; public RuleSeverity GetSeverity() { diff --git a/Rules/UseShouldProcessCorrectly.cs b/Rules/UseShouldProcessCorrectly.cs index 47bf5eb5c..6b14ee186 100644 --- a/Rules/UseShouldProcessCorrectly.cs +++ b/Rules/UseShouldProcessCorrectly.cs @@ -90,13 +90,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture,Strings.ShouldProcessDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. diff --git a/Rules/UseShouldProcessForStateChangingFunctions.cs b/Rules/UseShouldProcessForStateChangingFunctions.cs index 0d526042f..fad889aef 100644 --- a/Rules/UseShouldProcessForStateChangingFunctions.cs +++ b/Rules/UseShouldProcessForStateChangingFunctions.cs @@ -32,17 +32,17 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { throw new ArgumentNullException(Strings.NullAstErrorMessage); } - IEnumerable funcDefWithNoShouldProcessAttrAsts = ast.FindAll(IsStateChangingFunctionWithNoShouldProcessAttribute, true); + IEnumerable funcDefWithNoShouldProcessAttrAsts = ast.FindAll(IsStateChangingFunctionWithNoShouldProcessAttribute, true); foreach (FunctionDefinitionAst funcDefAst in funcDefWithNoShouldProcessAttrAsts) { yield return new DiagnosticRecord( - string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsError, funcDefAst.Name), - Helper.Instance.GetScriptExtentForFunctionName(funcDefAst), - this.GetName(), - DiagnosticSeverity.Warning, + string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsError, funcDefAst.Name), + Helper.Instance.GetScriptExtentForFunctionName(funcDefAst), + this.GetName(), + DiagnosticSeverity.Warning, fileName); } - + } /// /// Checks if the ast defines a state changing function @@ -57,7 +57,7 @@ private bool IsStateChangingFunctionWithNoShouldProcessAttribute(Ast ast) { return false; } - return Helper.Instance.IsStateChangingFunctionName(funcDefAst.Name) + return Helper.Instance.IsStateChangingFunctionName(funcDefAst.Name) && (funcDefAst.Body.ParamBlock == null || funcDefAst.Body.ParamBlock.Attributes == null || !HasShouldProcessTrue(funcDefAst.Body.ParamBlock.Attributes)); @@ -105,14 +105,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: built-in, managed or module. - /// - /// Source type {PS, PSDSC} - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseSingleValueFromPipelineParameter.cs b/Rules/UseSingleValueFromPipelineParameter.cs index 0b69880aa..0a52a94b9 100644 --- a/Rules/UseSingleValueFromPipelineParameter.cs +++ b/Rules/UseSingleValueFromPipelineParameter.cs @@ -16,7 +16,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules #if !CORECLR [Export(typeof(IScriptRule))] #endif - + /// /// Rule that identifies parameter blocks with multiple parameters in /// the same parameter set that are marked as ValueFromPipeline=true, which @@ -56,16 +56,16 @@ public override IEnumerable AnalyzeScript(Ast ast, string file // implicitly). Flatten the results into a single collection of // Annonymous objects relating the parameter with it's attribute // and then group them by parameter set name. - // - // + // + // // https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_parameter_sets?#reserved-parameter-set-name - // + // // The default parameter set name is '__AllParameterSets'. // Not specifying a parameter set name and using the parameter // set name '__AllParameterSets' are equivalent, so we shouldn't // treat them like they're different just because one is an // empty string and the other is not. - // + // // Filter the list to only keep parameter sets that have more // than one ValueFromPipeline parameter. var parameterSetGroups = paramBlock.FindAll(n => n is ParameterAst, true) diff --git a/Rules/UseSingularNouns.cs b/Rules/UseSingularNouns.cs index 21a6afa90..44fa785d3 100644 --- a/Rules/UseSingularNouns.cs +++ b/Rules/UseSingularNouns.cs @@ -27,12 +27,12 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules { /// /// CmdletSingularNoun: Analyzes scripts to check that all defined cmdlets use singular nouns. - /// + /// /// #if !CORECLR [Export(typeof(IScriptRule))] #endif - public class CmdletSingularNoun : ConfigurableRule + public class CmdletSingularNoun : ConfigurableRule { [ConfigurableRuleProperty(defaultValue: new string[] { "Data", "Windows" })] public string[] NounAllowList { get; set; } @@ -124,13 +124,7 @@ public override string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public override SourceType GetSourceType() - { - return SourceType.Builtin; - } + public override SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseStandardDSCFunctionsInResource.cs b/Rules/UseStandardDSCFunctionsInResource.cs index 386198ba5..37de27a1e 100644 --- a/Rules/UseStandardDSCFunctionsInResource.cs +++ b/Rules/UseStandardDSCFunctionsInResource.cs @@ -30,11 +30,11 @@ public class UseStandardDSCFunctionsInResource : IDSCResourceRule public IEnumerable AnalyzeDSCResource(Ast ast, string fileName) { if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); - + // Expected TargetResource functions in the DSC Resource module List expectedTargetResourceFunctionNames = new List(new string[] { "Get-TargetResource", "Set-TargetResource", "Test-TargetResource" }); - // Retrieve a list of Asts where the function name contains TargetResource + // Retrieve a list of Asts where the function name contains TargetResource IEnumerable functionDefinitionAsts = (ast.FindAll(dscAst => dscAst is FunctionDefinitionAst && ((dscAst as FunctionDefinitionAst).Name.IndexOf("targetResource", StringComparison.OrdinalIgnoreCase) != -1), true)); List targetResourceFunctionNamesInAst = new List(); @@ -42,17 +42,17 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName { targetResourceFunctionNamesInAst.Add(functionDefinitionAst.Name); } - + foreach (string expectedTargetResourceFunctionName in expectedTargetResourceFunctionNames) { // If the Ast does not contain the expected functions, provide a Rule violation message if (!targetResourceFunctionNamesInAst.Contains(expectedTargetResourceFunctionName, StringComparer.OrdinalIgnoreCase)) { yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceError, expectedTargetResourceFunctionName), - ast.Extent, GetName(), DiagnosticSeverity.Error, fileName); + ast.Extent, GetName(), DiagnosticSeverity.Error, fileName); } } - } + } /// /// AnalyzeDSCClass: Analyzes dsc classes and the file and check that they have get, set and test @@ -84,14 +84,14 @@ item is TypeDefinitionAst } } } - } + } /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule public string GetName() - { + { return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseStandardDSCFunctionsInResourceName); } @@ -113,13 +113,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseSupportsShouldProcess.cs b/Rules/UseSupportsShouldProcess.cs index a79fa4428..c8bfe0c9e 100644 --- a/Rules/UseSupportsShouldProcess.cs +++ b/Rules/UseSupportsShouldProcess.cs @@ -483,13 +483,7 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// Retrieves the severity of the rule: error, warning or information. diff --git a/Rules/UseToExportFieldsInManifest.cs b/Rules/UseToExportFieldsInManifest.cs index 9bf612f83..437d57abf 100644 --- a/Rules/UseToExportFieldsInManifest.cs +++ b/Rules/UseToExportFieldsInManifest.cs @@ -17,8 +17,8 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules { /// - /// UseToExportFieldsInManifest: Checks if AliasToExport, CmdletsToExport, FunctionsToExport and VariablesToExport - /// fields do not use wildcards and $null in their entries. + /// UseToExportFieldsInManifest: Checks if AliasToExport, CmdletsToExport, FunctionsToExport and VariablesToExport + /// fields do not use wildcards and $null in their entries. /// #if !CORECLR [Export(typeof(IScriptRule))] @@ -29,10 +29,10 @@ public class UseToExportFieldsInManifest : IScriptRule private const string functionsToExport = "FunctionsToExport"; private const string cmdletsToExport = "CmdletsToExport"; private const string aliasesToExport = "AliasesToExport"; - + /// - /// AnalyzeScript: Analyzes the AST to check if AliasToExport, CmdletsToExport, FunctionsToExport - /// and VariablesToExport fields do not use wildcards and $null in their entries. + /// AnalyzeScript: Analyzes the AST to check if AliasToExport, CmdletsToExport, FunctionsToExport + /// and VariablesToExport fields do not use wildcards and $null in their entries. /// /// The script's ast /// The script's file name @@ -43,7 +43,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { throw new ArgumentNullException(Strings.NullAstErrorMessage); } - + if (fileName == null || !Helper.IsModuleManifest(fileName)) { yield break; @@ -51,16 +51,16 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) // check if valid module manifest IEnumerable errorRecord = null; - PSModuleInfo psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecord); + PSModuleInfo psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecord); if ((errorRecord != null && errorRecord.Count() > 0) || psModuleInfo == null) - { + { yield break; } - + var hashtableAst = ast.Find(x => x is HashtableAst, false) as HashtableAst; - + if (hashtableAst == null) - { + { yield break; } @@ -72,10 +72,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (!HasAcceptableExportField(field, hashtableAst, out extent) && extent != null) { yield return new DiagnosticRecord( - GetError(field), - extent, - GetName(), - DiagnosticSeverity.Warning, + GetError(field), + extent, + GetName(), + DiagnosticSeverity.Warning, fileName, suggestedCorrections: GetCorrectionExtent(field, extent, psModuleInfo)); } @@ -83,8 +83,8 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { } - } - + } + } private string GetListLiteral(Dictionary exportedItemsDict) @@ -124,7 +124,7 @@ private string GetListLiteral(Dictionary exportedItemsDict) private List GetCorrectionExtent(string field, IScriptExtent extent, PSModuleInfo psModuleInfo) { - Debug.Assert(field != null); + Debug.Assert(field != null); Debug.Assert(psModuleInfo != null); Debug.Assert(extent != null); var corrections = new List(); @@ -159,7 +159,7 @@ private List GetCorrectionExtent(string field, IScriptExtent e } ///// - ///// Checks if the manifest file is valid. + ///// Checks if the manifest file is valid. ///// ///// ///// @@ -194,7 +194,7 @@ private bool HasNullInExpression(Ast ast) && varExprAst.VariablePath.IsUnqualified && varExprAst.VariablePath.UserPath.Equals("null", StringComparison.OrdinalIgnoreCase); } - + /// /// Checks if the *ToExport fields are explicitly set to arrays, eg. @(...), and the array entries do not contain any wildcard. /// @@ -208,7 +208,7 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, out IScript foreach (var pair in hast.KeyValuePairs) { var keyStrConstAst = pair.Item1 as StringConstantExpressionAst; - if (keyStrConstAst != null && keyStrConstAst.Value.Equals(key, StringComparison.OrdinalIgnoreCase)) + if (keyStrConstAst != null && keyStrConstAst.Value.Equals(key, StringComparison.OrdinalIgnoreCase)) { // Checks for wildcard character in the entry. var astWithWildcard = pair.Item2.Find(HasWildcardInExpression, false); @@ -219,7 +219,7 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, out IScript } else { - // Checks for $null in the entry. + // Checks for $null in the entry. var astWithNull = pair.Item2.Find(HasNullInExpression, false); if (astWithNull != null) { @@ -234,9 +234,9 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, out IScript } } return true; - } + } + - /// /// Gets the error string of the rule. /// @@ -274,13 +274,7 @@ public string GetDescription() return String.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseUTF8EncodingForHelpFile.cs b/Rules/UseUTF8EncodingForHelpFile.cs index d1a3b3fb7..a3990dcc5 100644 --- a/Rules/UseUTF8EncodingForHelpFile.cs +++ b/Rules/UseUTF8EncodingForHelpFile.cs @@ -75,13 +75,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseUTF8EncodingForHelpFileDescription); } - /// - /// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Rules/UseUsingScopeModifierInNewRunspaces.cs b/Rules/UseUsingScopeModifierInNewRunspaces.cs index 46e1eea8a..1ab519f64 100644 --- a/Rules/UseUsingScopeModifierInNewRunspaces.cs +++ b/Rules/UseUsingScopeModifierInNewRunspaces.cs @@ -66,13 +66,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseUsingScopeModifierInNewRunspacesDescription); } - /// - /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. @@ -115,11 +109,11 @@ private class SyntaxCompatibilityVisitor : AstVisitor2 private readonly Dictionary> _varsDeclaredPerSession; private readonly List _diagnosticAccumulator; - + private readonly UseUsingScopeModifierInNewRunspaces _rule; - + private readonly string _analyzedFilePath; - + public SyntaxCompatibilityVisitor(UseUsingScopeModifierInNewRunspaces rule, string analyzedScriptPath) { _diagnosticAccumulator = new List(); @@ -154,15 +148,15 @@ public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionA string cmdName = commandAst.GetCommandName(); if (cmdName == null) { - // Skip for situations where command name cannot be resolved like `& $commandName -ComputerName -ScriptBlock { $foo }` + // Skip for situations where command name cannot be resolved like `& $commandName -ComputerName -ScriptBlock { $foo }` return AstVisitAction.SkipChildren; } - + // We need this information, because some cmdlets can have more than one ScriptBlock parameter var scriptBlockParameterAst = commandAst.CommandElements[ commandAst.CommandElements.IndexOf(scriptBlockExpressionAst) - 1] as CommandParameterAst; - if (IsInlineScriptBlock(cmdName) || + if (IsInlineScriptBlock(cmdName) || IsJobScriptBlock(cmdName, scriptBlockParameterAst) || IsForeachScriptBlock(cmdName, scriptBlockParameterAst) || IsInvokeCommandComputerScriptBlock(cmdName, commandAst) || @@ -192,7 +186,7 @@ public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionA return AstVisitAction.SkipChildren; } - + return AstVisitAction.Continue; } @@ -231,7 +225,7 @@ private static bool TryGetVariableFromExpression(ExpressionAst expression, out V { case VariableExpressionAst variable: variableExpressionAst = variable; - return true; + return true; case AttributedExpressionAst attributedAst: return TryGetVariableFromExpression(attributedAst.Child, out variableExpressionAst); @@ -266,12 +260,12 @@ private static IEnumerable FindNonAssignedNonUsingVarAsts foreach (VariableExpressionAst variable in ast.FindAll(IsNonUsingNonSpecialVariableExpressionAst, true)) { var varName = string.Format(variable.VariablePath.UserPath, StringComparer.OrdinalIgnoreCase); - + if (varsInAssignments.Contains(varName)) { - yield break; + yield break; } - + yield return variable; } } @@ -282,8 +276,8 @@ private static IEnumerable FindNonAssignedNonUsingVarAsts /// private static bool IsNonUsingNonSpecialVariableExpressionAst(Ast ast) { - return ast is VariableExpressionAst variable && - !(variable.Parent is UsingExpressionAst) && + return ast is VariableExpressionAst variable && + !(variable.Parent is UsingExpressionAst) && !Helper.Instance.HasSpecialVars(variable.VariablePath.UserPath); } @@ -470,7 +464,7 @@ private bool IsForeachScriptBlock(string cmdName, CommandParameterAst scriptBloc { // 'pa' is the shortest unambiguous form for the '-Parallel' parameter return s_foreachObjectCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && - (scriptBlockParameterAst != null && + (scriptBlockParameterAst != null && scriptBlockParameterAst.ParameterName.StartsWith("pa", StringComparison.OrdinalIgnoreCase)); } @@ -486,7 +480,7 @@ private bool IsJobScriptBlock(string cmdName, CommandParameterAst scriptBlockPar // 'ini' is the shortest unambiguous form for the '-InitializationScript' parameter return (s_jobCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) || s_threadJobCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase)) && - !(scriptBlockParameterAst != null && + !(scriptBlockParameterAst != null && scriptBlockParameterAst.ParameterName.StartsWith("ini", StringComparison.OrdinalIgnoreCase)); } @@ -509,7 +503,7 @@ private bool IsDSCScriptResource(string cmdName, CommandAst commandAst) { // Inside DSC Script resource, GetScript is of the form 'Script foo { GetScript = {} }' // If we reach this point in the code, we are sure there are at least two CommandElements, so the index of [1] will not fail. - return s_dscScriptResourceCommandNames.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && + return s_dscScriptResourceCommandNames.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && commandAst.CommandElements[1].ToString() == "="; } } diff --git a/Rules/UseVerboseMessageInDSCResource.cs b/Rules/UseVerboseMessageInDSCResource.cs index d6038544f..1a82b65df 100644 --- a/Rules/UseVerboseMessageInDSCResource.cs +++ b/Rules/UseVerboseMessageInDSCResource.cs @@ -31,7 +31,7 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName if (ast == null) { throw new ArgumentNullException(Strings.NullAstErrorMessage); - } + } IEnumerable functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast); @@ -56,7 +56,7 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName } } - + /// /// AnalyzeDSCClass: This function returns nothing in the case of dsc class. /// @@ -94,13 +94,7 @@ public string GetDescription() return string.Format(CultureInfo.CurrentCulture, Strings.UseVerboseMessageInDSCResourceDescription); } - /// - /// Method: Retrieves the type of the rule: builtin, managed or module. - /// - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. diff --git a/Utils/RuleMaker.psm1 b/Utils/RuleMaker.psm1 index 71a5d551f..8f761d816 100644 --- a/Utils/RuleMaker.psm1 +++ b/Utils/RuleMaker.psm1 @@ -159,13 +159,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); }} - /// - /// Retrieves the type of the rule, Builtin, Managed or Module. - /// - public SourceType GetSourceType() - {{ - return SourceType.Builtin; - }} + public SourceType GetSourceType() => SourceType.Builtin; }} }} '@