|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Composition; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Globalization; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.CodeActions; |
| 10 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 11 | +using Microsoft.CodeAnalysis.CSharp; |
| 12 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | +using Microsoft.CodeAnalysis.Operations; |
| 14 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 15 | + |
| 16 | +namespace Microsoft.DurableTask.Analyzers.Orchestration; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Code fix provider for the <see cref="DelayOrchestrationAnalyzer"/>. |
| 20 | +/// </summary> |
| 21 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DelayOrchestrationFixer))] |
| 22 | +[Shared] |
| 23 | +public sealed class DelayOrchestrationFixer : OrchestrationContextFixer |
| 24 | +{ |
| 25 | + /// <inheritdoc/> |
| 26 | + public override ImmutableArray<string> FixableDiagnosticIds => [DelayOrchestrationAnalyzer.DiagnosticId]; |
| 27 | + |
| 28 | + /// <inheritdoc/> |
| 29 | + protected override void RegisterCodeFixes(CodeFixContext context, OrchestrationCodeFixContext orchestrationContext) |
| 30 | + { |
| 31 | + if (orchestrationContext.SyntaxNodeWithDiagnostic is not InvocationExpressionSyntax invocationExpressionsSyntax) |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (orchestrationContext.SemanticModel.GetOperation(invocationExpressionsSyntax) is not IInvocationOperation invocationOperation) |
| 37 | + { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Only fix Task.Delay(int[,CancellationToken]) or Task.Delay(TimeSpan[,CancellationToken]) invocations. |
| 42 | + // For now, fixing Thread.Sleep(int) is not supported |
| 43 | + if (!SymbolEqualityComparer.Default.Equals(invocationOperation.Type, orchestrationContext.KnownTypeSymbols.Task)) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + Compilation compilation = orchestrationContext.SemanticModel.Compilation; |
| 49 | + INamedTypeSymbol int32 = compilation.GetSpecialType(SpecialType.System_Int32); |
| 50 | + |
| 51 | + // Extracts the arguments from the Task.Delay invocation |
| 52 | + IMethodSymbol taskDelaySymbol = invocationOperation.TargetMethod; |
| 53 | + Debug.Assert(taskDelaySymbol.Parameters.Length >= 1, "Task.Delay should have at least one parameter"); |
| 54 | + bool isInt = SymbolEqualityComparer.Default.Equals(taskDelaySymbol.Parameters[0].Type, int32); |
| 55 | + IArgumentOperation delayArgumentOperation = invocationOperation.Arguments[0]; |
| 56 | + IArgumentOperation? cancellationTokenArgumentOperation = invocationOperation.Arguments.Length == 2 ? invocationOperation.Arguments[1] : null; |
| 57 | + |
| 58 | + // Gets the name of the TaskOrchestrationContext parameter (e.g. "context" or "ctx") |
| 59 | + string contextParameterName = orchestrationContext.TaskOrchestrationContextSymbol.Name; |
| 60 | + string recommendation = $"{contextParameterName}.CreateTimer"; |
| 61 | + |
| 62 | + // e.g: "Use 'context.CreateTimer' instead of 'Task.Delay'" |
| 63 | + string title = string.Format( |
| 64 | + CultureInfo.InvariantCulture, |
| 65 | + Resources.UseInsteadFixerTitle, |
| 66 | + recommendation, |
| 67 | + "Task.Delay"); |
| 68 | + |
| 69 | + context.RegisterCodeFix( |
| 70 | + CodeAction.Create( |
| 71 | + title: title, |
| 72 | + createChangedDocument: c => ReplaceTaskDelay( |
| 73 | + context.Document, orchestrationContext.Root, invocationExpressionsSyntax, contextParameterName, delayArgumentOperation, cancellationTokenArgumentOperation, isInt), |
| 74 | + equivalenceKey: title), // This key is used to prevent duplicate code fixes. |
| 75 | + context.Diagnostics); |
| 76 | + } |
| 77 | + |
| 78 | + static Task<Document> ReplaceTaskDelay( |
| 79 | + Document document, |
| 80 | + SyntaxNode oldRoot, |
| 81 | + InvocationExpressionSyntax incorrectTaskDelaySyntax, |
| 82 | + string contextParameterName, |
| 83 | + IArgumentOperation delayArgumentOperation, |
| 84 | + IArgumentOperation? cancellationTokenArgumentOperation, |
| 85 | + bool isInt) |
| 86 | + { |
| 87 | + if (delayArgumentOperation.Syntax is not ArgumentSyntax timeSpanOrIntArgumentSyntax) |
| 88 | + { |
| 89 | + return Task.FromResult(document); |
| 90 | + } |
| 91 | + |
| 92 | + // Either use the original TimeSpan argument, or in case it is an int, transform it into TimeSpan |
| 93 | + ArgumentSyntax timeSpanArgumentSyntax; |
| 94 | + if (isInt) |
| 95 | + { |
| 96 | + timeSpanArgumentSyntax = |
| 97 | + Argument( |
| 98 | + InvocationExpression( |
| 99 | + MemberAccessExpression( |
| 100 | + SyntaxKind.SimpleMemberAccessExpression, |
| 101 | + IdentifierName("TimeSpan"), |
| 102 | + IdentifierName("FromMilliseconds")), |
| 103 | + ArgumentList( |
| 104 | + SeparatedList(new[] { timeSpanOrIntArgumentSyntax })))); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + timeSpanArgumentSyntax = timeSpanOrIntArgumentSyntax; |
| 109 | + } |
| 110 | + |
| 111 | + // Either gets the original cancellation token argument or create a 'CancellationToken.None' |
| 112 | + ArgumentSyntax cancellationTokenArgumentSyntax = cancellationTokenArgumentOperation?.Syntax as ArgumentSyntax ?? |
| 113 | + Argument( |
| 114 | + MemberAccessExpression( |
| 115 | + SyntaxKind.SimpleMemberAccessExpression, |
| 116 | + IdentifierName("CancellationToken"), |
| 117 | + IdentifierName("None"))); |
| 118 | + |
| 119 | + // Builds a 'context.CreateTimer(TimeSpan.FromMilliseconds(1000), CancellationToken.None)' syntax node |
| 120 | + InvocationExpressionSyntax correctTimerSyntax = |
| 121 | + InvocationExpression( |
| 122 | + MemberAccessExpression( |
| 123 | + SyntaxKind.SimpleMemberAccessExpression, |
| 124 | + IdentifierName(contextParameterName), |
| 125 | + IdentifierName("CreateTimer")), |
| 126 | + ArgumentList( |
| 127 | + SeparatedList(new[] |
| 128 | + { |
| 129 | + timeSpanArgumentSyntax, |
| 130 | + cancellationTokenArgumentSyntax, |
| 131 | + }))); |
| 132 | + |
| 133 | + // Replaces the old local declaration with the new local declaration. |
| 134 | + SyntaxNode newRoot = oldRoot.ReplaceNode(incorrectTaskDelaySyntax, correctTimerSyntax); |
| 135 | + Document newDocument = document.WithSyntaxRoot(newRoot); |
| 136 | + |
| 137 | + return Task.FromResult(newDocument); |
| 138 | + } |
| 139 | +} |
0 commit comments