Skip to content

Refactor rules methods - #2182

Open
Denis Melnikov (Haimasker) wants to merge 1 commit into
PowerShell:mainfrom
Haimasker:replace-getter-method-with-property
Open

Refactor rules methods#2182
Denis Melnikov (Haimasker) wants to merge 1 commit into
PowerShell:mainfrom
Haimasker:replace-getter-method-with-property

Conversation

@Haimasker

@HaimaskerDenis Melnikov (Haimasker) commented May 4, 2026

Copy link
Copy Markdown
Contributor
  • Refactor methods into one-liners using lambda operator.
  • Unify methods style across all rules.
  • Add automatic code style check into build process.

PR Summary

Motivation:
I consider these changes useful, because such refactoring allows to reduce the total amount of code in the rules, thereby the developer will be more focused on the logic of the rule itself. In the future, it is planned to complete refactoring with other code style rules.

PR Checklist

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors rule metadata to expose SourceType as a property rather than a GetSourceType() method, and renames the enum backing that metadata to RuleSourceType, updating the engine and built-in rules to match. The goal is to reduce boilerplate and standardize how built-in rules report their origin.

Changes:

  • Replace GetSourceType() implementations across built-in rules (and the rule template) with a SourceType property.
  • Rename SourceType enum to RuleSourceType and update IRule/RuleInfo/command plumbing accordingly.
  • Apply minor whitespace/formatting cleanups in touched rule files.

Reviewed changes

Copilot reviewed 83 out of 83 changed files in this pull request and generated 1 comment.

Show a summary per file
FileDescription
Utils/RuleMaker.psm1Updates the rule-generation template to emit the new SourceType property.
Rules/UseVerboseMessageInDSCResource.csReplaces GetSourceType() with SourceType property.
Rules/UseUTF8EncodingForHelpFile.csReplaces GetSourceType() with SourceType property.
Rules/UseUsingScopeModifierInNewRunspaces.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseToExportFieldsInManifest.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseSupportsShouldProcess.csReplaces GetSourceType() with SourceType property.
Rules/UseStandardDSCFunctionsInResource.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseSingularNouns.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/UseSingleValueFromPipelineParameter.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/UseShouldProcessForStateChangingFunctions.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseShouldProcessCorrectly.csReplaces GetSourceType() with SourceType property.
Rules/UsePSCredentialType.csReplaces GetSourceType() with SourceType property.
Rules/UseProcessBlockForPipelineCommand.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseOutputTypeCorrectly.csReplaces GetSourceType() with SourceType property.
Rules/UseLiteralInitializerForHashtable.csReplaces GetSourceType() with SourceType property.
Rules/UseIdenticalParametersDSC.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseIdenticalMandatoryParametersDSC.csReplaces GetSourceType() with SourceType property.
Rules/UseDeclaredVarsMoreThanAssignments.csReplaces GetSourceType() with SourceType property.
Rules/UseCorrectCasing.csReplaces GetSourceType() override with SourceType override.
Rules/UseConstrainedLanguageMode.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/UseConsistentWhitespace.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/UseConsistentParametersKind.csReplaces GetSourceType() override with SourceType override.
Rules/UseConsistentParameterSetName.csReplaces GetSourceType() override with SourceType override.
Rules/UseConsistentIndentation.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/UseCompatibleCmdlets.csReplaces GetSourceType() with SourceType property.
Rules/UseCmdletCorrectly.csReplaces GetSourceType() with SourceType property.
Rules/UseBOMForUnicodeEncodedFile.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/UseApprovedVerbs.csReplaces GetSourceType() with SourceType property.
Rules/ReviewUnusedParameter.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/ReturnCorrectTypesForDSCFunctions.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/ProvideCommentHelp.csReplaces GetSourceType() override with SourceType override.
Rules/PossibleIncorrectUsageOfRedirectionOperator.csReplaces GetSourceType() with SourceType property.
Rules/PossibleIncorrectUsageOfAssignmentOperator.csReplaces GetSourceType() with SourceType property.
Rules/PossibleIncorrectComparisonWithNull.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/PlaceOpenBrace.csReplaces GetSourceType() override with SourceType override.
Rules/PlaceCloseBrace.csReplaces GetSourceType() override with SourceType override.
Rules/MissingModuleManifestField.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/MisleadingBacktick.csReplaces GetSourceType() with SourceType property.
Rules/DscTestsPresent.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/DscExamplesPresent.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/CompatibilityRules/UseCompatibleSyntax.csReplaces GetSourceType() override with SourceType override.
Rules/CompatibilityRules/CompatibilityRule.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUsingWriteHost.csReplaces GetSourceType() with SourceType property.
Rules/AvoidUsingWMICmdlet.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidUsingPlainTextForPassword.csReplaces GetSourceType() with SourceType property.
Rules/AvoidUsingInvokeExpression.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUsingDoubleQuotesForConstantString.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/AvoidUsingDeprecatedManifestFields.csReplaces GetSourceType() with SourceType property.
Rules/AvoidUsingConvertToSecureStringWithPlainText.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUsingComputerNameHardcoded.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUsingBrokenHashAlgorithms.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUsingAllowUnencryptedAuthentication.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidUserNameAndPasswordParams.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidTrailingWhitespace.csReplaces GetSourceType() with SourceType property.
Rules/AvoidShouldContinueWithoutForce.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidSemicolonsAsLineTerminators.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidReservedWordsAsFunctionNames.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidReservedParams.csReplaces GetSourceType() with SourceType property.
Rules/AvoidReservedCharInCmdlet.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidPositionalParameters.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/AvoidOverwritingBuiltInCmdlets.csReplaces GetSourceType() override with SourceType override.
Rules/AvoidNullOrEmptyHelpMessageAttribute.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidMultipleTypeAttributes.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidLongLines.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/AvoidInvokingEmptyMembers.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidGlobalVars.csReplaces GetSourceType() with SourceType property.
Rules/AvoidGlobalFunctions.csReplaces GetSourceType() with SourceType property.
Rules/AvoidGlobalAliases.csReplaces GetSourceType() with SourceType property.
Rules/AvoidExclaimOperator.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Rules/AvoidEmptyCatchBlock.csReplaces GetSourceType() with SourceType property.
Rules/AvoidDefaultValueForMandatoryParameter.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AvoidDefaultTrueValueSwitchParameter.csReplaces GetSourceType() with SourceType property.
Rules/AvoidAssignmentToAutomaticVariable.csReplaces GetSourceType() with SourceType property.
Rules/AvoidAlias.csReplaces GetSourceType() with SourceType property; whitespace cleanup.
Rules/AlignAssignmentStatement.csReplaces GetSourceType() override with SourceType override; whitespace cleanup.
Engine/Generic/RuleSourceType.csRenames the enum type from SourceType to RuleSourceType; updates doc comment.
Engine/Generic/RuleInfo.csUpdates RuleInfo.SourceType and constructors to use RuleSourceType.
Engine/Generic/IRule.csChanges the rule contract from GetSourceType() to a SourceType property.
Engine/Generic/ExternalRule.csUpdates external rule implementation to SourceType property.
Engine/Generic/ConfigurableRule.csReplaces abstract GetSourceType() with abstract SourceType property.
Engine/Generic/AvoidParameterGeneric.csReplaces abstract GetSourceType() with abstract SourceType property.
Engine/Generic/AvoidCmdletGeneric.csReplaces abstract GetSourceType() with abstract SourceType property.
Engine/Commands/GetScriptAnalyzerRuleCommand.csSwitches to reading rule.SourceType when producing RuleInfo.
Comments suppressed due to low confidence (1)

Engine/Generic/RuleSourceType.cs:10

  • Renaming the public enum SourceType to RuleSourceType (and changing the rule contract from GetSourceType() to a SourceType property) is a breaking API change for any external rule implementations or consumers compiled against previous versions. If maintaining compatibility is required, consider keeping SourceType (or providing an [Obsolete] shim) and adding the new property/mapping without removing the old symbols until a major version bump.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadEngine/Generic/IRule.cs Outdated
@HaimaskerDenis Melnikov (Haimasker) changed the title Refactor GetSourceType methodWIP: Refactor GetSourceType methodMay 4, 2026
@Haimasker
Denis Melnikov (Haimasker)force-pushed the replace-getter-method-with-property branch from dd25a91 to 23c7f9eCompareMay 5, 2026 19:36
@HaimaskerDenis Melnikov (Haimasker) changed the title WIP: Refactor GetSourceType methodRefactor GetSourceType methodMay 5, 2026

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 77 out of 79 changed files in this pull request and generated 4 comments.

Comment threadUtils/RuleMaker.psm1
Comment threadEngine/Generic/ConfigurableRule.cs
Comment threadEngine/Generic/AvoidCmdletGeneric.cs
Comment threadEngine/Generic/AvoidParameterGeneric.cs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the only value of this is to reduce number of lines of code and there are still many others methods left, it leaves state after this PR inconsistent. I think we have to make a decision first whether we want to go through this or not Andy Jordan (@andyleejordan) and if so we need to do it all in one go

@andyleejordan

Copy link
Copy Markdown
Member

since the only value of this is to reduce number of lines of code and there are still many others methods left, it leaves state after this PR inconsistent. I think we have to make a decision first whether we want to go through this or not Andy Jordan (@andyleejordan) and if so we need to do it all in one go

This seems like something that could all be done in go by Opus 4.7 pretty easily. Let's not piecemeal it.

@Haimasker

Copy link
Copy Markdown
ContributorAuthor

Let's not piecemeal it.

Sure. I am marking request as "wip", will finish it soon.

@HaimaskerDenis Melnikov (Haimasker) changed the title Refactor GetSourceType methodwip: Refactor GetSourceType methodMay 17, 2026
@Haimasker
Denis Melnikov (Haimasker)force-pushed the replace-getter-method-with-property branch from 23c7f9e to 1632c12CompareJune 20, 2026 17:10
* Refactor methods into one-liners using lambda operator.
* Add automatic code style check into build process.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 83 out of 85 changed files in this pull request and generated 1 comment.

Comment threadbuild.psm1
Comment on lines +84 to +100
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."
}
}
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@Haimasker@andyleejordan@bergmeister