Skip to content

Commit

Permalink
fix(xamlreader): Ensure adding properties on an explicit property rep…
Browse files Browse the repository at this point in the history
…orts an error

(cherry picked from commit c038beb)
  • Loading branch information
jeromelaban authored and mergify[bot] committed Mar 10, 2023
1 parent facdc38 commit 83e5279
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/SourceGenerators/System.Xaml/System.Xaml/XamlXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,15 +745,24 @@ IEnumerable<XamlXmlNodeInfo> ReadMemberElement (XamlType parentType, XamlType xt
}
else
{

if (r.Name.Contains(".") && r.IsEmptyElement && !r.HasAttributes)
if (r.Name.Contains(".") && r.IsEmptyElement)
{
// This case is present to handle self closing attached property nodes.
if (!r.HasAttributes)
{
// This case is present to handle self closing attached property nodes.

yield return Node(XamlNodeType.StartMember, xm);
yield return Node(XamlNodeType.EndMember, xm);
r.Read();
yield break;
yield return Node(XamlNodeType.StartMember, xm);
yield return Node(XamlNodeType.EndMember, xm);
r.Read();
yield break;
}
else
{
throw new XamlParseException(
string.Format(
CultureInfo.InvariantCulture,
"Member '{0}' cannot have properties", xm.Name)) { LineNumber = this.LineNumber, LinePosition = this.LinePosition };
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Controls.ParserTests.Controls.When_Invalid_Element_Property"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Uno.UI.Tests.Windows_UI_XAML_Controls.GridTests.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>

<NavigationView IsBackButtonVisible="Collapsed"
IsPaneToggleButtonVisible="False"
IsSettingsVisible="False"
PaneDisplayMode="Left">

<NavigationView.PaneTitle FontSize="16"
FontWeight="Bold"
Text="PaneTitle" />

</NavigationView>

</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_Xaml_Controls.ParserTests.Controls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class When_Invalid_Element_Property : Page
{
public When_Invalid_Element_Property()
{
this.InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Text;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Uno.UI.SourceGenerators.Tests.Verifiers;

namespace Uno.UI.SourceGenerators.Tests.Windows_UI_Xaml_Controls.ParserTests;

using Verify = XamlSourceGeneratorVerifier;

[TestClass]
public class Given_Parser
{
[TestMethod]
public async Task When_Invalid_Element_Property()
{
var xamlFiles = new[]
{
new XamlFile(
"MainPage.xaml",
"""
<Page x:Class="TestRepro.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid>
<NavigationView IsBackButtonVisible="Collapsed"
IsPaneToggleButtonVisible="False"
IsSettingsVisible="False"
PaneDisplayMode="Left">
<NavigationView.PaneTitle FontSize="16"
FontWeight="Bold"
Text="PaneTitle" />
</NavigationView>
</Grid>
</Page>
"""),
};

var test = new Verify.Test(xamlFiles)
{
TestState =
{
Sources =
{
"""
using Windows.UI.Xaml.Controls;
namespace TestRepro
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
}
"""
}
}
};

test.ExpectedDiagnostics.AddRange(
new[] {
// /0/MainPage.xaml(13,5): error UXAML0001: Member 'PaneTitle' cannot have properties at line 13, position 5
DiagnosticResult.CompilerError("UXAML0001").WithSpan("/0/MainPage.xaml", 13, 5, 13, 5).WithArguments("Member 'PaneTitle' cannot have properties at line 13, position 5"),
// /0/Test0.cs(9,9): error CS1061: 'MainPage' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'MainPage' could be found (are you missing a using directive or an assembly reference?)
DiagnosticResult.CompilerError("CS1061").WithSpan(9, 9, 9, 28).WithArguments("TestRepro.MainPage", "InitializeComponent")
}
);
await test.RunAsync();
}
}

0 comments on commit 83e5279

Please sign in to comment.