Skip to content

Commit

Permalink
Add 'partial' for nested types too, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 28, 2024
1 parent e37d7dc commit 21f3684
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,17 @@ private static void ConvertToPartialProperty(
// Also remove the field declaration (it'll be generated now)
syntaxEditor.RemoveNode(fieldDeclaration);

// Find the parent type for the property
TypeDeclarationSyntax typeDeclaration = propertyDeclaration.FirstAncestorOrSelf<TypeDeclarationSyntax>()!;

// Make sure it's partial (we create the updated node in the function to preserve the updated property declaration).
// If we created it separately and replaced it, the whole tree would also be replaced, and we'd lose the new property.
if (!typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
// Find the parent type for the property (we need to do this for all ancestor types, as the type might be bested)
for (TypeDeclarationSyntax? typeDeclaration = propertyDeclaration.FirstAncestor<TypeDeclarationSyntax>();
typeDeclaration is not null;
typeDeclaration = typeDeclaration.FirstAncestor<TypeDeclarationSyntax>())
{
syntaxEditor.ReplaceNode(typeDeclaration, static (node, generator) => generator.WithModifiers(node, generator.GetModifiers(node).WithPartial(true)));
// Make sure it's partial (we create the updated node in the function to preserve the updated property declaration).
// If we created it separately and replaced it, the whole tree would also be replaced, and we'd lose the new property.
if (!typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
{
syntaxEditor.ReplaceNode(typeDeclaration, static (node, generator) => generator.WithModifiers(node, generator.GetModifiers(node).WithPartial(true)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1254,4 +1254,60 @@ public double? VerticalOffset

await test.RunAsync();
}

[TestMethod]
public async Task SimpleProperty_NestedType_AddsAllRequiredPartialModifiers()
{
const string original = """
using Windows.UI.Xaml;
#nullable enable
namespace MyApp;
public class MyObject : DependencyObject
{
public class MyNestedObject : DependencyObject
{
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
name: "Name",
propertyType: typeof(string),
ownerType: typeof(MyNestedObject),
typeMetadata: null);
public string? [|Name|]
{
get => (string?)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
}
}
""";

const string @fixed = """
using CommunityToolkit.WinUI;
using Windows.UI.Xaml;
#nullable enable
namespace MyApp;
public partial class MyObject : DependencyObject
{
public partial class MyNestedObject : DependencyObject
{
[GeneratedDependencyProperty]
public partial string? {|CS9248:Name|} { get; set; }
}
}
""";

CSharpCodeFixTest test = new(LanguageVersion.Preview)
{
TestCode = original,
FixedCode = @fixed
};

await test.RunAsync();
}
}

0 comments on commit 21f3684

Please sign in to comment.