Skip to content

Commit 6c4a976

Browse files
committed
Fix For Reactive generator using value
1 parent 09b386f commit 6c4a976

6 files changed

+147
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//HintName: ReactiveUI.SourceGenerators.AccessModifier.g.cs
2+
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
3+
// Licensed to the .NET Foundation under one or more agreements.
4+
// The .NET Foundation licenses this file to you under the MIT license.
5+
// See the LICENSE file in the project root for full license information.
6+
7+
// <auto-generated/>
8+
#pragma warning disable
9+
#nullable enable
10+
namespace ReactiveUI.SourceGenerators;
11+
12+
/// <summary>
13+
/// AccessModifier.
14+
/// </summary>
15+
internal enum AccessModifier
16+
{
17+
Public,
18+
Protected,
19+
Internal,
20+
Private,
21+
InternalProtected,
22+
PrivateProtected,
23+
Init,
24+
}
25+
26+
/// <summary>
27+
/// InheritanceModifier.
28+
/// </summary>
29+
internal enum InheritanceModifier
30+
{
31+
None,
32+
Virtual,
33+
Override,
34+
New,
35+
}
36+
#nullable restore
37+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//HintName: ReactiveUI.SourceGenerators.ReactiveAttribute.g.cs
2+
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
3+
// Licensed to the .NET Foundation under one or more agreements.
4+
// The .NET Foundation licenses this file to you under the MIT license.
5+
// See the LICENSE file in the project root for full license information.
6+
7+
using System;
8+
9+
// <auto-generated/>
10+
#pragma warning disable
11+
#nullable enable
12+
namespace ReactiveUI.SourceGenerators;
13+
14+
/// <summary>
15+
/// ReactiveAttribute.
16+
/// </summary>
17+
/// <seealso cref="Attribute" />
18+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
19+
internal sealed class ReactiveAttribute : Attribute
20+
{
21+
/// <summary>
22+
/// Gets the AccessModifier of the set property.
23+
/// </summary>
24+
/// <value>
25+
/// The AccessModifier of the set property.
26+
/// </value>
27+
public AccessModifier SetModifier { get; init; }
28+
29+
/// <summary>
30+
/// Gets the InheritanceModifier of the property.
31+
/// </sumary>
32+
public InheritanceModifier Inheritance { get; init; }
33+
34+
/// <summary>
35+
/// Use Required attribute to indicate that the property is required.
36+
/// </summary>
37+
public bool UseRequired { get; init; }
38+
}
39+
#nullable restore
40+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//HintName: TestNs.TestVM.Properties.g.cs
2+
// <auto-generated/>
3+
using ReactiveUI;
4+
5+
#pragma warning disable
6+
#nullable enable
7+
8+
namespace TestNs
9+
{
10+
/// <summary>
11+
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
12+
/// </summary>
13+
public partial class TestVM
14+
{
15+
16+
/// <inheritdoc cref="this.value"/>
17+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
18+
public string Value
19+
{
20+
get => value;
21+
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("this.value")]
22+
set => this.RaiseAndSetIfChanged(ref this.value, value);
23+
}
24+
}
25+
}
26+
#nullable restore
27+
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/UnitTests/ReactiveGeneratorTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ public partial class TestVM : ReactiveObject
4141
return TestHelper.TestPass(sourceCode);
4242
}
4343

44+
/// <summary>
45+
/// Tests that the source generator correctly generates reactive properties.
46+
/// </summary>
47+
/// <returns>A task to monitor the async.</returns>
48+
[Fact]
49+
public Task FromReactivePropertiesCalledValue()
50+
{
51+
// Arrange: Setup the source code that matches the generator input expectations.
52+
const string sourceCode = """
53+
using System;
54+
using ReactiveUI;
55+
using ReactiveUI.SourceGenerators;
56+
using System.Reactive.Linq;
57+
58+
namespace TestNs;
59+
60+
public partial class TestVM : ReactiveObject
61+
{
62+
[Reactive]
63+
private string value = string.Empty;
64+
}
65+
""";
66+
67+
// Act: Initialize the helper and run the generator. Assert: Verify the generated code.
68+
return TestHelper.TestPass(sourceCode);
69+
}
70+
4471
/// <summary>
4572
/// Tests that the source generator correctly generates reactive properties.
4673
/// </summary>

src/ReactiveUI.SourceGenerators.Execute/TestClassOAPH_VM.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public partial class TestClassOAPH_VM : ReactiveObject
1919
[Reactive]
2020
private bool _reactiveTestField;
2121

22+
[Reactive]
23+
#pragma warning disable SX1309 // Field names should begin with underscore
24+
private string value = string.Empty;
25+
#pragma warning restore SX1309 // Field names should begin with underscore
26+
2227
/// <summary>
2328
/// Initializes a new instance of the <see cref="TestClassOAPH_VM"/> class.
2429
/// </summary>

src/ReactiveUI.SourceGenerators.Roslyn/Reactive/ReactiveGenerator.Execute.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ private static string GetPropertySyntax(PropertyInfo propertyInfo)
304304
return string.Empty;
305305
}
306306

307+
var fieldName = propertyInfo.FieldName;
308+
if (propertyInfo.FieldName == "value")
309+
{
310+
fieldName = "this.value";
311+
}
312+
307313
var fieldSyntax = string.Empty;
308314
var partialModifier = propertyInfo.IsProperty ? "partial " : string.Empty;
309315
if (propertyInfo.IsProperty)
@@ -318,23 +324,23 @@ private static string GetPropertySyntax(PropertyInfo propertyInfo)
318324
return
319325
$$"""
320326
{{fieldSyntax}}
321-
/// <inheritdoc cref="{{propertyInfo.FieldName}}"/>
327+
/// <inheritdoc cref="{{fieldName}}"/>
322328
{{propertyAttributes}}
323329
{{propertyInfo.TargetInfo.TargetVisibility}}{{propertyInfo.Inheritance}} {{partialModifier}}{{propertyInfo.UseRequired}}{{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}}
324330
{
325331
get => {{propertyInfo.FieldName}};
326-
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("{{propertyInfo.FieldName}}")]
327-
{{propertyInfo.AccessModifier}} => this.RaiseAndSetIfChanged(ref {{propertyInfo.FieldName}}, value);
332+
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("{{fieldName}}")]
333+
{{propertyInfo.AccessModifier}} => this.RaiseAndSetIfChanged(ref {{fieldName}}, value);
328334
}
329335
""";
330336
}
331337

332338
return
333339
$$"""
334340
{{fieldSyntax}}
335-
/// <inheritdoc cref="{{propertyInfo.FieldName}}"/>
341+
/// <inheritdoc cref="{{fieldName}}"/>
336342
{{propertyAttributes}}
337-
{{propertyInfo.TargetInfo.TargetVisibility}}{{propertyInfo.Inheritance}} {{partialModifier}}{{propertyInfo.UseRequired}}{{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} { get => {{propertyInfo.FieldName}}; {{propertyInfo.AccessModifier}} => this.RaiseAndSetIfChanged(ref {{propertyInfo.FieldName}}, value); }
343+
{{propertyInfo.TargetInfo.TargetVisibility}}{{propertyInfo.Inheritance}} {{partialModifier}}{{propertyInfo.UseRequired}}{{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} { get => {{propertyInfo.FieldName}}; {{propertyInfo.AccessModifier}} => this.RaiseAndSetIfChanged(ref {{fieldName}}, value); }
338344
""";
339345
}
340346
}

0 commit comments

Comments
 (0)