WIP: Refactor GetSourceType method#2182
Open
Haimasker wants to merge 1 commit intoPowerShell:mainfrom
Open
Conversation
* Refactor GetSourceType method into SourceType property. * Make code cleaner. * Unify SourceType property implementation across all rules.
Contributor
There was a problem hiding this comment.
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 aSourceTypeproperty. - Rename
SourceTypeenum toRuleSourceTypeand updateIRule/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
| File | Description |
|---|---|
| Utils/RuleMaker.psm1 | Updates the rule-generation template to emit the new SourceType property. |
| Rules/UseVerboseMessageInDSCResource.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseUTF8EncodingForHelpFile.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseUsingScopeModifierInNewRunspaces.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseToExportFieldsInManifest.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseSupportsShouldProcess.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseStandardDSCFunctionsInResource.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseSingularNouns.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/UseSingleValueFromPipelineParameter.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/UseShouldProcessForStateChangingFunctions.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseShouldProcessCorrectly.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UsePSCredentialType.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseProcessBlockForPipelineCommand.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseOutputTypeCorrectly.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseLiteralInitializerForHashtable.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseIdenticalParametersDSC.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseIdenticalMandatoryParametersDSC.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseDeclaredVarsMoreThanAssignments.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseCorrectCasing.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/UseConstrainedLanguageMode.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/UseConsistentWhitespace.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/UseConsistentParametersKind.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/UseConsistentParameterSetName.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/UseConsistentIndentation.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/UseCompatibleCmdlets.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseCmdletCorrectly.cs | Replaces GetSourceType() with SourceType property. |
| Rules/UseBOMForUnicodeEncodedFile.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/UseApprovedVerbs.cs | Replaces GetSourceType() with SourceType property. |
| Rules/ReviewUnusedParameter.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/ReturnCorrectTypesForDSCFunctions.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/ProvideCommentHelp.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/PossibleIncorrectUsageOfRedirectionOperator.cs | Replaces GetSourceType() with SourceType property. |
| Rules/PossibleIncorrectUsageOfAssignmentOperator.cs | Replaces GetSourceType() with SourceType property. |
| Rules/PossibleIncorrectComparisonWithNull.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/PlaceOpenBrace.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/PlaceCloseBrace.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/MissingModuleManifestField.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/MisleadingBacktick.cs | Replaces GetSourceType() with SourceType property. |
| Rules/DscTestsPresent.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/DscExamplesPresent.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/CompatibilityRules/UseCompatibleSyntax.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/CompatibilityRules/CompatibilityRule.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUsingWriteHost.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidUsingWMICmdlet.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidUsingPlainTextForPassword.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidUsingInvokeExpression.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUsingDoubleQuotesForConstantString.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/AvoidUsingDeprecatedManifestFields.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidUsingConvertToSecureStringWithPlainText.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUsingComputerNameHardcoded.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUsingBrokenHashAlgorithms.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUsingAllowUnencryptedAuthentication.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidUserNameAndPasswordParams.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidTrailingWhitespace.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidShouldContinueWithoutForce.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidSemicolonsAsLineTerminators.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidReservedWordsAsFunctionNames.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidReservedParams.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidReservedCharInCmdlet.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidPositionalParameters.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/AvoidOverwritingBuiltInCmdlets.cs | Replaces GetSourceType() override with SourceType override. |
| Rules/AvoidNullOrEmptyHelpMessageAttribute.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidMultipleTypeAttributes.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidLongLines.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/AvoidInvokingEmptyMembers.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidGlobalVars.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidGlobalFunctions.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidGlobalAliases.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidExclaimOperator.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Rules/AvoidEmptyCatchBlock.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidDefaultValueForMandatoryParameter.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AvoidDefaultTrueValueSwitchParameter.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidAssignmentToAutomaticVariable.cs | Replaces GetSourceType() with SourceType property. |
| Rules/AvoidAlias.cs | Replaces GetSourceType() with SourceType property; whitespace cleanup. |
| Rules/AlignAssignmentStatement.cs | Replaces GetSourceType() override with SourceType override; whitespace cleanup. |
| Engine/Generic/RuleSourceType.cs | Renames the enum type from SourceType to RuleSourceType; updates doc comment. |
| Engine/Generic/RuleInfo.cs | Updates RuleInfo.SourceType and constructors to use RuleSourceType. |
| Engine/Generic/IRule.cs | Changes the rule contract from GetSourceType() to a SourceType property. |
| Engine/Generic/ExternalRule.cs | Updates external rule implementation to SourceType property. |
| Engine/Generic/ConfigurableRule.cs | Replaces abstract GetSourceType() with abstract SourceType property. |
| Engine/Generic/AvoidParameterGeneric.cs | Replaces abstract GetSourceType() with abstract SourceType property. |
| Engine/Generic/AvoidCmdletGeneric.cs | Replaces abstract GetSourceType() with abstract SourceType property. |
| Engine/Commands/GetScriptAnalyzerRuleCommand.cs | Switches to reading rule.SourceType when producing RuleInfo. |
Comments suppressed due to low confidence (1)
Engine/Generic/RuleSourceType.cs:10
- Renaming the public enum
SourceTypetoRuleSourceType(and changing the rule contract fromGetSourceType()to aSourceTypeproperty) is a breaking API change for any external rule implementations or consumers compiled against previous versions. If maintaining compatibility is required, consider keepingSourceType(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.
| /// </summary> | ||
| /// <returns>The source type of the rule.</returns> | ||
| SourceType GetSourceType(); | ||
| public abstract RuleSourceType SourceType { get; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 common methods for the rules.
PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.