Skip to content

Commit 8a048a8

Browse files
committed
Strip trivia from forwarded attributes
1 parent 81e2ad3 commit 8a048a8

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ private static void ConvertToPartialProperty(
285285
attributeLists = attributeLists.Add(generatedDependencyPropertyAttributeList);
286286
}
287287

288-
// Append any attributes we want to forward (any attributes on the field, they've already been validated)
288+
// Append any attributes we want to forward (any attributes on the field, they've already been validated).
289+
// We also need to strip all trivia, to avoid accidentally carrying over XML docs from the field declaration.
289290
foreach (AttributeListSyntax fieldAttributeList in fieldDeclaration.AttributeLists)
290291
{
291-
attributeLists = attributeLists.Add(fieldAttributeList.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.StaticKeyword))));
292+
attributeLists = attributeLists.Add(fieldAttributeList.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.StaticKeyword))).WithoutTrivia());
292293
}
293294

294295
// Get a new property that is partial and with semicolon token accessors

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,95 @@ public partial class MyControl : Control
955955

956956
await test.RunAsync();
957957
}
958+
959+
[TestMethod]
960+
public async Task MultipleProperties_WithXmlDocs_WithForwardedAttributes_TrimsAttributTrivia()
961+
{
962+
const string original = """
963+
using System;
964+
using Windows.UI.Xaml;
965+
966+
#nullable enable
967+
968+
namespace MyApp;
969+
970+
public partial class MyObject : DependencyObject
971+
{
972+
/// <summary>
973+
/// Identifies the <seealso cref="Expression"/> dependency property.
974+
/// </summary>
975+
public static readonly DependencyProperty ExpressionProperty = DependencyProperty.Register(
976+
nameof(Expression),
977+
typeof(string),
978+
typeof(MyObject),
979+
null);
980+
981+
/// <summary>
982+
/// Identifies the <seealso cref="Input" /> dependency property.
983+
/// </summary>
984+
[Test(42, "Test")]
985+
public static readonly DependencyProperty InputProperty = DependencyProperty.Register(
986+
nameof(Input),
987+
typeof(object),
988+
typeof(MyObject),
989+
null);
990+
991+
/// <summary>
992+
/// Blah.
993+
/// </summary>
994+
public string? [|Expression|]
995+
{
996+
get => (string?)GetValue(ExpressionProperty);
997+
set => SetValue(ExpressionProperty, value);
998+
}
999+
1000+
/// <summary>
1001+
/// Blah.
1002+
/// </summary>
1003+
public object? [|Input|]
1004+
{
1005+
get => (object?)GetValue(InputProperty);
1006+
set => SetValue(InputProperty, value);
1007+
}
1008+
}
1009+
1010+
public class TestAttribute(int X, string Y) : Attribute;
1011+
""";
1012+
1013+
const string @fixed = """
1014+
using System;
1015+
using CommunityToolkit.WinUI;
1016+
using Windows.UI.Xaml;
1017+
1018+
#nullable enable
1019+
1020+
namespace MyApp;
1021+
1022+
public partial class MyObject : DependencyObject
1023+
{
1024+
/// <summary>
1025+
/// Blah.
1026+
/// </summary>
1027+
[GeneratedDependencyProperty]
1028+
public partial string? {|CS9248:Expression|} { get; set; }
1029+
1030+
/// <summary>
1031+
/// Blah.
1032+
/// </summary>
1033+
[GeneratedDependencyProperty]
1034+
[static: Test(42, "Test")]
1035+
public partial object? {|CS9248:Input|} { get; set; }
1036+
}
1037+
1038+
public class TestAttribute(int X, string Y) : Attribute;
1039+
""";
1040+
1041+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
1042+
{
1043+
TestCode = original,
1044+
FixedCode = @fixed
1045+
};
1046+
1047+
await test.RunAsync();
1048+
}
9581049
}

0 commit comments

Comments
 (0)