|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Composition; |
| 6 | + |
| 7 | +using Analyzer.Utilities; |
| 8 | + |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.CodeActions; |
| 11 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 12 | +using Microsoft.CodeAnalysis.CSharp; |
| 13 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 14 | +using Microsoft.CodeAnalysis.Editing; |
| 15 | +using Microsoft.CodeAnalysis.Text; |
| 16 | + |
| 17 | +using MSTest.Analyzers.Helpers; |
| 18 | + |
| 19 | +namespace MSTest.Analyzers; |
| 20 | + |
| 21 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ClassInitializeShouldBeValidFixer))] |
| 22 | +[Shared] |
| 23 | +public sealed class PreferTestCleanupOverDisposeFixer : CodeFixProvider |
| 24 | +{ |
| 25 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } |
| 26 | + = ImmutableArray.Create(DiagnosticIds.PreferTestCleanupOverDisposeRuleId); |
| 27 | + |
| 28 | + public override FixAllProvider GetFixAllProvider() |
| 29 | + // See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers |
| 30 | + => WellKnownFixAllProviders.BatchFixer; |
| 31 | + |
| 32 | + public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 33 | + { |
| 34 | + SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 35 | + |
| 36 | + Diagnostic diagnostic = context.Diagnostics[0]; |
| 37 | + TextSpan diagnosticSpan = diagnostic.Location.SourceSpan; |
| 38 | + |
| 39 | + SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start); |
| 40 | + if (syntaxToken.Parent is null) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Find the method declaration identified by the diagnostic. |
| 46 | + MethodDeclarationSyntax methodDeclaration = syntaxToken.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().First(); |
| 47 | + |
| 48 | + context.RegisterCodeFix( |
| 49 | + CodeAction.Create( |
| 50 | + CodeFixResources.ReplaceWithTestCleanuFix, |
| 51 | + c => ReplaceDisposeWithTestCleanupAsync(context.Document, methodDeclaration, c), |
| 52 | + nameof(TestMethodShouldBeValidCodeFixProvider)), |
| 53 | + diagnostic); |
| 54 | + } |
| 55 | + |
| 56 | + private static async Task<Document> ReplaceDisposeWithTestCleanupAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken) |
| 57 | + { |
| 58 | + DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); |
| 59 | + |
| 60 | + if (methodDeclaration.Parent is not TypeDeclarationSyntax newParent) |
| 61 | + { |
| 62 | + return editor.OriginalDocument; |
| 63 | + } |
| 64 | + |
| 65 | + TypeDeclarationSyntax parentClass = newParent; |
| 66 | + |
| 67 | + // Create a new method with the same body but named "TestCleanup" |
| 68 | + MethodDeclarationSyntax newMethodDeclaration = methodDeclaration |
| 69 | + .WithIdentifier(SyntaxFactory.Identifier("TestCleanup")) |
| 70 | + .WithAttributeLists(SyntaxFactory.SingletonList(CreateTestCleanupAttribute())); |
| 71 | + |
| 72 | + newParent = newParent.ReplaceNode(methodDeclaration, newMethodDeclaration); |
| 73 | + |
| 74 | + var newBaseTypes = newParent.BaseList?.Types |
| 75 | + .Where(type => !IsDisposableInterface(type)) |
| 76 | + .ToList(); |
| 77 | + |
| 78 | + if (newBaseTypes?.Count != 0) |
| 79 | + { |
| 80 | + // If other interfaces remain, replace the base list with updated interfaces |
| 81 | + newParent = newParent.WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList(newBaseTypes))); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + // If no interfaces left, remove the base list entirely |
| 86 | + newParent = newParent.WithBaseList(null); |
| 87 | + } |
| 88 | + |
| 89 | + editor.ReplaceNode(parentClass, newParent); |
| 90 | + |
| 91 | + return editor.GetChangedDocument(); |
| 92 | + } |
| 93 | + |
| 94 | + private static bool IsDisposableInterface(BaseTypeSyntax baseTypeSyntax) |
| 95 | + { |
| 96 | + string typeName = baseTypeSyntax.Type.ToString(); |
| 97 | + return typeName is "IDisposable" or "IAsyncDisposable"; |
| 98 | + } |
| 99 | + |
| 100 | + private static AttributeListSyntax CreateTestCleanupAttribute() => |
| 101 | + // [TestCleanup] attribute |
| 102 | + SyntaxFactory.AttributeList( |
| 103 | + SyntaxFactory.SingletonSeparatedList( |
| 104 | + SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("TestCleanup")))); |
| 105 | +} |
0 commit comments