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
Update generation to account for properties
  • Loading branch information
Sergio0694 committed Oct 24, 2024
commit 46d03e83b439decc7903c8d4f8f4b5bbd654a6aa
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

using CommunityToolkit.Mvvm.SourceGenerators.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;

/// <summary>
/// A model representing an generated property
/// </summary>
/// <param name="AnnotatedMemberKind">The syntax kind of the annotated member that triggered this property generation.</param>
/// <param name="TypeNameWithNullabilityAnnotations">The type name for the generated property, including nullability annotations.</param>
/// <param name="FieldName">The field name.</param>
/// <param name="PropertyName">The generated property name.</param>
Expand All @@ -27,6 +29,7 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
/// <param name="IncludeRequiresUnreferencedCodeOnSetAccessor">Indicates whether to annotate the setter as requiring unreferenced code.</param>
/// <param name="ForwardedAttributes">The sequence of forwarded attributes for the generated property.</param>
internal sealed record PropertyInfo(
SyntaxKind AnnotatedMemberKind,
string TypeNameWithNullabilityAnnotations,
string FieldName,
string PropertyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public static bool TryGetInfo(
token.ThrowIfCancellationRequested();

propertyInfo = new PropertyInfo(
memberSyntax.Kind(),
typeNameWithNullabilityAnnotations,
fieldName,
propertyName,
Expand Down Expand Up @@ -1092,11 +1093,17 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
ExpressionSyntax getterFieldExpression;
ExpressionSyntax setterFieldExpression;

// In case the backing field is exactly named "value", we need to add the "this." prefix to ensure that comparisons and assignments
// with it in the generated setter body are executed correctly and without conflicts with the implicit value parameter.
if (propertyInfo.FieldName == "value")
// If the annotated member is a partial property, we always use the 'field' keyword
if (propertyInfo.AnnotatedMemberKind is SyntaxKind.PropertyDeclaration)
{
// We only need to add "this." when referencing the field in the setter (getter and XML docs are not ambiguous)
getterFieldIdentifierName = "field";
getterFieldExpression = setterFieldExpression = IdentifierName(getterFieldIdentifierName);
}
else if (propertyInfo.FieldName == "value")
{
// In case the backing field is exactly named "value", we need to add the "this." prefix to ensure that comparisons and assignments
// with it in the generated setter body are executed correctly and without conflicts with the implicit value parameter. We only need
// to add "this." when referencing the field in the setter (getter and XML docs are not ambiguous)
getterFieldIdentifierName = "value";
getterFieldExpression = IdentifierName(getterFieldIdentifierName);
setterFieldExpression = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), (IdentifierNameSyntax)getterFieldExpression);
Expand All @@ -1116,6 +1123,13 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
getterFieldExpression = setterFieldExpression = IdentifierName(getterFieldIdentifierName);
}

// Prepare the XML docs:
// - For partial properties, always just inherit from the partial declaration
// - For fields, inherit from them
string xmlSummary = propertyInfo.AnnotatedMemberKind is SyntaxKind.PropertyDeclaration
? "/// <inheritdoc/>"
: $"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>";

if (propertyInfo.NotifyPropertyChangedRecipients || propertyInfo.IsOldPropertyValueDirectlyReferenced)
{
// Store the old value for later. This code generates a statement as follows:
Expand Down Expand Up @@ -1336,13 +1350,18 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
// Also add any forwarded attributes
setAccessor = setAccessor.AddAttributeLists(forwardedSetAccessorAttributes);

// Prepare the modifiers for the property
SyntaxTokenList propertyModifiers = propertyInfo.AnnotatedMemberKind is SyntaxKind.PropertyDeclaration
? propertyInfo.PropertyAccessibility.ToSyntaxTokenList().Add(Token(SyntaxKind.PartialKeyword))
: propertyInfo.PropertyAccessibility.ToSyntaxTokenList();

// Construct the generated property as follows:
//
// /// <inheritdoc cref="<FIELD_NAME>"/>
// <XML_SUMMARY>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <FORWARDED_ATTRIBUTES>
// <PROPERTY_ACCESSIBILITY> <FIELD_TYPE><NULLABLE_ANNOTATION?> <PROPERTY_NAME>
// <PROPERTY_MODIFIERS> <FIELD_TYPE><NULLABLE_ANNOTATION?> <PROPERTY_NAME>
// {
// <FORWARDED_ATTRIBUTES>
// <GETTER_ACCESSIBILITY> get => <FIELD_NAME>;
Expand All @@ -1356,10 +1375,10 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservablePropertyGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservablePropertyGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>")), SyntaxKind.OpenBracketToken, TriviaList())),
.WithOpenBracketToken(Token(TriviaList(Comment(xmlSummary)), SyntaxKind.OpenBracketToken, TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddAttributeLists(forwardedPropertyAttributes)
.WithModifiers(propertyInfo.PropertyAccessibility.ToSyntaxTokenList())
.WithModifiers(propertyModifiers)
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithModifiers(propertyInfo.GetterAccessibility.ToSyntaxTokenList())
Expand Down