Skip to content

Commit 0490e0b

Browse files
authored
Feature ReactiveCommand With Parameterised Attribute (#21)
ReactiveCommand With ParameterisedAttribute Feature ReactiveCommand With Parameterised Attribute Passthrough
1 parent 6b4a6d1 commit 0490e0b

File tree

7 files changed

+114
-107
lines changed

7 files changed

+114
-107
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,4 @@ public partial class MyReactiveControl : UserControl
283283
```
284284

285285
### TODO:
286-
- Add ReactiveCommand with parameterised Attribute pass through
287286
- Add ObservableAsProperty to generate from a IObservable creating a property and the property helper wired to the Observable.

src/Directory.build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@
6868
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
6969
</ItemGroup>
7070
</Project>
71+
<!--SyntaxFactory helper https://roslynquoter.azurewebsites.net/-->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
namespace SGReactiveUI.SourceGenerators.Test;
7+
8+
/// <summary>
9+
/// TestAttribute.
10+
/// </summary>
11+
/// <seealso cref="System.Attribute" />
12+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
13+
public sealed class TestAttribute : Attribute
14+
{
15+
/// <summary>
16+
/// Gets a parameter.
17+
/// </summary>
18+
/// <value>
19+
/// a parameter.
20+
/// </value>
21+
public string? AParameter { get; init; }
22+
}

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public TestViewModel()
9292
/// </summary>
9393
[ReactiveCommand(CanExecute = nameof(CanExecuteTest1))]
9494
[property: JsonInclude]
95+
[property: Test(AParameter = "Test Input")]
9596
private void Test1() => Console.Out.WriteLine("Test1");
9697

9798
/// <summary>

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace ReactiveUI.SourceGenerators;
2525
/// <seealso cref="IIncrementalGenerator" />
2626
public partial class ObservableAsPropertyGenerator
2727
{
28+
private const string GeneratedCode = "global::System.CodeDom.Compiler.GeneratedCode";
29+
private const string ExcludeFromCodeCoverage = "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage";
2830
/// <summary>
2931
/// A container for all the logic for <see cref="ObservableAsPropertyGenerator"/>.
3032
/// </summary>
@@ -86,7 +88,7 @@ internal static ImmutableArray<MemberDeclarationSyntax> GetPropertySyntax(Proper
8688
.AddDeclarationVariables(VariableDeclarator(getterFieldIdentifierName + "Helper"))
8789
.AddAttributeLists(
8890
AttributeList(SingletonSeparatedList(
89-
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
91+
Attribute(IdentifierName(GeneratedCode))
9092
.AddArgumentListArguments(
9193
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).FullName))),
9294
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).Assembly.GetName().Version.ToString()))))))
@@ -97,12 +99,12 @@ internal static ImmutableArray<MemberDeclarationSyntax> GetPropertySyntax(Proper
9799
PropertyDeclaration(propertyType, Identifier(propertyInfo.PropertyName))
98100
.AddAttributeLists(
99101
AttributeList(SingletonSeparatedList(
100-
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
102+
Attribute(IdentifierName(GeneratedCode))
101103
.AddArgumentListArguments(
102104
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).FullName))),
103105
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).Assembly.GetName().Version.ToString()))))))
104106
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>")), SyntaxKind.OpenBracketToken, TriviaList())),
105-
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
107+
AttributeList(SingletonSeparatedList(Attribute(IdentifierName(ExcludeFromCodeCoverage)))))
106108
.AddAttributeLists([.. forwardedAttributes])
107109
.AddModifiers(Token(SyntaxKind.PublicKeyword))
108110
.AddAccessorListAccessors(

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ namespace ReactiveUI.SourceGenerators;
2525
/// <seealso cref="IIncrementalGenerator" />
2626
public partial class ReactiveGenerator
2727
{
28+
private const string GeneratedCode = "global::System.CodeDom.Compiler.GeneratedCode";
29+
private const string ExcludeFromCodeCoverage = "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage";
30+
2831
/// <summary>
2932
/// A container for all the logic for <see cref="ReactiveCommandGenerator"/>.
3033
/// </summary>
@@ -120,12 +123,12 @@ internal static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyI
120123
PropertyDeclaration(propertyType, Identifier(propertyInfo.PropertyName))
121124
.AddAttributeLists(
122125
AttributeList(SingletonSeparatedList(
123-
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
126+
Attribute(IdentifierName(GeneratedCode))
124127
.AddArgumentListArguments(
125128
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ReactiveGenerator).FullName))),
126129
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ReactiveGenerator).Assembly.GetName().Version.ToString()))))))
127130
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>")), SyntaxKind.OpenBracketToken, TriviaList())),
128-
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
131+
AttributeList(SingletonSeparatedList(Attribute(IdentifierName(ExcludeFromCodeCoverage)))))
129132
.AddAttributeLists([.. forwardedAttributes])
130133
.AddModifiers(Token(SyntaxKind.PublicKeyword))
131134
.AddAccessorListAccessors(

0 commit comments

Comments
 (0)