Skip to content

Commit a3bafc6

Browse files
committed
Add RegisterConditionalSourceOutput API
1 parent bf4b5f6 commit a3bafc6

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Extensions/IncrementalGeneratorInitializationContextExtensions.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ public static void FilterWithLanguageVersion<T>(
5050
.Select(static (item, _) => item.Length > 0);
5151

5252
// Report them to the output
53-
context.RegisterSourceOutput(isUnsupportedAttributeUsed, (context, diagnostic) =>
53+
context.RegisterConditionalSourceOutput(isUnsupportedAttributeUsed, context =>
5454
{
55-
if (diagnostic)
56-
{
57-
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, null));
58-
}
55+
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, null));
5956
});
6057

6158
// Only let data through if the minimum language version is supported
@@ -65,10 +62,32 @@ public static void FilterWithLanguageVersion<T>(
6562
.Select(static (item, _) => item.Data);
6663
}
6764

65+
/// <summary>
66+
/// Conditionally invokes <see cref="IncrementalGeneratorInitializationContext.RegisterSourceOutput{TSource}(IncrementalValueProvider{TSource}, Action{SourceProductionContext, TSource})"/>
67+
/// if the value produced by the input <see cref="IncrementalValueProvider{TValue}"/> is <see langword="true"/>.
68+
/// </summary>
69+
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> value being used.</param>
70+
/// <param name="source">The source <see cref="IncrementalValueProvider{TValues}"/> instance.</param>
71+
/// <param name="action">The conditional <see cref="Action"/> to invoke.</param>
72+
public static void RegisterConditionalSourceOutput(
73+
this IncrementalGeneratorInitializationContext context,
74+
IncrementalValueProvider<bool> source,
75+
Action<SourceProductionContext> action)
76+
{
77+
context.RegisterSourceOutput(source, (context, condition) =>
78+
{
79+
if (condition)
80+
{
81+
action(context);
82+
}
83+
});
84+
}
85+
6886
/// <summary>
6987
/// Conditionally invokes <see cref="IncrementalGeneratorInitializationContext.RegisterImplementationSourceOutput{TSource}(IncrementalValueProvider{TSource}, Action{SourceProductionContext, TSource})"/>
7088
/// if the value produced by the input <see cref="IncrementalValueProvider{TValue}"/> is <see langword="true"/>, and also supplying a given input state.
7189
/// </summary>
90+
/// <typeparam name="T">The type of state to pass to the source production callback to invoke.</typeparam>
7291
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> value being used.</param>
7392
/// <param name="source">The source <see cref="IncrementalValueProvider{TValues}"/> instance.</param>
7493
/// <param name="action">The conditional <see cref="Action{T}"/> to invoke.</param>

0 commit comments

Comments
 (0)