| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 8 | | |
| | | 9 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 10 | | public sealed class IncorrectAttributeUsageAnalyzer : DiagnosticAnalyzer |
| | | 11 | | { |
| | 0 | 12 | | private static readonly ImmutableArray<DiagnosticDescriptor> supportedDescriptors = |
| | 0 | 13 | | ImmutableArray.Create(DiagnosticRules.IncorrectAttributeUsage); |
| | | 14 | | |
| | 0 | 15 | | private static readonly SyntaxKind[] supportedSyntaxKinds = [SyntaxKind.Attribute]; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| | | 19 | | { |
| | 0 | 20 | | get { return supportedDescriptors; } |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 0 | 26 | | context.EnableConcurrentExecution(); |
| | 0 | 27 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | | 28 | | |
| | 0 | 29 | | context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private static void Action(SyntaxNodeAnalysisContext context) |
| | | 33 | | { |
| | 0 | 34 | | if (context.Node is not AttributeSyntax attributeSyntax) |
| | | 35 | | { |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Get symbol |
| | 0 | 40 | | INamedTypeSymbol? attributeSymbol = attributeSyntax.GetAttributeSymbol(context.SemanticModel, context.Cancellati |
| | 0 | 41 | | if (attributeSymbol == null || !attributeSymbol.TryGetCallbackAttribute(out CallbackType _)) |
| | | 42 | | { |
| | | 43 | | // This is not a callback attribute, so we don't need to do anything. |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // Get member declaration |
| | 0 | 48 | | MemberDeclarationSyntax? memberDeclaration = attributeSyntax.FirstAncestorOrSelf<MemberDeclarationSyntax>(); |
| | 0 | 49 | | if (memberDeclaration == null) |
| | | 50 | | { |
| | 0 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Get wrapper |
| | 0 | 55 | | if (!memberDeclaration.TryGetFieldOrPropertyWrapper(context.SemanticModel, out ImmutableArray<FieldOrPropertyWra |
| | 0 | 56 | | wrappers.IsDefaultOrEmpty) |
| | | 57 | | { |
| | 0 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | FieldOrPropertyWrapper wrapper = wrappers.ItemRef(0); |
| | 0 | 62 | | if (ScriptableValueHelper.TryGetScriptableType(context.SemanticModel, (INamedTypeSymbol) wrapper.Type, out _, ou |
| | | 63 | | { |
| | | 64 | | // The type is a scriptable type, so we don't need to do anything. |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.IncorrectAttributeUsage, attributeSyntax.GetLocation( |
| | 0 | 69 | | } |
| | | 70 | | } |