Skip to content
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
19 changes: 15 additions & 4 deletions src/AutoLoggerMessageGenerator/Mappers/CallLocationMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal static class CallLocationMapper
{
public static CallLocation? Map(SemanticModel semanticModel, InvocationExpressionSyntax invocationExpression)
{
var memberAccessExpression = invocationExpression.Expression as MemberAccessExpressionSyntax;
if (memberAccessExpression?.Expression is not IdentifierNameSyntax identifierName)
var identifierName = GetIdentifierName(invocationExpression);
if (identifierName is null)
return null;

var skipSymbols = identifierName.Identifier.ValueText.Length + 1; // obj accessor + dot symbol
Expand All @@ -37,7 +37,18 @@ internal static class CallLocationMapper
return new CallLocation(filePath, line, character, interceptableLocation, location);
}

#if PATH_BASED_INTERCEPTORS
private static SimpleNameSyntax? GetIdentifierName(InvocationExpressionSyntax invocationExpression)
{
var memberAccessExpression = invocationExpression.Expression as MemberAccessExpressionSyntax;
return memberAccessExpression?.Expression switch
{
IdentifierNameSyntax i => i,
MemberAccessExpressionSyntax ma => ma.Name,
_ => null
};
}

#if PATH_BASED_INTERCEPTORS
private static string GeneratePathBasedInterceptableLocation(string filePath, int line, int character)
{
return $"[{Constants.InterceptorNamespace}.{Constants.InterceptorAttributeName}(" +
Expand All @@ -46,5 +57,5 @@ private static string GeneratePathBasedInterceptableLocation(string filePath, in
$"character: {character}" +
$")]";
}
#endif
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,49 @@ class Foo
await Assert.That(className).IsEqualTo("Foo");
await Assert.That(methodName).IsEqualTo(string.Empty);
}

[Test]
public async Task Extract_WithMemberAccessModifier_ShouldReturnExpectedNamespaceAndClassName()
{
const string additionalDeclaration = """
class Foo
{
private ILogger logger;
private void Bar() => this.logger.LogInformation("Hello world");
}
""";
var (_, syntaxTree) = await CompileSourceCode(string.Empty, additionalDeclaration);
var (invocationExpression, _, _) = FindMethodInvocation(null, syntaxTree);

var (ns, className, methodName) = LocationContextExtractor.Extract(invocationExpression);

await Assert.That(ns).IsEqualTo(Namespace);
await Assert.That(className).IsEqualTo("Foo");
await Assert.That(methodName).IsEqualTo("Bar");
}

[Test]
public async Task Extract_WithNestedMemberAccessModifier_ShouldReturnExpectedNamespaceAndClassName()
{
const string additionalDeclaration = """
class Outer
{
private readonly Foo foo;
class Foo
{
public ILogger logger;
}

private void Bar() => this.foo.logger.LogInformation("Hello world");
}
""";
var (_, syntaxTree) = await CompileSourceCode(string.Empty, additionalDeclaration);
var (invocationExpression, _, _) = FindMethodInvocation(null, syntaxTree);

var (ns, className, methodName) = LocationContextExtractor.Extract(invocationExpression);

await Assert.That(ns).IsEqualTo(Namespace);
await Assert.That(className).IsEqualTo("Outer");
await Assert.That(methodName).IsEqualTo("Bar");
}
}
Loading