From 2cfafc4f22e34a68be2b07fead167f76d88a1d2a Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Mon, 3 Apr 2023 15:32:15 -0600 Subject: [PATCH] Added support of suspend modifier on lambdas. fixes #56 #60 --- .../kotlin/internal/KotlinParserVisitor.java | 13 ++++++++++--- .../kotlin/internal/KotlinPrinter.java | 3 +++ .../java/org/openrewrite/kotlin/tree/K.java | 17 ++++++----------- .../org/openrewrite/kotlin/tree/LambdaTest.java | 2 -- .../kotlin/tree/MethodDeclarationTest.java | 2 -- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinParserVisitor.java index f97c82f81..320cdadce 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinParserVisitor.java @@ -1299,6 +1299,12 @@ public J visitFunctionTypeRef(FirFunctionTypeRef functionTypeRef, ExecutionConte skip("."); } + J.Annotation suspendModifier = null; + if (functionTypeRef.isSuspend()) { + Space suspendPrefix = whitespace(); + suspendModifier = convertToAnnotation(mapModifier(suspendPrefix, emptyList())); + } + Space prefix = whitespace(); boolean parenthesized = source.charAt(cursor) == '('; skip("("); @@ -1350,6 +1356,7 @@ public J visitFunctionTypeRef(FirFunctionTypeRef functionTypeRef, ExecutionConte return new K.FunctionType( randomId(), lambda, + suspendModifier, receiver); } @@ -1493,7 +1500,7 @@ public J visitProperty(FirProperty property, ExecutionContext ctx) { if (j instanceof TypeTree) { typeExpression = (TypeTree) j; } else { - typeExpression = new K.FunctionType(randomId(), (TypedTree) j, null); + typeExpression = new K.FunctionType(randomId(), (TypedTree) j, null, null); } } } @@ -2162,7 +2169,7 @@ public J visitValueParameter(FirValueParameter valueParameter, ExecutionContext if (j instanceof TypeTree) { typeExpression = (TypeTree) j; } else { - typeExpression = new K.FunctionType(randomId(), (TypedTree) j, null); + typeExpression = new K.FunctionType(randomId(), (TypedTree) j, null, null); } } } @@ -2904,7 +2911,7 @@ public J visitRegularClass(FirRegularClass regularClass, ExecutionContext ctx) { null, null ); - element = new K.FunctionType(randomId(), newClass, null); + element = new K.FunctionType(randomId(), newClass, null, null); } superTypes.add(JRightPadded.build(element) .withAfter(i == regularClass.getSuperTypeRefs().size() - 1 ? EMPTY : sourceBefore(","))); diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java index 7a5b31d78..4f9d310de 100755 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java @@ -112,6 +112,9 @@ public J visitBinary(K.Binary binary, PrintOutputCapture

p) { @Override public J visitFunctionType(K.FunctionType functionType, PrintOutputCapture

p) { + if (functionType.getSuspendModifier() != null) { + visitAnnotation(functionType.getSuspendModifier(), p); + } if (functionType.getReceiver() != null) { visitRightPadded(functionType.getReceiver(), KRightPadded.Location.FUNCTION_TYPE_RECEIVER, p); p.append("."); diff --git a/src/main/java/org/openrewrite/kotlin/tree/K.java b/src/main/java/org/openrewrite/kotlin/tree/K.java index 55267c9da..fd08fa7d4 100644 --- a/src/main/java/org/openrewrite/kotlin/tree/K.java +++ b/src/main/java/org/openrewrite/kotlin/tree/K.java @@ -439,23 +439,18 @@ public CoordinateBuilder.Statement getCoordinates() { } @SuppressWarnings("unchecked") - @ToString - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @Value @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) - @AllArgsConstructor - final class FunctionType implements K, Expression, Statement, TypeTree { + @With + class FunctionType implements K, Expression, Statement, TypeTree { - @With - @Getter UUID id; - - @With - @Getter TypedTree typedTree; @Nullable - @With - @Getter + J.Annotation suspendModifier; + + @Nullable JRightPadded receiver; @Override diff --git a/src/test/java/org/openrewrite/kotlin/tree/LambdaTest.java b/src/test/java/org/openrewrite/kotlin/tree/LambdaTest.java index e3104a38a..e0c319a39 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/LambdaTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/LambdaTest.java @@ -72,7 +72,6 @@ fun inputValues ( ) : List < Pair < String , Any ? > > { @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/60") @Test - @ExpectedToFail void suspendLambda() { rewriteRun( kotlin( @@ -87,7 +86,6 @@ fun method ( ) { @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/56") @Test - @ExpectedToFail void suspendLambdaWithParameter() { rewriteRun( kotlin( diff --git a/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java b/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java index 4f9a9b236..154f19ada 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java @@ -16,7 +16,6 @@ package org.openrewrite.kotlin.tree; import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.ExpectedToFail; import org.openrewrite.Issue; import org.openrewrite.test.RewriteTest; @@ -199,7 +198,6 @@ void typeParameterAndTypeReceiver() { @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/56") @Test - @ExpectedToFail void lambdaMethodParameterWithModifier() { rewriteRun( kotlin(