-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added analyzer to validate union syntax.
- Loading branch information
Showing
5 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/ZeroQL.SourceGenerators/Analyzers/QueryOnSyntaxAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace ZeroQL.SourceGenerators.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class QueryOnSyntaxAnalyzer : DiagnosticAnalyzer | ||
{ | ||
#pragma warning disable RS1026 | ||
public override void Initialize(AnalysisContext context) | ||
#pragma warning restore RS1026 | ||
{ | ||
#if !DEBUG | ||
context.EnableConcurrentExecution(); | ||
#endif | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | | ||
GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
context.RegisterSyntaxNodeAction(Handle, SyntaxKind.InvocationExpression); | ||
} | ||
|
||
private void Handle(SyntaxNodeAnalysisContext context) | ||
{ | ||
if (context.Node is not InvocationExpressionSyntax | ||
{ | ||
Expression: MemberAccessExpressionSyntax { Name.Identifier.ValueText: "On" } memberAccess | ||
} invocation) | ||
{ | ||
return; | ||
} | ||
|
||
var possibleMethod = context.SemanticModel.GetSymbolInfo(invocation); | ||
if (possibleMethod.Symbol is not IMethodSymbol method) | ||
{ | ||
return; | ||
} | ||
|
||
var onMethod = context.Compilation.GetTypeByMetadataName("ZeroQL.GraphQLSyntaxExtensions")? | ||
.GetMembers("On") | ||
.OfType<IMethodSymbol>() | ||
.FirstOrDefault(); | ||
|
||
if (!SymbolEqualityComparer.Default.Equals(onMethod, method.ReducedFrom)) | ||
{ | ||
return; | ||
} | ||
|
||
var typeArgument = method.TypeArguments.FirstOrDefault(); | ||
if (typeArgument is null) | ||
{ | ||
return; | ||
} | ||
|
||
var type = context.SemanticModel.GetTypeInfo(memberAccess.Expression); | ||
if (type.Type is not INamedTypeSymbol targetType) | ||
{ | ||
return; | ||
} | ||
|
||
if (typeArgument.AllInterfaces.Contains(targetType)) | ||
{ | ||
return; | ||
} | ||
|
||
context.ReportDiagnostic(Diagnostic.Create( | ||
Descriptors.GraphQLQueryInvalidUnionType, | ||
memberAccess.Name.Identifier.GetLocation(), | ||
typeArgument.Name, | ||
targetType.Name)); | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
= ImmutableArray.Create(Descriptors.GraphQLQueryInvalidUnionType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/ZeroQL.Tests/SourceGeneration/OnSyntaxTests.AppliedToWrongType.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
Id: GraphQLQueryInvalidUnionType, | ||
Highlighted: On<ImageContent>, | ||
Message: Type ImageContent has to implement IFigure interface. | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters