Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
11d15c9
Add blank 'Roslyn4110' project
Sergio0694 Oct 23, 2024
e27359f
Fix naming conventions for all Roslyn projects
Sergio0694 Oct 23, 2024
a67a31d
Add 'global.json' file
Sergio0694 Oct 23, 2024
9c9061d
Update .targets for Roslyn setup
Sergio0694 Oct 23, 2024
f4fdb27
Pack new source generator in MVVM Toolkit
Sergio0694 Oct 23, 2024
c8e794b
Add logic to match partial properties
Sergio0694 Oct 23, 2024
76264f7
Update the generator to also work on properties
Sergio0694 Oct 23, 2024
3df53f3
Add 'RequiresCSharpLanguageVersionPreviewAnalyzer'
Sergio0694 Oct 23, 2024
fff5f91
Suppress warnings about removed rules
Sergio0694 Oct 23, 2024
41dc834
Add blank test project for new generator
Sergio0694 Oct 23, 2024
34a7c6f
Update '[ObservableProperty]' attribute
Sergio0694 Oct 23, 2024
bbd59ec
Add unit tests for new analyzer
Sergio0694 Oct 23, 2024
f970ba6
Move forwarded attributes gathering to helper
Sergio0694 Oct 24, 2024
e5b2802
Gather accessibility information from nodes
Sergio0694 Oct 24, 2024
17263fe
Generalizing the generated accessibility modifiers
Sergio0694 Oct 24, 2024
5f2ffcf
Don't emit an error for collisions for properties
Sergio0694 Oct 24, 2024
46d03e8
Update generation to account for properties
Sergio0694 Oct 24, 2024
52ad254
Fix a generator crash when used on properties
Sergio0694 Oct 24, 2024
8ca2f3c
Omit implicit accessibility modifiers
Sergio0694 Oct 24, 2024
db221ed
Update the targets of additional attributes
Sergio0694 Oct 24, 2024
f9f7771
Fix handling notify data error info
Sergio0694 Oct 24, 2024
5d37b73
Fix nullability for generated partial properties
Sergio0694 Oct 24, 2024
aabcd15
Add initial codegen tests for partial properties
Sergio0694 Oct 24, 2024
62c06f1
Add 'UseObservablePropertyOnPartialPropertyAnalyzer'
Sergio0694 Oct 24, 2024
6342b83
Add unit tests for new analyzer
Sergio0694 Oct 24, 2024
efa90a3
Add 'InvalidPropertyLevelObservablePropertyAttributeAnalyzer'
Sergio0694 Oct 24, 2024
767c05b
Add 'UnsupportedRoslynVersionForPartialPropertyAnalyzer'
Sergio0694 Oct 24, 2024
eea59fd
Fix handling of 'private protected' accessors
Sergio0694 Oct 24, 2024
162bab5
Add unit tests for invalid property declarations
Sergio0694 Oct 24, 2024
5e02b9a
Add 'UsePartialPropertyForObservablePropertyCodeFixer'
Sergio0694 Oct 25, 2024
9ba8f27
Add new helper for testing code fixers
Sergio0694 Oct 25, 2024
b98a658
Add unit test for new code fixer
Sergio0694 Oct 25, 2024
8cdebe0
Fix attributes handling, add unit tests
Sergio0694 Oct 25, 2024
e6a5c09
Add unit tests for comments in code fixer
Sergio0694 Oct 25, 2024
00ebe42
Support updating field references as well
Sergio0694 Oct 25, 2024
3453824
Improve messages in diagnostics when emitted on properties
Sergio0694 Oct 25, 2024
7b402b6
Add workaround for older Roslyn versions
Sergio0694 Oct 25, 2024
ca5d9dd
Fix a unit test on .NET Framework
Sergio0694 Oct 25, 2024
648788c
Fix last remaining unit test, and test regex
Sergio0694 Oct 25, 2024
9c9ff44
Set version.json to 8.4.0
Sergio0694 Oct 25, 2024
f75edab
Don't suggest partial properties for static fields
Sergio0694 Oct 25, 2024
0a81f27
Support field initializers in code fixer
Sergio0694 Oct 25, 2024
a7840b2
Don't run analyzers on generated code
Sergio0694 Oct 26, 2024
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
Prev Previous commit
Next Next commit
Add 'RequiresCSharpLanguageVersionPreviewAnalyzer'
  • Loading branch information
Sergio0694 committed Oct 23, 2024
commit 3df53f345436d14bef8b1e39346b4aa8890123d2
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ MVVMTK0039 | CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator | Warn
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
MVVMTK0040 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/errors/mvvmtk0040

## Release 8.4.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
MVVMTK0041 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/errors/mvvmtk0041
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\ClassUsingAttributeInsteadOfInheritanceAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\FieldWithOrphanedDependentObservablePropertyAttributesAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\FieldReferenceForObservablePropertyFieldAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\RequiresCSharpLanguageVersionPreviewAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\UnsupportedCSharpLanguageVersionAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Suppressors\RelayCommandAttributeWithFieldOrPropertyTargetDiagnosticSuppressor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Suppressors\ObservablePropertyAttributeWithSupportedTargetDiagnosticSuppressor.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if ROSLYN_4_11_0_OR_GREATER

using System.Collections.Immutable;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;

namespace CommunityToolkit.Mvvm.SourceGenerators;

/// <summary>
/// A diagnostic analyzer that generates errors when a property using [ObservableProperty] on a partial property is in a project with the C# language version not set to preview.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class RequiresCSharpLanguageVersionPreviewAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [CSharpLanguageVersionIsNotPreviewForObservableProperty];

/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();

context.RegisterCompilationStartAction(static context =>
{
// If the language version is set to preview, we'll never emit diagnostics
if (context.Compilation.IsLanguageVersionPreview())
{
return;
}

// Get the symbol for [ObservableProperty]
if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is not INamedTypeSymbol observablePropertySymbol)
{
return;
}

context.RegisterSymbolAction(context =>
{
// We only want to target partial property definitions (also include non-partial ones for diagnostics)
if (context.Symbol is not IPropertySymbol { PartialDefinitionPart: null })
{
return;
}

// If the property is using [ObservableProperty], emit the diagnostic
if (context.Symbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
{
context.ReportDiagnostic(Diagnostic.Create(
CSharpLanguageVersionIsNotPreviewForObservableProperty,
observablePropertyAttribute.GetLocation(),
context.Symbol));
}
}, SymbolKind.Property);
});
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The backing fields of auto-properties cannot be annotated with [ObservableProperty] (the attribute can only be used directly on fields, and the generator will then handle generating the corresponding property).",
helpLinkUri: "https://aka.ms/mvvmtoolkit/errors/mvvmtk0040");

/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a CanvasEffect property with invalid accessors.
/// <para>
/// Format: <c>"Using [ObservableProperty] on partial properties requires the C# language version to be set to 'preview', as support for the 'field' keyword is needed by the source generators to emit valid code (add <LangVersion>preview</LangVersion> to your .csproj/.props file)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor CSharpLanguageVersionIsNotPreviewForObservableProperty = new(
id: "MVVMTK0041",
title: "C# language version is not 'preview'",
messageFormat: """Using [ObservableProperty] on partial properties requires the C# language version to be set to 'preview', as support for the 'field' keyword is needed by the source generators to emit valid code (add <LangVersion>preview</LangVersion> to your .csproj/.props file)""",
category: typeof(ObservablePropertyGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The C# language version must be set to 'preview' when using [ObservableProperty] on partial properties for the source generators to emit valid code (the <LangVersion>preview</LangVersion> option must be set in the .csproj/.props file).",
helpLinkUri: "https://aka.ms/mvvmtoolkit/errors/mvvmtk0041");
}