Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds code fixer for: Report error on duplicate [Id(x)] (ORLEANS0012) #8808

Merged
merged 2 commits into from
Jan 9, 2024
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
11 changes: 8 additions & 3 deletions src/Orleans.Analyzers/IdClashAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void CheckSyntaxNode(SyntaxNodeAnalysisContext context)
return;
}

List<AttributeArgumentBag<int>> bags = new();
List<AttributeArgumentBag<int>> bags = [];
foreach (var memberDeclaration in typeDeclaration.Members.OfType<MemberDeclarationSyntax>())
{
var attributes = memberDeclaration.AttributeLists.GetAttributeSyntaxes(Constants.IdAttributeName);
Expand Down Expand Up @@ -78,11 +78,16 @@ private void CheckSyntaxNode(SyntaxNodeAnalysisContext context)
{
foreach (var bag in filteredBags)
{
var builder = ImmutableDictionary.CreateBuilder<string, string>();

builder.Add("IdValue", bag.Value.ToString());

context.ReportDiagnostic(Diagnostic.Create(
descriptor: Rule,
location: bag.Location));
location: bag.Location,
properties: builder.ToImmutable()));
}
}
}
}
}
}
56 changes: 56 additions & 0 deletions src/Orleans.Analyzers/IdClashAttributeCodeFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Orleans.Analyzers;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(IdClashAttributeCodeFix)), Shared]
public class IdClashAttributeCodeFix : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(IdClashAttributeAnalyzer.RuleId);
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
if (root.FindNode(diagnostic.Location.SourceSpan) is not AttributeSyntax attribute)
{
return;
}

var idValue = diagnostic.Properties["IdValue"];

context.RegisterCodeFix(
CodeAction.Create(
Resources.IdClashDetectedTitle,
createChangedDocument: _ =>
{
var newIdValue = root
.DescendantNodes()
.OfType<AttributeSyntax>()
.Where(a => a.IsAttribute(Constants.IdAttributeName))
.Select(a => int.Parse(a.ArgumentList.Arguments.Single().ToString()))
.ToList()
.Max() + 1;

var newAttribute = attribute.ReplaceNode(
attribute.ArgumentList.Arguments[0].Expression,
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(newIdValue)));

var newRoot = root.ReplaceNode(attribute, newAttribute);
var newDocument = context.Document.WithSyntaxRoot(newRoot);

return Task.FromResult(newDocument);
},
equivalenceKey: IdClashAttributeAnalyzer.RuleId),
diagnostic);
}
}
4 changes: 2 additions & 2 deletions src/Orleans.Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Orleans.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
<value>The [Id] attribute must be unique to each members of the declaring type.</value>
</data>
<data name="IdClashDetectedMessageFormat" xml:space="preserve">
<value>Substitute duplicated [Id] value with the correct unique identity of this member</value>
<value>Change duplicated [Id]</value>
</data>
<data name="IdClashDetectedTitle" xml:space="preserve">
<value>Substitute duplicated [Id] value with the correct unique identity of this member</value>
<value>Change duplicated [Id]</value>
</data>
<data name="IncorrectAttributeUseMessageFormat" xml:space="preserve">
<value>Remove attribute [{0}]</value>
Expand Down
Loading