Skip to content

Commit

Permalink
Fix For Code Analyzer and Reorganise Code (#52)
Browse files Browse the repository at this point in the history
* Fix For Code Analyzer and Reorganise Code

* Format code for ease of reading

* Update Constants to use central values

* Update ObservableAsPropertyGenerator.cs
  • Loading branch information
ChrisPulman authored Sep 19, 2024
1 parent 26b2371 commit 84614b3
Show file tree
Hide file tree
Showing 38 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReleaseNotes>https://github.com/reactiveui/ReactiveUI.SourceGenerators/releases</PackageReleaseNotes>
<RepositoryUrl>https://github.com/reactiveui/reactiveui.sourcegenerators</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<NoWarn>$(NoWarn);IDE0060;IDE1006;VSSpell001</NoWarn>
<NoWarn>$(NoWarn);IDE0060;IDE1006;IDE0130;VSSpell001</NoWarn>
<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
Expand Down
2 changes: 0 additions & 2 deletions src/ReactiveUI.SourceGenerators.Execute.Maui/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public partial class TestViewModel : ReactiveObject
/// </summary>
public TestViewModel()
{
InitializeCommands();

Console.Out.WriteLine(Test1Command);
Console.Out.WriteLine(Test2Command);
Console.Out.WriteLine(Test3AsyncCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ public override void Initialize(AnalysisContext context)
}

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

var isAutoProperty = propertyDeclaration.ExpressionBody == null && (propertyDeclaration.AccessorList?.Accessors.All(a => a.Body == null && a.ExpressionBody == null) != false);

if (isAutoProperty && propertyDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword) && !propertyDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class PropertyToReactiveFieldCodeFixProvider : CodeFixProvider
/// <summary>
/// Gets a list of diagnostic IDs that this provider can provide fixes for.
/// </summary>
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(DiagnosticDescriptors.PropertyToReactiveFieldRule.Id);
public sealed override ImmutableArray<string> FixableDiagnosticIds =>
ImmutableArray.Create(DiagnosticDescriptors.PropertyToReactiveFieldRule.Id);

/// <summary>
/// Gets an optional <see cref="T:Microsoft.CodeAnalysis.CodeFixes.FixAllProvider" /> that can fix all/multiple occurrences of diagnostics fixed by this code fix provider.
Expand Down Expand Up @@ -77,7 +78,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

// Apply the code fix
context.RegisterCodeFix(
CodeAction.Create("Convert to Reactive field", c => Task.FromResult(context.Document.WithSyntaxRoot(newRoot!)), "Convert to Reactive field"),
CodeAction.Create(
"Convert to Reactive field",
c => Task.FromResult(context.Document.WithSyntaxRoot(newRoot!)),
"Convert to Reactive field"),
diagnostic);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ internal static class DiagnosticDescriptors
id: "RXUISG0016",
title: "Property To Reactive Field, change to [Reactive] private type _fieldName;",
messageFormat: "Replace the property with a INPC Reactive Property for ReactiveUI",
category: typeof(PropertyToReactiveFieldCodeFixProvider).FullName,
category: typeof(PropertyToReactiveFieldAnalyzer).FullName,
defaultSeverity: DiagnosticSeverity.Info,
isEnabledByDefault: true,
description: "Used to create a Read Write INPC Reactive Property for ReactiveUI, annotated with [Reactive].",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace ReactiveUI.SourceGenerators;
/// <seealso cref="IIncrementalGenerator" />
public partial class ObservableAsPropertyGenerator
{
private const string GeneratedCode = "global::System.CodeDom.Compiler.GeneratedCode";
private const string ExcludeFromCodeCoverage = "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage";
/// <summary>
/// A container for all the logic for <see cref="ObservableAsPropertyGenerator"/>.
/// </summary>
Expand Down Expand Up @@ -88,7 +86,7 @@ internal static ImmutableArray<MemberDeclarationSyntax> GetPropertySyntax(Proper
.AddDeclarationVariables(VariableDeclarator(getterFieldIdentifierName + "Helper"))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName(GeneratedCode))
Attribute(IdentifierName(AttributeDefinitions.GeneratedCode))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).Assembly.GetName().Version.ToString()))))))
Expand All @@ -99,12 +97,12 @@ internal static ImmutableArray<MemberDeclarationSyntax> GetPropertySyntax(Proper
PropertyDeclaration(propertyType, Identifier(propertyInfo.PropertyName))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName(GeneratedCode))
Attribute(IdentifierName(AttributeDefinitions.GeneratedCode))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ObservableAsPropertyGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <inheritdoc cref=\"{getterFieldIdentifierName}\"/>")), SyntaxKind.OpenBracketToken, TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName(ExcludeFromCodeCoverage)))))
AttributeList(SingletonSeparatedList(Attribute(IdentifierName(AttributeDefinitions.ExcludeFromCodeCoverage)))))
.AddAttributeLists([.. forwardedAttributes])
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAccessorListAccessors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ namespace ReactiveUI.SourceGenerators;
[Generator(LanguageNames.CSharp)]
public sealed partial class ObservableAsPropertyGenerator : IIncrementalGenerator
{
private const string ObservableAsPropertyAttribute = "ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute";

/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(ctx =>
ctx.AddSource($"{ObservableAsPropertyAttribute}.g.cs", SourceText.From(AttributeDefinitions.ObservableAsPropertyAttribute, Encoding.UTF8)));
ctx.AddSource($"{AttributeDefinitions.ObservableAsPropertyAttributeType}.g.cs", SourceText.From(AttributeDefinitions.ObservableAsPropertyAttribute, Encoding.UTF8)));

// Gather info for all annotated command methods (starting from method declarations with at least one attribute)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo> Info)> propertyInfoWithErrors =
context.SyntaxProvider
.ForAttributeWithMetadataName(
ObservableAsPropertyAttribute,
AttributeDefinitions.ObservableAsPropertyAttributeType,
static (node, _) => node is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: ClassDeclarationSyntax or RecordDeclarationSyntax, AttributeLists.Count: > 0 } } },
static (context, token) =>
{
Expand Down

0 comments on commit 84614b3

Please sign in to comment.