Skip to content

Commit

Permalink
feat: diagnostic if an incompatible language version is used
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Jul 14, 2023
1 parent cbf5d0f commit 9d34c98
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Riok.Mapperly/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ RMG040 | Mapper | Error | A target enum member value does not match the ta
RMG041 | Mapper | Error | A source enum member value does not match the source enum type
RMG042 | Mapper | Error | The type of the enum fallback value does not match the target enum type
RMG043 | Mapper | Warning | Enum fallback values are only supported for the ByName and ByValueCheckDefined strategies, but not for the ByValue strategy
RMG044 | Mapper | Error | This C# language version is not supported by Mapperly
9 changes: 9 additions & 0 deletions src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,13 @@ internal static class DiagnosticDescriptors
DiagnosticSeverity.Warning,
true
);

public static readonly DiagnosticDescriptor LanguageVersionNotSupported = new DiagnosticDescriptor(
"RMG044",
"This C# language version is not supported by Mapperly",
"Mapperly does not support the C# language version {0} but requires at C# least version {1}",
DiagnosticCategories.Mapper,
DiagnosticSeverity.Error,
true
);
}
20 changes: 20 additions & 0 deletions src/Riok.Mapperly/MapperGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Descriptors;
using Riok.Mapperly.Diagnostics;
using Riok.Mapperly.Emit;
using Riok.Mapperly.Helpers;

Expand All @@ -23,6 +25,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var mapperClassDeclarations = SyntaxProvider.GetClassDeclarations(context);

context.ReportDiagnostics(context.CompilationProvider.Select(static (compilation, ct) => BuildCompilationDiagnostics(compilation)));

var compilationAndMappers = context.CompilationProvider.Combine(mapperClassDeclarations.Collect());
var mappersWithDiagnostics = compilationAndMappers.Select(
static (x, cancellationToken) => BuildDescriptors(x.Left, x.Right, cancellationToken)
Expand Down Expand Up @@ -89,4 +93,20 @@ CancellationToken cancellationToken
cancellationToken.ThrowIfCancellationRequested();
return new MapperResults(members.ToImmutableEquatableArray(), diagnostics.ToImmutableEquatableArray());
}

private static ImmutableEquatableArray<Diagnostic> BuildCompilationDiagnostics(Compilation compilation)
{
if (compilation is CSharpCompilation { LanguageVersion: < LanguageVersion.CSharp9 } cSharpCompilation)
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptors.LanguageVersionNotSupported,
null,
cSharpCompilation.LanguageVersion.ToDisplayString(),
LanguageVersion.CSharp9.ToDisplayString()
);
return ImmutableEquatableArray.Create(diagnostic);
}

return ImmutableEquatableArray.Empty<Diagnostic>();
}
}
16 changes: 8 additions & 8 deletions test/Riok.Mapperly.Tests/Generator/IncrementalGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public void AddingNewMapperDoesNotRegenerateOriginal()

var secondary = TestSourceBuilder.SyntaxTree(
"""
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Abstractions;

namespace Test.B
namespace Test.B
{
[Mapper]
internal partial class BarFooMapper
{
[Mapper]
internal partial class BarFooMapper
{
internal partial string BarToFoo(string value);
}
internal partial string BarToFoo(string value);
}
"""
}
"""
);

var syntaxTree = CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default);
Expand Down
17 changes: 17 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/MapperTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Riok.Mapperly.Diagnostics;

namespace Riok.Mapperly.Tests.Mapping;

[UsesVerify]
Expand Down Expand Up @@ -108,4 +111,18 @@ public partial class CarMapper

return TestHelper.VerifyGenerator(source);
}

[Fact]
public void LanguageLevelLower9ShouldDiagnostic()
{
var source = TestSourceBuilder.Mapping("string", "int");
TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics with { LanguageVersion = LanguageVersion.CSharp8 })
.Should()
.HaveDiagnostic(
DiagnosticDescriptors.LanguageVersionNotSupported,
"Mapperly does not support the C# language version 8.0 but requires at C# least version 9.0"
)
.HaveAssertedAllDiagnostics();
}
}

0 comments on commit 9d34c98

Please sign in to comment.