-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
TypeAttributeInspector.cs
54 lines (47 loc) · 2.07 KB
/
TypeAttributeInspector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static System.StringComparison;
using static HotChocolate.Types.Analyzers.WellKnownAttributes;
namespace HotChocolate.Types.Analyzers.Inspectors;
public class TypeAttributeInspector : ISyntaxInspector
{
public bool TryHandle(
GeneratorSyntaxContext context,
[NotNullWhen(true)] out ISyntaxInfo? syntaxInfo)
{
if (context.Node is BaseTypeDeclarationSyntax { AttributeLists.Count: > 0 } possibleType)
{
foreach (var attributeListSyntax in possibleType.AttributeLists)
{
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
var symbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol;
if (symbol is not IMethodSymbol attributeSymbol)
{
continue;
}
var attributeContainingTypeSymbol = attributeSymbol.ContainingType;
var fullName = attributeContainingTypeSymbol.ToDisplayString();
// We do a start with here to capture the generic and non generic variant of
// the object type extension attribute.
if (fullName.StartsWith(ExtendObjectTypeAttribute, Ordinal) &&
context.SemanticModel.GetDeclaredSymbol(possibleType) is { } typeExt)
{
syntaxInfo = new TypeExtensionInfo(typeExt.ToDisplayString());
return true;
}
if (TypeAttributes.Contains(fullName) &&
context.SemanticModel.GetDeclaredSymbol(possibleType) is { } type)
{
syntaxInfo = new TypeInfo(type.ToDisplayString());
return true;
}
}
}
}
syntaxInfo = null;
return false;
}
}