Skip to content

Commit af626a6

Browse files
authored
Merge pull request #918 from CommunityToolkit/dev/observable-validator-trim-generator
Generate trim annotations for [NotifyDataErrorInfo] properties
2 parents b4bc12b + 2e3ca71 commit af626a6

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/Models/PropertyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
2020
/// <param name="IsOldPropertyValueDirectlyReferenced">Whether the old property value is being directly referenced.</param>
2121
/// <param name="IsReferenceTypeOrUnconstraindTypeParameter">Indicates whether the property is of a reference type or an unconstrained type parameter.</param>
2222
/// <param name="IncludeMemberNotNullOnSetAccessor">Indicates whether to include nullability annotations on the setter.</param>
23+
/// <param name="IncludeRequiresUnreferencedCodeOnSetAccessor">Indicates whether to annotate the setter as requiring unreferenced code.</param>
2324
/// <param name="ForwardedAttributes">The sequence of forwarded attributes for the generated property.</param>
2425
internal sealed record PropertyInfo(
2526
string TypeNameWithNullabilityAnnotations,
@@ -33,4 +34,5 @@ internal sealed record PropertyInfo(
3334
bool IsOldPropertyValueDirectlyReferenced,
3435
bool IsReferenceTypeOrUnconstraindTypeParameter,
3536
bool IncludeMemberNotNullOnSetAccessor,
37+
bool IncludeRequiresUnreferencedCodeOnSetAccessor,
3638
EquatableArray<AttributeInfo> ForwardedAttributes);

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ public static bool TryGetInfo(
297297

298298
token.ThrowIfCancellationRequested();
299299

300+
// We should generate [RequiresUnreferencedCode] on the setter if [NotifyDataErrorInfo] was used and the attribute is available
301+
bool includeRequiresUnreferencedCodeOnSetAccessor =
302+
notifyDataErrorInfo &&
303+
semanticModel.Compilation.HasAccessibleTypeWithMetadataName("System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute");
304+
305+
token.ThrowIfCancellationRequested();
306+
300307
// Prepare the effective property changing/changed names. For the property changing names,
301308
// there are two possible cases: if the mode is disabled, then there are no names to report
302309
// at all. If the mode is enabled, then the list is just the same as for property changed.
@@ -321,6 +328,7 @@ public static bool TryGetInfo(
321328
isOldPropertyValueDirectlyReferenced,
322329
isReferenceTypeOrUnconstraindTypeParameter,
323330
includeMemberNotNullOnSetAccessor,
331+
includeRequiresUnreferencedCodeOnSetAccessor,
324332
forwardedAttributes.ToImmutable());
325333

326334
diagnostics = builder.ToImmutable();
@@ -1044,6 +1052,19 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
10441052
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(propertyInfo.FieldName)))))));
10451053
}
10461054

1055+
// Add the [RequiresUnreferencedCode] attribute if needed:
1056+
//
1057+
// [RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
1058+
// <SET_ACCESSOR>
1059+
if (propertyInfo.IncludeRequiresUnreferencedCodeOnSetAccessor)
1060+
{
1061+
setAccessor = setAccessor.AddAttributeLists(
1062+
AttributeList(SingletonSeparatedList(
1063+
Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode"))
1064+
.AddArgumentListArguments(
1065+
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("The type of the current instance cannot be statically discovered.")))))));
1066+
}
1067+
10471068
// Construct the generated property as follows:
10481069
//
10491070
// /// <inheritdoc cref="<FIELD_NAME>"/>

0 commit comments

Comments
 (0)