Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using System.Linq;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using Microsoft.CodeAnalysis;
#if ROSLYN_5_0_0_OR_GREATER
using Microsoft.CodeAnalysis.CSharp;
#endif
using Microsoft.CodeAnalysis.Diagnostics;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;

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

context.RegisterCompilationStartAction(static context =>
{
// Using [ObservableProperty] on partial properties is only supported when using C# preview.
// Using [ObservableProperty] on partial properties is only supported without C# 14.0 or above.
// As such, if that is not the case, return immediately, as no diagnostic should be produced.
#if ROSLYN_5_0_0_OR_GREATER
if (!context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
#else
if (!context.Compilation.IsLanguageVersionPreview())
#endif
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public override void Initialize(AnalysisContext context)
{
// Using [ObservableProperty] on partial properties is only supported when using C# preview.
// As such, if that is not the case, return immediately, as no diagnostic should be produced.
#if ROSLYN_5_0_0_OR_GREATER
if (!context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
#else
if (!context.Compilation.IsLanguageVersionPreview())
#endif
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using System.Threading;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using Microsoft.CodeAnalysis;
#if ROSLYN_5_0_0_OR_GREATER
using Microsoft.CodeAnalysis.CSharp;
#endif
using Microsoft.CodeAnalysis.Diagnostics;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;

Expand Down Expand Up @@ -78,9 +81,13 @@ public override void Initialize(AnalysisContext context)
}
}, SymbolKind.Field);

// If C# preview is already in use, we can stop here. The last diagnostic is only needed when partial properties
// If C# is version 14.0 or above, we can stop here. The last diagnostic is only needed when partial properties
// cannot be used, to inform developers that they'll need to bump the language version to enable the code fixer.
#if ROSLYN_5_0_0_OR_GREATER
if (context.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp14))
#else
if (context.Compilation.IsLanguageVersionPreview())
#endif
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ public partial class SampleViewModel : ObservableObject
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<UseObservablePropertyOnPartialPropertyAnalyzer>(source, LanguageVersion.Preview);
}

#if ROSLYN_5_0_0_OR_GREATER
[TestMethod]
public async Task UseObservablePropertyOnPartialPropertyAnalyzer_LanguageVersionIsCSharp14_Warns()
{
const string source = """
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp
{
public partial class SampleViewModel : ObservableObject
{
[ObservableProperty]
private string {|MVVMTK0042:name|};
}
}
""";

await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<UseObservablePropertyOnPartialPropertyAnalyzer>(source, LanguageVersion.CSharp14);
}
#endif

[TestMethod]
public async Task UseObservablePropertyOnPartialPropertyAnalyzer_LanguageVersionIsPreview_OnPartialProperty_DoesNotWarn()
{
Expand Down Expand Up @@ -503,6 +524,30 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNot
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}

#if ROSLYN_5_0_0_OR_GREATER
[TestMethod]
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_Auto_LanguageVersionIsCSharp14_Warns()
{
const string source = """
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp
{
public partial class SampleViewModel : ObservableObject
{
[ObservableProperty]
private string {|MVVMTK0045:name|};
}
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp14,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}
#endif

[TestMethod]
public async Task WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer_TargetingWindows_CsWinRTAotOptimizerEnabled_Auto_NotCSharpPreview_Warns_WithCompilationWarning()
{
Expand Down Expand Up @@ -1471,6 +1516,29 @@ public partial class SampleViewModel : ObservableObject
await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.Preview);
}

#if ROSLYN_5_0_0_OR_GREATER
[TestMethod]
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_LanguageVersionIsCSharp14_Warns()
{
const string source = """
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

public partial class SampleViewModel : ObservableObject
{
public string {|MVVMTK0056:Name|}
{
get => field;
set => SetProperty(ref field, value);
}
}
""";

await CSharpAnalyzerWithLanguageVersionTest<UseObservablePropertyOnSemiAutoPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp14);
}
#endif

[TestMethod]
public async Task UseObservablePropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithModifiers_Warns()
{
Expand Down
Loading