• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ImmediatePlatform / Immediate.Handlers / 41874380682

04 Nov 2026 05:07PM UTC coverage: 95.813% (+0.03%) from 95.781%
41874380682

push

github

web-flow
Add IHR0012 code fix for missing CancellationToken parameters (#407)

* Add IHR0012 cancellation token code fix

* Handle cancellation token code fix edge cases from review

* Preserve direct handler calls when adding cancellation tokens

* Revert corner cases I don't want to care about

---------

Co-authored-by: Codex <codex@openai.com>

35 of 36 new or added lines in 1 file covered. (97.22%)

1556 of 1624 relevant lines covered (95.81%)

2.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/Immediate.Handlers.CodeFixes/HandlerShouldUseCancellationTokenCodeFixProvider.cs
1
using System.Collections.Immutable;
2
using System.Globalization;
3
using Immediate.Handlers.Analyzers;
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.Formatting;
10
using Microsoft.CodeAnalysis.Simplification;
11
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
12

13
namespace Immediate.Handlers.CodeFixes;
14

15
[ExportCodeFixProvider(LanguageNames.CSharp)]
16
public sealed class HandlerShouldUseCancellationTokenCodeFixProvider : CodeFixProvider
17
{
18
        public override ImmutableArray<string> FixableDiagnosticIds { get; } =
19
                ImmutableArray.Create([DiagnosticIds.IHR0012HandlerShouldUseCancellationToken]);
3 ✔
20

21
        public override FixAllProvider? GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
3 ✔
22

23
        /// <summary>Registers a fix when a cancellation token can be appended to the diagnosed handler.</summary>
24
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
25
        {
26
                var (document, span, diagnostics, token) = context;
3 ✔
27
                token.ThrowIfCancellationRequested();
3 ✔
28

29
                var root = await document.GetRequiredSyntaxRootAsync(token);
3 ✔
30
                if (root.FindNode(span) is not MethodDeclarationSyntax method
3 ✔
31
                        || method.ParameterList.Parameters.Any(p => p.Modifiers.Any(SyntaxKind.ParamsKeyword)))
3 ✔
32
                {
NEW
33
                        return;
×
34
                }
35

36
                context.RegisterCodeFix(
3 ✔
37
                        CodeAction.Create(
3 ✔
38
                                title: "Add CancellationToken parameter",
3 ✔
39
                                createChangedDocument: cancellationToken => AddCancellationTokenAsync(document, root, method, cancellationToken),
3 ✔
40
                                equivalenceKey: nameof(HandlerShouldUseCancellationTokenCodeFixProvider)
3 ✔
41
                        ),
3 ✔
42
                        diagnostics[0]
3 ✔
43
                );
3 ✔
44
        }
3 ✔
45

46
        /// <summary>Appends an optional token without conflicting with existing names or breaking existing calls.</summary>
47
        private static Task<Document> AddCancellationTokenAsync(
48
                Document document,
49
                SyntaxNode root,
50
                MethodDeclarationSyntax method,
51
                CancellationToken cancellationToken
52
        )
53
        {
54
                cancellationToken.ThrowIfCancellationRequested();
3 ✔
55

56
                var parameter = Parameter(Identifier(GetParameterName(method)))
3 ✔
57
                        .WithType(
3 ✔
58
                                ParseTypeName("global::System.Threading.CancellationToken")
3 ✔
59
                                        .WithAdditionalAnnotations(Simplifier.Annotation)
3 ✔
60
                        )
3 ✔
61
                        .WithAdditionalAnnotations(Formatter.Annotation);
3 ✔
62

63
                var newMethod = method.AddParameterListParameters(parameter);
3 ✔
64
                return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(method, newMethod)));
3 ✔
65
        }
66

67
        /// <summary>Chooses a name that cannot shadow identifiers or conflict with declarations in the method.</summary>
68
        private static string GetParameterName(MethodDeclarationSyntax method)
69
        {
70
                var names = new HashSet<string>(
3 ✔
71
                        method.DescendantTokens()
3 ✔
72
                                .Where(static token => token.IsKind(SyntaxKind.IdentifierToken))
3 ✔
73
                                .Select(static token => token.ValueText),
3 ✔
74
                        StringComparer.Ordinal
3 ✔
75
                );
3 ✔
76

77
                var parameterName = "cancellationToken";
3 ✔
78
                for (var suffix = 1; names.Contains(parameterName); suffix++)
3 ✔
79
                        parameterName = "cancellationToken" + suffix.ToString(CultureInfo.InvariantCulture);
3 ✔
80

81
                return parameterName;
3 ✔
82
        }
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc