Skip to content

Commit

Permalink
Allow users to suppress the sync generator (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
j3parker authored Aug 1, 2024
1 parent e27c8b6 commit 7e86084
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
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 ) )
// 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>

</Project>

0 comments on commit 7e86084

Please sign in to comment.