Skip to content

Commit

Permalink
feat(MustBePartialAnalyzer): check ancestry for partial
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffijoe committed Sep 1, 2024
1 parent 50b88c7 commit 3e65b82
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
(7,15): error FXKIT0001: The type 'ForgotToAddPartial' must be partial in order to use it with 'Union'
(6,15): error FXKIT0001: The type 'ForgotToAddPartial' must be partial in order to use it with 'Union'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
(6,14): error FXKIT0001: The type 'Super' must be partial in order to use it with 'Union',
(8,18): error FXKIT0001: The type 'Duper' must be partial in order to use it with 'Union'
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,64 @@ public class MustBePartialAnalyzerTests
[Test]
public async Task DoesNotReportWhenPartial()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record ForgotToAddPartial
{
partial record Woah;
}
";
[Union]
public partial record ForgotToAddPartial
{
partial record Woah;
}
""";
var diagnostics = await Analyze(Source);
diagnostics.Should().BeEmpty();
}

[Test]
public async Task ReportsDiagnostics()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public record ForgotToAddPartial
{
partial record Woah;
}
""";
var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
}

[Test]
public async Task ReportsWhenTypeHierarchyIsMissingPartial()
{
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
// missing partials
public class Super
{
public class Duper
{
[Union]
public partial record Nested
{
partial record Incorrect;
}
}
}
""";

[Union]
public record ForgotToAddPartial
{
partial record Woah;
}
";
var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
(9,12): error FXKIT0003: Use 'partial record Woah' to declare a constituent of union type 'ForgotToAddPartial'
(8,12): error FXKIT0003: Use 'partial record Woah' to declare a constituent of union type 'ForgotToAddPartial'
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
(7,23): warning FXKIT0002: The type 'MissingConstituents' is marked as [Union] and should declare at least one nested 'partial record'
(6,23): warning FXKIT0002: The type 'MissingConstituents' is marked as [Union] and should declare at least one nested 'partial record'
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
(12,32): error FXKIT0004: 'NonClosedUsage' inherits union type 'NonClosed' which is not allowed. Only declare constituents by nesting them inside of the union using 'partial record'.
(11,32): error FXKIT0004: 'NonClosedUsage' inherits union type 'NonClosed' which is not allowed. Only declare constituents by nesting them inside of the union using 'partial record'.
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
(15,58): error FXKIT0007: 'HaseBaseTypeButItDoesntEvenCompile' is marked as a union and therefore is not allowed to inherit from other types,
(9,36): error FXKIT0007: 'HaseBaseType' is marked as a union and therefore is not allowed to inherit from other types
(14,58): error FXKIT0007: 'HaseBaseTypeButItDoesntEvenCompile' is marked as a union and therefore is not allowed to inherit from other types,
(8,36): error FXKIT0007: 'HaseBaseType' is marked as a union and therefore is not allowed to inherit from other types
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
(7,30): error FXKIT0005: 'HasCtor' is marked as a union and cannot declare a primary constructor
(6,30): error FXKIT0005: 'HasCtor' is marked as a union and cannot declare a primary constructor
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
(13,16): error FXKIT0006: 'IsAbstract' is marked as a union and cannot be marked as abstract because unions will be automatically marked as abstract,
(7,16): error FXKIT0006: 'IsSealed' is marked as a union and cannot be marked as sealed because unions will be automatically marked as abstract
(12,16): error FXKIT0006: 'IsAbstract' is marked as a union and cannot be marked as abstract because unions will be automatically marked as abstract,
(6,16): error FXKIT0006: 'IsSealed' is marked as a union and cannot be marked as sealed because unions will be automatically marked as abstract
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ public class UnionAnalyzerTests
[Test]
public async Task DoesNotReportForCorrectUsage()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record ForgotToAddPartial
{
partial record Woah(string Parameter);
partial record Right;
}
";
[Union]
public partial record ForgotToAddPartial
{
partial record Woah(string Parameter);
partial record Right;
}
""";

var diagnostics = await Analyze(Source);
diagnostics.Should().BeEmpty();
Expand All @@ -30,18 +31,19 @@ partial record Right;
[Test]
public async Task ReportsForInvalidConstituent()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record ForgotToAddPartial
{
public record Woah(string Parameter); // missing partial keyword
partial record Right;
}
";
[Union]
public partial record ForgotToAddPartial
{
public record Woah(string Parameter); // missing partial keyword
partial record Right;
}
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand All @@ -50,16 +52,17 @@ partial record Right;
[Test]
public async Task ReportsForMissingConstituents()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record MissingConstituents
{
}
";
[Union]
public partial record MissingConstituents
{
}
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand All @@ -68,19 +71,20 @@ public partial record MissingConstituents
[Test]
public async Task ReportsForNonClosedUsage()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record NonClosed
{
partial record Correct;
}
[Union]
public partial record NonClosed
{
partial record Correct;
}
public record NonClosedUsage : NonClosed;
";
public record NonClosedUsage : NonClosed;
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand All @@ -89,17 +93,18 @@ public record NonClosedUsage : NonClosed;
[Test]
public async Task ReportsWhenDeclaringPrimaryConstructor()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
[Union]
public partial record HasCtor(bool NotAllowed)
{
partial record Incorrect;
}
";
[Union]
public partial record HasCtor(bool NotAllowed)
{
partial record Incorrect;
}
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand All @@ -108,25 +113,27 @@ partial record Incorrect;
[Test]
public async Task ReportsWhenDeclaringBaseType()
{
const string Source = @"
using FxKit.CompilerServices;
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
namespace Super.Duper.Unions;
public record TheBaseType;
public record TheBaseType;
[Union]
public partial record HaseBaseType : TheBaseType
{
partial record Incorrect;
}
[Union]
public partial record HaseBaseType : TheBaseType
{
partial record Incorrect;
}
[Union]
public partial record HaseBaseTypeButItDoesntEvenCompile : DontKnowWhatThisIs
{
partial record Incorrect;
}
";
[Union]
public partial record HaseBaseTypeButItDoesntEvenCompile : DontKnowWhatThisIs
{
partial record Incorrect;
}
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand All @@ -135,23 +142,24 @@ partial record Incorrect;
[Test]
public async Task ReportsWhenInvalidModifiersAreUsed()
{
const string Source = @"
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
[Union]
public partial sealed record IsSealed
{
partial record Incorrect;
}
[Union]
public partial abstract record IsAbstract
{
partial record Incorrect;
}
";
const string Source =
"""
using FxKit.CompilerServices;
namespace Super.Duper.Unions;
[Union]
public partial sealed record IsSealed
{
partial record Incorrect;
}
[Union]
public partial abstract record IsAbstract
{
partial record Incorrect;
}
""";

var diagnostics = await Analyze(Source);
await diagnostics.VerifyDiagnostics();
Expand Down
Loading

0 comments on commit 3e65b82

Please sign in to comment.