Skip to content

Commit 67c676c

Browse files
authored
Merge pull request #1159 from CommunityToolkit/dev/mvvm-analyzers-fixes
Update analyzers for C# 14.0 language version checks
2 parents 52ef125 + 4222078 commit 67c676c

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using System.Linq;
99
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1010
using Microsoft.CodeAnalysis;
11+
#if ROSLYN_5_0_0_OR_GREATER
12+
using Microsoft.CodeAnalysis.CSharp;
13+
#endif
1114
using Microsoft.CodeAnalysis.Diagnostics;
1215
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1316

@@ -30,9 +33,13 @@ public override void Initialize(AnalysisContext context)
3033

3134
context.RegisterCompilationStartAction(static context =>
3235
{
33-
// Using [ObservableProperty] on partial properties is only supported when using C# preview.
36+
// Using [ObservableProperty] on partial properties is only supported without C# 14.0 or above.
3437
// As such, if that is not the case, return immediately, as no diagnostic should be produced.
38+
#if ROSLYN_5_0_0_OR_GREATER
39+
if (!context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
40+
#else
3541
if (!context.Compilation.IsLanguageVersionPreview())
42+
#endif
3643
{
3744
return;
3845
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public override void Initialize(AnalysisContext context)
5555
{
5656
// Using [ObservableProperty] on partial properties is only supported when using C# preview.
5757
// As such, if that is not the case, return immediately, as no diagnostic should be produced.
58+
#if ROSLYN_5_0_0_OR_GREATER
59+
if (!context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
60+
#else
5861
if (!context.Compilation.IsLanguageVersionPreview())
62+
#endif
5963
{
6064
return;
6165
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using System.Threading;
1212
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1313
using Microsoft.CodeAnalysis;
14+
#if ROSLYN_5_0_0_OR_GREATER
15+
using Microsoft.CodeAnalysis.CSharp;
16+
#endif
1417
using Microsoft.CodeAnalysis.Diagnostics;
1518
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1619

@@ -78,9 +81,13 @@ public override void Initialize(AnalysisContext context)
7881
}
7982
}, SymbolKind.Field);
8083

81-
// If C# preview is already in use, we can stop here. The last diagnostic is only needed when partial properties
84+
// If C# is version 14.0 or above, we can stop here. The last diagnostic is only needed when partial properties
8285
// cannot be used, to inform developers that they'll need to bump the language version to enable the code fixer.
86+
#if ROSLYN_5_0_0_OR_GREATER
87+
if (context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
88+
#else
8389
if (context.Compilation.IsLanguageVersionPreview())
90+
#endif
8491
{
8592
return;
8693
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ public partial class SampleViewModel : ObservableObject
188188
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<UseObservablePropertyOnPartialPropertyAnalyzer>(source, LanguageVersion.Preview);
189189
}
190190

191+
#if ROSLYN_5_0_0_OR_GREATER
192+
[TestMethod]
193+
public async Task UseObservablePropertyOnPartialPropertyAnalyzer_LanguageVersionIsCSharp14_Warns()
194+
{
195+
const string source = """
196+
using CommunityToolkit.Mvvm.ComponentModel;
197+
198+
namespace MyApp
199+
{
200+
public partial class SampleViewModel : ObservableObject
201+
{
202+
[ObservableProperty]
203+
private string {|MVVMTK0042:name|};
204+
}
205+
}
206+
""";
207+
208+
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<UseObservablePropertyOnPartialPropertyAnalyzer>(source, LanguageVersion.CSharp14);
209+
}
210+
#endif
211+
191212
[TestMethod]
192213
public async Task UseObservablePropertyOnPartialPropertyAnalyzer_LanguageVersionIsPreview_OnPartialProperty_DoesNotWarn()
193214
{
@@ -503,6 +524,30 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNot
503524
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
504525
}
505526

527+
#if ROSLYN_5_0_0_OR_GREATER
528+
[TestMethod]
529+
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_Auto_LanguageVersionIsCSharp14_Warns()
530+
{
531+
const string source = """
532+
using CommunityToolkit.Mvvm.ComponentModel;
533+
534+
namespace MyApp
535+
{
536+
public partial class SampleViewModel : ObservableObject
537+
{
538+
[ObservableProperty]
539+
private string {|MVVMTK0045:name|};
540+
}
541+
}
542+
""";
543+
544+
await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer>.VerifyAnalyzerAsync(
545+
source,
546+
LanguageVersion.CSharp14,
547+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
548+
}
549+
#endif
550+
506551
[TestMethod]
507552
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_Auto_NotCSharpPreview_Warns_WithCompilationWarning()
508553
{
@@ -1471,6 +1516,29 @@ public partial class SampleViewModel : ObservableObject
14711516
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
14721517
}
14731518

1519+
#if ROSLYN_5_0_0_OR_GREATER
1520+
[TestMethod]
1521+
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_LanguageVersionIsCSharp14_Warns()
1522+
{
1523+
const string source = """
1524+
using CommunityToolkit.Mvvm.ComponentModel;
1525+
1526+
namespace MyApp;
1527+
1528+
public partial class SampleViewModel : ObservableObject
1529+
{
1530+
public string {|MVVMTK0056:Name|}
1531+
{
1532+
get => field;
1533+
set => SetProperty(ref field, value);
1534+
}
1535+
}
1536+
""";
1537+
1538+
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp14);
1539+
}
1540+
#endif
1541+
14741542
[TestMethod]
14751543
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithModifiers_Warns()
14761544
{

0 commit comments

Comments
 (0)