From 74c2a6f1c3c7eed1a065ad9ee95816bfdb3590aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Tar=C4=B1k=20G=C3=BCnayd=C4=B1n?= Date: Sun, 4 Dec 2022 15:29:34 +0300 Subject: [PATCH 1/2] Check for Control base type since IControl interface is removed --- .../Generator/XamlXViewResolver.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs b/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs index 334574a..1a315ee 100644 --- a/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs +++ b/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs @@ -6,6 +6,7 @@ using XamlX; using XamlX.Ast; using XamlX.Parsers; +using XamlX.TypeSystem; namespace Avalonia.NameGenerator.Generator; @@ -56,13 +57,26 @@ public ResolvedView ResolveView(string xaml) } } + private bool IsAvaloniaControl(IXamlType clrType) + { + // Check for the base type since IControl interface is removed. + // https://github.com/AvaloniaUI/Avalonia/pull/9553 + if (clrType.FullName == "Avalonia.Controls.Control") + return true; + + if (clrType.BaseType != null) + return IsAvaloniaControl(clrType.BaseType); + + return false; + } + IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node) { if (node is not XamlAstObjectNode objectNode) return node; var clrType = objectNode.Type.GetClrType(); - var isAvaloniaControl = clrType + var isAvaloniaControl = IsAvaloniaControl(clrType) || clrType .Interfaces .Any(abstraction => abstraction.IsInterface && abstraction.FullName == "Avalonia.Controls.IControl"); @@ -102,4 +116,4 @@ IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node) void IXamlAstVisitor.Push(IXamlAstNode node) { } void IXamlAstVisitor.Pop() { } -} \ No newline at end of file +} From c546e05411eefbc58ef7bed116a6eb2b87a33c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Tar=C4=B1k=20G=C3=BCnayd=C4=B1n?= Date: Sun, 4 Dec 2022 15:48:57 +0300 Subject: [PATCH 2/2] Apply base type check fix to name resolver too. --- .../Generator/ResolverExtensions.cs | 33 +++++++++++++++++++ .../Generator/XamlXNameResolver.cs | 6 +--- .../Generator/XamlXViewResolver.cs | 21 ++---------- 3 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 src/Avalonia.NameGenerator/Generator/ResolverExtensions.cs diff --git a/src/Avalonia.NameGenerator/Generator/ResolverExtensions.cs b/src/Avalonia.NameGenerator/Generator/ResolverExtensions.cs new file mode 100644 index 0000000..529e7b0 --- /dev/null +++ b/src/Avalonia.NameGenerator/Generator/ResolverExtensions.cs @@ -0,0 +1,33 @@ +using System.Linq; +using XamlX.TypeSystem; + +namespace Avalonia.NameGenerator.Generator; + +internal static class ResolverExtensions +{ + public static bool IsAvaloniaControl(this IXamlType clrType) + { + return clrType.HasControlBaseType() || clrType.HasIControlInterface(); + } + + private static bool HasControlBaseType(this IXamlType clrType) + { + // Check for the base type since IControl interface is removed. + // https://github.com/AvaloniaUI/Avalonia/pull/9553 + if (clrType.FullName == "Avalonia.Controls.Control") + return true; + + if (clrType.BaseType != null) + return IsAvaloniaControl(clrType.BaseType); + + return false; + } + + private static bool HasIControlInterface(this IXamlType clrType) + { + return clrType + .Interfaces + .Any(abstraction => abstraction.IsInterface && + abstraction.FullName == "Avalonia.Controls.IControl"); + } +} \ No newline at end of file diff --git a/src/Avalonia.NameGenerator/Generator/XamlXNameResolver.cs b/src/Avalonia.NameGenerator/Generator/XamlXNameResolver.cs index 746a81f..e2b4fa2 100644 --- a/src/Avalonia.NameGenerator/Generator/XamlXNameResolver.cs +++ b/src/Avalonia.NameGenerator/Generator/XamlXNameResolver.cs @@ -31,12 +31,8 @@ IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node) return node; var clrType = objectNode.Type.GetClrType(); - var isAvaloniaControl = clrType - .Interfaces - .Any(abstraction => abstraction.IsInterface && - abstraction.FullName == "Avalonia.Controls.IControl"); - if (!isAvaloniaControl) + if (!clrType.IsAvaloniaControl()) return node; foreach (var child in objectNode.Children) diff --git a/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs b/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs index 1a315ee..2e79934 100644 --- a/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs +++ b/src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs @@ -56,32 +56,15 @@ public ResolvedView ResolveView(string xaml) return null; } } - - private bool IsAvaloniaControl(IXamlType clrType) - { - // Check for the base type since IControl interface is removed. - // https://github.com/AvaloniaUI/Avalonia/pull/9553 - if (clrType.FullName == "Avalonia.Controls.Control") - return true; - - if (clrType.BaseType != null) - return IsAvaloniaControl(clrType.BaseType); - - return false; - } - + IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node) { if (node is not XamlAstObjectNode objectNode) return node; var clrType = objectNode.Type.GetClrType(); - var isAvaloniaControl = IsAvaloniaControl(clrType) || clrType - .Interfaces - .Any(abstraction => abstraction.IsInterface && - abstraction.FullName == "Avalonia.Controls.IControl"); - if (!isAvaloniaControl) + if (!clrType.IsAvaloniaControl()) return node; foreach (var child in objectNode.Children)