Skip to content

Commit bd3ab73

Browse files
[XSG] report a warning on usage of required (#32274)
only for non valuenodes - fixes #32272
1 parent c8d2b23 commit bd3ab73

File tree

7 files changed

+24
-7
lines changed

7 files changed

+24
-7
lines changed

src/Controls/src/SourceGen/AnalyzerReleases.Unshipped.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ MAUIX2001 | XamlInflation | Error | Descriptors
1414
MAUIX2002 | XamlInflation | Error | Descriptors
1515
MAUIX2003 | XamlInflation | Error | Descriptors
1616
MAUIX2004 | XamlInflation | Error | Descriptors
17+
MAUIX2005 | XamlInflation | Warning | Descriptors

src/Controls/src/SourceGen/Descriptors.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ public static class Descriptors
186186
defaultSeverity: DiagnosticSeverity.Error,
187187
isEnabledByDefault: true);
188188

189+
public static DiagnosticDescriptor RequiredProperty = new DiagnosticDescriptor(
190+
id: "MAUIX2005",
191+
title: new LocalizableResourceString(nameof(MauiGResources.RequiredPropertyTitle), MauiGResources.ResourceManager, typeof(MauiGResources)),
192+
messageFormat: new LocalizableResourceString(nameof(MauiGResources.RequiredPropertyMessage), MauiGResources.ResourceManager, typeof(MauiGResources)),
193+
category: "XamlInflation",
194+
defaultSeverity: DiagnosticSeverity.Warning,
195+
isEnabledByDefault: true);
196+
189197
// public static BuildExceptionCode TypeResolution = new BuildExceptionCode("XC", 0000, nameof(TypeResolution), "");
190198
// public static BuildExceptionCode PropertyResolution = new BuildExceptionCode("XC", 0001, nameof(PropertyResolution), "");
191199
// public static BuildExceptionCode MissingEventHandler = new BuildExceptionCode("XC", 0002, nameof(MissingEventHandler), "");

src/Controls/src/SourceGen/MauiGResources.resx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@
189189
</data>
190190
<data name="DuplicateKeyInRD" xml:space="preserve">
191191
<value>duplicate key in resource dictionary</value>
192-
<comment>
193-
</comment>
194192
</data>
195193
<data name="ConversionFailedTitle" xml:space="preserve">
196194
<value>Conversion Failed</value>
197195
<comment>
198196
</comment>
199197
</data>
198+
<data name="RequiredPropertyTitle" xml:space="preserve">
199+
<value>Use of 'required'</value>
200+
<comment>/</comment>
201+
</data>
202+
<data name="RequiredPropertyMessage" xml:space="preserve">
203+
<value>{0} has the 'required' keyword. Support for this in XAML is limited at the moment, avoid using it. We're still doing a best effort to provide a value.</value>
204+
<comment>0 is a property identifier</comment>
205+
</data>
200206
</root>

src/Controls/src/SourceGen/Visitors/CreateValuesVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public static void CreateValue(ElementNode node, IndentedTextWriter writer, IDic
224224
propValue = vn.ConvertTo(pType, pConverter, writer, Context);
225225
else if (propNode is ElementNode en)
226226
{
227+
Context.ReportDiagnostic(Diagnostic.Create(Descriptors.RequiredProperty, LocationCreate(Context.ProjectItem.RelativePath!, node, type.Name), $"'{type.ToFQDisplayString()}.{propXmlName.LocalName}'"));
227228
en.TryProvideValue(writer, Context);
228229
propValue = variables[en].ValueAccessor;
229230
}
@@ -346,4 +347,4 @@ static string ValueForLanguagePrimitive(ITypeSymbol type, ElementNode node, Sour
346347

347348
return NodeSGExtensions.ValueForLanguagePrimitive(valueString, toType: type, context, node);
348349
}
349-
}
350+
}

src/Controls/src/SourceGen/xlf/MauiGResources.Designer.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests</AssemblyName>
77
<WarningLevel>4</WarningLevel>
88
<NoWarn>$(NoWarn);0672;0219;0414;CS0436;CS0618</NoWarn>
9-
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022;XC0023;XC0025;XC0045;MAUID1000</WarningsNotAsErrors>
9+
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022;XC0023;XC0025;XC0045;MAUID1000;MAUIX2005</WarningsNotAsErrors>
1010
<IsPackable>false</IsPackable>
1111
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
1212
<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

src/Controls/tests/Xaml.UnitTests/Required.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using NUnit.Framework;
34

45
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
@@ -52,7 +53,7 @@ public override string ToString()
5253
""")
5354
.RunMauiSourceGenerator(typeof(Required));
5455

55-
Assert.That(result.Diagnostics, Is.Empty, "No diagnostics expected");
56+
Assert.That(result.Diagnostics.Length, Is.EqualTo(1), "warning expected");
5657
}
5758

5859
var layout = new Required(inflator);
@@ -76,8 +77,6 @@ public class RequiredRandomSelector : DataTemplateSelector
7677
public class RequiredPerson
7778
{
7879
public required string Name { get; set; }
79-
80-
8180
public override string ToString()
8281
=> Name;
8382
}

0 commit comments

Comments
 (0)