-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
Showing
5 changed files
with
80 additions
and
68 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,44 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace VContainer.SourceGenerator | ||
namespace VContainer.SourceGenerator; | ||
|
||
class SyntaxCollector : ISyntaxReceiver | ||
{ | ||
class SyntaxCollector : ISyntaxReceiver | ||
{ | ||
public List<string> Log { get; } = new(); | ||
public List<WorkItem> WorkItems { get; } = new(); | ||
public List<string> Log { get; } = new(); | ||
public List<WorkItem> WorkItems { get; } = new(); | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode.IsKind(SyntaxKind.ClassDeclaration)) | ||
{ | ||
if (syntaxNode.IsKind(SyntaxKind.ClassDeclaration)) | ||
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax) | ||
{ | ||
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax) | ||
if (!classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) || | ||
modifier.IsKind(SyntaxKind.StaticKeyword))) | ||
{ | ||
if (!classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) || | ||
{ | ||
WorkItems.Add(new WorkItem(classDeclarationSyntax)); | ||
} | ||
WorkItems.Add(new WorkItem(classDeclarationSyntax)); | ||
} | ||
} | ||
else if (syntaxNode.IsKind(SyntaxKind.InvocationExpression)) | ||
{ | ||
if (syntaxNode is InvocationExpressionSyntax | ||
{ | ||
Expression: MemberAccessExpressionSyntax | ||
{ | ||
Expression: IdentifierNameSyntax | ||
} memberAccess | ||
}) | ||
} | ||
else if (syntaxNode.IsKind(SyntaxKind.InvocationExpression)) | ||
{ | ||
if (syntaxNode is InvocationExpressionSyntax | ||
{ | ||
if (memberAccess.Name.Identifier.Text.StartsWith("Register")) | ||
Expression: MemberAccessExpressionSyntax | ||
{ | ||
} | ||
Expression: IdentifierNameSyntax | ||
} memberAccess | ||
} invocationExpressionSyntax) | ||
{ | ||
if (memberAccess.Name.Identifier.Text.StartsWith("Register")) | ||
{ | ||
WorkItems.Add(new WorkItem(invocationExpressionSyntax)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,19 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace VContainer.SourceGenerator; | ||
|
||
class WorkItem | ||
{ | ||
public TypeDeclarationSyntax Syntax { get; } | ||
public TypeDeclarationSyntax? TypeDeclarationSyntax { get; } | ||
public InvocationExpressionSyntax? RegisterInvocationSyntax { get; } | ||
|
||
public WorkItem(TypeDeclarationSyntax syntax) | ||
public WorkItem(TypeDeclarationSyntax typeDeclarationSyntax) | ||
{ | ||
Syntax = syntax; | ||
TypeDeclarationSyntax = typeDeclarationSyntax; | ||
} | ||
|
||
public TypeMeta? Analyze(in GeneratorExecutionContext context, ReferenceSymbols references) | ||
public WorkItem(InvocationExpressionSyntax registerInvocationCandidate) | ||
{ | ||
var semanticModel = context.Compilation.GetSemanticModel(Syntax.SyntaxTree); | ||
var symbol = semanticModel.GetDeclaredSymbol(Syntax, context.CancellationToken); | ||
if (symbol is not INamedTypeSymbol typeSymbol) | ||
{ | ||
return null; | ||
} | ||
var isAttribute = typeSymbol.GetAllBaseTypes().Any(x => SymbolEqualityComparer.Default.Equals(x, references.AttributeBase)); | ||
if (isAttribute) | ||
{ | ||
return null; | ||
} | ||
|
||
var injectIgnore = symbol.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, references.VContainerInjectIgnoreAttribute)); | ||
if (injectIgnore) | ||
{ | ||
return null; | ||
} | ||
|
||
return new TypeMeta(typeSymbol, references, Syntax); | ||
RegisterInvocationSyntax = registerInvocationCandidate; | ||
} | ||
} |
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