From 9456833a3577f7941d608c7b620155c7512a0928 Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Tue, 22 Jul 2014 13:08:31 +0400 Subject: [PATCH] JS Backend: #KT-5320 Fixed (generated code for ternary operator) --- .../backend/js/JsToStringGenerationVisitor.java | 10 +++++++--- .../jetbrains/k2js/test/semantics/MiscTest.java | 4 ++++ .../rightAssocForGeneratedConditionalOperator.kt | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 js/js.translator/testData/expression/misc/cases/rightAssocForGeneratedConditionalOperator.kt diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 6977040853b..28c45fa9aaa 100644 --- a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -365,7 +365,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { // another // ternary expression, but if the test expression is a ternary, it should // get parentheses around it. - printPair(x, x.getTestExpression()); + printPair(x, x.getTestExpression(), true); spaceOpt(); p.print('?'); spaceOpt(); @@ -376,8 +376,8 @@ public class JsToStringGenerationVisitor extends JsVisitor { printPair(x, x.getElseExpression()); } - private void printPair(JsExpression parent, JsExpression expression) { - boolean isNeedParen = parenCalc(parent, expression, false); + private void printPair(JsExpression parent, JsExpression expression, boolean wrongAssoc) { + boolean isNeedParen = parenCalc(parent, expression, wrongAssoc); if (isNeedParen) { leftParen(); } @@ -387,6 +387,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { } } + private void printPair(JsExpression parent, JsExpression expression) { + printPair(parent, expression, false); + } + @Override public void visitDebugger(JsDebugger x) { p.print(CHARS_DEBUGGER); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java index a6f3f105edf..e0e7743e915 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java @@ -194,4 +194,8 @@ public final class MiscTest extends AbstractExpressionTest { public void testKt5058() throws Exception { checkFooBoxIsTrue("KT-5058.kt"); } + + public void testRightAssocForGeneratedConditionalOperator() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/expression/misc/cases/rightAssocForGeneratedConditionalOperator.kt b/js/js.translator/testData/expression/misc/cases/rightAssocForGeneratedConditionalOperator.kt new file mode 100644 index 00000000000..5aed2fd8565 --- /dev/null +++ b/js/js.translator/testData/expression/misc/cases/rightAssocForGeneratedConditionalOperator.kt @@ -0,0 +1,14 @@ +// http://youtrack.jetbrains.com/issue/KT-5320 +// KT-5320 Invalid JS code generated for typecast inside ternary operator + +package foo + +fun test(x: Any?): Int = if (x as Boolean) 1 else 2; + +fun box(): String { + assertEquals(1, test(true), "true") + assertEquals(2, test(false), "false") + assertEquals("OK", if (if (0 < 1) false else true) "Not OK" else "OK") + + return "OK" +} \ No newline at end of file