Skip to content

Commit 3742195

Browse files
committed
Fix forwarding attributes with negative enum values
1 parent 4ad7fef commit 3742195

File tree

2 files changed

+210
-4
lines changed

2 files changed

+210
-4
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/Models/TypedConstantInfo.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,9 @@ public sealed record Enum(string TypeName, object Value) : TypedConstantInfo
134134
/// <inheritdoc/>
135135
public override ExpressionSyntax GetSyntax()
136136
{
137-
return
138-
CastExpression(
139-
IdentifierName(TypeName),
140-
LiteralExpression(SyntaxKind.NumericLiteralExpression, ParseToken(Value.ToString())));
137+
// We let Roslyn parse the value expression, so that it can automatically handle both positive and negative values. This
138+
// is needed because negative values have a different syntax tree (UnaryMinuxExpression holding the numeric expression).
139+
return CastExpression(IdentifierName(TypeName), ParseExpression(Value.ToString()));
141140
}
142141
}
143142

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsCodegen.cs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,123 @@ partial class MyViewModel
12831283
VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyApp.MyViewModel.Test.g.cs", result));
12841284
}
12851285

1286+
// See https://github.com/CommunityToolkit/dotnet/issues/681
1287+
[TestMethod]
1288+
public void ObservablePropertyWithForwardedAttributesWithEnumWithNegativeValue_WorksCorrectly()
1289+
{
1290+
string source = """
1291+
using System.ComponentModel;
1292+
using CommunityToolkit.Mvvm.ComponentModel;
1293+
1294+
#nullable enable
1295+
1296+
namespace MyApp;
1297+
1298+
partial class MyViewModel : ObservableObject
1299+
{
1300+
[ObservableProperty]
1301+
[property: DefaultValue(NegativeEnum1.Problem)]
1302+
[property: DefaultValue(NegativeEnum2.Problem)]
1303+
[property: DefaultValue(NegativeEnum3.Problem)]
1304+
[property: DefaultValue(NegativeEnum4.Problem)]
1305+
private object? a;
1306+
}
1307+
1308+
public class DefaultValueAttribute : Attribute
1309+
{
1310+
public DefaultValueAttribute(object value)
1311+
{
1312+
}
1313+
}
1314+
1315+
public enum NegativeEnum1
1316+
{
1317+
Problem = -1073741824,
1318+
OK = 0
1319+
}
1320+
1321+
public enum NegativeEnum2 : long
1322+
{
1323+
Problem = long.MinValue,
1324+
OK = 0
1325+
}
1326+
1327+
public enum NegativeEnum3 : short
1328+
{
1329+
Problem = -1234,
1330+
OK = 0
1331+
}
1332+
1333+
public enum NegativeEnum4 : sbyte
1334+
{
1335+
Problem = -1,
1336+
OK = 0
1337+
}
1338+
""";
1339+
1340+
string result = """
1341+
// <auto-generated/>
1342+
#pragma warning disable
1343+
#nullable enable
1344+
namespace MyApp
1345+
{
1346+
/// <inheritdoc/>
1347+
partial class MyViewModel
1348+
{
1349+
/// <inheritdoc cref="a"/>
1350+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1351+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1352+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)-1073741824)]
1353+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)-9223372036854775808)]
1354+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)-1234)]
1355+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-1)]
1356+
public object? A
1357+
{
1358+
get => a;
1359+
set
1360+
{
1361+
if (!global::System.Collections.Generic.EqualityComparer<object?>.Default.Equals(a, value))
1362+
{
1363+
OnAChanging(value);
1364+
OnAChanging(default, value);
1365+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.A);
1366+
a = value;
1367+
OnAChanged(value);
1368+
OnAChanged(default, value);
1369+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.A);
1370+
}
1371+
}
1372+
}
1373+
1374+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
1375+
/// <param name="value">The new property value being set.</param>
1376+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
1377+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1378+
partial void OnAChanging(object? value);
1379+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
1380+
/// <param name="oldValue">The previous property value that is being replaced.</param>
1381+
/// <param name="newValue">The new property value being set.</param>
1382+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
1383+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1384+
partial void OnAChanging(object? oldValue, object? newValue);
1385+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
1386+
/// <param name="value">The new property value that was set.</param>
1387+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
1388+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1389+
partial void OnAChanged(object? value);
1390+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
1391+
/// <param name="oldValue">The previous property value that was replaced.</param>
1392+
/// <param name="newValue">The new property value that was set.</param>
1393+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
1394+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1395+
partial void OnAChanged(object? oldValue, object? newValue);
1396+
}
1397+
}
1398+
""";
1399+
1400+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
1401+
}
1402+
12861403
// See https://github.com/CommunityToolkit/dotnet/issues/632
12871404
[TestMethod]
12881405
public void RelayCommandMethodWithPartialDeclarations_TriggersCorrectly()
@@ -1450,6 +1567,96 @@ partial class MyViewModel
14501567
VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyApp.MyViewModel.Test1.g.cs", result1), ("MyApp.MyViewModel.Test2.g.cs", result2));
14511568
}
14521569

1570+
// See https://github.com/CommunityToolkit/dotnet/issues/681
1571+
[TestMethod]
1572+
public void RelayCommandMethodWithForwardedAttributesWithEnumValues_WorksCorrectly()
1573+
{
1574+
string source = """
1575+
using CommunityToolkit.Mvvm.Input;
1576+
1577+
#nullable enable
1578+
1579+
namespace MyApp;
1580+
1581+
partial class MyViewModel
1582+
{
1583+
[RelayCommand]
1584+
[field: DefaultValue(NegativeEnum1.Problem)]
1585+
[field: DefaultValue(NegativeEnum2.Problem)]
1586+
[field: DefaultValue(NegativeEnum3.Problem)]
1587+
[field: DefaultValue(NegativeEnum4.Problem)]
1588+
[property: DefaultValue(NegativeEnum1.Problem)]
1589+
[property: DefaultValue(NegativeEnum2.Problem)]
1590+
[property: DefaultValue(NegativeEnum3.Problem)]
1591+
[property: DefaultValue(NegativeEnum4.Problem)]
1592+
private void Test()
1593+
{
1594+
}
1595+
}
1596+
1597+
public class DefaultValueAttribute : Attribute
1598+
{
1599+
public DefaultValueAttribute(object value)
1600+
{
1601+
}
1602+
}
1603+
1604+
public enum NegativeEnum1
1605+
{
1606+
Problem = -1073741824,
1607+
OK = 0
1608+
}
1609+
1610+
public enum NegativeEnum2 : long
1611+
{
1612+
Problem = long.MinValue,
1613+
OK = 0
1614+
}
1615+
1616+
public enum NegativeEnum3 : short
1617+
{
1618+
Problem = -1234,
1619+
OK = 0
1620+
}
1621+
1622+
public enum NegativeEnum4 : sbyte
1623+
{
1624+
Problem = -1,
1625+
OK = 0
1626+
}
1627+
""";
1628+
1629+
string result = """
1630+
// <auto-generated/>
1631+
#pragma warning disable
1632+
#nullable enable
1633+
namespace MyApp
1634+
{
1635+
/// <inheritdoc/>
1636+
partial class MyViewModel
1637+
{
1638+
/// <summary>The backing field for <see cref="TestCommand"/>.</summary>
1639+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
1640+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)-1073741824)]
1641+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)-9223372036854775808)]
1642+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)-1234)]
1643+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-1)]
1644+
private global::CommunityToolkit.Mvvm.Input.RelayCommand? testCommand;
1645+
/// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IRelayCommand"/> instance wrapping <see cref="Test"/>.</summary>
1646+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
1647+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1648+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)-1073741824)]
1649+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)-9223372036854775808)]
1650+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)-1234)]
1651+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-1)]
1652+
public global::CommunityToolkit.Mvvm.Input.IRelayCommand TestCommand => testCommand ??= new global::CommunityToolkit.Mvvm.Input.RelayCommand(new global::System.Action(Test));
1653+
}
1654+
}
1655+
""";
1656+
1657+
VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyApp.MyViewModel.Test.g.cs", result));
1658+
}
1659+
14531660
[TestMethod]
14541661
public void ObservablePropertyWithinGenericAndNestedTypes()
14551662
{

0 commit comments

Comments
 (0)