Skip to content

Commit

Permalink
Report and fix ALL partials of template without file-local modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Dec 10, 2024
1 parent 1d22873 commit 6440e26
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/StructId.Analyzer/TemplateAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ static void Analyze(SyntaxNodeAnalysisContext context)
if (symbol is null)
return;

if (!symbol.IsFileLocal || !symbol.IsPartial() || !typeDeclaration.IsKind(SyntaxKind.RecordStructDeclaration))
if (!symbol.IsPartial() || !typeDeclaration.IsKind(SyntaxKind.RecordStructDeclaration))
{
context.ReportDiagnostic(Diagnostic.Create(TemplateMustBeFileRecordStruct, typeDeclaration.Identifier.GetLocation(), symbol.Name));
}

foreach (var nonLocal in symbol.DeclaringSyntaxReferences
.Select(x => x.GetSyntax() as TypeDeclarationSyntax)
.Where(x => x != null && !x.Modifiers.Any(m => m.IsKind(SyntaxKind.FileKeyword))))
{
// We report the issue at each declaration, since ALL need to be file-local to be picked up by the code template cleanup.
context.ReportDiagnostic(Diagnostic.Create(TemplateMustBeFileRecordStruct, nonLocal!.Identifier.GetLocation(), symbol.Name));
}

// If there are parameters, it must be only one, and be named Value
if (typeDeclaration.ParameterList is { } parameters)
{
Expand Down
1 change: 1 addition & 0 deletions src/StructId.CodeFix/TemplateCodeFix.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down
22 changes: 22 additions & 0 deletions src/StructId.Tests/TemplateAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,26 @@ public async Task TemplateNotTSelf()

await test.RunAsync();
}

[Fact]
public async Task PartialTSelfNotFileLocal()
{
var test = new Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
TestCode =
"""
using StructId;
[TStructId]
file partial record struct TSelf;
partial record struct {|#0:TSelf|};
""",
}.WithAnalyzerDefaults();

test.ExpectedDiagnostics.Add(Verifier.Diagnostic(Diagnostics.TemplateMustBeFileRecordStruct).WithLocation(0).WithArguments("TSelf"));

await test.RunAsync();
}
}
64 changes: 64 additions & 0 deletions src/StructId.Tests/TemplateCodeFixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,68 @@ public async Task RenameTSelf()

await test.RunAsync();
}

[Fact]
public async Task AddFileLocal()
{
var test = new CSharpCodeFixTest<TemplateAnalyzer, TemplateCodeFix, DefaultVerifier>
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
TestCode =
"""
using StructId;
[TStructId]
partial record struct {|#0:TSelf|};
""",
FixedCode =
"""
using StructId;
[TStructId]
file partial record struct TSelf;
""",
}.WithCodeFixDefaults();

test.ExpectedDiagnostics.Add(new DiagnosticResult(TemplateMustBeFileRecordStruct).WithLocation(0).WithArguments("TSelf"));

await test.RunAsync();
}

[Fact]
public async Task AddFileLocalToPartial()
{
var test = new CSharpCodeFixTest<TemplateAnalyzer, TemplateCodeFix, DefaultVerifier>
{
CodeFixTestBehaviors = CodeFixTestBehaviors.SkipLocalDiagnosticCheck,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
TestCode =
"""
using StructId;
[TStructId]
file partial record struct TSelf;
partial record struct {|#0:TSelf|};
partial record struct {|#1:TSelf|};
""",
FixedCode =
"""
using StructId;
[TStructId]
file partial record struct TSelf;
file partial record struct TSelf;
file partial record struct TSelf;
""",
}.WithCodeFixDefaults();

test.ExpectedDiagnostics.Add(new DiagnosticResult(TemplateMustBeFileRecordStruct).WithLocation(0).WithArguments("TSelf"));
test.ExpectedDiagnostics.Add(new DiagnosticResult(TemplateMustBeFileRecordStruct).WithLocation(1).WithArguments("TSelf"));

await test.RunAsync();
}
}

0 comments on commit 6440e26

Please sign in to comment.