Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ These Source Generators were designed to work in full with ReactiveUI V19.5.31 a
- [ReactiveCommand(CanExecute = nameof(IObservableBoolName))] with CanExecute
- [ReactiveCommand][property: AttribueToAddToCommand] with Attribute passthrough
- [IViewFor(nameof(ViewModelName))]
- [RoutedControlHost("YourNameSpace.CustomControl")]
- [ViewModelControlHost("YourNameSpace.CustomControl")]

Versions older than V19.5.31 to this:
- [ReactiveCommand] all options supported except Cancellation Token asnyc methods.
Expand Down Expand Up @@ -84,6 +86,18 @@ public partial class MyReactiveClass : ReactiveObject
}
```

### Usage Reactive property with property Attribute pass through
```csharp
using ReactiveUI.SourceGenerators;

public partial class MyReactiveClass : ReactiveObject
{
[Reactive]
[property: JsonIgnore]
private string _myProperty;
}
```

## Usage ObservableAsPropertyHelper `[ObservableAsProperty]`

ObservableAsPropertyHelper is used to create a read-only property from an IObservable. The generated code will create a backing field and a property that returns the value of the backing field. The backing field is initialized with the value of the IObservable when the class is instantiated.
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageVersion Include="ReactiveUI" Version="20.1.63" />
<PackageVersion Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(CodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(CodeAnalysisVersion)" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

namespace TestNs
{
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test3"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[System.Text.Json.Serialization.JsonInclude]
[System.Runtime.Serialization.DataMember]
public int Test3
{
get => _test3;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test3")]
set => this.RaiseAndSetIfChanged(ref _test3, value);
}
}
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test3"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
public int Test3
{
get => _test3;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test3")]
set => this.RaiseAndSetIfChanged(ref _test3, value);
}
}
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

namespace TestNs
{
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test1"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Test1
{
get => _test1;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test1")]
set => this.RaiseAndSetIfChanged(ref _test1, value);
}
}
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test1"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Test1
{
get => _test1;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test1")]
set => this.RaiseAndSetIfChanged(ref _test1, value);
}
}
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

namespace TestNs
{
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test2"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Test2
{
get => _test2;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test2")]
set => this.RaiseAndSetIfChanged(ref _test2, value);
}
}
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
/// </summary>
public partial class TestVM
{
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "1.1.0.0")]
/// <inheritdoc cref="_test2"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Test2
{
get => _test2;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("_test2")]
set => this.RaiseAndSetIfChanged(ref _test2, value);
}
}
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
<PackageReference Include="FluentAssertions" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
Expand All @@ -40,4 +36,8 @@
<Folder Include="REACTIVE\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace TestNs;

public partial class TestVM : ReactiveObject
{
[JsonInclude]
[property: JsonInclude]
[DataMember]
[Reactive]
private int _test3 = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

## Release 1.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
RXUISG0016 | ReactiveUI.SourceGenerators.PropertyToReactiveFieldCodeFixProvider | Info | See https://www.reactiveui.net/docs/handbook/view-models/boilerplate-code.html


## Rules
Shipped in ReactiveUI.SourceGenerators

- RXUISG0001 - Unsupported C# Language Version
This rule checks if the project is using an unsupported C# language version. The supported versions are C# 12.0 and above. If the project is using an unsupported version, the rule will raise an error.

- RXUISG0002 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has a Invalid ReactiveCommand method signature.

- RXUISG0003 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has a Invalid ReactiveCommand.CanExecute member name.

- RXUISG0004 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has Multiple ReactiveCommand.CanExecute member name matches.

- RXUISG0005 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has No valid ReactiveCommand.CanExecute member match.

- RXUISG0006 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has Invalid field or property targeted attribute type.

- RXUISG0007 - ReactiveCommandGenerator
This rule checks if the `ReactiveCommand` has Invalid field or property targeted attribute expression.

- RXUISG0008 - AsyncVoidReturningReactiveCommandMethodAnalyzer
This rule checks if the `ReactiveCommand` has Async void returning method annotated with ReactiveCommand.

- RXUISG0009 - ReactiveGenerator
This rule checks if the `Reactive` has Name collision for generated property.

- RXUISG0010 - ReactiveGenerator
This rule checks if the `Reactive` has Invalid property targeted attribute type.

- RXUISG0011 - ReactiveGenerator
This rule checks if the `Reactive` has Invalid property targeted attribute expression.

- RXUISG0012 - ObservableAsPropertyGenerator
This rule checks if the `ObservableAsProperty` has Invalid property targeted attribute type.

- RXUISG0013 - ObservableAsPropertyGenerator
This rule checks if the `ObservableAsProperty` has Invalid property targeted attribute expression.

- RXUISG0014 - ObservableAsPropertyGenerator
This rule checks if the `ObservableAsProperty` has Invalid generated property declaration.

- RXUISG0015 - ReactiveGenerator
This rule checks if the `Reactive` attribute is being used correctly. If the `Reactive` has Invalid generated property declaration.

- RXUISG0016 - PropertyToReactiveFieldCodeFixProvider
This rule checks if there are any Properties to change to Reactive Field, change to [Reactive] private type _fieldName;.

- RXUISG0017 - ObservableAsPropertyFromObservableGenerator
This rule checks if the `ObservableAsProperty` has Invalid generated property declaration.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; Unshipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using static ReactiveUI.SourceGenerators.CodeFixers.Diagnostics.DiagnosticDescriptors;

namespace ReactiveUI.SourceGenerators.CodeFixers;

/// <summary>
/// PropertyToFieldAnalyzer.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class PropertyToReactiveFieldAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// Gets the supported diagnostics.
/// </summary>
/// <value>
/// The supported diagnostics.
/// </value>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(PropertyToReactiveFieldRule);

/// <summary>
/// Initializes the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Initialize(AnalysisContext context)
{
if (context is null)
{
throw new System.ArgumentNullException(nameof(context));
}

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.PropertyDeclaration);
}

private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.Node is not PropertyDeclarationSyntax propertyDeclaration)
{
return;
}

var isAutoProperty = propertyDeclaration.ExpressionBody == null && (propertyDeclaration.AccessorList?.Accessors.All(a => a.Body == null && a.ExpressionBody == null) != false);
var hasCorrectModifiers = propertyDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword) && !propertyDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword);
var doesNotHavePrivateSetOrInternalSet = propertyDeclaration.AccessorList?.Accessors.Any(a => a.Modifiers.Any(SyntaxKind.PrivateKeyword) || a.Modifiers.Any(SyntaxKind.InternalKeyword)) == false;
var namesToIgnore = new List<string> { "ReactiveCommand", "ReactiveProperty", "ViewModelActivator" };
var isNotIgnored = !namesToIgnore.Any(n => propertyDeclaration.Type.ToString().Contains(n));

if (isAutoProperty && hasCorrectModifiers && doesNotHavePrivateSetOrInternalSet && isNotIgnored)
{
var diagnostic = Diagnostic.Create(PropertyToReactiveFieldRule, propertyDeclaration.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}
Loading