-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add CodeFix for LockObjectsMustBeReadonlyAnalyzer (PH2066) with configurable modifier ordering and comprehensive test coverage #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-948
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2405aaf
Initial plan
Copilot db91927
Implement CodeFixer for LockObjectsMustBeReadonlyAnalyzer
Copilot b49488c
Use Roslyn formatter to respect configured modifier order preferences
Copilot e004bc6
Merge branch 'main' into copilot/fix-948
bcollamore beedf60
test: Enhance LockObjectsMustBeReadonly test coverage to 80%+
Copilot 639fb5f
test: Add comprehensive test coverage for LockObjectsMustBeReadonlyCo…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
87 changes: 87 additions & 0 deletions
87
...ysis.MaintainabilityAnalyzers/Maintainability/LockObjectsMustBeReadonlyCodeFixProvider.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // © 2025 Koninklijke Philips N.V. See License.md in the project root for license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Philips.CodeAnalysis.Common; | ||
|
|
||
| namespace Philips.CodeAnalysis.MaintainabilityAnalyzers.Maintainability | ||
| { | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(LockObjectsMustBeReadonlyCodeFixProvider)), Shared] | ||
| public class LockObjectsMustBeReadonlyCodeFixProvider : SingleDiagnosticCodeFixProvider<IdentifierNameSyntax> | ||
| { | ||
| protected override string Title => "Add readonly modifier to field"; | ||
|
|
||
| protected override DiagnosticId DiagnosticId => DiagnosticId.LocksShouldBeReadonly; | ||
|
|
||
| protected override async Task<Document> ApplyFix(Document document, IdentifierNameSyntax node, ImmutableDictionary<string, string> properties, CancellationToken cancellationToken) | ||
| { | ||
| SyntaxNode rootNode = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
| SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| // Get the symbol referenced by the identifier | ||
| SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(node, cancellationToken); | ||
| if (symbolInfo.Symbol is not IFieldSymbol fieldSymbol) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| // Use the field symbol's declaration reference to find the actual syntax node | ||
| SyntaxReference declaringSyntaxReference = fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault(); | ||
| if (declaringSyntaxReference == null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| SyntaxNode declaringSyntax = await declaringSyntaxReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false); | ||
| FieldDeclarationSyntax fieldDeclaration = declaringSyntax.FirstAncestorOrSelf<FieldDeclarationSyntax>(); | ||
|
|
||
| if (fieldDeclaration == null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| // Add readonly modifier if not already present | ||
| SyntaxTokenList modifiers = fieldDeclaration.Modifiers; | ||
| if (!modifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword))) | ||
| { | ||
| SyntaxToken readonlyModifier = SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword).WithTrailingTrivia(SyntaxFactory.Space); | ||
|
|
||
| // Insert readonly after access modifiers but before static | ||
| SyntaxTokenList newModifiers = modifiers; | ||
| var insertIndex = 0; | ||
|
|
||
| // Find appropriate position for readonly modifier | ||
| for (var i = 0; i < modifiers.Count; i++) | ||
| { | ||
| if (modifiers[i].IsKind(SyntaxKind.PublicKeyword) || | ||
| modifiers[i].IsKind(SyntaxKind.PrivateKeyword) || | ||
| modifiers[i].IsKind(SyntaxKind.ProtectedKeyword) || | ||
| modifiers[i].IsKind(SyntaxKind.InternalKeyword)) | ||
| { | ||
| insertIndex = i + 1; | ||
| } | ||
| else if (modifiers[i].IsKind(SyntaxKind.StaticKeyword)) | ||
| { | ||
| insertIndex = i + 1; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| newModifiers = newModifiers.Insert(insertIndex, readonlyModifier); | ||
| FieldDeclarationSyntax newFieldDeclaration = fieldDeclaration.WithModifiers(newModifiers); | ||
|
|
||
| SyntaxNode newRoot = rootNode.ReplaceNode(fieldDeclaration, newFieldDeclaration); | ||
| return document.WithSyntaxRoot(newRoot); | ||
| } | ||
|
|
||
| return document; | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The position of the modifier, while generally standard practice, is governed by settings. Is it straightforward for the analyzer to respect the configured modifier order preference? (e.g., by having roslyn Format the modified line of code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the implementation to use Roslyn's formatting capabilities instead of hardcoded modifier positioning. The CodeFix now adds the
readonlymodifier and usesFormatter.Annotationto let Roslyn determine the correct position based on the configuredcsharp_preferred_modifier_orderpreference in.editorconfig. This ensures the CodeFix respects project-specific formatting rules. Commit: b49488c