Skip to content

Commit

Permalink
Merge pull request #62 from openrewrite/add_suspend_modifier
Browse files Browse the repository at this point in the history
Added support of suspend modifier on lambdas.
  • Loading branch information
traceyyoshima authored Apr 3, 2023
2 parents 7093527 + 2cfafc4 commit 7c4cdcb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("(");
Expand Down Expand Up @@ -1350,6 +1356,7 @@ public J visitFunctionTypeRef(FirFunctionTypeRef functionTypeRef, ExecutionConte
return new K.FunctionType(
randomId(),
lambda,
suspendModifier,
receiver);
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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(",")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public J visitBinary(K.Binary binary, PrintOutputCapture<P> p) {

@Override
public J visitFunctionType(K.FunctionType functionType, PrintOutputCapture<P> p) {
if (functionType.getSuspendModifier() != null) {
visitAnnotation(functionType.getSuspendModifier(), p);
}
if (functionType.getReceiver() != null) {
visitRightPadded(functionType.getReceiver(), KRightPadded.Location.FUNCTION_TYPE_RECEIVER, p);
p.append(".");
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<NameTree> receiver;

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/openrewrite/kotlin/tree/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -87,7 +86,6 @@ fun method ( ) {

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/56")
@Test
@ExpectedToFail
void suspendLambdaWithParameter() {
rewriteRun(
kotlin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -199,7 +198,6 @@ void typeParameterAndTypeReceiver() {

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/56")
@Test
@ExpectedToFail
void lambdaMethodParameterWithModifier() {
rewriteRun(
kotlin(
Expand Down

0 comments on commit 7c4cdcb

Please sign in to comment.