-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f419ab6
commit 8456dd0
Showing
7 changed files
with
426 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Carter/Analyzers/CarterModuleShouldNotHaveDependenciesAnalyzer.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,71 @@ | ||
namespace Carter.Analyzers; | ||
|
||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
internal sealed class CarterModuleShouldNotHaveDependenciesAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.RegisterCompilationStartAction(OnCompilationStart); | ||
} | ||
|
||
private static void OnCompilationStart(CompilationStartAnalysisContext context) | ||
{ | ||
var carterModuleType = context.Compilation.GetTypeByMetadataName("Carter.ICarterModule"); | ||
if (carterModuleType is null) | ||
{ | ||
return; | ||
} | ||
|
||
context.RegisterSymbolAction(ctx => OnTypeAnalysis(ctx, carterModuleType), SymbolKind.NamedType); | ||
} | ||
|
||
private static void OnTypeAnalysis(SymbolAnalysisContext context, INamedTypeSymbol carterModuleType) | ||
{ | ||
var typeSymbol = (INamedTypeSymbol)context.Symbol; | ||
if (!typeSymbol.Interfaces.Contains(carterModuleType, SymbolEqualityComparer.Default)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var constructor in typeSymbol.Constructors) | ||
{ | ||
if (constructor.DeclaredAccessibility == Accessibility.Private || constructor.Parameters.Length == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var syntaxReference in constructor.DeclaringSyntaxReferences) | ||
{ | ||
var node = syntaxReference.GetSyntax(); | ||
SyntaxToken identifier; | ||
if (node is ConstructorDeclarationSyntax constructorDeclaration) | ||
{ | ||
identifier = constructorDeclaration.Identifier; | ||
} else if (node is RecordDeclarationSyntax recordDeclaration) | ||
{ | ||
identifier = recordDeclaration.Identifier; | ||
} | ||
else | ||
{ | ||
continue; | ||
} | ||
|
||
var diagnostic = Diagnostic.Create( | ||
DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies, | ||
identifier.GetLocation(), | ||
identifier.Text | ||
); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies]; | ||
} |
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,16 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Carter.Analyzers; | ||
|
||
internal static class DiagnosticDescriptors | ||
{ | ||
public static readonly DiagnosticDescriptor CarterModuleShouldNotHaveDependencies = new( | ||
"CARTER1", | ||
"Carter module should not have dependencies", | ||
"'{0}' should not have dependencies", | ||
"Usage", | ||
DiagnosticSeverity.Warning, | ||
true, | ||
"When a class implements ICarterModule, it should not have any dependencies. This is because Carter uses minimal APIs and dependencies should be declared in the request delegate." | ||
); | ||
} |
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
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
Oops, something went wrong.