Skip to content

Commit

Permalink
feat: Support inline expression containing CastExpression or BinaryEx…
Browse files Browse the repository at this point in the history
…pression
  • Loading branch information
trejjam committed Jul 31, 2024
1 parent 78e2e16 commit e259090
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Descriptors.Mappings;
using Riok.Mapperly.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper;

Expand Down Expand Up @@ -66,6 +67,42 @@ public override SyntaxNode VisitTypeArgumentList(TypeArgumentListSyntax node)
return base.VisitMemberAccessExpression(node);
}

public override SyntaxNode? VisitCastExpression(CastExpressionSyntax node)
{
var result = base.VisitCastExpression(node);

if (result is CastExpressionSyntax typedResult && semanticModel.GetSymbolInfo(node.Type).Symbol is ITypeSymbol namedTypeSymbol)
{
TypeSyntax fullyQualifiedType = FullyQualifiedIdentifier(namedTypeSymbol);
if (namedTypeSymbol.IsNullable())
{
fullyQualifiedType = NullableType(fullyQualifiedType);
}

return typedResult.WithType(fullyQualifiedType.WithTriviaFrom(typedResult.Type));
}

return result;
}

public override SyntaxNode? VisitBinaryExpression(BinaryExpressionSyntax node)
{
var result = base.VisitBinaryExpression(node);

if (
result is BinaryExpressionSyntax typedResult
&& typedResult.Kind() is SyntaxKind.AsExpression
&& semanticModel.GetSymbolInfo(node.Right).Symbol is ITypeSymbol namedTypeSymbol
)
{
var fullyQualifiedType = FullyQualifiedIdentifier(namedTypeSymbol);

return typedResult.WithRight(fullyQualifiedType.WithTriviaFrom(typedResult.Right));
}

return result;
}

public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
{
if (
Expand Down
98 changes: 98 additions & 0 deletions test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,104 @@ public static string FooBar(string s)
result.Should().Be(inlinedExpression ?? expression);
}

[Fact]
public void RewriteExpressionContainingCasting()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;
public class Test
{
public bool MapExpression(int value) => ((MyEnum) value & MyEnum.OptionA) > 0;
}
namespace AnotherAssembly {
enum MyEnum
{
OptionA = 1,
OptionB = 2,
}
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("((global::AnotherAssembly.MyEnum) value & global::AnotherAssembly.MyEnum.OptionA) > 0");
}

[Fact]
public void RewriteExpressionContainingBinaryAnd()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;
public class Test
{
public bool MapExpression(MyEnum value) => (value & MyEnum.OptionA) > 0;
}
namespace AnotherAssembly {
enum MyEnum
{
OptionA = 1,
OptionB = 2,
}
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(value & global::AnotherAssembly.MyEnum.OptionA) > 0");
}

[Fact]
public void RewriteExpressionContainingAsStatement()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;
public class Test
{
public int MapExpression(A value) => (value as B).Value;
}
namespace AnotherAssembly {
record A;
record B(int Value) : A;
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(value as global::AnotherAssembly.B).Value");
}

[Fact]
public void RewriteExpressionContainingAsStatementAndCasting()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;
public class Test
{
public string MapExpression(A value) => (string)(object)(value as B).Value;
}
namespace AnotherAssembly {
record A;
record B(string Value) : A;
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(string?)(object?)(value as global::AnotherAssembly.B).Value");
}

private (string Result, bool CanBeInlined) Rewrite([StringSyntax(StringSyntax.CSharp)] string source)
{
var compilation = TestHelper.BuildCompilation(source);
Expand Down

0 comments on commit e259090

Please sign in to comment.