From 8358c0c725d3d6610136efaf9462e3e040b01a0e Mon Sep 17 00:00:00 2001 From: Haimasker Date: Mon, 4 May 2026 18:29:58 +0300 Subject: [PATCH] Refactor rules methods * Refactor methods into one-liners using lambda operator. * Add automatic code style check into build process. --- .editorconfig | 2 + Engine/Generic/ExternalRule.cs | 6 +- Rules/AlignAssignmentStatement.cs | 38 ++--- Rules/AvoidAlias.cs | 32 +--- Rules/AvoidAssignmentToAutomaticVariable.cs | 30 +--- Rules/AvoidDefaultTrueValueSwitchParameter.cs | 32 +--- .../AvoidDefaultValueForMandatoryParameter.cs | 36 ++--- .../AvoidDynamicallyCreatingVariableNames.cs | 35 +---- Rules/AvoidEmptyCatchBlock.cs | 30 +--- Rules/AvoidExclaimOperator.cs | 43 ++---- Rules/AvoidGlobalAliases.cs | 30 +--- Rules/AvoidGlobalFunctions.cs | 30 +--- Rules/AvoidGlobalVars.cs | 32 +--- Rules/AvoidInvokingEmptyMembers.cs | 36 ++--- Rules/AvoidLongLines.cs | 37 +---- Rules/AvoidMultipleTypeAttributes.cs | 38 ++--- Rules/AvoidNullOrEmptyHelpMessageAttribute.cs | 54 +++---- Rules/AvoidOverwritingBuiltInCmdlets.cs | 40 +---- Rules/AvoidPositionalParameters.cs | 34 +---- Rules/AvoidReservedCharInCmdlet.cs | 32 +--- Rules/AvoidReservedParams.cs | 36 +---- Rules/AvoidReservedWordsAsFunctionNames.cs | 2 +- Rules/AvoidSemicolonsAsLineTerminators.cs | 35 +---- Rules/AvoidShouldContinueWithoutForce.cs | 32 +--- Rules/AvoidTrailingWhitespace.cs | 35 +---- Rules/AvoidUserNameAndPasswordParams.cs | 39 ++--- ...voidUsingAllowUnencryptedAuthentication.cs | 50 ++----- Rules/AvoidUsingArrayList.cs | 35 +---- Rules/AvoidUsingBrokenHashAlgorithms.cs | 40 +---- Rules/AvoidUsingComputerNameHardcoded.cs | 40 +---- ...UsingConvertToSecureStringWithPlainText.cs | 41 ++--- Rules/AvoidUsingDeprecatedManifestFields.cs | 32 +--- ...AvoidUsingDoubleQuotesForConstantString.cs | 42 ++---- Rules/AvoidUsingInvokeExpression.cs | 47 ++---- Rules/AvoidUsingPlainTextForPassword.cs | 30 +--- Rules/AvoidUsingWMICmdlet.cs | 36 ++--- Rules/AvoidUsingWriteHost.cs | 30 +--- Rules/ClangSuppresion.cs | 5 +- Rules/CompatibilityRules/CompatibilityRule.cs | 15 +- .../UseCompatibleCommands.cs | 20 +-- .../CompatibilityRules/UseCompatibleSyntax.cs | 40 +---- .../CompatibilityRules/UseCompatibleTypes.cs | 20 +-- Rules/DscExamplesPresent.cs | 36 ++--- Rules/DscTestsPresent.cs | 32 +--- Rules/InvalidMultiDotValue.cs | 35 +---- Rules/MisleadingBacktick.cs | 30 +--- Rules/MissingModuleManifestField.cs | 44 ++---- Rules/MissingTryBlock.cs | 30 +--- Rules/PlaceCloseBrace.cs | 40 +---- Rules/PlaceOpenBrace.cs | 45 ++---- Rules/PossibleIncorrectComparisonWithNull.cs | 36 ++--- ...sibleIncorrectUsageOfAssignmentOperator.cs | 30 +--- ...ibleIncorrectUsageOfRedirectionOperator.cs | 30 +--- Rules/ProvideCommentHelp.cs | 77 ++-------- Rules/ReturnCorrectTypesForDSCFunctions.cs | 30 +--- Rules/ReviewUnusedParameter.cs | 34 +---- Rules/UseApprovedVerbs.cs | 29 +--- Rules/UseBOMForUnicodeEncodedFile.cs | 38 ++--- Rules/UseCmdletCorrectly.cs | 30 +--- Rules/UseCompatibleCmdlets.cs | 45 ++---- Rules/UseConsistentIndentation.cs | 66 +++----- Rules/UseConsistentParameterSetName.cs | 12 +- Rules/UseConsistentParametersKind.cs | 35 +---- Rules/UseConsistentWhitespace.cs | 84 +++-------- Rules/UseConstrainedLanguageMode.cs | 141 ++++++++---------- Rules/UseCorrectCasing.cs | 30 +--- Rules/UseDeclaredVarsMoreThanAssignments.cs | 30 +--- Rules/UseIdenticalMandatoryParametersDSC.cs | 56 ++----- Rules/UseIdenticalParametersDSC.cs | 39 ++--- Rules/UseLiteralInitializerForHashtable.cs | 35 +---- Rules/UseOutputTypeCorrectly.cs | 30 +--- Rules/UsePSCredentialType.cs | 30 +--- Rules/UseProcessBlockForPipelineCommand.cs | 34 +---- Rules/UseShouldProcessCorrectly.cs | 80 ++-------- ...eShouldProcessForStateChangingFunctions.cs | 44 ++---- Rules/UseSingleValueFromPipelineParameter.cs | 10 +- Rules/UseSingularNouns.cs | 34 +---- Rules/UseStandardDSCFunctionsInResource.cs | 42 ++---- Rules/UseSupportsShouldProcess.cs | 65 ++------ Rules/UseToExportFieldsInManifest.cs | 83 ++++------- Rules/UseUTF8EncodingForHelpFile.cs | 30 +--- Rules/UseUsingScopeModifierInNewRunspaces.cs | 112 +++++--------- Rules/UseVerboseMessageInDSCResource.cs | 39 ++--- Utils/RuleMaker.psm1 | 5 +- build.psm1 | 21 +++ 85 files changed, 783 insertions(+), 2424 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..ade67ead2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[**Rules/**.cs] +csharp_style_expression_bodied_methods = true:error \ No newline at end of file diff --git a/Engine/Generic/ExternalRule.cs b/Engine/Generic/ExternalRule.cs index 55379c3ba..b7eda56be 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,6 @@ public string GetFullModulePath() #endregion #region Constructors - public ExternalRule() { diff --git a/Rules/AlignAssignmentStatement.cs b/Rules/AlignAssignmentStatement.cs index 5b941924a..81cf07a8e 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 @@ -636,13 +636,12 @@ private List GetCorrectionExtent( IScriptExtent lhsExtent, IScriptExtent equalsExtent, int targetColumn - ) - { + ) => // We generate a correction extent which replaces the text between // the end of the lhs and the start of the equals sign with the // appropriate number of spaces to align the equals sign to the // target column. - return new List + new List { new CorrectionExtent( lhsExtent.EndLineNumber, @@ -653,58 +652,39 @@ int targetColumn string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementError) ) }; - } /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AlignAssignmentStatementName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..303c06ea6 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; } @@ -237,52 +237,34 @@ private List GetCorrectionExtent(CommandAst cmdAst, string cmd /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingCmdletAliasesName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingCmdletAliasesName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidAssignmentToAutomaticVariable.cs b/Rules/AvoidAssignmentToAutomaticVariable.cs index c1ce88462..6cdb71d22 100644 --- a/Rules/AvoidAssignmentToAutomaticVariable.cs +++ b/Rules/AvoidAssignmentToAutomaticVariable.cs @@ -155,53 +155,35 @@ private bool IsPowerShellVersion6OrGreater() /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidAssignmentToAutomaticVariableName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidAssignmentToAutomaticVariableName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidDefaultTrueValueSwitchParameter.cs b/Rules/AvoidDefaultTrueValueSwitchParameter.cs index 3889959c0..9104de344 100644 --- a/Rules/AvoidDefaultTrueValueSwitchParameter.cs +++ b/Rules/AvoidDefaultTrueValueSwitchParameter.cs @@ -57,53 +57,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueSwitchParameterName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueSwitchParameterName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterDescription); /// - /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidDefaultValueForMandatoryParameter.cs b/Rules/AvoidDefaultValueForMandatoryParameter.cs index 17925ae97..a2c6572a2 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) @@ -99,53 +99,35 @@ private static bool HasMandatoryInAllParameterAttributes(ParameterAst paramAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueForMandatoryParameterName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueForMandatoryParameterName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidDynamicallyCreatingVariableNames.cs b/Rules/AvoidDynamicallyCreatingVariableNames.cs index 5e5df7566..c03b644cb 100644 --- a/Rules/AvoidDynamicallyCreatingVariableNames.cs +++ b/Rules/AvoidDynamicallyCreatingVariableNames.cs @@ -80,62 +80,41 @@ testAst is CommandAst cmdAst && /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDynamicallyCreatingVariableNamesCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDynamicallyCreatingVariableNamesCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDynamicallyCreatingVariableNamesDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDynamicallyCreatingVariableNamesDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDynamicallyCreatingVariableNamesName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Information; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Information; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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/AvoidEmptyCatchBlock.cs b/Rules/AvoidEmptyCatchBlock.cs index 116102961..282104622 100644 --- a/Rules/AvoidEmptyCatchBlock.cs +++ b/Rules/AvoidEmptyCatchBlock.cs @@ -47,53 +47,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingEmptyCatchBlockName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingEmptyCatchBlockName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingEmptyCatchBlockCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingEmptyCatchBlockCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingEmptyCatchBlockDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidExclaimOperator.cs b/Rules/AvoidExclaimOperator.cs index 5521463ea..6b96c916a 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 )); @@ -85,63 +85,42 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidExclaimOperatorName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..1827aeb82 100644 --- a/Rules/AvoidGlobalAliases.cs +++ b/Rules/AvoidGlobalAliases.cs @@ -101,38 +101,20 @@ private bool IsNewAliasCmdlet(CommandAst commandAst) return false; } - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesCommonName); - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesDescription); - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidGlobalAliasesName); - } - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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..f96b8b1aa 100644 --- a/Rules/AvoidGlobalFunctions.cs +++ b/Rules/AvoidGlobalFunctions.cs @@ -72,38 +72,20 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun } #endregion - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsCommonName); - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsDescription); - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidGlobalFunctionsName); - } - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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..d51ed7cb8 100644 --- a/Rules/AvoidGlobalVars.cs +++ b/Rules/AvoidGlobalVars.cs @@ -51,53 +51,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidGlobalVarsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidGlobalVarsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalVarsDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidInvokingEmptyMembers.cs b/Rules/AvoidInvokingEmptyMembers.cs index 3f625e31c..04033828e 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,61 +55,43 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } } } - + } - + /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidInvokingEmptyMembersName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidInvokingEmptyMembersName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidInvokingEmptyMembersCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidInvokingEmptyMembersCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidInvokingEmptyMembersDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidLongLines.cs b/Rules/AvoidLongLines.cs index ad527bb37..f009a2c0d 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(), @@ -95,62 +95,41 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidLongLinesName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - private DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + private DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..a63d272de 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,63 +42,45 @@ 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. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidMultipleTypeAttributesName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidMultipleTypeAttributesName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs b/Rules/AvoidNullOrEmptyHelpMessageAttribute.cs index 3dbe949d9..e73923417 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); - } - } + } + } } } - + } } @@ -125,53 +125,35 @@ private bool HasNullInExpression(Ast ast) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidNullOrEmptyHelpMessageAttributeName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidNullOrEmptyHelpMessageAttributeName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidOverwritingBuiltInCmdlets.cs b/Rules/AvoidOverwritingBuiltInCmdlets.cs index a390178d5..fb3d2d008 100644 --- a/Rules/AvoidOverwritingBuiltInCmdlets.cs +++ b/Rules/AvoidOverwritingBuiltInCmdlets.cs @@ -138,10 +138,7 @@ private DiagnosticRecord CreateDiagnosticRecord(string FunctionName, string PSVe } - private bool ContainsReferenceFile(string directory, string reference) - { - return File.Exists(Path.Combine(directory, reference + ".json")); - } + private bool ContainsReferenceFile(string directory, string reference) => File.Exists(Path.Combine(directory, reference + ".json")); private void ProcessDirectory(string path, IEnumerable acceptablePlatformSpecs) @@ -201,62 +198,41 @@ private HashSet GetCmdletsFromData(dynamic deserializedObject) /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidOverwritingBuiltInCmdletsCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidOverwritingBuiltInCmdletsCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidOverwritingBuiltInCmdletsDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidOverwritingBuiltInCmdletsDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidOverwritingBuiltInCmdletsName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..386533993 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))) { @@ -96,53 +96,35 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingPositionalParametersName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingPositionalParametersName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidReservedCharInCmdlet.cs b/Rules/AvoidReservedCharInCmdlet.cs index e4ae3de6e..4cebd7083 100644 --- a/Rules/AvoidReservedCharInCmdlet.cs +++ b/Rules/AvoidReservedCharInCmdlet.cs @@ -55,60 +55,44 @@ 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); - } + } } - } + } } /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReservedCmdletCharName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReservedCmdletCharName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() { - return string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidReservedParams.cs b/Rules/AvoidReservedParams.cs index 4035a9c89..5090b91ff 100644 --- a/Rules/AvoidReservedParams.cs +++ b/Rules/AvoidReservedParams.cs @@ -71,61 +71,41 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReservedParamsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReservedParamsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() { - return string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Error; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Error; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } 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..067e25f57 100644 --- a/Rules/AvoidSemicolonsAsLineTerminators.cs +++ b/Rules/AvoidSemicolonsAsLineTerminators.cs @@ -114,62 +114,41 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidSemicolonsAsLineTerminatorsName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..859daff19 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; @@ -85,53 +85,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidShouldContinueWithoutForceName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidShouldContinueWithoutForceName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index a7567d6e6..ec273a62e 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -107,62 +107,41 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// /// Retrieves the common name of this rule. /// - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceCommonName); /// /// Retrieves the description of this rule. /// - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceDescription); /// /// Retrieves the name of this rule. /// - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidTrailingWhitespaceName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Information; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Information; /// /// Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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..8894e5be1 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) @@ -91,10 +91,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } } - private AttributeBaseAst GetAttributeOfType(IEnumerable attributeAsts, Type type) - { - return attributeAsts.FirstOrDefault(x => IsAttributeOfType(x, type)); - } + private AttributeBaseAst GetAttributeOfType(IEnumerable attributeAsts, Type type) => attributeAsts.FirstOrDefault(x => IsAttributeOfType(x, type)); private bool IsAttributeOfType(AttributeBaseAst attributeAst, Type type) { @@ -116,7 +113,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; @@ -147,52 +144,34 @@ var usrBeforePwd /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsernameAndPasswordParamsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsernameAndPasswordParamsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingAllowUnencryptedAuthentication.cs b/Rules/AvoidUsingAllowUnencryptedAuthentication.cs index 955de8113..7597e92ff 100644 --- a/Rules/AvoidUsingAllowUnencryptedAuthentication.cs +++ b/Rules/AvoidUsingAllowUnencryptedAuthentication.cs @@ -26,10 +26,7 @@ public class AvoidUsingAllowUnencryptedAuthentication : AvoidParameterGeneric /// /// /// - public override bool CommandCondition(CommandAst CmdAst) - { - return true; - } + public override bool CommandCondition(CommandAst CmdAst) => true; /// /// Condition on the parameter that must be satisfied for the error to be raised. @@ -37,10 +34,7 @@ public override bool CommandCondition(CommandAst CmdAst) /// /// /// - public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) - { - return CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AllowUnencryptedAuthentication", StringComparison.OrdinalIgnoreCase); - } + public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) => CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AllowUnencryptedAuthentication", StringComparison.OrdinalIgnoreCase); /// /// Retrieves the error message @@ -48,70 +42,46 @@ public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeA /// /// /// - public override string GetError(string fileName, CommandAst cmdAst) - { - return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationError); - } + public override string GetError(string fileName, CommandAst cmdAst) => String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationError); /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingAllowUnencryptedAuthenticationName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingAllowUnencryptedAuthenticationName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. /// /// - public override DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public override DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingArrayList.cs b/Rules/AvoidUsingArrayList.cs index 14808d2b3..f5de4e34e 100644 --- a/Rules/AvoidUsingArrayList.cs +++ b/Rules/AvoidUsingArrayList.cs @@ -133,63 +133,42 @@ typeNameBinding.ConstantValue is string typeName && /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingArrayListName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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/AvoidUsingBrokenHashAlgorithms.cs b/Rules/AvoidUsingBrokenHashAlgorithms.cs index b43116020..164d9d089 100644 --- a/Rules/AvoidUsingBrokenHashAlgorithms.cs +++ b/Rules/AvoidUsingBrokenHashAlgorithms.cs @@ -33,10 +33,7 @@ public class AvoidUsingBrokenHashAlgorithms : AvoidParameterGeneric /// /// /// - public override bool CommandCondition(CommandAst CmdAst) - { - return true; - } + public override bool CommandCondition(CommandAst CmdAst) => true; /// /// Condition on the parameter that must be satisfied for the error to be raised. @@ -122,62 +119,41 @@ public override string GetError(string FileName, CommandAst CmdAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingBrokenHashAlgorithmsName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingBrokenHashAlgorithmsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. /// /// - public override DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public override DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingComputerNameHardcoded.cs b/Rules/AvoidUsingComputerNameHardcoded.cs index 2202abe1d..f8b6d6882 100644 --- a/Rules/AvoidUsingComputerNameHardcoded.cs +++ b/Rules/AvoidUsingComputerNameHardcoded.cs @@ -33,10 +33,7 @@ public class AvoidUsingComputerNameHardcoded : AvoidParameterGeneric /// /// /// - public override bool CommandCondition(CommandAst CmdAst) - { - return true; - } + public override bool CommandCondition(CommandAst CmdAst) => true; /// /// Condition on the parameter that must be satisfied for the error to be raised. @@ -122,62 +119,41 @@ public override string GetError(string FileName, CommandAst CmdAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidComputerNameHardcodedName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidComputerNameHardcodedName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidComputerNameHardcodedCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidComputerNameHardcodedCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidComputerNameHardcodedDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. /// /// - public override DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Error; - } + public override DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs index ff7873a5e..f62efdaa3 100644 --- a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs +++ b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs @@ -44,10 +44,7 @@ public override bool CommandCondition(CommandAst CmdAst) /// /// /// - public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) - { - return CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AsPlainText", StringComparison.OrdinalIgnoreCase); - } + public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) => CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AsPlainText", StringComparison.OrdinalIgnoreCase); /// /// Retrieves the error message @@ -71,62 +68,42 @@ public override string GetError(string fileName, CommandAst cmdAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingConvertToSecureStringWithPlainTextName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingConvertToSecureStringWithPlainTextName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingConvertToSecureStringWithPlainTextCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingConvertToSecureStringWithPlainTextCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingConvertToSecureStringWithPlainTextDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. /// /// - public override DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Error; - } + public override DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingDeprecatedManifestFields.cs b/Rules/AvoidUsingDeprecatedManifestFields.cs index fd3431251..fe3d35c99 100644 --- a/Rules/AvoidUsingDeprecatedManifestFields.cs +++ b/Rules/AvoidUsingDeprecatedManifestFields.cs @@ -122,52 +122,34 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDeprecatedManifestFieldsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDeprecatedManifestFieldsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsCommonName); - } + public string GetCommonName() => String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription); - } + public string GetDescription() => String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingDoubleQuotesForConstantString.cs b/Rules/AvoidUsingDoubleQuotesForConstantString.cs index 5b2c232b7..ea608ec49 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("`")) @@ -79,9 +79,7 @@ This is because we'd have to replace the backtick in some cases like `$ and in o } private DiagnosticRecord GetDiagnosticRecord(StringConstantExpressionAst stringConstantExpressionAst, - string suggestedCorrection) - { - return new DiagnosticRecord( + string suggestedCorrection) => new DiagnosticRecord( Strings.AvoidUsingDoubleQuotesForConstantStringError, stringConstantExpressionAst.Extent, GetName(), @@ -95,67 +93,45 @@ private DiagnosticRecord GetDiagnosticRecord(StringConstantExpressionAst stringC ) } ); - } /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDoubleQuotesForConstantStringName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Information; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Information; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..c5fef2ddf 100644 --- a/Rules/AvoidUsingInvokeExpression.cs +++ b/Rules/AvoidUsingInvokeExpression.cs @@ -22,80 +22,53 @@ public class InvokeExpressionRule : AvoidCmdletGeneric /// GetCmdletName: Retrieves the name of the cmdlet /// /// - public override string GetCmdletName() - { - return "invoke-expression"; - } + public override string GetCmdletName() => "invoke-expression"; /// /// GetError: Retrieves the error message /// /// - public override string GetError(String FileName) - { - return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionError); - } + public override string GetError(String FileName) => String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionError); /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingInvokeExpressionRuleName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingInvokeExpressionRuleName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionRuleCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionRuleCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionRuleDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingInvokeExpressionRuleDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. /// /// - public override DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public override DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingPlainTextForPassword.cs b/Rules/AvoidUsingPlainTextForPassword.cs index 6d877e400..731dc8293 100644 --- a/Rules/AvoidUsingPlainTextForPassword.cs +++ b/Rules/AvoidUsingPlainTextForPassword.cs @@ -123,52 +123,34 @@ private TypeConstraintAst GetTypeAttributeAst(ParameterAst paramAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingPlainTextForPasswordName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingPlainTextForPasswordName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingWMICmdlet.cs b/Rules/AvoidUsingWMICmdlet.cs index 09fcca7a7..630c1d03a 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) @@ -86,55 +86,37 @@ private int GetPSMajorVersion(Ast ast) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWMICmdletName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWMICmdletName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletDescription); - } + public string GetDescription() => 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; /// /// GetSeverity:Retrieves the severity of the rule: error, warning of information. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/AvoidUsingWriteHost.cs b/Rules/AvoidUsingWriteHost.cs index fdd4feafa..6fc131fc8 100644 --- a/Rules/AvoidUsingWriteHost.cs +++ b/Rules/AvoidUsingWriteHost.cs @@ -117,53 +117,35 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWriteHostName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingWriteHostName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWriteHostCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWriteHostCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWriteHostDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/ClangSuppresion.cs b/Rules/ClangSuppresion.cs index d0fcfec99..0809f10a3 100644 --- a/Rules/ClangSuppresion.cs +++ b/Rules/ClangSuppresion.cs @@ -16,9 +16,6 @@ internal static class ClangSuppresion /// /// /// - internal static bool ScriptExtendIsWrappedInParenthesis(IScriptExtent scriptExtent) - { - return scriptExtent.Text.StartsWith("(") && scriptExtent.Text.EndsWith(")"); - } + internal static bool ScriptExtendIsWrappedInParenthesis(IScriptExtent scriptExtent) => scriptExtent.Text.StartsWith("(") && scriptExtent.Text.EndsWith(")"); } } diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 8418202e4..de21ef68c 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -113,26 +113,17 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Gets the severity of this rule. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => 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. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); /// /// Overrides the ConfigurableRule method to allow setting of a non-constant diff --git a/Rules/CompatibilityRules/UseCompatibleCommands.cs b/Rules/CompatibilityRules/UseCompatibleCommands.cs index bad708e2c..83698f473 100644 --- a/Rules/CompatibilityRules/UseCompatibleCommands.cs +++ b/Rules/CompatibilityRules/UseCompatibleCommands.cs @@ -33,30 +33,21 @@ public class UseCompatibleCommands : CompatibilityRule /// /// Get the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCommandsCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCommandsCommonName); /// /// Get the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCommandsDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCommandsDescription); /// /// Get the localized name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCompatibleCommandsName); - } /// /// Create an AST visitor to generate command-compatiblity diagnostics. @@ -151,10 +142,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst) return AstVisitAction.Continue; } - public override IEnumerable GetDiagnosticRecords() - { - return _diagnosticAccumulator; - } + public override IEnumerable GetDiagnosticRecords() => _diagnosticAccumulator; private void CheckCommandInvocationParameters( CompatibilityProfileData targetProfile, diff --git a/Rules/CompatibilityRules/UseCompatibleSyntax.cs b/Rules/CompatibilityRules/UseCompatibleSyntax.cs index c7d10c19d..57e91403e 100644 --- a/Rules/CompatibilityRules/UseCompatibleSyntax.cs +++ b/Rules/CompatibilityRules/UseCompatibleSyntax.cs @@ -69,58 +69,40 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Get the common name of this rule. /// - public override string GetCommonName() - { - return string.Format( + public override string GetCommonName() => string.Format( CultureInfo.CurrentCulture, Strings.UseCompatibleSyntaxCommonName); - } /// /// Get the description of this rule. /// - public override string GetDescription() - { - return string.Format( + public override string GetDescription() => string.Format( CultureInfo.CurrentCulture, Strings.UseCompatibleSyntaxDescription); - } /// /// Get the localized name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCompatibleSyntaxName); - } /// /// Get the severity of this rule. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// Get the name of the source of this rule. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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) { @@ -170,10 +152,7 @@ public SyntaxCompatibilityVisitor( _targetVersions = targetVersions; } - public IEnumerable GetDiagnosticRecords() - { - return _diagnosticAccumulator; - } + public IEnumerable GetDiagnosticRecords() => _diagnosticAccumulator; public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst) { @@ -394,13 +373,10 @@ public override AstVisitAction VisitPipelineChain(PipelineChainAst statementChai return AstVisitAction.Continue; } - private bool TargetsNonPS7() - { - return _targetVersions.Contains(s_v3) + private bool TargetsNonPS7() => _targetVersions.Contains(s_v3) || _targetVersions.Contains(s_v4) || _targetVersions.Contains(s_v5) || _targetVersions.Contains(s_v6); - } #endif private void AddDiagnostic( diff --git a/Rules/CompatibilityRules/UseCompatibleTypes.cs b/Rules/CompatibilityRules/UseCompatibleTypes.cs index 4d10c6b3c..a80aa8999 100644 --- a/Rules/CompatibilityRules/UseCompatibleTypes.cs +++ b/Rules/CompatibilityRules/UseCompatibleTypes.cs @@ -30,30 +30,21 @@ public class UseCompatibleTypes : CompatibilityRule /// /// Get the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleTypesCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleTypesCommonName); /// /// Get the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleTypesDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleTypesDescription); /// /// Get the localized name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCompatibleTypesName); - } /// /// Create a visitor to check type compatibility of a PowerShell AST. @@ -95,10 +86,7 @@ public TypeCompatibilityVisitor( _diagnosticAccumulator = new List(); } - public override IEnumerable GetDiagnosticRecords() - { - return _diagnosticAccumulator; - } + public override IEnumerable GetDiagnosticRecords() => _diagnosticAccumulator; public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpressionAst) { diff --git a/Rules/DscExamplesPresent.cs b/Rules/DscExamplesPresent.cs index 17cca2a1c..80e93def7 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,60 +111,42 @@ item is TypeDefinitionAst yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentNoExamplesError, resourceName), dscClass.Extent, GetName(), DiagnosticSeverity.Information, fileName); } - } + } } /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscExamplesPresent); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscExamplesPresent); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Rules/DscTestsPresent.cs b/Rules/DscTestsPresent.cs index 3a19902d9..79a4972eb 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 @@ -118,53 +118,35 @@ 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.DscTestsPresent); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscTestsPresent); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Rules/InvalidMultiDotValue.cs b/Rules/InvalidMultiDotValue.cs index 008d2cc79..756e60fc9 100644 --- a/Rules/InvalidMultiDotValue.cs +++ b/Rules/InvalidMultiDotValue.cs @@ -93,63 +93,42 @@ memberAst.Member is ConstantExpressionAst constantAst && /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.InvalidMultiDotValueName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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/MisleadingBacktick.cs b/Rules/MisleadingBacktick.cs index ea709b7af..002f30b69 100644 --- a/Rules/MisleadingBacktick.cs +++ b/Rules/MisleadingBacktick.cs @@ -88,53 +88,35 @@ private List GetCorrectionExtent(IScriptExtent violationExtent /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MisleadingBacktickName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MisleadingBacktickName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/MissingModuleManifestField.cs b/Rules/MissingModuleManifestField.cs index 180767766..bde7cc80b 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,57 +133,39 @@ private List GetCorrectionExtent(HashtableAst ast) correctionExtents.Add(correctionExtent); return correctionExtents; } - + /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MissingModuleManifestFieldName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MissingModuleManifestFieldName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return String.Format(CultureInfo.CurrentCulture, Strings.MissingModuleManifestFieldCommonName); - } + public string GetCommonName() => String.Format(CultureInfo.CurrentCulture, Strings.MissingModuleManifestFieldCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return String.Format(CultureInfo.CurrentCulture, Strings.MissingModuleManifestFieldDescription); - } + public string GetDescription() => String.Format(CultureInfo.CurrentCulture, Strings.MissingModuleManifestFieldDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/MissingTryBlock.cs b/Rules/MissingTryBlock.cs index 3a415f647..d8b1ae799 100644 --- a/Rules/MissingTryBlock.cs +++ b/Rules/MissingTryBlock.cs @@ -75,54 +75,36 @@ stringAst.Parent is CommandAst commandAst && /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.MissingTryBlockCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.MissingTryBlockCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.MissingTryBlockDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.MissingTryBlockDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MissingTryBlockName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index 652197afd..d60d46928 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -159,63 +159,42 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PlaceCloseBraceName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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, @@ -496,9 +475,6 @@ private List GetTokens(Ast ast, List tokens, ref Dictionary string.Format(CultureInfo.CurrentCulture, errorString); } } diff --git a/Rules/PlaceOpenBrace.cs b/Rules/PlaceOpenBrace.cs index 5a74626d9..b9d2a91a5 100644 --- a/Rules/PlaceOpenBrace.cs +++ b/Rules/PlaceOpenBrace.cs @@ -127,68 +127,44 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PlaceOpenBraceName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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) - { - return string.Format(CultureInfo.CurrentCulture, errorString); - } + private static string GetError(string errorString) => string.Format(CultureInfo.CurrentCulture, errorString); private IEnumerable FindViolationsForBraceShouldBeOnSameLine( Token[] tokens, @@ -324,10 +300,7 @@ private List GetCorrectionsForBraceShouldNotBeOnSameLine( return corrections; } - private string GetIndentation(Token[] tokens, int refTokenPos) - { - return new String(' ', GetStartColumnNumberOfTokenLine(tokens, refTokenPos) - 1); - } + private string GetIndentation(Token[] tokens, int refTokenPos) => new String(' ', GetStartColumnNumberOfTokenLine(tokens, refTokenPos) - 1); private int GetStartColumnNumberOfTokenLine(Token[] tokens, int refTokenPos) { diff --git a/Rules/PossibleIncorrectComparisonWithNull.cs b/Rules/PossibleIncorrectComparisonWithNull.cs index 692f49f13..463ee40fd 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) { @@ -114,51 +114,35 @@ private IEnumerable GetCorrectionExtent(BinaryExpressionAst bi /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectComparisonWithNullName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectComparisonWithNullName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectComparisonWithNullCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectComparisonWithNullCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectComparisonWithNullDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs b/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs index e18bf54f9..ffe03a24e 100644 --- a/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs +++ b/Rules/PossibleIncorrectUsageOfAssignmentOperator.cs @@ -100,52 +100,34 @@ private DiagnosticRecord AnalyzePipelineBaseAst(PipelineBaseAst pipelineBaseAst, /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectUsageOfAssignmentOperatorName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectUsageOfAssignmentOperatorName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfAssignmentOperatorCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfAssignmentOperatorCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfAssignmentOperatorDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs b/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs index 9e33f19f2..dd713d17b 100644 --- a/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs +++ b/Rules/PossibleIncorrectUsageOfRedirectionOperator.cs @@ -48,52 +48,34 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectUsageOfRedirectionOperatorName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.PossibleIncorrectUsageOfRedirectionOperatorName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfRedirectionOperatorCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfRedirectionOperatorCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.PossibleIncorrectUsageOfRedirectionOperatorDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/ProvideCommentHelp.cs b/Rules/ProvideCommentHelp.cs index 3a46f0ada..70d7f3010 100644 --- a/Rules/ProvideCommentHelp.cs +++ b/Rules/ProvideCommentHelp.cs @@ -118,64 +118,40 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ProvideCommentHelpName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ProvideCommentHelpName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ProvideCommentHelpCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ProvideCommentHelpCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ProvideCommentHelpDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.ProvideCommentHelpDescription); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); // TODO replace with extension version - private static IEnumerable GetLines(string text) - { - return text.Split('\n').Select(l => l.Trim('\r')); - } + private static IEnumerable GetLines(string text) => text.Split('\n').Select(l => l.Trim('\r')); - private DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Information; - } + private DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Information; private IEnumerable GetCorrection(Ast ast, FunctionDefinitionAst funcDefnAst) { @@ -326,10 +302,7 @@ public CommentHelpBuilder() notes = new CommentHelpNode("Notes", "General notes"); } - public void AddParameter(string paramName) - { - parameters.Add(new ParameterHelpNode(paramName, "Parameter description")); - } + public void AddParameter(string paramName) => parameters.Add(new ParameterHelpNode(paramName, "Parameter description")); public string GetCommentHelp(bool blockComment, bool snippet) { @@ -358,16 +331,10 @@ public string GetCommentHelp(bool blockComment, bool snippet) return sb.ToString(); } - public override string ToString() - { - return ToString(false); - } + public override string ToString() => ToString(false); // todo remove code duplication - public string ToSnippetString() - { - return ToString(true); - } + public string ToSnippetString() => ToString(true); private string ToString(bool snippetify) { @@ -394,10 +361,7 @@ public Counter(int? start) count = start; } - public int? Next() - { - return count.HasValue ? count++ : null; - } + public int? Next() => count.HasValue ? count++ : null; } private class CommentHelpNode @@ -411,10 +375,7 @@ public CommentHelpNode(string nodeName, string description) public string Name { get; } public string Description { get; set; } - public override string ToString() - { - return ToString(null); - } + public override string ToString() => ToString(null); public virtual string ToString(int? tabStop) { @@ -428,10 +389,7 @@ public virtual string ToString(int? tabStop) return sb.ToString(); } - protected string Snippetify(int? tabStop, string defaultValue) - { - return tabStop == null ? defaultValue : $"${{{tabStop}:{defaultValue}}}"; - } + protected string Snippetify(int? tabStop, string defaultValue) => tabStop == null ? defaultValue : $"${{{tabStop}:{defaultValue}}}"; } private class ParameterHelpNode : CommentHelpNode @@ -444,10 +402,7 @@ public ParameterHelpNode(string parameterName, string parameterDescription) public string ParameterName { get; } - public override string ToString() - { - return ToString(null); - } + public override string ToString() => ToString(null); public override string ToString(int? tabStop) { diff --git a/Rules/ReturnCorrectTypesForDSCFunctions.cs b/Rules/ReturnCorrectTypesForDSCFunctions.cs index 0c750571c..ef6317671 100644 --- a/Rules/ReturnCorrectTypesForDSCFunctions.cs +++ b/Rules/ReturnCorrectTypesForDSCFunctions.cs @@ -174,53 +174,35 @@ 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.ReturnCorrectTypeDSCFunctionsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReturnCorrectTypeDSCFunctionsName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReturnCorrectTypesForDSCFunctionsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ReturnCorrectTypesForDSCFunctionsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReturnCorrectTypesForDSCFunctionsDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Rules/ReviewUnusedParameter.cs b/Rules/ReviewUnusedParameter.cs index 9b727e7fc..ad4918024 100644 --- a/Rules/ReviewUnusedParameter.cs +++ b/Rules/ReviewUnusedParameter.cs @@ -196,53 +196,35 @@ private static bool IsBoundParametersReference(Ast ast) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReviewUnusedParameterName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReviewUnusedParameterName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterDescription); /// - /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. + /// 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); /// /// Returns a dictionary including all variables in the scriptblock and their count @@ -259,7 +241,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..b8999d544 100644 --- a/Rules/UseApprovedVerbs.cs +++ b/Rules/UseApprovedVerbs.cs @@ -74,52 +74,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseApprovedVerbsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseApprovedVerbsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() { - return string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseBOMForUnicodeEncodedFile.cs b/Rules/UseBOMForUnicodeEncodedFile.cs index e399b6d0a..be855a489 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,60 +89,42 @@ private Encoding GetByteStreamEncoding(byte[] byteStream) // Did not detect BOM OR Unknown File encoding return null; - + } /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseBOMForUnicodeEncodedFileName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseBOMForUnicodeEncodedFileName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseCmdletCorrectly.cs b/Rules/UseCmdletCorrectly.cs index ccec27e0b..6fe83a431 100644 --- a/Rules/UseCmdletCorrectly.cs +++ b/Rules/UseCmdletCorrectly.cs @@ -204,53 +204,35 @@ private bool MandatoryParameterExists(CommandAst cmdAst) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCmdletCorrectlyName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCmdletCorrectlyName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCmdletCorrectlyCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseCmdletCorrectlyCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCmdletCorrectlyDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseCompatibleCmdlets.cs b/Rules/UseCompatibleCmdlets.cs index c74665e95..d50fdeb3b 100644 --- a/Rules/UseCompatibleCmdlets.cs +++ b/Rules/UseCompatibleCmdlets.cs @@ -58,63 +58,42 @@ public UseCompatibleCmdlets() /// /// Retrieves the common name of this rule. /// - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCmdletsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCmdletsCommonName); /// /// Retrieves the description of this rule. /// - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCmdletsDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseCompatibleCmdletsDescription); /// /// Retrieves the name of this rule. /// - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCompatibleCmdletsName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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] @@ -363,10 +342,7 @@ private void SetupCmdletsDictionary() /// Checks if the given directory has the reference file /// directory must be non-null /// - private bool ContainsReferenceFile(string directory) - { - return File.Exists(Path.Combine(directory, reference + ".json")); - } + private bool ContainsReferenceFile(string directory) => File.Exists(Path.Combine(directory, reference + ".json")); /// /// Resets the values in curCmdletCompatibilityMap to true @@ -491,11 +467,8 @@ private HashSet GetCmdletsFromData(dynamic deserializedObject) /// /// Check if rule arguments are valid /// - private bool RuleParamsValid(Dictionary ruleArgs) - { - return ruleArgs.Keys.All( + private bool RuleParamsValid(Dictionary ruleArgs) => ruleArgs.Keys.All( key => validParameters.Any(x => x.Equals(key, StringComparison.OrdinalIgnoreCase))); - } /// /// Check if current command is present in the allowlists diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 2c77787c6..a8f7d21ee 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; } @@ -539,73 +539,49 @@ private static PipelineAst FindInnermostContainingPipeline(List pipelineAst return best; } - private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2) - { - return position1.ColumnNumber == position2.ColumnNumber && + private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2) => position1.ColumnNumber == position2.ColumnNumber && position1.LineNumber == position2.LineNumber && position1.File == position2.File; - } /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseConsistentIndentationName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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, @@ -666,20 +642,12 @@ private List GetSuggestedCorrections( return corrections; } - private static int ClipNegative(int x) - { - return x > 0 ? x : 0; - } + private static int ClipNegative(int x) => x > 0 ? x : 0; - private int GetIndentation(int indentationLevel) - { + private int GetIndentation(int indentationLevel) => // todo if condition can be evaluated during rule configuration - return indentationLevel * indentationLevelMultiplier; - } + indentationLevel * indentationLevelMultiplier; - private string GetIndentationString(int indentationLevel) - { - return new string(indentationChar, GetIndentation(indentationLevel)); - } + private string GetIndentationString(int indentationLevel) => new string(indentationChar, GetIndentation(indentationLevel)); } } diff --git a/Rules/UseConsistentParameterSetName.cs b/Rules/UseConsistentParameterSetName.cs index bf12f37e2..b151870e3 100644 --- a/Rules/UseConsistentParameterSetName.cs +++ b/Rules/UseConsistentParameterSetName.cs @@ -338,10 +338,7 @@ private static string GetNamedArgumentValue(AttributeAst attributeAst, string ar /// /// The AttributeAst to search. /// The IScriptExtent of the ParameterSetName value, or null if not found. - private static IScriptExtent GetParameterSetNameValueExtent(AttributeAst attributeAst) - { - return GetAttributeNamedArgumentValueExtent(attributeAst, "ParameterSetName", "Parameter"); - } + private static IScriptExtent GetParameterSetNameValueExtent(AttributeAst attributeAst) => GetAttributeNamedArgumentValueExtent(attributeAst, "ParameterSetName", "Parameter"); /// /// Finds the IScriptExtent of the value assigned to the DefaultParameterSetName argument @@ -350,10 +347,7 @@ private static IScriptExtent GetParameterSetNameValueExtent(AttributeAst attribu /// /// The AttributeAst to search. /// The IScriptExtent of the DefaultParameterSetName value, or null if not found. - private static IScriptExtent GetDefaultParameterSetNameValueExtent(AttributeAst attributeAst) - { - return GetAttributeNamedArgumentValueExtent(attributeAst, "DefaultParameterSetName", "CmdletBinding"); - } + private static IScriptExtent GetDefaultParameterSetNameValueExtent(AttributeAst attributeAst) => GetAttributeNamedArgumentValueExtent(attributeAst, "DefaultParameterSetName", "CmdletBinding"); /// /// Finds the IScriptExtent of the value of a named argument in the given AttributeAst. @@ -433,7 +427,7 @@ public override string GetDescription() => string.Format( ); /// - /// Method: Retrieves the type of the rule: builtin, managed or module. + /// GetSourceType: 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..a24afc7d5 100644 --- a/Rules/UseConsistentParametersKind.cs +++ b/Rules/UseConsistentParametersKind.cs @@ -114,58 +114,37 @@ private IEnumerable checkParamBlockParameters(IEnumerable /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentParametersKindCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentParametersKindCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentParametersKindDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentParametersKindDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseConsistentParametersKindName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseConsistentParametersKindName); /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..faa430a25 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -131,72 +131,48 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentWhitespaceCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentWhitespaceCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentWhitespaceDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentWhitespaceDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseConsistentWhitespaceName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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) - { - return TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator) + private bool IsOperator(Token token) => TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceMultiply) || token.Kind == TokenKind.AndAnd || token.Kind == TokenKind.OrOr; - } private string GetError(ErrorKind kind) { @@ -243,7 +219,7 @@ private IEnumerable FindOpenBraceViolations(TokenOperations to { continue; } - + yield return new DiagnosticRecord( GetError(ErrorKind.BeforeOpeningBrace), lcurly.Value.Extent, @@ -451,10 +427,7 @@ private IEnumerable FindParameterViolations(Ast ast) } } - private bool IsSeparator(Token token) - { - return token.Kind == TokenKind.Comma || token.Kind == TokenKind.Semi; - } + private bool IsSeparator(Token token) => token.Kind == TokenKind.Comma || token.Kind == TokenKind.Semi; private IEnumerable FindSeparatorViolations(TokenOperations tokenOperations) { @@ -487,9 +460,7 @@ private IEnumerable FindSeparatorViolations(TokenOperations to private DiagnosticRecord getDiagnosticRecord( Token token, ErrorKind errKind, - List corrections) - { - return new DiagnosticRecord( + List corrections) => new DiagnosticRecord( GetError(errKind), token.Extent, GetName(), @@ -497,17 +468,10 @@ private DiagnosticRecord getDiagnosticRecord( token.Extent.File, null, corrections); - } - private bool IsKeyword(Token token) - { - return openParenKeywordAllowList.Contains(token.Kind); - } + private bool IsKeyword(Token token) => openParenKeywordAllowList.Contains(token.Kind); - private static bool IsPreviousTokenApartByWhitespace(LinkedListNode tokenNode) - { - return IsPreviousTokenApartByWhitespace(tokenNode, out _); - } + private static bool IsPreviousTokenApartByWhitespace(LinkedListNode tokenNode) => IsPreviousTokenApartByWhitespace(tokenNode, out _); private static bool IsPreviousTokenApartByWhitespace(LinkedListNode tokenNode, out bool hasRedundantWhitespace) { @@ -520,16 +484,10 @@ 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; - } - private static bool IsNextTokenApartByWhitespace(LinkedListNode tokenNode) - { - return IsNextTokenApartByWhitespace(tokenNode, out _); - } + private static bool IsPreviousTokenLParen(LinkedListNode tokenNode) => tokenNode.Previous.Value.Kind == TokenKind.LParen; + + private static bool IsNextTokenApartByWhitespace(LinkedListNode tokenNode) => IsNextTokenApartByWhitespace(tokenNode, out _); private static bool IsNextTokenApartByWhitespace(LinkedListNode tokenNode, out bool hasRedundantWhitespace) { @@ -538,10 +496,7 @@ private static bool IsNextTokenApartByWhitespace(LinkedListNode tokenNode return whiteSpaceSize == actualWhitespaceSize; } - private bool IsPreviousTokenOnSameLineAndApartByWhitespace(LinkedListNode tokenNode) - { - return IsPreviousTokenOnSameLine(tokenNode) && IsPreviousTokenApartByWhitespace(tokenNode); - } + private bool IsPreviousTokenOnSameLineAndApartByWhitespace(LinkedListNode tokenNode) => IsPreviousTokenOnSameLine(tokenNode) && IsPreviousTokenApartByWhitespace(tokenNode); private IEnumerable FindOperatorViolations(TokenOperations tokenOperations) { @@ -637,10 +592,7 @@ private List GetCorrections( } - private static bool IsPreviousTokenOnSameLine(LinkedListNode lparen) - { - return lparen.Previous.Value.Extent.EndLineNumber == lparen.Value.Extent.StartLineNumber; - } + private static bool IsPreviousTokenOnSameLine(LinkedListNode lparen) => lparen.Previous.Value.Extent.EndLineNumber == lparen.Value.Extent.StartLineNumber; } } diff --git a/Rules/UseConstrainedLanguageMode.cs b/Rules/UseConstrainedLanguageMode.cs index 7d73dd5eb..9b60f212d 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); @@ -1041,61 +1041,40 @@ private void CheckForPs1Files(ExpressionAst valueAst, string fieldName, string f /// /// Retrieves the common name of this rule. /// - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConstrainedLanguageModeCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseConstrainedLanguageModeCommonName); /// /// Retrieves the description of this rule. /// - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseConstrainedLanguageModeDescription); - } + public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseConstrainedLanguageModeDescription); /// /// Retrieves the name of this rule. /// - public override string GetName() - { - return string.Format( + public override string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseConstrainedLanguageModeName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => 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..76f6d43aa 100644 --- a/Rules/UseCorrectCasing.cs +++ b/Rules/UseCorrectCasing.cs @@ -224,52 +224,34 @@ private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, strin /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCorrectCasingName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseCorrectCasingName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseDeclaredVarsMoreThanAssignments.cs b/Rules/UseDeclaredVarsMoreThanAssignments.cs index b35caafbc..dcb7b9d2c 100644 --- a/Rules/UseDeclaredVarsMoreThanAssignments.cs +++ b/Rules/UseDeclaredVarsMoreThanAssignments.cs @@ -54,53 +54,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseDeclaredVarsMoreThanAssignmentsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseDeclaredVarsMoreThanAssignmentsName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseDeclaredVarsMoreThanAssignmentsDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); /// /// Checks if a variable is initialized and referenced in either its assignment or children scopes diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 9c453e3a1..d346536ea 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -134,70 +134,47 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName /// /// /// - public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) - { + public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) => // For DSC Class based resource, this rule is N/A, since the Class Properties // are declared only once and available to Get(), Set(), Test() functions - return Enumerable.Empty(); - } + Enumerable.Empty(); /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseIdenticalMandatoryParametersDSCName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseIdenticalMandatoryParametersDSCName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalMandatoryParametersDSCCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalMandatoryParametersDSCCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalMandatoryParametersDSCDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - private IEnumerable GetMandatoryParameters(FunctionDefinitionAst functionDefinitionAst) - { - return functionDefinitionAst.GetParameterAsts()?.Where(IsParameterMandatory) ?? + private IEnumerable GetMandatoryParameters(FunctionDefinitionAst functionDefinitionAst) => functionDefinitionAst.GetParameterAsts()?.Where(IsParameterMandatory) ?? Enumerable.Empty(); - } private bool IsParameterMandatory(ParameterAst paramAst) { @@ -208,16 +185,10 @@ where IsParameterAttribute(attr) && attr is AttributeAst return attrAsts.Any(a => a.NamedArguments.Any(IsNamedAttributeArgumentMandatory)); } - private bool IsParameterAttribute(AttributeBaseAst attributeBaseAst) - { - return attributeBaseAst.TypeName.GetReflectionType().Name.Equals("ParameterAttribute"); - } + private bool IsParameterAttribute(AttributeBaseAst attributeBaseAst) => attributeBaseAst.TypeName.GetReflectionType().Name.Equals("ParameterAttribute"); - private bool IsNamedAttributeArgumentMandatory(NamedAttributeArgumentAst namedAttrArgAst) - { - return namedAttrArgAst.ArgumentName.Equals("mandatory", StringComparison.OrdinalIgnoreCase) && + private bool IsNamedAttributeArgumentMandatory(NamedAttributeArgumentAst namedAttrArgAst) => namedAttrArgAst.ArgumentName.Equals("mandatory", StringComparison.OrdinalIgnoreCase) && namedAttrArgAst.GetValue(); - } private IDictionary GetKeys(string fileName) { @@ -321,15 +292,12 @@ private Tuple GetModuleInfo(string fileName) return version == null ? null : Tuple.Create(moduleName, version); } - private FileInfo GetModuleManifest(string fileName) - { - return Directory + private FileInfo GetModuleManifest(string fileName) => Directory .GetParent(fileName)? .Parent? .Parent? .GetFiles("*.psd1") .Where(f => Helper.IsModuleManifest(f.FullName)) .FirstOrDefault(); - } } } \ No newline at end of file diff --git a/Rules/UseIdenticalParametersDSC.cs b/Rules/UseIdenticalParametersDSC.cs index 785db5066..056495cb5 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); } } } @@ -74,10 +74,7 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName /// /// /// - public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) - { - return Enumerable.Empty(); - } + public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) => Enumerable.Empty(); // We assume they have the same name private bool CompareParamAsts(ParameterAst paramAst1, ParameterAst paramAst2) @@ -118,59 +115,41 @@ 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); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseIdenticalParametersDSCName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalParametersDSCCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalParametersDSCCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseIdenticalParametersDSCDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Rules/UseLiteralInitializerForHashtable.cs b/Rules/UseLiteralInitializerForHashtable.cs index 56a31508f..17a1d93d3 100644 --- a/Rules/UseLiteralInitializerForHashtable.cs +++ b/Rules/UseLiteralInitializerForHashtable.cs @@ -60,63 +60,42 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// /// Retrieves the common name of this rule. /// - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableCommonName); /// /// Retrieves the description of this rule. /// - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableDescription); /// /// Retrieves the name of this rule. /// - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseLiteralInitializerForHashtableName); - } /// /// Retrieves the severity of the rule: error, warning or information. /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; /// /// Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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..94e65f9e6 100644 --- a/Rules/UseOutputTypeCorrectly.cs +++ b/Rules/UseOutputTypeCorrectly.cs @@ -129,53 +129,35 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseOutputTypeCorrectlyName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseOutputTypeCorrectlyName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UsePSCredentialType.cs b/Rules/UsePSCredentialType.cs index de0b2e5a5..e09c265bc 100644 --- a/Rules/UsePSCredentialType.cs +++ b/Rules/UsePSCredentialType.cs @@ -168,53 +168,35 @@ private bool WrongCredentialUsage(ParameterAst parameter, bool requiresTransform /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UsePSCredentialTypeName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UsePSCredentialTypeName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseProcessBlockForPipelineCommand.cs b/Rules/UseProcessBlockForPipelineCommand.cs index 4b2298855..37b599c64 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) @@ -65,34 +65,16 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } } - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseProcessBlockForPipelineCommandName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseProcessBlockForPipelineCommandName); - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandCommonName); - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandDescription); - public SourceType GetSourceType() - { - return SourceType.Builtin; - } + public SourceType GetSourceType() => SourceType.Builtin; - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseShouldProcessCorrectly.cs b/Rules/UseShouldProcessCorrectly.cs index 47bf5eb5c..c78d44bb1 100644 --- a/Rules/UseShouldProcessCorrectly.cs +++ b/Rules/UseShouldProcessCorrectly.cs @@ -67,58 +67,37 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ShouldProcessName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ShouldProcessName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.ShouldProcessCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.ShouldProcessCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture,Strings.ShouldProcessDescription); - } + public string GetDescription() => 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. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); /// /// GetSeverity: Retrieves the severity of the rule: error, warning of information. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; - private DiagnosticSeverity GetDianosticSeverity() - { - return DiagnosticSeverity.Warning; - } + private DiagnosticSeverity GetDianosticSeverity() => DiagnosticSeverity.Warning; /// /// Find the violations in the current AST @@ -232,10 +211,7 @@ private static bool IsShouldProcessCall(Ast ast) return false; } - private bool callsShouldProcessDirectly(Vertex vertex) - { - return funcDigraph.GetNeighbors(vertex).Contains(shouldProcessVertex); - } + private bool callsShouldProcessDirectly(Vertex vertex) => funcDigraph.GetNeighbors(vertex).Contains(shouldProcessVertex); /// /// Checks if an upstream function declares SupportsShouldProcess @@ -475,10 +451,7 @@ public Vertex (String name, Ast ast, bool isNestedFunctionDefinition) /// /// Returns string representation of a Vertex instance /// - public override string ToString() - { - return name; - } + public override string ToString() => name; /// /// Compares two instances of Vertex class to check for equality @@ -502,10 +475,7 @@ public override bool Equals(Object other) /// /// Returns the Hash code of the given Vertex instance /// - public override int GetHashCode() - { - return name.ToLowerInvariant().GetHashCode(); - } + public override int GetHashCode() => name.ToLowerInvariant().GetHashCode(); } /// @@ -520,26 +490,17 @@ class FunctionReferenceDigraph : AstVisitor /// /// Checks if the AST being visited is in an instance FunctionDefinitionAst type /// - private bool IsWithinFunctionDefinition() - { - return functionVisitStack.Count > 0; - } + private bool IsWithinFunctionDefinition() => functionVisitStack.Count > 0; /// /// Returns the function vertex whose children are being currently visited /// - private Vertex GetCurrentFunctionContext() - { - return functionVisitStack.Peek(); - } + private Vertex GetCurrentFunctionContext() => functionVisitStack.Peek(); /// /// Return the constructed digraph /// - public Digraph GetDigraph() - { - return digraph; - } + public Digraph GetDigraph() => digraph; /// /// Public constructor @@ -678,10 +639,7 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio /// /// Return the vertices in the graph /// - public IEnumerable GetVertices() - { - return digraph.GetVertices(); - } + public IEnumerable GetVertices() => digraph.GetVertices(); /// /// Check if two vertices are connected @@ -702,14 +660,8 @@ public bool IsConnected(Vertex vertex, Vertex shouldVertex) /// /// Get the number of edges out of the given vertex /// - public int GetOutDegree(Vertex v) - { - return digraph.GetOutDegree(v); - } + public int GetOutDegree(Vertex v) => digraph.GetOutDegree(v); - public IEnumerable GetNeighbors(Vertex v) - { - return digraph.GetNeighbors(v); - } + public IEnumerable GetNeighbors(Vertex v) => digraph.GetNeighbors(v); } } diff --git a/Rules/UseShouldProcessForStateChangingFunctions.cs b/Rules/UseShouldProcessForStateChangingFunctions.cs index 0d526042f..3aa240601 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)); @@ -82,55 +82,37 @@ private bool HasShouldProcessTrue(IEnumerable attributeAsts) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, this.GetSourceName(), Strings.UseShouldProcessForStateChangingFunctionsName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, this.GetSourceName(), Strings.UseShouldProcessForStateChangingFunctionsName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsDescription); - } + public string GetDescription() => 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. /// /// Rule severity {Information, Warning, Error} - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// /// Source name - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } 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..352c01bf1 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; } @@ -101,53 +101,35 @@ public override IEnumerable AnalyzeScript(Ast ast, string file /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public override string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseSingularNounsName); - } + public override string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseSingularNounsName); /// /// GetName: Retrieves the common name of this rule. /// /// The common name of this rule - public override string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsCommonName); - } + public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public override string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsDescription); - } + public override string GetDescription() => 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. /// /// - public override RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public override RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public override string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); private CorrectionExtent GetCorrection(PluralizerProxy pluralizer, IScriptExtent extent, string commandName, string noun) { diff --git a/Rules/UseStandardDSCFunctionsInResource.cs b/Rules/UseStandardDSCFunctionsInResource.cs index 386198ba5..7b7872c94 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,59 +84,41 @@ 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); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseStandardDSCFunctionsInResourceName); /// /// GetCommonName: Retrieves the Common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Error; - } + public RuleSeverity GetSeverity() => RuleSeverity.Error; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture,Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Rules/UseSupportsShouldProcess.cs b/Rules/UseSupportsShouldProcess.cs index a79fa4428..9bf4d4ae1 100644 --- a/Rules/UseSupportsShouldProcess.cs +++ b/Rules/UseSupportsShouldProcess.cs @@ -197,16 +197,13 @@ private List GetCorrections( return result; } - private CorrectionExtent GetCorrectionsToSetShouldProcessToTrue(ExpressionAst argAst) - { - return new CorrectionExtent( + private CorrectionExtent GetCorrectionsToSetShouldProcessToTrue(ExpressionAst argAst) => new CorrectionExtent( argAst.Extent.StartLineNumber, argAst.Extent.EndLineNumber, argAst.Extent.StartColumnNumber, argAst.Extent.EndColumnNumber, "$true", argAst.Extent.File); - } private CorrectionExtent GetCorrectionToAddParamBlock( FunctionDefinitionAst funcDefnAst, @@ -291,9 +288,7 @@ private CorrectionExtent Normalize( correctionExtent.Description); } - private static CorrectionExtent GetCorrectionToAddAttribute(ParamBlockAst paramBlockAst) - { - return new CorrectionExtent( + private static CorrectionExtent GetCorrectionToAddAttribute(ParamBlockAst paramBlockAst) => new CorrectionExtent( paramBlockAst.Extent.StartLineNumber, paramBlockAst.Extent.StartLineNumber, 1, @@ -305,7 +300,6 @@ private static CorrectionExtent GetCorrectionToAddAttribute(ParamBlockAst paramB }, null, null); - } private static CorrectionExtent GetCorrectionToAddShouldProcess(AttributeAst cmdletBindingAttributeAst) { // 1 for the next position. @@ -423,89 +417,56 @@ private bool TryGetParameterAst( return false; } - private static bool IsParameter(ParameterAst parameterAst, string parameterName) - { - return parameterAst.Name.VariablePath.UserPath.Equals( + private static bool IsParameter(ParameterAst parameterAst, string parameterName) => parameterAst.Name.VariablePath.UserPath.Equals( parameterName, StringComparison.OrdinalIgnoreCase); - } - private static bool IsWhatIf(ParameterAst parameterAst) - { - return IsParameter(parameterAst, "whatif"); - } + private static bool IsWhatIf(ParameterAst parameterAst) => IsParameter(parameterAst, "whatif"); - private static bool IsConfirm(ParameterAst parameterAst) - { - return IsParameter(parameterAst, "confirm"); - } + private static bool IsConfirm(ParameterAst parameterAst) => IsParameter(parameterAst, "confirm"); - private string GetError(string functionName) - { - return string.Format( + private string GetError(string functionName) => string.Format( CultureInfo.CurrentCulture, Strings.UseSupportsShouldProcessError, functionName); - } /// /// Retrieves the common name of this rule. /// - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseSupportsShouldProcessCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseSupportsShouldProcessCommonName); /// /// Retrieves the description of this rule. /// - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseSupportsShouldProcessDescription); - } + public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.UseSupportsShouldProcessDescription); /// /// Retrieves the name of this rule. /// - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseSupportsShouldProcessName); - } /// /// Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => 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. /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Gets the severity of the returned diagnostic record: error, warning, or information. /// /// - public DiagnosticSeverity GetDiagnosticSeverity() - { - return DiagnosticSeverity.Warning; - } + public DiagnosticSeverity GetDiagnosticSeverity() => DiagnosticSeverity.Warning; } } diff --git a/Rules/UseToExportFieldsInManifest.cs b/Rules/UseToExportFieldsInManifest.cs index 9bf612f83..5ad86b7fd 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,69 +234,48 @@ private bool HasAcceptableExportField(string key, HashtableAst hast, out IScript } } return true; - } + } + - /// /// Gets the error string of the rule. /// /// /// - public string GetError(string field) - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestError, field); - } + public string GetError(string field) => string.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestError, field); /// /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseToExportFieldsInManifestName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseToExportFieldsInManifestName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return String.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestCommonName); - } + public string GetCommonName() => String.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return String.Format(CultureInfo.CurrentCulture, Strings.UseToExportFieldsInManifestDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseUTF8EncodingForHelpFile.cs b/Rules/UseUTF8EncodingForHelpFile.cs index d1a3b3fb7..46122387e 100644 --- a/Rules/UseUTF8EncodingForHelpFile.cs +++ b/Rules/UseUTF8EncodingForHelpFile.cs @@ -52,53 +52,35 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseUTF8EncodingForHelpFileName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseUTF8EncodingForHelpFileName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseUTF8EncodingForHelpFileCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseUTF8EncodingForHelpFileCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseUTF8EncodingForHelpFileDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } diff --git a/Rules/UseUsingScopeModifierInNewRunspaces.cs b/Rules/UseUsingScopeModifierInNewRunspaces.cs index 46e1eea8a..4e033882c 100644 --- a/Rules/UseUsingScopeModifierInNewRunspaces.cs +++ b/Rules/UseUsingScopeModifierInNewRunspaces.cs @@ -39,57 +39,39 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// GetName: Retrieves the name of this rule. /// /// The name of this rule - public string GetName() - { - return string.Format( + public string GetName() => string.Format( CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseUsingScopeModifierInNewRunspacesName); - } /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseUsingScopeModifierInNewRunspacesCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseUsingScopeModifierInNewRunspacesCommonName); /// /// GetDescription: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseUsingScopeModifierInNewRunspacesDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Warning; - } + public RuleSeverity GetSeverity() => RuleSeverity.Warning; /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName); private class SyntaxCompatibilityVisitor : AstVisitor2 { @@ -115,11 +97,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(); @@ -131,10 +113,7 @@ public SyntaxCompatibilityVisitor(UseUsingScopeModifierInNewRunspaces rule, stri /// /// GetDiagnosticRecords: Retrieves all Diagnostic Records that were generated during visiting /// - public IEnumerable GetDiagnosticRecords() - { - return _diagnosticAccumulator; - } + public IEnumerable GetDiagnosticRecords() => _diagnosticAccumulator; /// /// VisitScriptBlockExpression: When a ScriptBlockExpression is visited, see if it belongs to a command that needs its variables @@ -154,15 +133,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 +171,7 @@ public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionA return AstVisitAction.SkipChildren; } - + return AstVisitAction.Continue; } @@ -231,7 +210,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); @@ -246,10 +225,7 @@ private static bool TryGetVariableFromExpression(ExpressionAst expression, out V /// IsAssignmentStatementAst: helper function to prevent allocation of closures for FindAll predicate. /// /// - private static bool IsAssignmentStatementAst(Ast ast) - { - return ast is AssignmentStatementAst; - } + private static bool IsAssignmentStatementAst(Ast ast) => ast is AssignmentStatementAst; /// /// FindNonAssignedNonUsingVarAsts: Retrieve variables that are: @@ -266,12 +242,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; } } @@ -280,12 +256,9 @@ private static IEnumerable FindNonAssignedNonUsingVarAsts /// IsNonUsingNonSpecialVariableExpressionAst: helper function to prevent allocation of closures for FindAll predicate. /// /// - private static bool IsNonUsingNonSpecialVariableExpressionAst(Ast ast) - { - return ast is VariableExpressionAst variable && - !(variable.Parent is UsingExpressionAst) && + private static bool IsNonUsingNonSpecialVariableExpressionAst(Ast ast) => ast is VariableExpressionAst variable && + !(variable.Parent is UsingExpressionAst) && !Helper.Instance.HasSpecialVars(variable.VariablePath.UserPath); - } /// /// GetSuggestedCorrections: Retrieves a CorrectionExtent for a given variable @@ -364,10 +337,7 @@ private static bool TryGetSessionNameFromInvokeCommand(CommandAst invokeCommandA /// GetAssignedVarsInSession: Retrieves all previously declared vars for a given session (as in Invoke-Command -Session $session). /// /// - private IReadOnlyCollection GetAssignedVarsInSession(string sessionName) - { - return _varsDeclaredPerSession[sessionName]; - } + private IReadOnlyCollection GetAssignedVarsInSession(string sessionName) => _varsDeclaredPerSession[sessionName]; /// /// AddAssignedVarsToSession: Adds variables to the list of assigned variables for a given Invoke-Command session. @@ -435,13 +405,10 @@ private void GenerateDiagnosticRecords(IEnumerable nonAss /// /// /// - private bool IsInvokeCommandSessionScriptBlock(string cmdName, CommandAst commandAst) - { - return s_invokeCommandCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && + private bool IsInvokeCommandSessionScriptBlock(string cmdName, CommandAst commandAst) => s_invokeCommandCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && commandAst.CommandElements.Any( e => e is CommandParameterAst parameterAst && parameterAst.ParameterName.Equals("session", StringComparison.OrdinalIgnoreCase)); - } /// /// IsInvokeCommandComputerScriptBlock: Returns true if: @@ -450,14 +417,12 @@ private bool IsInvokeCommandSessionScriptBlock(string cmdName, CommandAst comman /// /// /// - private bool IsInvokeCommandComputerScriptBlock(string cmdName, CommandAst commandAst) - { + private bool IsInvokeCommandComputerScriptBlock(string cmdName, CommandAst commandAst) => // 'com' is the shortest unambiguous form for the '-Computer' parameter - return s_invokeCommandCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && + s_invokeCommandCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && commandAst.CommandElements.Any( e => e is CommandParameterAst parameterAst && parameterAst.ParameterName.StartsWith("com", StringComparison.OrdinalIgnoreCase)); - } /// /// IsForeachScriptBlock: Returns true if: @@ -466,13 +431,11 @@ private bool IsInvokeCommandComputerScriptBlock(string cmdName, CommandAst comma /// /// /// - private bool IsForeachScriptBlock(string cmdName, CommandParameterAst scriptBlockParameterAst) - { + private bool IsForeachScriptBlock(string cmdName, CommandParameterAst scriptBlockParameterAst) => // 'pa' is the shortest unambiguous form for the '-Parallel' parameter - return s_foreachObjectCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && - (scriptBlockParameterAst != null && + s_foreachObjectCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && + (scriptBlockParameterAst != null && scriptBlockParameterAst.ParameterName.StartsWith("pa", StringComparison.OrdinalIgnoreCase)); - } /// /// IsJobScriptBlock: Returns true if: @@ -481,37 +444,30 @@ private bool IsForeachScriptBlock(string cmdName, CommandParameterAst scriptBloc /// /// /// - private bool IsJobScriptBlock(string cmdName, CommandParameterAst scriptBlockParameterAst) - { + private bool IsJobScriptBlock(string cmdName, CommandParameterAst scriptBlockParameterAst) => // 'ini' is the shortest unambiguous form for the '-InitializationScript' parameter - return (s_jobCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) || + (s_jobCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase) || s_threadJobCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase)) && - !(scriptBlockParameterAst != null && + !(scriptBlockParameterAst != null && scriptBlockParameterAst.ParameterName.StartsWith("ini", StringComparison.OrdinalIgnoreCase)); - } /// /// IsInlineScriptBlock: Returns true if: /// - command is 'InlineScript' (or alias) /// /// - private bool IsInlineScriptBlock(string cmdName) - { - return s_inlineScriptCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase); - } + private bool IsInlineScriptBlock(string cmdName) => s_inlineScriptCmdletNamesAndAliases.Contains(cmdName, StringComparer.OrdinalIgnoreCase); /// /// IsDSCScriptResource: Returns true if: /// - command is 'GetScript', 'TestScript' or 'SetScript' /// /// - private bool IsDSCScriptResource(string cmdName, CommandAst commandAst) - { + 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) && + s_dscScriptResourceCommandNames.Contains(cmdName, StringComparer.OrdinalIgnoreCase) && commandAst.CommandElements[1].ToString() == "="; - } } } } diff --git a/Rules/UseVerboseMessageInDSCResource.cs b/Rules/UseVerboseMessageInDSCResource.cs index d6038544f..3b5377afb 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,68 +56,47 @@ public IEnumerable AnalyzeDSCResource(Ast ast, string fileName } } - + /// /// AnalyzeDSCClass: This function returns nothing in the case of dsc class. /// /// /// /// - public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) - { - return Enumerable.Empty(); - } + public IEnumerable AnalyzeDSCClass(Ast ast, string fileName) => Enumerable.Empty(); /// /// Method: Retrieves the name of this rule. /// - public string GetName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseVerboseMessageInDSCResourceName); - } + public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseVerboseMessageInDSCResourceName); /// /// GetCommonName: Retrieves the common name of this rule. /// /// The common name of this rule - public string GetCommonName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseVerboseMessageInDSCResourceCommonName); - } + public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.UseVerboseMessageInDSCResourceCommonName); /// /// Method: Retrieves the description of this rule. /// /// The description of this rule - public string GetDescription() - { - return string.Format(CultureInfo.CurrentCulture, Strings.UseVerboseMessageInDSCResourceDescription); - } + public string GetDescription() => 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. /// /// - public RuleSeverity GetSeverity() - { - return RuleSeverity.Information; - } + public RuleSeverity GetSeverity() => RuleSeverity.Information; /// /// Method: Retrieves the module/assembly name the rule is from. /// - public string GetSourceName() - { - return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); - } + public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName); } } diff --git a/Utils/RuleMaker.psm1 b/Utils/RuleMaker.psm1 index 71a5d551f..d16dae893 100644 --- a/Utils/RuleMaker.psm1 +++ b/Utils/RuleMaker.psm1 @@ -162,10 +162,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules /// /// Retrieves the type of the rule, Builtin, Managed or Module. /// - public SourceType GetSourceType() - {{ - return SourceType.Builtin; - }} + public SourceType GetSourceType() => SourceType.Builtin; }} }} '@ diff --git a/build.psm1 b/build.psm1 index 041b207a9..8fdcb066b 100644 --- a/build.psm1 +++ b/build.psm1 @@ -81,6 +81,24 @@ function Copy-CompatibilityProfiles Copy-Item -Force $profileDir/* $targetProfileDir } +function Test-CodeStyle +{ + if ( -not $script:DotnetExe ) { + $script:DotnetExe = Get-DotnetExe + } + $dotnetArgs = "format", + "--verify-no-changes", + "style" + $formatOutput = & $script:DotnetExe $dotnetArgs 2>&1 + if ( $LASTEXITCODE -ne 0 ) { + Write-Verbose -Verbose -Message "dotnet is $(${script:DotnetExe}.Source)" + $dotnetArgs | Foreach-Object {"dotnetArg: $_"} | Write-Verbose -Verbose + Get-PSCallStack | Write-Verbose -Verbose + Write-Verbose -Verbose -Message "$formatOutput" + throw "Please, fix code style via running 'dotnet format style' command." + } +} + # build script analyzer (and optionally build everything with -All) function Start-ScriptAnalyzerBuild { @@ -110,6 +128,9 @@ function Start-ScriptAnalyzerBuild if ( $PSBoundParameters['Verbose'] ) { $verboseWanted = $PSBoundParameters['Verbose'].ToBool() } + + # don't allow the build to be started unless we have the proper code formatting + Test-CodeStyle } END {