From 4e18ea2d047abddf0f17cd919475e789c0dfbe37 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 22 Jun 2017 17:07:56 +0300 Subject: [PATCH] Fix JS source map of decomposed conditional expression --- .../kotlin/js/inline/ExpressionDecomposer.kt | 11 ++++++++--- .../js/test/JsLineNumberTestGenerated.java | 6 ++++++ .../lineNumbers/conditionalDecomposed.kt | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 js/js.translator/testData/lineNumbers/conditionalDecomposed.kt diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/ExpressionDecomposer.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/ExpressionDecomposer.kt index 35737d71645..2c9bfc3aa53 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/ExpressionDecomposer.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/ExpressionDecomposer.kt @@ -232,7 +232,7 @@ internal class ExpressionDecomposer private constructor( test = accept(test) if (then !in containsExtractable && otherwise !in containsExtractable) return - val tmp = Temporary() + val tmp = Temporary(sourceInfo = source) addStatement(tmp.variable) val thenBlock = withNewAdditionalStatements { @@ -248,6 +248,7 @@ internal class ExpressionDecomposer private constructor( } val lazyEval = JsIf(test, thenBlock, elseBlock) + lazyEval.source = source lazyEval.synthetic = true addStatement(lazyEval) ctx.replaceMe(tmp.nameRef) @@ -361,19 +362,23 @@ internal class ExpressionDecomposer private constructor( return tmp.nameRef } - private inner class Temporary(val value: JsExpression? = null) { + private inner class Temporary(val value: JsExpression? = null, val sourceInfo: Any? = null) { val name: JsName = JsScope.declareTemporary() val variable: JsVars = newVar(name, value).apply { synthetic = true name.staticRef = value + source = sourceInfo ?: value?.source } val nameRef: JsExpression get() = name.makeRef() fun assign(value: JsExpression): JsStatement { - val statement = JsExpressionStatement(assignment(nameRef, value)).apply { synthetic = true } + val statement = JsExpressionStatement(assignment(nameRef, value)).apply { + synthetic = true + expression.source = sourceInfo ?: value.source + } return statement } } 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 73a68c3ff4a..30d28f753d0 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 @@ -78,6 +78,12 @@ public class JsLineNumberTestGenerated extends AbstractJsLineNumberTest { doTest(fileName); } + @TestMetadata("conditionalDecomposed.kt") + public void testConditionalDecomposed() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/conditionalDecomposed.kt"); + doTest(fileName); + } + @TestMetadata("coroutine.kt") public void testCoroutine() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/coroutine.kt"); diff --git a/js/js.translator/testData/lineNumbers/conditionalDecomposed.kt b/js/js.translator/testData/lineNumbers/conditionalDecomposed.kt new file mode 100644 index 00000000000..7990dcddc64 --- /dev/null +++ b/js/js.translator/testData/lineNumbers/conditionalDecomposed.kt @@ -0,0 +1,17 @@ +fun box(x: Int) { + println( + if ( + x > 100 + ) + 42 + else + foo() + ) +} + +private inline fun foo(): Int { + println("foo") + return 23 +} + +// LINES: 3 3 4 3 6 13 3 14 2 13 14 \ No newline at end of file