From 7e86084daac093a26797c212d7e84a352a9e659a Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Thu, 1 Aug 2024 13:34:21 -0400 Subject: [PATCH] Allow users to suppress the sync generator (#951) --- .../Async/Generator/SyncGenerator.cs | 43 ++++++++++++++++--- .../D2L.CodeStyle.Analyzers.csproj | 4 ++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/D2L.CodeStyle.Analyzers/Async/Generator/SyncGenerator.cs b/src/D2L.CodeStyle.Analyzers/Async/Generator/SyncGenerator.cs index bc0ed84a..c2deb806 100644 --- a/src/D2L.CodeStyle.Analyzers/Async/Generator/SyncGenerator.cs +++ b/src/D2L.CodeStyle.Analyzers/Async/Generator/SyncGenerator.cs @@ -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 = @@ -20,7 +24,8 @@ public void Initialize( IncrementalGeneratorInitializationContext context ) { // Do the generation per method IncrementalValuesProvider 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 ); @@ -94,15 +99,21 @@ 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; @@ -110,10 +121,10 @@ CancellationToken cancellationToken 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 ); @@ -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" + ); + } } diff --git a/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj b/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj index 712ad99e..1c168a54 100644 --- a/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj +++ b/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj @@ -46,4 +46,8 @@ + + + +