Skip to content

Commit

Permalink
Generator Change
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Jun 8, 2022
1 parent 1d44b2f commit c22809d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 35 deletions.
34 changes: 2 additions & 32 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private sealed class Parser
private const string JsonPropertyNameAttributeFullName = "System.Text.Json.Serialization.JsonPropertyNameAttribute";
private const string JsonPropertyOrderAttributeFullName = "System.Text.Json.Serialization.JsonPropertyOrderAttribute";
private const string JsonSerializerContextFullName = "System.Text.Json.Serialization.JsonSerializerContext";
private const string JsonSerializableAttributeFullName = "System.Text.Json.Serialization.JsonSerializableAttribute";
internal const string JsonSerializableAttributeFullName = "System.Text.Json.Serialization.JsonSerializableAttribute";
private const string JsonSourceGenerationOptionsAttributeFullName = "System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute";

private const string DateOnlyFullName = "System.DateOnly";
Expand Down Expand Up @@ -552,37 +552,7 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not
return typeGenerationSpec;
}

internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) => node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 }, BaseList: { Types : {Count : > 0 } } };

This comment has been minimized.

Copy link
@CyrusNajmabadi

CyrusNajmabadi Jun 9, 2022

Member

note: this fixes a bug as well where there was a requirement that the type-decl that had the attribute list was also the decl that listed the base types. this is not necessary in c# as you can have separate parts, one with attributes, one with the base-type list.


internal static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node;

foreach (AttributeListSyntax attributeListSyntax in classDeclarationSyntax.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
{
cancellationToken.ThrowIfCancellationRequested();

IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax, cancellationToken).Symbol as IMethodSymbol;
if (attributeSymbol == null)
{
continue;
}

INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
string fullName = attributeContainingTypeSymbol.ToDisplayString();

if (fullName == JsonSerializableAttributeFullName)
{
return classDeclarationSyntax;
}
}

}

return null;
}
internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) => node is ClassDeclarationSyntax;

private static JsonSourceGenerationMode? GetJsonSourceGenerationModeEnumVal(SyntaxNode propertyValueMode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public sealed partial class JsonSourceGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<ClassDeclarationSyntax> classDeclarations = context.SyntaxProvider
.CreateSyntaxProvider(static (s, _) => Parser.IsSyntaxTargetForGeneration(s), static (s, c) => Parser.GetSemanticTargetForGeneration(s, c))
.Where(static c => c is not null);
IncrementalValuesProvider<ClassDeclarationSyntax> classDeclarations = context
.ForAttributeWithMetadataName(
Parser.JsonSerializableAttributeFullName,
static (c, _) => Parser.IsSyntaxTargetForGeneration(c),
static (c, _) => (ClassDeclarationSyntax)c.TargetNode);

IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =
context.CompilationProvider.Combine(classDeclarations.Collect());
Expand Down

1 comment on commit c22809d

@AraHaan
Copy link
Member

@AraHaan AraHaan commented on c22809d Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for JsonSourceGenerationContext in the last file instead of only using SourceProductionContext?

Please sign in to comment.