From 8dccf2f43933ed1e5fd8cb2cebbc491f5c57a8ac Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Wed, 6 Mar 2019 09:39:12 +0100 Subject: [PATCH] [XamlC] throw on invalid DataType (#5335) Invalid x:DataType was ignored up to now. Change the behavior so it throws a XPE. - fixes #5330 --- Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs | 6 +++- Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml | 8 ++++++ Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml.cs | 32 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml.cs diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 5d6194a..c02070b 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -378,8 +378,12 @@ namespace Xamarin.Forms.Build.Tasks break; n = n.Parent as IElementNode; } + + if (dataTypeNode is null) + yield break; + if (!((dataTypeNode as ValueNode)?.Value is string dataType)) - yield break; //throw + throw new XamlParseException("x:DataType expects a string literal", dataTypeNode as IXmlLineInfo); var prefix = dataType.Contains(":") ? dataType.Substring(0, dataType.IndexOf(":", StringComparison.Ordinal)) : ""; var namespaceuri = node.NamespaceResolver.LookupNamespace(prefix) ?? ""; diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml new file mode 100644 index 0000000..162f20c --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml @@ -0,0 +1,8 @@ + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml.cs new file mode 100644 index 0000000..f480a03 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5330.xaml.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + [XamlCompilation(XamlCompilationOptions.Skip)] + public partial class Gh5330 : ContentPage + { + public Gh5330() => InitializeComponent(); + public Gh5330(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); + [TearDown] public void TearDown() => Device.PlatformServices = null; + + [Test] + public void FailOnUnresolvedDataType([Values(true)]bool useCompiledXaml) + { + if (useCompiledXaml) + Assert.Throws(() => MockCompiler.Compile(typeof(Gh5330))); + } + } + } +} -- 2.7.4