Don't share instances of JsNameRef nodes to avoid wrong source maps

When to name references to the same name are represented by
a shared JS AST node, setting line number of the second usage may
override line number of the first usage.

Also, supply more JS AST nodes related to default parameters,
with corresponding source information.
This commit is contained in:
Alexey Andreev
2017-05-02 19:44:00 +03:00
parent e5662ac2ad
commit 4332f1cb2c
5 changed files with 43 additions and 3 deletions
@@ -48,6 +48,18 @@ public class JsLineNumberTestGenerated extends AbstractJsLineNumberTest {
doTest(fileName);
}
@TestMetadata("multipleReferences.kt")
public void testMultipleReferences() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/multipleReferences.kt");
doTest(fileName);
}
@TestMetadata("optionalArgs.kt")
public void testOptionalArgs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/optionalArgs.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/lineNumbers/simple.kt");
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -390,7 +390,8 @@ public class TranslationContext {
return nameRef;
}
return aliasingContext.getAliasForDescriptor(descriptor);
JsExpression alias = aliasingContext.getAliasForDescriptor(descriptor);
return alias != null ? alias.deepCopy() : null;
}
@NotNull
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.js.translate.general.Translation;
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator;
import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.ArrayList;
@@ -86,6 +87,7 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
JsStatement assignStatement = assignment(jsNameRef, defaultValue).makeStmt();
JsStatement thenStatement = JsAstUtils.mergeStatementInBlockIfNeeded(assignStatement, defaultArgBlock);
JsBinaryOperation checkArgIsUndefined = equality(jsNameRef, Namer.getUndefinedExpression());
checkArgIsUndefined.source(KotlinSourceElementKt.getPsi(valueParameter.getSource()));
JsIf jsIf = JsAstUtils.newJsIf(checkArgIsUndefined, thenStatement);
result.add(jsIf);
}
@@ -0,0 +1,15 @@
fun box(x: Int) {
println(x)
println("split")
println(x)
println(O.y)
println("split")
println(O.y)
}
object O {
val y = 23
}
// LINES: 2 3 4 6 7 8 12
+10
View File
@@ -0,0 +1,10 @@
fun box(
x: Int = 23,
y: Int =
42
) {
println(x)
println(y)
}
// LINES: 2 2 3 4 6 7