From 1111f2d5cdd3ce9f7f7f821f61cf8c840ecd12c0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 12 May 2017 14:56:20 +0300 Subject: [PATCH] Fix JS source maps for while/do..while statements --- .../js/test/JsLineNumberTestGenerated.java | 12 ++++++++++++ .../kotlin/js/test/utils/LineCollector.kt | 3 +++ .../js/translate/expression/LoopTranslator.kt | 11 ++++++----- .../lineNumbers/doWhileWithComplexCondition.kt | 16 ++++++++++++++++ .../lineNumbers/whileWithComplexCondition.kt | 15 +++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 js/js.translator/testData/lineNumbers/doWhileWithComplexCondition.kt create mode 100644 js/js.translator/testData/lineNumbers/whileWithComplexCondition.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/JsLineNumberTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsLineNumberTestGenerated.java index 6721ac43a9b..042c9d4e2db 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/JsLineNumberTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsLineNumberTestGenerated.java @@ -66,6 +66,12 @@ public class JsLineNumberTestGenerated extends AbstractJsLineNumberTest { doTest(fileName); } + @TestMetadata("doWhileWithComplexCondition.kt") + public void testDoWhileWithComplexCondition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/doWhileWithComplexCondition.kt"); + doTest(fileName); + } + @TestMetadata("for.kt") public void testFor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/for.kt"); @@ -185,4 +191,10 @@ public class JsLineNumberTestGenerated extends AbstractJsLineNumberTest { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/whenIs.kt"); doTest(fileName); } + + @TestMetadata("whileWithComplexCondition.kt") + public void testWhileWithComplexCondition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/whileWithComplexCondition.kt"); + doTest(fileName); + } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/LineCollector.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/LineCollector.kt index 2cad8d7df0e..8e4d1be89de 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/LineCollector.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/LineCollector.kt @@ -60,6 +60,7 @@ class LineCollector : RecursiveJsVisitor() { override fun visitIf(x: JsIf) { withStatement(x) { + handleNodeLocation(x) x.ifExpression.accept(this) } x.thenStatement.accept(this) @@ -68,6 +69,7 @@ class LineCollector : RecursiveJsVisitor() { override fun visitWhile(x: JsWhile) { withStatement(x) { + handleNodeLocation(x) x.condition.accept(this) } x.body.accept(this) @@ -75,6 +77,7 @@ class LineCollector : RecursiveJsVisitor() { override fun visitDoWhile(x: JsDoWhile) { withStatement(x) { + handleNodeLocation(x) x.condition.accept(this) } x.body.accept(this) diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/LoopTranslator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/LoopTranslator.kt index dd82e473728..5347dc63e0b 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/LoopTranslator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/LoopTranslator.kt @@ -51,17 +51,18 @@ fun createWhile(doWhile: Boolean, expression: KtWhileExpressionBase, context: Tr JsEmpty if (!conditionBlock.isEmpty) { - val breakIfConditionIsFalseStatement = JsIf(not(jsCondition), JsBreak()) + val breakIfConditionIsFalseStatement = JsIf(not(jsCondition), JsBreak().apply { source = expression }) + .apply { source = expression } val bodyBlock = convertToBlock(bodyStatement) jsCondition = JsBooleanLiteral(true) if (doWhile) { // translate to: tmpSecondRun = false; // do { if(tmpSecondRun) { if(!tmpExprVar) break; } else tmpSecondRun=true; } while(true) - val secondRun = context.defineTemporary(JsBooleanLiteral(false)) + val secondRun = context.defineTemporary(JsBooleanLiteral(false).source(expression)) conditionBlock.statements.add(breakIfConditionIsFalseStatement) - val ifStatement = JsIf(secondRun, conditionBlock, assignment(secondRun, JsBooleanLiteral(true)).makeStmt()) - bodyBlock.statements.add(0, ifStatement) + val ifStatement = JsIf(secondRun, conditionBlock, assignment(secondRun, JsBooleanLiteral(true)).source(expression).makeStmt()) + bodyBlock.statements.add(0, ifStatement.apply { source = expression }) } else { conditionBlock.statements.add(breakIfConditionIsFalseStatement) @@ -74,7 +75,7 @@ fun createWhile(doWhile: Boolean, expression: KtWhileExpressionBase, context: Tr val result = if (doWhile) JsDoWhile() else JsWhile() result.condition = jsCondition result.body = bodyStatement - return result.source(expression)!! + return result.source(expression) } fun translateForExpression(expression: KtForExpression, context: TranslationContext): JsStatement { diff --git a/js/js.translator/testData/lineNumbers/doWhileWithComplexCondition.kt b/js/js.translator/testData/lineNumbers/doWhileWithComplexCondition.kt new file mode 100644 index 00000000000..50d4ff5dfc4 --- /dev/null +++ b/js/js.translator/testData/lineNumbers/doWhileWithComplexCondition.kt @@ -0,0 +1,16 @@ +fun box() { + var i = 0 + do { + println("body: $i") + } + while( + try { + i++ + } + finally { + println("finally: $i") + } < i + ) +} + +// LINES: 8 3 2 3 3 3 8 11 3 7 12 3 3 4 \ No newline at end of file diff --git a/js/js.translator/testData/lineNumbers/whileWithComplexCondition.kt b/js/js.translator/testData/lineNumbers/whileWithComplexCondition.kt new file mode 100644 index 00000000000..a926551781e --- /dev/null +++ b/js/js.translator/testData/lineNumbers/whileWithComplexCondition.kt @@ -0,0 +1,15 @@ +fun box() { + var i = 0 + while( + try { + i++ + } + finally { + println("finally: $i") + } < i + ) { + println("body: $i") + } +} + +// LINES: 5 2 3 5 8 3 4 9 3 11 \ No newline at end of file