Skip to content

Commit fb8b47e

Browse files
authored
chore: remove SyntaxFactory to improve performance and maintainability (#103)
* Update OAPH From Observable * Update formatting
1 parent 7ee7f18 commit fb8b47e

18 files changed

+393
-347
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
//HintName: TestNs.TestVM.ObservableAsPropertyFromObservable.g.cs
2-
using ReactiveUI;
1+
//HintName: TestVM.ObservableAsPropertyFromObservable.g.cs
32
// <auto-generated/>
3+
using ReactiveUI;
4+
45
#pragma warning disable
56
#nullable enable
7+
68
namespace TestNs
79
{
8-
/// <inheritdoc/>
9-
partial class TestVM
10+
/// <summary>
11+
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
12+
/// </summary>
13+
public partial class TestVM
1014
{
11-
/// <inheritdoc cref="Test2Property"/>
1215
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
16+
/// <inheritdoc cref="Test2Property"/>
1317
private int _test2Property;
18+
1419
/// <inheritdoc cref="_test2PropertyHelper"/>
15-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1620
private ReactiveUI.ObservableAsPropertyHelper<int>? _test2PropertyHelper;
21+
1722
/// <inheritdoc cref="_test2Property"/>
18-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1923
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2024
public int Test2Property { get => _test2Property = _test2PropertyHelper?.Value ?? _test2Property; }
2125

@@ -26,4 +30,6 @@ protected void InitializeOAPH()
2630
_test2PropertyHelper = Test2()!.ToProperty(this, nameof(Test2Property));
2731
}
2832
}
29-
}
33+
}
34+
#nullable restore
35+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
//HintName: TestNs.TestVM.ObservableAsPropertyFromObservable.g.cs
2-
using ReactiveUI;
1+
//HintName: TestVM.ObservableAsPropertyFromObservable.g.cs
32
// <auto-generated/>
3+
using ReactiveUI;
4+
45
#pragma warning disable
56
#nullable enable
7+
68
namespace TestNs
79
{
8-
/// <inheritdoc/>
9-
partial class TestVM
10+
/// <summary>
11+
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
12+
/// </summary>
13+
public partial class TestVM
1014
{
11-
/// <inheritdoc cref="Test3Property"/>
1215
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
16+
/// <inheritdoc cref="Test3Property"/>
1317
private int _test3Property;
18+
1419
/// <inheritdoc cref="_test3PropertyHelper"/>
15-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1620
private ReactiveUI.ObservableAsPropertyHelper<int>? _test3PropertyHelper;
21+
1722
/// <inheritdoc cref="_test3Property"/>
18-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1923
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2024
public int Test3Property { get => _test3Property = _test3PropertyHelper?.Value ?? _test3Property; }
2125

@@ -26,4 +30,6 @@ protected void InitializeOAPH()
2630
_test3PropertyHelper = Test3()!.ToProperty(this, nameof(Test3Property));
2731
}
2832
}
29-
}
33+
}
34+
#nullable restore
35+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
//HintName: TestNs.TestVM.ObservableAsPropertyFromObservable.g.cs
2-
using ReactiveUI;
1+
//HintName: TestVM.ObservableAsPropertyFromObservable.g.cs
32
// <auto-generated/>
3+
using ReactiveUI;
4+
45
#pragma warning disable
56
#nullable enable
7+
68
namespace TestNs
79
{
8-
/// <inheritdoc/>
9-
partial class TestVM
10+
/// <summary>
11+
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
12+
/// </summary>
13+
public partial class TestVM
1014
{
11-
/// <inheritdoc cref="Test1Property"/>
1215
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
16+
/// <inheritdoc cref="Test1Property"/>
1317
private int _test1Property;
18+
1419
/// <inheritdoc cref="_test1PropertyHelper"/>
15-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1620
private ReactiveUI.ObservableAsPropertyHelper<int>? _test1PropertyHelper;
21+
1722
/// <inheritdoc cref="_test1Property"/>
18-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1923
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2024
public int Test1Property { get => _test1Property = _test1PropertyHelper?.Value ?? _test1Property; }
2125

@@ -26,4 +30,6 @@ protected void InitializeOAPH()
2630
_test1PropertyHelper = Test1!.ToProperty(this, nameof(Test1Property));
2731
}
2832
}
29-
}
33+
}
34+
#nullable restore
35+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//HintName: ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.cs
2+
// Copyright (c) 2024 .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+
/// ObservableAsPropertyAttribute.
16+
/// </summary>
17+
/// <seealso cref="Attribute" />
18+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
19+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
20+
internal sealed class ObservableAsPropertyAttribute : Attribute
21+
{
22+
/// <summary>
23+
/// Gets the name of the property.
24+
/// </summary>
25+
/// <value>
26+
/// The name of the property.
27+
/// </value>
28+
public string? PropertyName { get; init; }
29+
30+
/// <summary>
31+
/// Gets the Readonly state of the OAPH property.
32+
/// </summary>
33+
/// <value>
34+
/// The is read only of the OAPH property.
35+
/// </value>
36+
public bool ReadOnly { get; init; } = true;
37+
}
38+
#nullable restore
39+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//HintName: TestVM.ObservableAsPropertyFromObservable.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+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
16+
/// <inheritdoc cref="Test5Property"/>
17+
private int _test5Property;
18+
19+
/// <inheritdoc cref="_test5PropertyHelper"/>
20+
private ReactiveUI.ObservableAsPropertyHelper<int>? _test5PropertyHelper;
21+
22+
/// <inheritdoc cref="_test5Property"/>
23+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
24+
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
25+
public int Test5Property { get => _test5Property = _test5PropertyHelper?.Value ?? _test5Property; }
26+
27+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
28+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
29+
protected void InitializeOAPH()
30+
{
31+
_test5PropertyHelper = Test5!.ToProperty(this, nameof(Test5Property));
32+
}
33+
}
34+
}
35+
#nullable restore
36+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
//HintName: TestNs.TestVM.ObservableAsPropertyFromObservable.g.cs
2-
using ReactiveUI;
1+
//HintName: TestVM.ObservableAsPropertyFromObservable.g.cs
32
// <auto-generated/>
3+
using ReactiveUI;
4+
45
#pragma warning disable
56
#nullable enable
7+
68
namespace TestNs
79
{
8-
/// <inheritdoc/>
9-
partial class TestVM
10+
/// <summary>
11+
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
12+
/// </summary>
13+
public partial class TestVM
1014
{
11-
/// <inheritdoc cref="Test4Property"/>
1215
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
16+
/// <inheritdoc cref="Test4Property"/>
1317
private int _test4Property;
18+
1419
/// <inheritdoc cref="_test4PropertyHelper"/>
15-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1620
private ReactiveUI.ObservableAsPropertyHelper<int>? _test4PropertyHelper;
21+
1722
/// <inheritdoc cref="_test4Property"/>
18-
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ObservableAsPropertyGenerator", "1.1.0.0")]
1923
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2024
public int Test4Property { get => _test4Property = _test4PropertyHelper?.Value ?? _test4Property; }
2125

@@ -26,4 +30,6 @@ protected void InitializeOAPH()
2630
_test4PropertyHelper = Test4!.ToProperty(this, nameof(Test4Property));
2731
}
2832
}
29-
}
33+
}
34+
#nullable restore
35+
#pragma warning restore

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,38 @@ public partial class TestVM : ReactiveObject
133133
// Assert: Verify the generated code.
134134
return Verify(driver).UseDirectory(TestHelper.VerifiedFilePath());
135135
}
136+
137+
/// <summary>
138+
/// Tests that the source generator correctly generates observable properties.
139+
/// </summary>
140+
/// <returns>A task to monitor the async.</returns>
141+
[Fact]
142+
public Task FromObservablePropertiesWithAttribute()
143+
{
144+
// Arrange: Setup the source code that matches the generator input expectations.
145+
const string sourceCode = """
146+
using System;
147+
using System.Runtime.Serialization;
148+
using System.Text.Json.Serialization;
149+
using ReactiveUI;
150+
using ReactiveUI.SourceGenerators;
151+
using System.Reactive.Linq;
152+
153+
namespace TestNs;
154+
155+
public partial class TestVM : ReactiveObject
156+
{
157+
[ObservableAsProperty(PropertyName = "MyNamedProperty")]
158+
[property: JsonInclude]
159+
[DataMember]
160+
public IObservable<int> Test5 => Observable.Return(42);
161+
}
162+
""";
163+
164+
// Act: Initialize the helper and run the generator.
165+
var driver = TestHelper.TestPass(sourceCode);
166+
167+
// Assert: Verify the generated code.
168+
return Verify(driver).UseDirectory(TestHelper.VerifiedFilePath());
169+
}
136170
}

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class TestViewModel : ReactiveObject, IActivatableViewModel, IDis
2727
private readonly Subject<double?> _testSubject = new();
2828
private readonly Subject<double> _testNonNullSubject = new();
2929

30-
[JsonInclude]
30+
[property: JsonInclude]
3131
[DataMember]
3232
[ObservableAsProperty]
3333
private double? _test2Property = 1.1d;
@@ -226,6 +226,7 @@ public TestViewModel()
226226
/// The observable as property test2.
227227
/// </value>
228228
[ObservableAsProperty]
229+
[property: Test(AParameter = "Test Input")]
229230
public IObservable<int> ObservableAsPropertyTest2 => Observable.Return(9);
230231

231232
/// <summary>

src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal static class AttributeDefinitions
1616

1717
public const string AccessModifierType = "ReactiveUI.SourceGenerators.AccessModifier";
1818
public static string[] ExcludeFromCodeCoverage = ["[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]"];
19-
public static string ExcludeFromCodeCoverageString = "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage";
2019

2120
public static string GetAccessModifierEnum() => $$"""
2221
// Copyright (c) {{DateTime.Now.Year}} .NET Foundation and Contributors. All rights reserved.

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public partial class IViewForGenerator
9797

9898
token.ThrowIfCancellationRequested();
9999

100-
return new IViewForInfo(
100+
return new(
101101
targetInfo.FileHintName,
102102
targetInfo.TargetName,
103103
targetInfo.TargetNamespace,

0 commit comments

Comments
 (0)