From 4e75cef5c720f4a65f3ebcb46092f977a185c576 Mon Sep 17 00:00:00 2001 From: Tracey Yoshima Date: Wed, 15 Nov 2023 15:48:58 -0500 Subject: [PATCH] Convert j to expression in visitPostfixExpression (#393) --- .../internal/KotlinTreeParserVisitor.java | 2 +- .../org/openrewrite/kotlin/tree/IfTest.java | 32 +++++++++++++++++++ .../kotlin/tree/MethodDeclarationTest.java | 15 --------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 01c3126ef..d72a23a82 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -2731,7 +2731,7 @@ public J visitPostfixExpression(KtPostfixExpression expression, ExecutionContext } else if (type instanceof JavaType.Variable) { type = ((JavaType.Variable) type).getType(); } - J j = requireNonNull(expression.getBaseExpression()).accept(this, data); + J j = convertToExpression(requireNonNull(expression.getBaseExpression()).accept(this, data)); IElementType referencedNameElementType = expression.getOperationReference().getReferencedNameElementType(); if (referencedNameElementType == KtTokens.EXCLEXCL) { // j = j.withMarkers(j.getMarkers().addIfAbsent(new CheckNotNull(randomId(), prefix(expression.getOperationReference())))); diff --git a/src/test/java/org/openrewrite/kotlin/tree/IfTest.java b/src/test/java/org/openrewrite/kotlin/tree/IfTest.java index b82ea4331..e31354213 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/IfTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/IfTest.java @@ -173,4 +173,36 @@ fun foo(choices: List>, peekedHeader: Regex) { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/390") + @Test + void typeParameter() { + rewriteRun( + kotlin( + """ + val enabled = true + fun foo(arg: String = if (enabled) "foo" else "bar"): String { + return arg + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/392") + @Test + void postfixExpression() { + rewriteRun( + kotlin( + """ + fun foo(arg: Boolean): String? { + return if (arg) "" else null + } + fun bar(arg: Boolean) { + if (arg) { foo(true) } else { foo(false) }!! + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java b/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java index 159ff103c..9fef0d7f2 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/MethodDeclarationTest.java @@ -366,21 +366,6 @@ fun method2() { ); } - @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/390") - @Test - void ifExpression() { - rewriteRun( - kotlin( - """ - val enabled = true - fun foo(arg: String = if (enabled) "foo" else "bar"): String { - return arg - } - """ - ) - ); - } - @Test void argumentTrailingComma() { rewriteRun(