Skip to content

Commit 24f5271

Browse files
committed
don't report CAF008 error on CommunityToolkit.Mvvm.ObservableProperty "Add" calls
1 parent 082a4bf commit 24f5271

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

sandbox/GeneratorSandbox/GeneratorSandbox.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<ItemGroup>
2020
<!--<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
2121
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />-->
22+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
2223
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
2324
<!--<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />-->
2425
<!--<PackageReference Include="ZLogger" Version="2.5.9" />-->

sandbox/GeneratorSandbox/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22

3+
using CommunityToolkit.Mvvm.ComponentModel;
34
using ConsoleAppFramework;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Hosting;
@@ -14,7 +15,20 @@
1415
services.AddSingleton<ITest, Test>();
1516
services.AddKeyedSingleton<ITest, KeyedTest>("Key");
1617

18+
19+
MyObj obj = new();
20+
while (obj.Data.Count < 1)
21+
{
22+
obj.Data.Add(0); // <-- CAF008 error here
23+
}
24+
1725
var app = builder.ToConsoleAppBuilder();
26+
// var app = ConsoleApp.Create();
27+
//for (int i = 0; i < 10; i++)
28+
//{
29+
// app.Add("foo", (int x) => { });
30+
//}
31+
1832
app.Run(args);
1933

2034
interface ITest
@@ -42,7 +56,11 @@ public void Run()
4256
}
4357
}
4458

45-
59+
public partial class MyObj : ObservableObject
60+
{
61+
[ObservableProperty]
62+
private List<int> data = [];
63+
}
4664

4765

4866
//args = ["echo", "--msg", "zzzz"];

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
151151
context.SemanticModel,
152152
ct))
153153
.WithTrackingName("ConsoleApp.Builder.0_CreateSyntaxProvider")
154-
.Where(x =>
154+
.Select((x, ct) =>
155155
{
156156
var model = x.Model.GetTypeInfo((x.Node.Expression as MemberAccessExpressionSyntax)!.Expression, x.CancellationToken);
157-
return model.Type?.Name is "ConsoleAppBuilder" or "IHostBuilder" || model.Type?.Kind == SymbolKind.ErrorType; // allow ErrorType(ConsoleAppBuilder from Configure***(Source Generator generated method) is unknown in Source Generator)
157+
return (x, model.Type?.Name, model.Type?.Kind);
158158
})
159+
.Where(x => x.Name is "ConsoleAppBuilder" or "IHostBuilder" || x.Kind == SymbolKind.ErrorType) // allow ErrorType(ConsoleAppBuilder from Configure***(Source Generator generated method) is unknown in Source Generator)
159160
.WithTrackingName("ConsoleApp.Builder.1_Where")
160161
.Collect()
161162
.Combine(generatorOptions)
@@ -353,7 +354,7 @@ class CollectBuilderContext : IEquatable<CollectBuilderContext>
353354
FilterInfo[]? globalFilters { get; }
354355
ConsoleAppFrameworkGeneratorOptions generatorOptions { get; }
355356

356-
public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOptions, ImmutableArray<BuilderContext> contexts, CancellationToken cancellationToken)
357+
public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOptions, ImmutableArray<(BuilderContext, string?, SymbolKind?)> contexts, CancellationToken cancellationToken)
357358
{
358359
this.DiagnosticReporter = new DiagnosticReporter();
359360
this.CancellationToken = cancellationToken;
@@ -362,19 +363,24 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
362363
// validation, invoke in loop is not allowed.
363364
foreach (var item in contexts)
364365
{
365-
if (item.Name is "Run" or "RunAsync") continue;
366-
foreach (var n in item.Node.Ancestors())
366+
var (ctx, name, kind) = item;
367+
if (kind == SymbolKind.ErrorType) continue; // ErrorType can't distinguished from ConsoleAppFramework or others so ignore all.
368+
369+
if (ctx.Name is "Run" or "RunAsync") continue;
370+
foreach (var n in ctx.Node.Ancestors())
367371
{
368372
if (n.Kind() is SyntaxKind.WhileStatement or SyntaxKind.DoStatement or SyntaxKind.ForStatement or SyntaxKind.ForEachStatement)
369373
{
370-
DiagnosticReporter.ReportDiagnostic(DiagnosticDescriptors.AddInLoopIsNotAllowed, item.Node.GetLocation());
374+
DiagnosticReporter.ReportDiagnostic(DiagnosticDescriptors.AddInLoopIsNotAllowed, ctx.Node.GetLocation());
371375
return;
372376
}
373377
}
374378
}
375379

376-
var methodGroup = contexts.ToLookup(x =>
380+
var methodGroup = contexts.ToLookup(ctx =>
377381
{
382+
var x = ctx.Item1;
383+
378384
if (x.Name == "Add" && ((x.Node.Expression as MemberAccessExpressionSyntax)?.Name.IsKind(SyntaxKind.GenericName) ?? false))
379385
{
380386
return "Add<T>";
@@ -384,6 +390,7 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
384390
});
385391

386392
globalFilters = methodGroup["UseFilter"]
393+
.Select(x => x.Item1)
387394
.OrderBy(x => x.Node.GetLocation().SourceSpan) // sort by line number
388395
.Select(x =>
389396
{
@@ -413,6 +420,7 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
413420

414421
var names = new HashSet<string>();
415422
var commands1 = methodGroup["Add"]
423+
.Select(x => x.Item1)
416424
.Select(x =>
417425
{
418426
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);
@@ -432,6 +440,7 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
432440
.ToArray(); // evaluate first.
433441

434442
var commands2 = methodGroup["Add<T>"]
443+
.Select(x => x.Item1)
435444
.SelectMany(x =>
436445
{
437446
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);

0 commit comments

Comments
 (0)