|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Diagnostics; |
4 | 5 | using System.Threading; |
5 | 6 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
6 | 7 | using Microsoft.CodeAnalysis.Operations; |
7 | 8 | using Microsoft.CodeAnalysis; |
8 | 9 |
|
9 | 10 | namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration |
10 | 11 | { |
11 | | - internal sealed record BinderInvocation |
| 12 | + internal sealed record BinderInvocation(IInvocationOperation Operation, Location Location) |
12 | 13 | { |
13 | | - public IInvocationOperation Operation { get; private set; } |
14 | | - public Location? Location { get; private set; } |
15 | | - |
16 | 14 | public static BinderInvocation? Create(GeneratorSyntaxContext context, CancellationToken cancellationToken) |
17 | 15 | { |
18 | | - if (!IsCandidateInvocationExpressionSyntax(context.Node, out InvocationExpressionSyntax? invocationSyntax) || |
19 | | - context.SemanticModel.GetOperation(invocationSyntax, cancellationToken) is not IInvocationOperation operation || |
20 | | - !IsCandidateInvocation(operation)) |
21 | | - { |
22 | | - return null; |
23 | | - } |
| 16 | + Debug.Assert(IsCandidateSyntaxNode(context.Node)); |
| 17 | + InvocationExpressionSyntax invocationSyntax = (InvocationExpressionSyntax)context.Node; |
24 | 18 |
|
25 | | - return new BinderInvocation() |
26 | | - { |
27 | | - Operation = operation, |
28 | | - Location = invocationSyntax.GetLocation() |
29 | | - }; |
| 19 | + return context.SemanticModel.GetOperation(invocationSyntax, cancellationToken) is IInvocationOperation operation && |
| 20 | + IsBindingOperation(operation) |
| 21 | + ? new BinderInvocation(operation, invocationSyntax.GetLocation()) |
| 22 | + : null; |
30 | 23 | } |
31 | 24 |
|
32 | | - private static bool IsCandidateInvocationExpressionSyntax(SyntaxNode node, out InvocationExpressionSyntax? invocationSyntax) |
| 25 | + public static bool IsCandidateSyntaxNode(SyntaxNode node) |
33 | 26 | { |
34 | | - if (node is InvocationExpressionSyntax |
35 | | - { |
36 | | - Expression: MemberAccessExpressionSyntax |
37 | | - { |
38 | | - Name.Identifier.ValueText: string memberName |
39 | | - } |
40 | | - } syntax && IsCandidateBindingMethodName(memberName)) |
| 27 | + return node is InvocationExpressionSyntax |
41 | 28 | { |
42 | | - invocationSyntax = syntax; |
43 | | - return true; |
44 | | - } |
45 | | - |
46 | | - invocationSyntax = null; |
47 | | - return false; |
| 29 | + // TODO: drill further into this evaluation for a declaring-type name check. |
| 30 | + // https://github.com/dotnet/runtime/issues/90687. |
| 31 | + Expression: MemberAccessExpressionSyntax |
| 32 | + { |
| 33 | + Name.Identifier.ValueText: string memberName, |
| 34 | + } |
| 35 | + } && IsCandidateBindingMethodName(memberName); |
48 | 36 |
|
49 | 37 | static bool IsCandidateBindingMethodName(string name) => |
50 | 38 | IsCandidateMethodName_ConfigurationBinder(name) || |
51 | 39 | IsCandidateMethodName_OptionsBuilderConfigurationExtensions(name) || |
52 | 40 | IsValidMethodName_OptionsConfigurationServiceCollectionExtensions(name); |
53 | 41 | } |
54 | 42 |
|
55 | | - private static bool IsCandidateInvocation(IInvocationOperation operation) |
| 43 | + private static bool IsBindingOperation(IInvocationOperation operation) |
56 | 44 | { |
57 | 45 | if (operation.TargetMethod is not IMethodSymbol |
58 | 46 | { |
59 | 47 | IsExtensionMethod: true, |
60 | 48 | Name: string methodName, |
61 | | - ContainingType: ITypeSymbol |
| 49 | + ContainingType: INamedTypeSymbol |
62 | 50 | { |
63 | 51 | Name: string containingTypeName, |
64 | | - ContainingNamespace: INamespaceSymbol { } containingNamespace, |
65 | | - } containingType |
66 | | - } method || |
67 | | - containingNamespace.ToDisplayString() is not string containingNamespaceName) |
| 52 | + ContainingNamespace: INamespaceSymbol containingNamespace, |
| 53 | + } |
| 54 | + }) |
68 | 55 | { |
69 | 56 | return false; |
70 | 57 | } |
71 | 58 |
|
| 59 | + string containingNamespaceName = containingNamespace.ToDisplayString(); |
| 60 | + |
72 | 61 | return (containingTypeName) switch |
73 | 62 | { |
74 | 63 | "ConfigurationBinder" => |
|
0 commit comments