diff --git a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs index 502f297e..8907683a 100644 --- a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs +++ b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs @@ -285,10 +285,11 @@ private static void ConvertToPartialProperty( attributeLists = attributeLists.Add(generatedDependencyPropertyAttributeList); } - // Append any attributes we want to forward (any attributes on the field, they've already been validated) + // Append any attributes we want to forward (any attributes on the field, they've already been validated). + // We also need to strip all trivia, to avoid accidentally carrying over XML docs from the field declaration. foreach (AttributeListSyntax fieldAttributeList in fieldDeclaration.AttributeLists) { - attributeLists = attributeLists.Add(fieldAttributeList.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.StaticKeyword)))); + attributeLists = attributeLists.Add(fieldAttributeList.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.StaticKeyword))).WithoutTrivia()); } // Get a new property that is partial and with semicolon token accessors diff --git a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs index b2a8860b..0d2cf9c0 100644 --- a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs +++ b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs @@ -955,4 +955,95 @@ public partial class MyControl : Control await test.RunAsync(); } + + [TestMethod] + public async Task MultipleProperties_WithXmlDocs_WithForwardedAttributes_TrimsAttributTrivia() + { + const string original = """ + using System; + using Windows.UI.Xaml; + + #nullable enable + + namespace MyApp; + + public partial class MyObject : DependencyObject + { + /// + /// Identifies the dependency property. + /// + public static readonly DependencyProperty ExpressionProperty = DependencyProperty.Register( + nameof(Expression), + typeof(string), + typeof(MyObject), + null); + + /// + /// Identifies the dependency property. + /// + [Test(42, "Test")] + public static readonly DependencyProperty InputProperty = DependencyProperty.Register( + nameof(Input), + typeof(object), + typeof(MyObject), + null); + + /// + /// Blah. + /// + public string? [|Expression|] + { + get => (string?)GetValue(ExpressionProperty); + set => SetValue(ExpressionProperty, value); + } + + /// + /// Blah. + /// + public object? [|Input|] + { + get => (object?)GetValue(InputProperty); + set => SetValue(InputProperty, value); + } + } + + public class TestAttribute(int X, string Y) : Attribute; + """; + + const string @fixed = """ + using System; + using CommunityToolkit.WinUI; + using Windows.UI.Xaml; + + #nullable enable + + namespace MyApp; + + public partial class MyObject : DependencyObject + { + /// + /// Blah. + /// + [GeneratedDependencyProperty] + public partial string? {|CS9248:Expression|} { get; set; } + + /// + /// Blah. + /// + [GeneratedDependencyProperty] + [static: Test(42, "Test")] + public partial object? {|CS9248:Input|} { get; set; } + } + + public class TestAttribute(int X, string Y) : Attribute; + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed + }; + + await test.RunAsync(); + } }