Skip to content

Commit f877f7c

Browse files
committed
Add unit test for new diagnostic
1 parent 0895925 commit f877f7c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override void Initialize(AnalysisContext context)
4646
}
4747

4848
// Track whether we produced any diagnostics, for the compilation end scenario
49-
bool hasProducedAnyDiagnostics = false;
49+
AttributeData? firstObservablePropertyAttribute = null;
5050

5151
context.RegisterSymbolAction(context =>
5252
{
@@ -57,7 +57,7 @@ public override void Initialize(AnalysisContext context)
5757
}
5858

5959
// Emit a diagnostic if the field is using the [ObservableProperty] attribute
60-
if (fieldSymbol.HasAttributeWithType(observablePropertySymbol))
60+
if (fieldSymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
6161
{
6262
context.ReportDiagnostic(Diagnostic.Create(
6363
WinRTObservablePropertyOnFieldsIsNotAotCompatible,
@@ -69,7 +69,7 @@ public override void Initialize(AnalysisContext context)
6969
fieldSymbol.Name));
7070

7171
// Notify that we did produce at least one diagnostic
72-
Volatile.Write(ref hasProducedAnyDiagnostics, true);
72+
_ = Interlocked.CompareExchange(ref firstObservablePropertyAttribute, observablePropertyAttribute, null);
7373
}
7474
}, SymbolKind.Field);
7575

@@ -83,9 +83,11 @@ public override void Initialize(AnalysisContext context)
8383
context.RegisterCompilationEndAction(context =>
8484
{
8585
// If we have produced at least one diagnostic, also emit the info message
86-
if (Volatile.Read(ref hasProducedAnyDiagnostics))
86+
if (Volatile.Read(ref firstObservablePropertyAttribute) is { } observablePropertyAttribute)
8787
{
88-
context.ReportDiagnostic(Diagnostic.Create(WinRTObservablePropertyOnFieldsIsNotAotCompatibleCompilationEndInfo, location: null));
88+
context.ReportDiagnostic(Diagnostic.Create(
89+
WinRTObservablePropertyOnFieldsIsNotAotCompatibleCompilationEndInfo,
90+
observablePropertyAttribute.GetLocation()));
8991
}
9092
});
9193
});

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4110.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,28 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNot
434434
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
435435
}
436436

437+
[TestMethod]
438+
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_Auto_NotCSharpPreview_Warns_WithCompilationWarning()
439+
{
440+
const string source = """
441+
using CommunityToolkit.Mvvm.ComponentModel;
442+
443+
namespace MyApp
444+
{
445+
public partial class SampleViewModel : ObservableObject
446+
{
447+
[{|MVVMTK0051:ObservableProperty|}]
448+
private string {|MVVMTK0045:name|};
449+
}
450+
}
451+
""";
452+
453+
await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer>.VerifyAnalyzerAsync(
454+
source,
455+
LanguageVersion.CSharp12,
456+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
457+
}
458+
437459
[TestMethod]
438460
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_True_NoXaml_Level1_DoesNotWarn()
439461
{

0 commit comments

Comments
 (0)