Skip to content
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

Optimization of the search for the target of SourceGenerator #674

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix the algorithm to target generated type
  • Loading branch information
hadashiA committed Jun 15, 2024
commit 5b52278ce69443efc04a169cd38e0ae2da0f2cf2
48 changes: 27 additions & 21 deletions VContainer.SourceGenerator.Roslyn3/SyntaxCollector.cs
Original file line number Diff line number Diff line change
@@ -1,38 +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 (IsCandidateType(syntaxNode))
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax)
{
WorkItems.Add(new WorkItem((TypeDeclarationSyntax)syntaxNode));
if (!classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) ||
modifier.IsKind(SyntaxKind.StaticKeyword)))
{
WorkItems.Add(new WorkItem(classDeclarationSyntax));
}
}
}

static bool IsCandidateType(SyntaxNode syntax)
else if (syntaxNode.IsKind(SyntaxKind.InvocationExpression))
{
if (syntax is not ClassDeclarationSyntax classDeclarationSyntax)
{
return false;
}

if (classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) ||
modifier.IsKind(SyntaxKind.StaticKeyword)))
if (syntaxNode is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax
{
Expression: IdentifierNameSyntax
} memberAccess
} invocationExpressionSyntax)
{
return false;
if (memberAccess.Name.Identifier.Text.StartsWith("Register"))
{
WorkItems.Add(new WorkItem(invocationExpressionSyntax));
}
}
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<DevelopmentDependency>true</DevelopmentDependency>
<IsRoslynComponent>true</IsRoslynComponent>
<DefineConstants>ROSLYN3</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading
Loading