Skip to content

Commit

Permalink
Code fixer for MA0053 now supports records
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Nov 28, 2024
1 parent 1b0e98c commit 58436c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override FixAllProvider GetFixAllProvider()
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not ClassDeclarationSyntax nodeToFix)
if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not TypeDeclarationSyntax nodeToFix)
return;

var title = "Add sealed modifier";
Expand All @@ -42,7 +42,7 @@ private static async Task<Document> AddSealedModifier(Document document, SyntaxN
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

var classNode = (ClassDeclarationSyntax)nodeToFix;
var classNode = (TypeDeclarationSyntax)nodeToFix;
var modifiers = classNode.Modifiers.Add(SyntaxKind.SealedKeyword);
editor.ReplaceNode(classNode, classNode.WithModifiers(modifiers).WithAdditionalAnnotations(Formatter.Annotation));
return editor.GetChangedDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task Record()
{
await CreateProjectBuilder()
.AddAnalyzerConfiguration("MA0053.class_with_virtual_member_shoud_be_sealed", "true")
.WithSourceCode("""
internal record [||]Sample();
""")
.ShouldFixCodeWith("""
internal sealed record Sample();
""")
.ValidateAsync();
}

#if CSHARP10_OR_GREATER
[Fact]
public async Task TopLevelStatement_10()
Expand Down

0 comments on commit 58436c3

Please sign in to comment.