Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #451 - Add pragma restore directive for the CS0108 warning #773

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -20,6 +20,7 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
/// <param name="IsOldPropertyValueDirectlyReferenced">Whether the old property value is being directly referenced.</param>
/// <param name="IsReferenceTypeOrUnconstraindTypeParameter">Indicates whether the property is of a reference type or an unconstrained type parameter.</param>
/// <param name="IncludeMemberNotNullOnSetAccessor">Indicates whether to include nullability annotations on the setter.</param>
/// <param name="hidesInheritedProperty">Indicates whether the generated property should hide an inherited property declaration.</param>
/// <param name="ForwardedAttributes">The sequence of forwarded attributes for the generated property.</param>
internal sealed record PropertyInfo(
string TypeNameWithNullabilityAnnotations,
Expand All @@ -33,4 +34,5 @@ internal sealed record PropertyInfo(
bool IsOldPropertyValueDirectlyReferenced,
bool IsReferenceTypeOrUnconstraindTypeParameter,
bool IncludeMemberNotNullOnSetAccessor,
bool hidesInheritedProperty,
EquatableArray<AttributeInfo> ForwardedAttributes);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
Expand Down Expand Up @@ -118,6 +119,7 @@ public static bool TryGetInfo(
bool hasOrInheritsClassLevelNotifyPropertyChangedRecipients = false;
bool hasOrInheritsClassLevelNotifyDataErrorInfo = false;
bool hasAnyValidationAttributes = false;
bool hidesInheritedProperty = false;
bool isOldPropertyValueDirectlyReferenced = IsOldPropertyValueDirectlyReferenced(fieldSymbol, propertyName);

token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -194,6 +196,15 @@ public static bool TryGetInfo(
forwardedAttributes.Add(AttributeInfo.Create(attributeData));
}

// Check if the generated property should hide an inherited property declaration
if (attributeData.AttributeClass?.HasFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") == true)
{
if (Convert.ToBoolean(attributeData.ConstructorArguments[0].Value) == true)
{
hidesInheritedProperty = true;
}
}

// Also track the current attribute for forwarding if it is of any of the following types:
// - Display attributes (System.ComponentModel.DataAnnotations.DisplayAttribute)
// - UI hint attributes(System.ComponentModel.DataAnnotations.UIHintAttribute)
Expand Down Expand Up @@ -308,6 +319,7 @@ public static bool TryGetInfo(
isOldPropertyValueDirectlyReferenced,
isReferenceTypeOrUnconstraindTypeParameter,
includeMemberNotNullOnSetAccessor,
hidesInheritedProperty,
forwardedAttributes.ToImmutable());

diagnostics = builder.ToImmutable();
Expand Down Expand Up @@ -1016,11 +1028,18 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <FORWARDED_ATTRIBUTES>
// public <FIELD_TYPE><NULLABLE_ANNOTATION?> <PROPERTY_NAME>
// public <NEW_KEYWORD> <FIELD_TYPE><NULLABLE_ANNOTATION?> <PROPERTY_NAME>
// {
// get => <FIELD_NAME>;
// <SET_ACCESSOR>
// }

List<SyntaxToken> propertyDeclarationModifier = new() { Token(SyntaxKind.PublicKeyword) };
if (propertyInfo.hidesInheritedProperty)
{
propertyDeclarationModifier.Add(Token(SyntaxKind.NewKeyword));
}

return
PropertyDeclaration(propertyType, Identifier(propertyInfo.PropertyName))
.AddAttributeLists(
Expand All @@ -1032,7 +1051,7 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>")), SyntaxKind.OpenBracketToken, TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddAttributeLists(forwardedAttributes.ToArray())
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddModifiers(propertyDeclarationModifier.ToArray())
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithExpressionBody(ArrowExpressionClause(getterFieldExpression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ public CompilationUnitSyntax GetCompilationUnit(
//
// <auto-generated/>
// #pragma warning disable
// #pragma warning restore CS0108 // Member hides inherited member; missing new keyword
//
// #nullable enable

SeparatedSyntaxList<ExpressionSyntax> pragmaWarningCodesToPreserve = new SeparatedSyntaxList<ExpressionSyntax>()
.Add(IdentifierName(Identifier(default, SyntaxKind.IdentifierName, "CS0108", "CS0108", TriviaList(Comment("// Member hides inherited member; missing new keyword")))));

SyntaxTriviaList syntaxTriviaList = TriviaList(
Comment("// <auto-generated/>"),
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true)),
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), pragmaWarningCodesToPreserve, true)),
Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)));

if (Namespace is "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ namespace CommunityToolkit.Mvvm.ComponentModel;
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class ObservablePropertyAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ObservablePropertyAttribute"/> class.
/// </summary>
/// <param name="hidesInheritedProperty">
/// Specifies if the generated property will hide an inherited property declaration.
/// </param>
public ObservablePropertyAttribute(bool hidesInheritedProperty = false)
{
Hidesinheritedproperty = hidesInheritedProperty;
}

/// <summary>
/// Specifies if the generated property will hide an inherited property declaration.
/// </summary>
public bool Hidesinheritedproperty { get; }
}