From 58436c35050d3775e3e8df2324662e9eaf6165f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Wed, 27 Nov 2024 20:31:38 -0500 Subject: [PATCH] Code fixer for MA0053 now supports records --- .../Rules/ClassMustBeSealedFixer.cs | 4 ++-- .../Rules/ClassMustBeSealedAnalyzerTests.cs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs index 5a62796e6..f6effc163 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs @@ -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"; @@ -42,7 +42,7 @@ private static async Task 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(); diff --git a/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs index ec63074b1..230c32db1 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs @@ -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()