JS: fix error in generation of if/else statements in some cases

See KT-19495
This commit is contained in:
Alexey Andreev
2017-09-07 19:21:14 +03:00
parent 4779f4fefb
commit ff0efe59f6
3 changed files with 43 additions and 1 deletions
@@ -644,7 +644,7 @@ public class JsToStringGenerationVisitor extends JsVisitor {
JsStatement thenStmt = x.getThenStatement();
JsStatement elseStatement = x.getElseStatement();
if (elseStatement != null && thenStmt instanceof JsIf && ((JsIf)thenStmt).getElseStatement() == null) {
if (elseStatement != null && isIfWithoutElse(thenStmt)) {
thenStmt = new JsBlock(thenStmt);
}
nestedPush(thenStmt);
@@ -680,6 +680,18 @@ public class JsToStringGenerationVisitor extends JsVisitor {
}
}
private static boolean isIfWithoutElse(@NotNull JsStatement statement) {
while (statement instanceof JsIf) {
JsIf ifStatement = (JsIf) statement;
if (ifStatement.getElseStatement() == null) {
return true;
}
statement = ifStatement.getElseStatement();
}
return false;
}
@Override
public void visitInvocation(@NotNull JsInvocation invocation) {
pushSourceInfo(invocation.getSource());
@@ -2578,6 +2578,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("ifElseCurlyBraces.kt")
public void testIfElseCurlyBraces() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/if/ifElseCurlyBraces.kt");
doTest(fileName);
}
@TestMetadata("ifInsideLambda.kt")
public void testIfInsideLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/if/ifInsideLambda.kt");
@@ -0,0 +1,24 @@
// EXPECTED_REACHABLE_NODES: 996
var log = ""
fun foo(s: String, a: Int, b: Int) {
when (s) {
"A" -> if (a > b) {
log += "1"
}
else if (b > a) {
log += "2"
}
"B" -> log += "3"
}
}
fun box(): String {
foo("A", 3, 2)
foo("A", 2, 3)
foo("B", 2, 3)
if (log != "123") return "fail: $log"
return "OK"
}