|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CodeActions; |
| 6 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | +using Microsoft.CodeAnalysis.Editing; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Collections.Immutable; |
| 13 | +using System.Linq; |
| 14 | +using System.Threading; |
| 15 | +using System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace Epi.Source.Updater |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// As with the analzyers, code fix providers that are registered into Upgrade Assistant's |
| 21 | + /// dependency injection container (by IExtensionServiceProvider) will be used during |
| 22 | + /// the source update step. |
| 23 | + /// </summary> |
| 24 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = "EP00010 CodeFix Provider")] |
| 25 | + public class EpiHttpContextBaseAccessorCodeFixProvider : CodeFixProvider |
| 26 | + { |
| 27 | + |
| 28 | + // The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including |
| 29 | + // the analyzer's ID in the code fix provider's FixableDiagnosticIds array. |
| 30 | + public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(EpiHttpContextBaseAccessorAnalyzer.DiagnosticId); |
| 31 | + private const string MicrosoftAspNetCoreHttpNamespace = "Microsoft.AspNetCore.Http"; |
| 32 | + |
| 33 | + public sealed override FixAllProvider GetFixAllProvider() => |
| 34 | + // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers |
| 35 | + WellKnownFixAllProviders.BatchFixer; |
| 36 | + |
| 37 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 38 | + { |
| 39 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 40 | + |
| 41 | + if (root is null) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + var diagnostic = context.Diagnostics.First(); |
| 47 | + var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 48 | + var classDeclarationSyntax = root.FindNode(diagnosticSpan) as ClassDeclarationSyntax; |
| 49 | + if (classDeclarationSyntax is null) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + context.RegisterCodeFix( |
| 55 | + CodeAction.Create( |
| 56 | + Resources.EpiHttpContextBaseTitle, |
| 57 | + c => ReplaceParameterTypeAsync(context.Document, classDeclarationSyntax, c), |
| 58 | + nameof(Resources.EpiHttpContextBaseTitle)), |
| 59 | + diagnostic); |
| 60 | + } |
| 61 | + |
| 62 | + private static async Task<Document> ReplaceParameterTypeAsync(Document document, ClassDeclarationSyntax classDeclarationSyntax, CancellationToken cancellationToken) |
| 63 | + { |
| 64 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 65 | + var updatedClassDeclaration = UpdateMemberField(classDeclarationSyntax); |
| 66 | + var updatedClassDeclarationByCostr = UpdateConstructor(updatedClassDeclaration); |
| 67 | + var newRoot = root!.ReplaceNode(classDeclarationSyntax, updatedClassDeclarationByCostr); |
| 68 | + |
| 69 | + // Return document with transformed tree. |
| 70 | + var updatedDocument = document.WithSyntaxRoot(newRoot); |
| 71 | + return await updatedDocument.AddUsingIfMissingAsync(cancellationToken, MicrosoftAspNetCoreHttpNamespace); |
| 72 | + } |
| 73 | + |
| 74 | + private static ClassDeclarationSyntax UpdateMemberField(ClassDeclarationSyntax classDecSyntax) |
| 75 | + { |
| 76 | + var updatedMembers = new List<MemberDeclarationSyntax>(); |
| 77 | + foreach (var m in classDecSyntax.Members) |
| 78 | + { |
| 79 | + if (m is FieldDeclarationSyntax field && field.Declaration.Type.ToString().Equals(EpiHttpContextBaseAccessorAnalyzer.HttpContextBaseParameterType, StringComparison.OrdinalIgnoreCase)) |
| 80 | + { |
| 81 | + var svcAccessorHttpContextBaseMember = m as FieldDeclarationSyntax; |
| 82 | + var variableDeclaration = svcAccessorHttpContextBaseMember.Declaration.WithType(SyntaxFactory.ParseTypeName("IHttpContextAccessor").WithTrailingTrivia(SyntaxFactory.Whitespace(" "))).WithVariables(svcAccessorHttpContextBaseMember.Declaration.Variables); |
| 83 | + var iHttpContextBaseMember = SyntaxFactory.FieldDeclaration(svcAccessorHttpContextBaseMember.AttributeLists, (m as FieldDeclarationSyntax).Modifiers, variableDeclaration); |
| 84 | + updatedMembers.Add(iHttpContextBaseMember); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + updatedMembers.Add(m); |
| 89 | + } |
| 90 | + } |
| 91 | + return classDecSyntax.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>().AddRange(updatedMembers)); |
| 92 | + } |
| 93 | + |
| 94 | + private static ClassDeclarationSyntax UpdateConstructor(ClassDeclarationSyntax classDeclarationSyntax) |
| 95 | + { |
| 96 | + ClassDeclarationSyntax updatedClassDeclaration = classDeclarationSyntax; |
| 97 | + foreach (var constructorSyntax in classDeclarationSyntax.Members.Where(m => m is ConstructorDeclarationSyntax)) |
| 98 | + { |
| 99 | + var parameters = new List<ParameterSyntax>(); |
| 100 | + |
| 101 | + foreach (var parameter in (constructorSyntax as ConstructorDeclarationSyntax).ParameterList.Parameters) |
| 102 | + { |
| 103 | + if (parameter.Type != null && parameter.Type.ToString().Equals(EpiHttpContextBaseAccessorAnalyzer.HttpContextBaseParameterType, StringComparison.OrdinalIgnoreCase)) |
| 104 | + { |
| 105 | + var updatedParameter = parameter.WithType(SyntaxFactory.ParseTypeName("IHttpContextAccessor")).WithTrailingTrivia(); |
| 106 | + parameters.Add(updatedParameter); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + parameters.Add(parameter); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + var updateConstructor = (constructorSyntax as ConstructorDeclarationSyntax).WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameters))); |
| 115 | + updatedClassDeclaration = updatedClassDeclaration.ReplaceNode(constructorSyntax, updateConstructor); |
| 116 | + } |
| 117 | + return updatedClassDeclaration; |
| 118 | + } |
| 119 | + |
| 120 | + private static CompilationUnitSyntax AddUsing(CompilationUnitSyntax compilationRoot, string namespaceToAdd) |
| 121 | + { |
| 122 | + var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(namespaceToAdd).WithLeadingTrivia(SyntaxFactory.Whitespace(" "))) |
| 123 | + .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); |
| 124 | + return compilationRoot.AddUsings(usingDirective); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments