Skip to content

Commit

Permalink
[X] Warn on non compiled bindings (#19360)
Browse files Browse the repository at this point in the history
* [X] Warn on non compiled bindings

When XamlC can't compile a Binding due to missing x:DataType, log a
warning.

If you use TreatWarningsAsErrors, add XC10101 to WarningsNotAsErrors to
avoid the build failing

* warn on datatype explicitly null

* allow localization
  • Loading branch information
StephaneDelcroix committed Dec 20, 2023
1 parent 4d833ea commit 6d6c9cb
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 12 deletions.
Expand Up @@ -3,21 +3,20 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Microsoft.Maui.Controls.Compatibility.ControlGallery</AssemblyName>
<RootNamespace>Microsoft.Maui.Controls.Compatibility.ControlGallery</RootNamespace>
<WarningLevel>4</WarningLevel>
<NoWarn>0114;0108;0109;4014;0649;0169;0472;0414;0168;0219;0429;0618;0612</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0022;XC0023</WarningsNotAsErrors>
</PropertyGroup>
<PropertyGroup>
<XFDisableTargetsValidation>True</XFDisableTargetsValidation>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>TRACE;DEBUG;PERF;APP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0114;0108;0109;4014;0649;0169;0472;0414;0168;0219;0429;0618;0612</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineConstants>TRACE;APP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0114;0108;0109;4014;0649;0169;0472;0414;0168;0219;0429;0618;0612</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('uap10.0'))">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
Expand Down
3 changes: 2 additions & 1 deletion src/Controls/samples/Directory.Build.props
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<SampleProject>true</SampleProject>
<UseMaui Condition=" '$(UseWorkload)' == 'true' ">true</UseMaui>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0022;XC0023</WarningsNotAsErrors>
</PropertyGroup>
<Import Project="../../../Directory.Build.props" />
</Project>
</Project>
4 changes: 3 additions & 1 deletion src/Controls/src/Build.Tasks/BuildException.cs
Expand Up @@ -55,6 +55,8 @@ class BuildExceptionCode
//BP,BO
public static BuildExceptionCode BPName = new BuildExceptionCode("XFC", 0020, nameof(BPName), "");
public static BuildExceptionCode BPMissingGetter = new BuildExceptionCode("XFC", 0021, nameof(BPMissingGetter), "");
public static BuildExceptionCode BindingWithoutDataType = new BuildExceptionCode("XC", 0022, nameof(BindingWithoutDataType), ""); //warning
public static BuildExceptionCode BindingWithNullDataType = new BuildExceptionCode("XC", 0023, nameof(BindingWithNullDataType), ""); //warning

//Bindings, conversions
public static BuildExceptionCode Conversion = new BuildExceptionCode("XFC", 0040, nameof(Conversion), "");
Expand Down Expand Up @@ -90,7 +92,7 @@ class BuildExceptionCode
public static BuildExceptionCode XKeyNotLiteral = new BuildExceptionCode("XFC", 0127, nameof(XKeyNotLiteral), "");

//CSC equivalents
public static BuildExceptionCode ObsoleteProperty = new BuildExceptionCode("XC", 0618, nameof(ObsoleteProperty), "");
public static BuildExceptionCode ObsoleteProperty = new BuildExceptionCode("XC", 0618, nameof(ObsoleteProperty), ""); //warning

public string Code { get; }
public string CodePrefix { get; }
Expand Down
21 changes: 20 additions & 1 deletion src/Controls/src/Build.Tasks/ErrorMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Controls/src/Build.Tasks/ErrorMessages.resx
Expand Up @@ -135,6 +135,12 @@
<value>Binding: Unsupported indexer index type: "{0}".</value>
<comment>0 is indexer type name</comment>
</data>
<data name="BindingWithoutDataType" xml:space="preserve">
<value>Binding could be compiled if x:DataType is specified.</value>
</data>
<data name="BindingWithNullDataType" xml:space="preserve">
<value>Binding could be compiled if x:DataType is not explicitly null.</value>
</data>
<data name="BindingPropertyNotFound" xml:space="preserve">
<value>Binding: Property "{0}" not found on "{1}".</value>
<comment>0 is property name, 1 is type name</comment>
Expand Down
8 changes: 7 additions & 1 deletion src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Expand Up @@ -389,13 +389,19 @@ static IEnumerable<Instruction> CompileBindingPath(ElementNode node, ILContext c
n = n.Parent as IElementNode;
}

if (dataTypeNode is null)
if (dataTypeNode is null) {
context.LoggingHelper.LogWarningOrError(BuildExceptionCode.BindingWithoutDataType, context.XamlFilePath, node.LineNumber, node.LinePosition, 0, 0, null);

yield break;
}

if (dataTypeNode is ElementNode enode
&& enode.XmlType.NamespaceUri == XamlParser.X2009Uri
&& enode.XmlType.Name == nameof(Microsoft.Maui.Controls.Xaml.NullExtension))
{
context.LoggingHelper.LogWarningOrError(BuildExceptionCode.BindingWithNullDataType, context.XamlFilePath, node.LineNumber, node.LinePosition, 0, 0, null);
yield break;
}

string dataType = null;

Expand Down
Expand Up @@ -5,8 +5,8 @@
<RootNamespace>Microsoft.Maui.Controls.Xaml.UnitTests</RootNamespace>
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests</AssemblyName>
<WarningLevel>4</WarningLevel>
<NoWarn>0672;0219;0414;CS0436;CS0618</NoWarn>
<WarningsNotAsErrors>XC0618</WarningsNotAsErrors>
<NoWarn>$(NoWarn);0672;0219;0414;CS0436;CS0618</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022;XC0023</WarningsNotAsErrors>
<IsPackable>false</IsPackable>
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Essentials/samples/Directory.Build.props
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<SampleProject>true</SampleProject>
<UseMaui Condition=" '$(UseWorkload)' == 'true' ">true</UseMaui>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0022;XC0023</WarningsNotAsErrors>
</PropertyGroup>
<Import Project="../../../Directory.Build.props" />
</Project>
</Project>
Expand Up @@ -6,7 +6,8 @@
<RootNamespace>Microsoft.Maui.TestUtils.DeviceTests.Runners</RootNamespace>
<AssemblyName>Microsoft.Maui.TestUtils.DeviceTests.Runners</AssemblyName>
<!--<Nullable>enable</Nullable>-->
<NoWarn>$(NoWarn),CA1416</NoWarn>
<NoWarn>$(NoWarn);CA1416</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0022;XC0023</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 6d6c9cb

Please sign in to comment.