-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Warning AVLN2208: Item container in the data template #18132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
494f9d9
Implement AVLN2208 diagnostic - ItemContainerInsideTemplate
maxkatz6 e9c4994
Add AVLN2208 tests
maxkatz6 1fc3e86
Enable AVLN2208 as an error in the repository globally
maxkatz6 3a054a3
Fix invalid ListBoxItem inside of DataTemplate in devtools
maxkatz6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...l.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataTemplateWarningsTransformer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; | ||
using XamlX; | ||
using XamlX.Ast; | ||
using XamlX.Transform; | ||
using XamlX.TypeSystem; | ||
|
||
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; | ||
|
||
#if !XAMLX_INTERNAL | ||
public | ||
#endif | ||
class AvaloniaXamlIlDataTemplateWarningsTransformer : IXamlAstTransformer | ||
{ | ||
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) | ||
{ | ||
var avaloniaTypes = context.GetAvaloniaTypes(); | ||
var contentControl = context.GetAvaloniaTypes().ContentControl; | ||
|
||
// This transformers only looks for ContentControl delivered objects inside of DataTemplate | ||
if ((node is not XamlAstObjectNode objectNode) | ||
|| !contentControl.IsAssignableFrom(objectNode.Type.GetClrType()) | ||
|| context.ParentNodes().FirstOrDefault() is not XamlAstObjectNode parentNode | ||
|| !avaloniaTypes.IDataTemplate.IsAssignableFrom(parentNode.Type.GetClrType())) | ||
{ | ||
return node; | ||
} | ||
|
||
// And only inside of ItemTemplate or DataTemplates property value. | ||
if (context.ParentNodes().OfType<XamlAstXamlPropertyValueNode>().FirstOrDefault() is not { } valueNode | ||
|| valueNode.Property.GetClrProperty() is not { } clrProperty | ||
|| !((clrProperty.Name == "ItemTemplate" && clrProperty.DeclaringType == avaloniaTypes.ItemsControl) | ||
|| (clrProperty.Name == "DataTemplates" && clrProperty.DeclaringType == avaloniaTypes.Control))) | ||
{ | ||
return node; | ||
} | ||
|
||
// And only inside of ItemsControl | ||
if (context.ParentNodes().SkipWhile(p => p != valueNode) | ||
.OfType<XamlAstObjectNode>().FirstOrDefault() is not { } itemsControlNode | ||
|| !avaloniaTypes.ItemsControl.IsAssignableFrom(itemsControlNode.Type.GetClrType())) | ||
{ | ||
return node; | ||
} | ||
|
||
// Avalonia doesn't have any reliable way to determine container type from the API. | ||
if (GetKnownItemContainerTypeFullName(itemsControlNode.Type.GetClrType()) is not { } knownItemContainerTypeName | ||
|| itemsControlNode.Type.GetClrType().Assembly?.FindType(knownItemContainerTypeName) is not { } knownItemContainerType) | ||
{ | ||
return node; | ||
} | ||
|
||
if (knownItemContainerType.IsAssignableFrom(objectNode.Type.GetClrType())) | ||
{ | ||
context.ReportDiagnostic(new XamlDiagnostic( | ||
AvaloniaXamlDiagnosticCodes.ItemContainerInsideTemplate, | ||
XamlDiagnosticSeverity.Warning, | ||
$"Unexpected '{knownItemContainerType.Name}' inside of '{itemsControlNode.Type.GetClrType().Name}.{clrProperty.Name}'. " | ||
+ $"'{itemsControlNode.Type.GetClrType().Name}.{clrProperty.Name}' defines template of the container content, not the container itself.", node)); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
private static string? GetKnownItemContainerTypeFullName(IXamlType itemsControlType) => itemsControlType.FullName switch | ||
{ | ||
"Avalonia.Controls.ListBox" => "Avalonia.Controls.ListBoxItem", | ||
"Avalonia.Controls.ComboBox" => "Avalonia.Controls.ComboBoxItem", | ||
"Avalonia.Controls.Menu" => "Avalonia.Controls.MenuItem", | ||
"Avalonia.Controls.MenuItem" => "Avalonia.Controls.MenuItem", | ||
"Avalonia.Controls.Primitives.TabStrip" => "Avalonia.Controls.Primitives.TabStripItem", | ||
"Avalonia.Controls.TabControl" => "Avalonia.Controls.TabItem", | ||
"Avalonia.Controls.TreeView" => "Avalonia.Controls.TreeViewItem", | ||
_ => null | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an attribute for this at some point? It could be useful. (Not as part of this PR of course.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about it.
And there was a PR for that #13104
The problem is that such attribute won't be followed by third party controls, and it gives a little of benefit otherwise, for built-in controls.
Let's add it to next api review, I suppose. If we will find a better idea.