|
1 | 1 | namespace ReflectionAnalyzers |
2 | 2 | { |
3 | | - using System.Diagnostics.CodeAnalysis; |
4 | 3 | using System.Threading; |
5 | 4 |
|
6 | 5 | using Gu.Roslyn.AnalyzerExtensions; |
|
14 | 13 | /// </summary> |
15 | 14 | internal static class GetX |
16 | 15 | { |
17 | | - internal static bool TryGetNestedType(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken, [NotNullWhen(true)] out INamedTypeSymbol? type) |
18 | | - { |
19 | | - if (expression is MemberAccessExpressionSyntax memberAccess && |
20 | | - TryFindInvocation(memberAccess, KnownSymbol.Type.GetNestedType, semanticModel, cancellationToken, out var invocation) && |
21 | | - TryMatchGetNestedType(invocation, semanticModel, cancellationToken, out var nestedType, out _, out _) && |
22 | | - nestedType is { Match: FilterMatch.Single, Symbol: INamedTypeSymbol namedNested }) |
23 | | - { |
24 | | - type = namedNested; |
25 | | - return true; |
26 | | - } |
27 | | - |
28 | | - type = null; |
29 | | - return false; |
30 | | - } |
31 | | - |
32 | | - /// <summary> |
33 | | - /// Check if <paramref name="invocation"/> is a call to Type.GetMethod. |
34 | | - /// </summary> |
35 | | - internal static bool TryMatchGetConstructor(InvocationExpressionSyntax invocation, SemanticModel semanticModel, CancellationToken cancellationToken, out ReflectedMember member, out Flags flags, out Types types) |
36 | | - { |
37 | | - if (invocation.TryGetTarget(KnownSymbol.Type.GetConstructor, semanticModel, cancellationToken, out var getX)) |
38 | | - { |
39 | | - if (ReflectedMember.TryGetType(invocation, semanticModel, cancellationToken, out var type, out var typeSource) && |
40 | | - IsKnownSignature(invocation, getX) && |
41 | | - Flags.TryCreate(invocation, getX, semanticModel, cancellationToken, out flags) && |
42 | | - Types.TryCreate(invocation, getX, semanticModel, cancellationToken, out types)) |
43 | | - { |
44 | | - return ReflectedMember.TryCreate(getX, invocation, type, typeSource, Name.Ctor, flags.Effective, types, semanticModel.Compilation, out member); |
45 | | - } |
46 | | - |
47 | | - if (Flags.TryCreate(invocation, getX, semanticModel, cancellationToken, out flags) && |
48 | | - flags.AreInSufficient) |
49 | | - { |
50 | | - member = new ReflectedMember(type, typeSource, null, getX, invocation, FilterMatch.InSufficientFlags); |
51 | | - _ = Types.TryCreate(invocation, getX, semanticModel, cancellationToken, out types); |
52 | | - return true; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - member = default; |
57 | | - flags = default; |
58 | | - types = default; |
59 | | - return false; |
60 | | - } |
61 | | - |
62 | 16 | /// <summary> |
63 | 17 | /// Check if <paramref name="invocation"/> is a call to Type.GetEvent. |
64 | 18 | /// </summary> |
@@ -116,30 +70,6 @@ internal static bool TryMatchGetProperty(InvocationExpressionSyntax invocation, |
116 | 70 | return false; |
117 | 71 | } |
118 | 72 |
|
119 | | - private static bool TryFindInvocation(MemberAccessExpressionSyntax memberAccess, QualifiedMethod expected, SemanticModel semanticModel, CancellationToken cancellationToken, [NotNullWhen(true)] out InvocationExpressionSyntax? invocation) |
120 | | - { |
121 | | - switch (memberAccess.Expression) |
122 | | - { |
123 | | - case PostfixUnaryExpressionSyntax { RawKind: (int)SyntaxKind.SuppressNullableWarningExpression, Operand: InvocationExpressionSyntax candidate } |
124 | | - when candidate.TryGetTarget(expected, semanticModel, cancellationToken, out _): |
125 | | - invocation = candidate; |
126 | | - return true; |
127 | | - case InvocationExpressionSyntax candidate |
128 | | - when candidate.TryGetTarget(expected, semanticModel, cancellationToken, out _): |
129 | | - invocation = candidate; |
130 | | - return true; |
131 | | - case IdentifierNameSyntax identifierName |
132 | | - when semanticModel.TryGetSymbol(identifierName, cancellationToken, out ILocalSymbol? local) && |
133 | | - AssignedValue.FindSingle(local, semanticModel, cancellationToken) is InvocationExpressionSyntax candidate && |
134 | | - candidate.TryGetTarget(expected, semanticModel, cancellationToken, out _): |
135 | | - invocation = candidate; |
136 | | - return true; |
137 | | - } |
138 | | - |
139 | | - invocation = null; |
140 | | - return false; |
141 | | - } |
142 | | - |
143 | 73 | /// <summary> |
144 | 74 | /// Defensive check to only handle known cases. Don't know how the binder works. |
145 | 75 | /// </summary> |
|
0 commit comments