Skip to content

Commit 4acfa64

Browse files
authored
Use forattributename jsgenerator (#78136)
* Use ForAttributeWithMetadataName instead of CreateSyntaxProvider * Remove unused code * Update export generator too
1 parent 389f286 commit 4acfa64

File tree

2 files changed

+6
-56
lines changed

2 files changed

+6
-56
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3535
{
3636
// Collect all methods adorned with JSExportAttribute
3737
var attributedMethods = context.SyntaxProvider
38-
.CreateSyntaxProvider(
39-
static (node, ct) => ShouldVisitNode(node),
40-
static (context, ct) =>
41-
{
42-
MethodDeclarationSyntax syntax = (MethodDeclarationSyntax)context.Node;
43-
if (context.SemanticModel.GetDeclaredSymbol(syntax, ct) is IMethodSymbol methodSymbol
44-
&& methodSymbol.GetAttributes().Any(static attribute => attribute.AttributeClass?.ToDisplayString() == Constants.JSExportAttribute))
45-
{
46-
return new { Syntax = syntax, Symbol = methodSymbol };
47-
}
48-
49-
return null;
50-
})
51-
.Where(
52-
static modelData => modelData is not null);
38+
.ForAttributeWithMetadataName(Constants.JSExportAttribute,
39+
static (node, ct) => node is MethodDeclarationSyntax,
40+
static (context, ct) => new { Syntax = (MethodDeclarationSyntax)context.TargetNode, Symbol = (IMethodSymbol)context.TargetSymbol });
5341

5442
// Validate if attributed methods can have source generated
5543
var methodsWithDiagnostics = attributedMethods.Select(static (data, ct) =>
@@ -233,19 +221,6 @@ private static (MemberDeclarationSyntax, ImmutableArray<Diagnostic>) GenerateSou
233221
return (PrintGeneratedSource(incrementalContext.StubMethodSyntaxTemplate, incrementalContext.SignatureContext, incrementalContext.ContainingSyntaxContext, wrapper, registration), incrementalContext.Diagnostics.Array.AddRange(diagnostics.Diagnostics));
234222
}
235223

236-
private static bool ShouldVisitNode(SyntaxNode syntaxNode)
237-
{
238-
// We only support C# method declarations.
239-
if (syntaxNode.Language != LanguageNames.CSharp
240-
|| !syntaxNode.IsKind(SyntaxKind.MethodDeclaration))
241-
{
242-
return false;
243-
}
244-
245-
// Filter out methods with no attributes early.
246-
return ((MethodDeclarationSyntax)syntaxNode).AttributeLists.Count > 0;
247-
}
248-
249224
private static Diagnostic? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method)
250225
{
251226
// Verify the method has no generic types or defined implementation

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3737
{
3838
// Collect all methods adorned with JSImportAttribute
3939
var attributedMethods = context.SyntaxProvider
40-
.CreateSyntaxProvider(
41-
static (node, ct) => ShouldVisitNode(node),
42-
static (context, ct) =>
43-
{
44-
MethodDeclarationSyntax syntax = (MethodDeclarationSyntax)context.Node;
45-
if (context.SemanticModel.GetDeclaredSymbol(syntax, ct) is IMethodSymbol methodSymbol
46-
&& methodSymbol.GetAttributes().Any(static attribute => attribute.AttributeClass?.ToDisplayString() == Constants.JSImportAttribute))
47-
{
48-
return new { Syntax = syntax, Symbol = methodSymbol };
49-
}
50-
51-
return null;
52-
})
53-
.Where(
54-
static modelData => modelData is not null);
40+
.ForAttributeWithMetadataName(Constants.JSImportAttribute,
41+
static (node, ct) => node is MethodDeclarationSyntax,
42+
static (context, ct) => new { Syntax = (MethodDeclarationSyntax)context.TargetNode, Symbol = (IMethodSymbol)context.TargetSymbol });
5543

5644
// Validate if attributed methods can have source generated
5745
var methodsWithDiagnostics = attributedMethods.Select(static (data, ct) =>
@@ -244,19 +232,6 @@ private static (MemberDeclarationSyntax, ImmutableArray<Diagnostic>) GenerateSou
244232
return (PrintGeneratedSource(incrementalContext.StubMethodSyntaxTemplate, incrementalContext.SignatureContext, incrementalContext.ContainingSyntaxContext, code), incrementalContext.Diagnostics.Array.AddRange(diagnostics.Diagnostics));
245233
}
246234

247-
private static bool ShouldVisitNode(SyntaxNode syntaxNode)
248-
{
249-
// We only support C# method declarations.
250-
if (syntaxNode.Language != LanguageNames.CSharp
251-
|| !syntaxNode.IsKind(SyntaxKind.MethodDeclaration))
252-
{
253-
return false;
254-
}
255-
256-
// Filter out methods with no attributes early.
257-
return ((MethodDeclarationSyntax)syntaxNode).AttributeLists.Count > 0;
258-
}
259-
260235
private static Diagnostic? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method)
261236
{
262237
// Verify the method has no generic types or defined implementation

0 commit comments

Comments
 (0)