|
| 1 | +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Composition; |
| 5 | +using Analyzer.Utilities; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CodeActions; |
| 8 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 9 | +using Microsoft.CodeAnalysis.CSharp; |
| 10 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | +using Microsoft.NetCore.Analyzers; |
| 12 | +using Microsoft.NetCore.Analyzers.Performance; |
| 13 | + |
| 14 | +namespace Microsoft.NetCore.CSharp.Analyzers.Performance |
| 15 | +{ |
| 16 | + [ExportCodeFixProvider(LanguageNames.CSharp), Shared] |
| 17 | + public sealed class CSharpCollapseMultiplePathOperationsFixer : CodeFixProvider |
| 18 | + { |
| 19 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(CollapseMultiplePathOperationsAnalyzer.RuleId); |
| 20 | + |
| 21 | + public override FixAllProvider GetFixAllProvider() |
| 22 | + => WellKnownFixAllProviders.BatchFixer; |
| 23 | + |
| 24 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 25 | + { |
| 26 | + var document = context.Document; |
| 27 | + var diagnostic = context.Diagnostics[0]; |
| 28 | + var root = await document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 29 | + var node = root.FindNode(context.Span, getInnermostNodeForTie: true); |
| 30 | + |
| 31 | + if (node is not InvocationExpressionSyntax invocation || |
| 32 | + await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false) is not { } semanticModel || |
| 33 | + semanticModel.Compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemIOPath) is not { } pathType) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Get the method name from diagnostic properties |
| 39 | + if (!diagnostic.Properties.TryGetValue(CollapseMultiplePathOperationsAnalyzer.MethodNameKey, out var methodName)) |
| 40 | + { |
| 41 | + methodName = "Path"; |
| 42 | + } |
| 43 | + |
| 44 | + context.RegisterCodeFix( |
| 45 | + CodeAction.Create( |
| 46 | + string.Format(MicrosoftNetCoreAnalyzersResources.CollapseMultiplePathOperationsCodeFixTitle, methodName), |
| 47 | + createChangedDocument: cancellationToken => CollapsePathOperationAsync(document, root, invocation, pathType, semanticModel, cancellationToken), |
| 48 | + equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.CollapseMultiplePathOperationsCodeFixTitle)), |
| 49 | + diagnostic); |
| 50 | + } |
| 51 | + |
| 52 | + private static Task<Document> CollapsePathOperationAsync(Document document, SyntaxNode root, InvocationExpressionSyntax invocation, INamedTypeSymbol pathType, SemanticModel semanticModel, CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + // Collect all arguments by recursively unwrapping nested Path.Combine/Join calls |
| 55 | + var allArguments = CollectAllArguments(invocation, pathType, semanticModel); |
| 56 | + |
| 57 | + // Create new argument list with all collected arguments |
| 58 | + var newArgumentList = SyntaxFactory.ArgumentList( |
| 59 | + SyntaxFactory.SeparatedList(allArguments)); |
| 60 | + |
| 61 | + // Create the new invocation with all arguments |
| 62 | + var newInvocation = invocation.WithArgumentList(newArgumentList) |
| 63 | + .WithTriviaFrom(invocation); |
| 64 | + |
| 65 | + var newRoot = root.ReplaceNode(invocation, newInvocation); |
| 66 | + |
| 67 | + return Task.FromResult(document.WithSyntaxRoot(newRoot)); |
| 68 | + } |
| 69 | + |
| 70 | + private static ArgumentSyntax[] CollectAllArguments(InvocationExpressionSyntax invocation, INamedTypeSymbol pathType, SemanticModel semanticModel) |
| 71 | + { |
| 72 | + var arguments = ImmutableArray.CreateBuilder<ArgumentSyntax>(); |
| 73 | + |
| 74 | + foreach (var argument in invocation.ArgumentList.Arguments) |
| 75 | + { |
| 76 | + if (argument.Expression is InvocationExpressionSyntax nestedInvocation && |
| 77 | + IsPathCombineOrJoin(nestedInvocation, pathType, semanticModel, out var methodName) && |
| 78 | + IsPathCombineOrJoin(invocation, pathType, semanticModel, out var outerMethodName) && |
| 79 | + methodName == outerMethodName) |
| 80 | + { |
| 81 | + // Recursively collect arguments from nested invocation |
| 82 | + arguments.AddRange(CollectAllArguments(nestedInvocation, pathType, semanticModel)); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + arguments.Add(argument); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return arguments.ToArray(); |
| 91 | + } |
| 92 | + |
| 93 | + private static bool IsPathCombineOrJoin(InvocationExpressionSyntax invocation, INamedTypeSymbol pathType, SemanticModel semanticModel, out string methodName) |
| 94 | + { |
| 95 | + if (semanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol methodSymbol && |
| 96 | + SymbolEqualityComparer.Default.Equals(methodSymbol.ContainingType, pathType) && |
| 97 | + methodSymbol.Name is "Combine" or "Join") |
| 98 | + { |
| 99 | + methodName = methodSymbol.Name; |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + methodName = string.Empty; |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments