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

LFT-1285 - Allow users to suppress the sync generator #951

Merged
merged 4 commits into from
Aug 1, 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
43 changes: 36 additions & 7 deletions src/D2L.CodeStyle.Analyzers/Async/Generator/SyncGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace D2L.CodeStyle.Analyzers.Async.Generator;

[Generator]
internal sealed partial class SyncGenerator : IIncrementalGenerator {
public void Initialize( IncrementalGeneratorInitializationContext context ) {
var options = context.AnalyzerConfigOptionsProvider
.Select( ParseConfig );

// Collect all the methods we want to generate sync versions for
// individually (for better incremental builds)
IncrementalValuesProvider<(MethodDeclarationSyntax, Compilation)> methodsToGenerate =
Expand All @@ -20,7 +24,8 @@ public void Initialize( IncrementalGeneratorInitializationContext context ) {
// Do the generation per method
IncrementalValuesProvider<MethodGenerationResult> generatedMethods =
methodsToGenerate
.Select( GenerateSyncMethod )
.Combine( options )
.Select( (x, ct) => GenerateSyncMethod( x.Left.Item1, x.Left.Item2, x.Right, ct ) )
Copy link
Member Author

Choose a reason for hiding this comment

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

x.Left.Item1, x.Left.Item2, x.Right pretty ugly lol

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I thought Combine magiced that. And you can't destructure into lambda variables yet so that's also sad

// Filter out things we simply couldn't generate anything for.
.Where( static mr => mr.HasValue )
.Select( static ( mr, _ ) => mr!.Value );
Expand Down Expand Up @@ -94,26 +99,32 @@ CancellationToken cancellationToken
}

private static MethodGenerationResult? GenerateSyncMethod(
(MethodDeclarationSyntax MethodDeclaration, Compilation Compilation) data,
MethodDeclarationSyntax methodDeclaration,
Compilation compilation,
SyncGeneratorOptions options,
CancellationToken cancellationToken
) {
if( !data.Compilation.ContainsSyntaxTree( data.MethodDeclaration.SyntaxTree ) ) {
if( options.SuppressSyncGenerator ) {
return null;
}

if( !compilation.ContainsSyntaxTree( methodDeclaration.SyntaxTree ) ) {
return null;
}

var model = data.Compilation.GetSemanticModel( data.MethodDeclaration.SyntaxTree );
var methodSymbol = model.GetDeclaredSymbol( data.MethodDeclaration, cancellationToken );
var model = compilation.GetSemanticModel( methodDeclaration.SyntaxTree );
var methodSymbol = model.GetDeclaredSymbol( methodDeclaration, cancellationToken );

if( methodSymbol == null || methodSymbol.Kind == SymbolKind.ErrorType ) {
return null;
}

var transformer = new AsyncToSyncMethodTransformer( model, cancellationToken );

var transformResult = transformer.Transform( data.MethodDeclaration );
var transformResult = transformer.Transform( methodDeclaration );

return new(
Original: data.MethodDeclaration,
Original: methodDeclaration,
GeneratedSyntax: transformResult.Success ? transformResult.Value!.ToFullString() : "",
Diagnostics: transformResult.Diagnostics
);
Expand Down Expand Up @@ -178,4 +189,22 @@ FileGenerationResult result
source: result.GeneratedSource
);
}

private sealed record SyncGeneratorOptions(
bool SuppressSyncGenerator
);

private static SyncGeneratorOptions ParseConfig(
AnalyzerConfigOptionsProvider options,
CancellationToken ct
) {
options.GlobalOptions.TryGetValue(
"build_property.SuppressSyncGenerator",
out var supressValue
);

return new SyncGeneratorOptions(
SuppressSyncGenerator: supressValue == "true"
);
}
}
4 changes: 4 additions & 0 deletions src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<CompilerVisibleProperty Include="SuppressSyncGenerator" />
</ItemGroup>
Copy link
Member Author

Choose a reason for hiding this comment

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

Technically not necessary but I believe putting this here will mean that users will not have to do it.

Users will configure it like this:

<PropertyGroup>
  <SuppressSyncGenerator>true</SupressSyncGenerator>
</PropertyGroup>

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we prefix that with D2LCodeStyle or something then?

Copy link
Member Author

Choose a reason for hiding this comment

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

🤷‍♂️ I've seen microsoft not prefix as long as its unambiguous, but I have no strong feelings.

Copy link
Member Author

Choose a reason for hiding this comment

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


</Project>
Loading