Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CancellationTokenAnalyzer fluent style API bug #6751

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,96 @@ public Task Bar(IMessageHandlerContext context)
return Assert(original, expected);
}

[Test]
public Task Fluent()
{
var original =
@"using NServiceBus;
using System;
using System.Threading;
using System.Threading.Tasks;
public class Foo
{
public Task Bar(IMessageHandlerContext context)
{
var someReturnValue = ""someValue"";
return
Include(() => someReturnValue)
.Include(() => someReturnValue)
.FindSingle();
}

Foo Include(Func<string> action) => this;

Task FindSingle(CancellationToken token = default(CancellationToken)) { return Task.CompletedTask; }
}";

var expected =
@"using NServiceBus;
using System;
using System.Threading;
using System.Threading.Tasks;
public class Foo
{
public Task Bar(IMessageHandlerContext context)
{
var someReturnValue = ""someValue"";
return
Include(() => someReturnValue)
.Include(() => someReturnValue)
.FindSingle(context.CancellationToken);
}

Foo Include(Func<string> action) => this;

Task FindSingle(CancellationToken token = default(CancellationToken)) { return Task.CompletedTask; }
}";

return Assert(original, expected);
}

[Test]
public Task FluentAsync()
{
var original =
@"using NServiceBus;
using System;
using System.Threading;
using System.Threading.Tasks;
public class Foo
{
public async Task Bar(IMessageHandlerContext context)
{
var someReturnValue = ""someValue"";
await (await (await Include(() => someReturnValue)).Include(() => someReturnValue)).FindSingle();
}

Task<Foo> Include(Func<string> action, CancellationToken token = default(CancellationToken)) => Task.FromResult(this);

Task<Foo> FindSingle(CancellationToken token = default(CancellationToken)) => Task.FromResult(this);
}";

var expected =
@"using NServiceBus;
using System;
using System.Threading;
using System.Threading.Tasks;
public class Foo
{
public async Task Bar(IMessageHandlerContext context)
{
var someReturnValue = ""someValue"";
await (await (await Include(() => someReturnValue, context.CancellationToken)).Include(() => someReturnValue, context.CancellationToken)).FindSingle(context.CancellationToken);
}

Task<Foo> Include(Func<string> action, CancellationToken token = default(CancellationToken)) => Task.FromResult(this);

Task<Foo> FindSingle(CancellationToken token = default(CancellationToken)) => Task.FromResult(this);
}";

return Assert(original, expected);
}

[Test]
public Task NonStandardContextVariableName()
{
Expand Down Expand Up @@ -245,4 +335,4 @@ public class ForwardCancellationTokenFixerTestsCSharp10 : ForwardCancellationTok
protected override LanguageVersion AnalyzerLanguageVersion => LanguageVersion.CSharp10;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var requiredArgName = diagnostic.Properties["RequiredArgName"];

var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var startToken = root.FindToken(diagnostic.Location.SourceSpan.Start);
var invocationSyntax = startToken.Parent.AncestorsAndSelf().OfType<InvocationExpressionSyntax>().First();
var startToken = root.FindNode(diagnostic.Location.SourceSpan);
var invocationSyntax = (InvocationExpressionSyntax)startToken;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the previous implementation always assumed there is an InvocationExpressionSyntax found, I left this assumption intact by doing a cast.


// TODO: consider whether analyzer should pass in name of property, i.e. "CancellationToken",
// so that that information doesn't have to spread across both the analyzer and the fixer
Expand Down Expand Up @@ -89,4 +89,4 @@ static async Task<Document> ForwardCancellationToken(
// the title is dynamic based on the context name and target method name.
static readonly string EquivalenceKey = typeof(ForwardCancellationTokenFixer).FullName;
}
}
}