Skip to content

Resolve interceptor location using identifier name #50245

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

Merged
merged 2 commits into from
Aug 22, 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 @@ -142,14 +142,23 @@ public static int GetSignatureHashCode(Endpoint endpoint)

private static (string, int, int) GetLocation(IInvocationOperation operation)
{
var operationSpan = operation.Syntax.Span;
// The invocation expression consists of two properties:
// - Expression: which is a `MemberAccessExpressionSyntax` that represents the method being invoked.
// - ArgumentList: the list of arguments being invoked.
// Here, we resolve the `MemberAccessExpressionSyntax` to get the location of the method being invoked.
var memberAccessorExpression = ((MemberAccessExpressionSyntax)((InvocationExpressionSyntax)operation.Syntax).Expression);
// The `MemberAccessExpressionSyntax` in turn includes three properties:
// - Expression: the expression that is being accessed.
// - OperatorToken: the operator token, typically the dot separate.
// - Name: the name of the member being accessed, typically `MapGet` or `MapPost`, etc.
// Here, we resolve the `Name` to extract the location of the method being invoked.
var invocationNameSpan = memberAccessorExpression.Name.Span;
// Resolve LineSpan associated with the name span so we can resolve the line and character number.
var lineSpan = operation.Syntax.SyntaxTree.GetLineSpan(invocationNameSpan);
// Resolve the filepath of the invocation while accounting for source mapped paths.
var filePath = operation.Syntax.SyntaxTree.GetInterceptorFilePath(operation.SemanticModel?.Compilation.Options.SourceReferenceResolver);
var span = operation.Syntax.SyntaxTree.GetLineSpan(operationSpan);
var lineNumber = span.StartLinePosition.Line + 1;
// Calculate the character offset to the end of the Map invocation detected
var invocationLength = ((MemberAccessExpressionSyntax)((InvocationExpressionSyntax)operation.Syntax).Expression).Expression.Span.Length;
var characterNumber = span.StartLinePosition.Character + invocationLength + 2;
return (filePath, lineNumber, characterNumber);
// LineSpan.LinePosition is 0-indexed, but we want to display 1-indexed line and character numbers in the interceptor attribute.
return (filePath, lineSpan.StartLinePosition.Line + 1, lineSpan.StartLinePosition.Character + 1);
}

private static string GetHttpMethod(IInvocationOperation operation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using System.Globalization;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator;
Expand Down Expand Up @@ -96,6 +97,57 @@ public async Task SupportsDifferentInterceptorsFromSameLocation()
await VerifyAgainstBaselineUsingFile(updatedCompilation);
}

[Fact]
public async Task SupportsMapCallOnNewLine()
{
var source = """
app
.MapGet("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions
.MapGet(app, "/hello2/{id}", (int id) => $"Hello {id}!");
app.
MapGet("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions.
MapGet(app, "/hello2/{id}", (int id) => $"Hello {id}!");
app.
MapGet("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions.
MapGet(app, "/hello2/{id}", (int id) => $"Hello {id}!");
app.


MapGet("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions.


MapGet(app, "/hello2/{id}", (int id) => $"Hello {id}!");
app.
MapGet
("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions.
MapGet
(app, "/hello2/{id}", (int id) => $"Hello {id}!");
app
.
MapGet
("/hello1/{id}", (int id) => $"Hello {id}!");
EndpointRouteBuilderExtensions
.
MapGet
(app, "/hello2/{id}", (int id) => $"Hello {id}!");
""";
var (_, compilation) = await RunGeneratorAsync(source);
var endpoints = GetEndpointsFromCompilation(compilation);

for (int i = 0; i < endpoints.Length; i++)
{
var httpContext = CreateHttpContext();
httpContext.Request.RouteValues["id"] = i.ToString(CultureInfo.InvariantCulture);
await endpoints[i].RequestDelegate(httpContext);
await VerifyResponseBodyAsync(httpContext, $"Hello {i}!");
}
}

[Fact]
public async Task SourceMapsAllPathsInAttribute()
{
Expand Down