Fix JS source map of decomposed conditional expression

This commit is contained in:
Alexey Andreev
2017-06-22 17:07:56 +03:00
parent b5218915f8
commit 4e18ea2d04
3 changed files with 31 additions and 3 deletions
@@ -232,7 +232,7 @@ internal class ExpressionDecomposer private constructor(
test = accept(test) test = accept(test)
if (then !in containsExtractable && otherwise !in containsExtractable) return if (then !in containsExtractable && otherwise !in containsExtractable) return
val tmp = Temporary() val tmp = Temporary(sourceInfo = source)
addStatement(tmp.variable) addStatement(tmp.variable)
val thenBlock = withNewAdditionalStatements { val thenBlock = withNewAdditionalStatements {
@@ -248,6 +248,7 @@ internal class ExpressionDecomposer private constructor(
} }
val lazyEval = JsIf(test, thenBlock, elseBlock) val lazyEval = JsIf(test, thenBlock, elseBlock)
lazyEval.source = source
lazyEval.synthetic = true lazyEval.synthetic = true
addStatement(lazyEval) addStatement(lazyEval)
ctx.replaceMe(tmp.nameRef) ctx.replaceMe(tmp.nameRef)
@@ -361,19 +362,23 @@ internal class ExpressionDecomposer private constructor(
return tmp.nameRef 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 name: JsName = JsScope.declareTemporary()
val variable: JsVars = newVar(name, value).apply { val variable: JsVars = newVar(name, value).apply {
synthetic = true synthetic = true
name.staticRef = value name.staticRef = value
source = sourceInfo ?: value?.source
} }
val nameRef: JsExpression val nameRef: JsExpression
get() = name.makeRef() get() = name.makeRef()
fun assign(value: JsExpression): JsStatement { 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 return statement
} }
} }
@@ -78,6 +78,12 @@ public class JsLineNumberTestGenerated extends AbstractJsLineNumberTest {
doTest(fileName); 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") @TestMetadata("coroutine.kt")
public void testCoroutine() throws Exception { public void testCoroutine() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/coroutine.kt"); String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/coroutine.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