Skip to content

Commit

Permalink
Strip trivia from forwarded attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 27, 2024
1 parent 81e2ad3 commit 8a048a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Identifies the <seealso cref="Expression"/> dependency property.
/// </summary>
public static readonly DependencyProperty ExpressionProperty = DependencyProperty.Register(
nameof(Expression),
typeof(string),
typeof(MyObject),
null);
/// <summary>
/// Identifies the <seealso cref="Input" /> dependency property.
/// </summary>
[Test(42, "Test")]
public static readonly DependencyProperty InputProperty = DependencyProperty.Register(
nameof(Input),
typeof(object),
typeof(MyObject),
null);
/// <summary>
/// Blah.
/// </summary>
public string? [|Expression|]
{
get => (string?)GetValue(ExpressionProperty);
set => SetValue(ExpressionProperty, value);
}
/// <summary>
/// Blah.
/// </summary>
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
{
/// <summary>
/// Blah.
/// </summary>
[GeneratedDependencyProperty]
public partial string? {|CS9248:Expression|} { get; set; }
/// <summary>
/// Blah.
/// </summary>
[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();
}
}

0 comments on commit 8a048a8

Please sign in to comment.