Skip to content

Commit 21f3684

Browse files
committed
Add 'partial' for nested types too, add tests
1 parent e37d7dc commit 21f3684

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,17 @@ private static void ConvertToPartialProperty(
319319
// Also remove the field declaration (it'll be generated now)
320320
syntaxEditor.RemoveNode(fieldDeclaration);
321321

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

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,4 +1254,60 @@ public double? VerticalOffset
12541254

12551255
await test.RunAsync();
12561256
}
1257+
1258+
[TestMethod]
1259+
public async Task SimpleProperty_NestedType_AddsAllRequiredPartialModifiers()
1260+
{
1261+
const string original = """
1262+
using Windows.UI.Xaml;
1263+
1264+
#nullable enable
1265+
1266+
namespace MyApp;
1267+
1268+
public class MyObject : DependencyObject
1269+
{
1270+
public class MyNestedObject : DependencyObject
1271+
{
1272+
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
1273+
name: "Name",
1274+
propertyType: typeof(string),
1275+
ownerType: typeof(MyNestedObject),
1276+
typeMetadata: null);
1277+
1278+
public string? [|Name|]
1279+
{
1280+
get => (string?)GetValue(NameProperty);
1281+
set => SetValue(NameProperty, value);
1282+
}
1283+
}
1284+
}
1285+
""";
1286+
1287+
const string @fixed = """
1288+
using CommunityToolkit.WinUI;
1289+
using Windows.UI.Xaml;
1290+
1291+
#nullable enable
1292+
1293+
namespace MyApp;
1294+
1295+
public partial class MyObject : DependencyObject
1296+
{
1297+
public partial class MyNestedObject : DependencyObject
1298+
{
1299+
[GeneratedDependencyProperty]
1300+
public partial string? {|CS9248:Name|} { get; set; }
1301+
}
1302+
}
1303+
""";
1304+
1305+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
1306+
{
1307+
TestCode = original,
1308+
FixedCode = @fixed
1309+
};
1310+
1311+
await test.RunAsync();
1312+
}
12571313
}

0 commit comments

Comments
 (0)