|
| 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 Microsoft.CodeAnalysis.Simplification; |
| 11 | +using System; |
| 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 = "EP0009 CodeFix Provider")] |
| 25 | + public class EpiPartialRouterCodeFixProvider : CodeFixProvider |
| 26 | + { |
| 27 | + // The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including |
| 28 | + // the analyzer's ID in the code fix provider's FixableDiagnosticIds array. |
| 29 | + public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(EpiPartialRouterAnalyzer.DiagnosticId); |
| 30 | + private const string RoutingNamespace = "EPiServer.Core.Routing"; |
| 31 | + private const string RoutingPipelineNamespace = "EPiServer.Core.Routing.Pipeline"; |
| 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 methodDeclaration = root.FindNode(diagnosticSpan) as ClassDeclarationSyntax; |
| 49 | + if (methodDeclaration is null) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + context.RegisterCodeFix( |
| 55 | + CodeAction.Create( |
| 56 | + Resources.EpiPartialRouterTitle, |
| 57 | + c => ReplaceParametersAsync(context.Document, methodDeclaration, c), |
| 58 | + nameof(Resources.EpiPartialRouterTitle)), |
| 59 | + diagnostic); |
| 60 | + } |
| 61 | + |
| 62 | + private static async Task<Document> ReplaceParametersAsync(Document document, ClassDeclarationSyntax localDeclaration, CancellationToken cancellationToken) |
| 63 | + { |
| 64 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 65 | + |
| 66 | + var routePartialMethod = FindMethod(localDeclaration, "RoutePartial"); |
| 67 | + var parameters = routePartialMethod.ParameterList.Parameters; |
| 68 | + var obsoleteParameter = routePartialMethod.ParameterList.Parameters[1]; |
| 69 | + parameters = parameters.Remove(obsoleteParameter); |
| 70 | + parameters = parameters.Add(obsoleteParameter.WithType(SyntaxFactory.ParseTypeName("UrlResolverContext"))); |
| 71 | + var updatedMethod = routePartialMethod.WithParameterList(SyntaxFactory.ParameterList(parameters)); |
| 72 | + |
| 73 | + var newRoot = root.ReplaceNode(routePartialMethod, updatedMethod); |
| 74 | + var updatedDocument = document.WithSyntaxRoot(newRoot); |
| 75 | + localDeclaration = newRoot.FindNode(localDeclaration.Span) as ClassDeclarationSyntax; |
| 76 | + |
| 77 | + var virtualPathMethod = FindMethod(localDeclaration, "GetPartialVirtualPath"); |
| 78 | + parameters = virtualPathMethod.ParameterList.Parameters; |
| 79 | + parameters = parameters.RemoveAt(3); |
| 80 | + parameters = parameters.RemoveAt(2); |
| 81 | + parameters = parameters.RemoveAt(1); |
| 82 | + parameters = parameters.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier("urlGeneratorContext")).WithType(SyntaxFactory.ParseTypeName("UrlGeneratorContext"))); |
| 83 | + updatedMethod = virtualPathMethod.WithParameterList(SyntaxFactory.ParameterList(parameters)); |
| 84 | + |
| 85 | + newRoot = newRoot.ReplaceNode(virtualPathMethod, updatedMethod); |
| 86 | + updatedDocument = document.WithSyntaxRoot(newRoot); |
| 87 | + |
| 88 | + return await updatedDocument.AddUsingIfMissingAsync(cancellationToken, RoutingNamespace, RoutingPipelineNamespace); |
| 89 | + } |
| 90 | + |
| 91 | + private static MethodDeclarationSyntax FindMethod(ClassDeclarationSyntax classDeclaration, string methodName) |
| 92 | + { |
| 93 | + return classDeclaration.Members.OfType<MethodDeclarationSyntax>().FirstOrDefault(m => m.Identifier.Text == methodName); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments