-
-
Notifications
You must be signed in to change notification settings - Fork 805
Add analyzer "Lookup Must Not Return List Type" #9553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
115 changes: 115 additions & 0 deletions
115
src/HotChocolate/Core/src/Types.Analyzers/LookupReturnsListTypeAnalyzer.cs
This file contains hidden or 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,115 @@ | ||
| using System.Collections.Immutable; | ||
| using HotChocolate.Types.Analyzers.Helpers; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace HotChocolate.Types.Analyzers; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class LookupReturnsListTypeAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
| [Errors.LookupReturnsListType]; | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration); | ||
| context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration); | ||
| } | ||
|
|
||
| private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var methodDeclaration = (MethodDeclarationSyntax)context.Node; | ||
|
|
||
| if (!HasLookupAttribute(context, methodDeclaration.AttributeLists)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration); | ||
| if (methodSymbol is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var returnType = context.Compilation.IsTaskOrValueTask(methodSymbol.ReturnType, out var innerType) | ||
| ? innerType | ||
| : methodSymbol.ReturnType; | ||
|
|
||
| if (!IsListType(returnType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var diagnostic = Diagnostic.Create( | ||
| Errors.LookupReturnsListType, | ||
| methodDeclaration.ReturnType.GetLocation()); | ||
|
|
||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
|
|
||
| private static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var propertyDeclaration = (PropertyDeclarationSyntax)context.Node; | ||
|
|
||
| if (!HasLookupAttribute(context, propertyDeclaration.AttributeLists)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration); | ||
| if (propertySymbol is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var propertyType = context.Compilation.IsTaskOrValueTask(propertySymbol.Type, out var innerType) | ||
| ? innerType | ||
| : propertySymbol.Type; | ||
|
|
||
| if (!IsListType(propertyType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var diagnostic = Diagnostic.Create( | ||
| Errors.LookupReturnsListType, | ||
| propertyDeclaration.Type.GetLocation()); | ||
|
|
||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
|
|
||
| private static bool IsListType(ITypeSymbol typeSymbol) | ||
| => typeSymbol is IArrayTypeSymbol || typeSymbol.IsListType(out _); | ||
|
|
||
| private static bool HasLookupAttribute( | ||
| SyntaxNodeAnalysisContext context, | ||
| SyntaxList<AttributeListSyntax> attributeLists) | ||
| { | ||
| var semanticModel = context.SemanticModel; | ||
|
|
||
| foreach (var attributeList in attributeLists) | ||
| { | ||
| foreach (var attribute in attributeList.Attributes) | ||
| { | ||
| var symbolInfo = semanticModel.GetSymbolInfo(attribute); | ||
| if (symbolInfo.Symbol is not IMethodSymbol attributeSymbol) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var attributeType = attributeSymbol.ContainingType; | ||
| if (attributeType.ToDisplayString() == WellKnownAttributes.LookupAttribute) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or 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
205 changes: 205 additions & 0 deletions
205
src/HotChocolate/Core/test/Types.Analyzers.Tests/LookupReturnsListTypeAnalyzerTests.cs
This file contains hidden or 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,205 @@ | ||
| namespace HotChocolate.Types; | ||
|
|
||
| public class LookupReturnsListTypeAnalyzerTests | ||
| { | ||
| [Fact] | ||
| public async Task Method_ListReturn_RaisesError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static List<User?> GetUsersById(int id) => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Method_ArrayReturn_RaisesError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static User?[] GetUsersById(int id) => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Method_IEnumerableReturn_RaisesError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static IEnumerable<User?> GetUsersById(int id) => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Method_TaskListReturn_RaisesError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static Task<List<User?>> GetUsersByIdAsync(int id) => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Property_ListReturn_RaisesError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static List<User?> AllUsers => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Method_SingleReturn_NoError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| [Lookup] | ||
| public static User? GetUserById(int id) => default; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Method_NoLookupAttribute_NoError() | ||
| { | ||
| await TestHelper.GetGeneratedSourceSnapshot( | ||
| [""" | ||
| #nullable enable | ||
| using HotChocolate; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Composite; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace TestNamespace; | ||
|
|
||
| [QueryType] | ||
| internal static partial class Query | ||
| { | ||
| public static List<User?> GetUsersById(int id) => default!; | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| } | ||
| """], | ||
| enableAnalyzers: true).MatchMarkdownAsync(); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.