Fix JS source maps for objects and enums
This commit is contained in:
@@ -132,6 +132,8 @@ class LineCollector : RecursiveJsVisitor() {
|
||||
|
||||
override fun visit(x: JsSwitch) {
|
||||
withStatement(x) {
|
||||
handleNodeLocation(x)
|
||||
lineNumbersByStatement[x]?.add(-1)
|
||||
x.expression.accept(this)
|
||||
x.cases.forEach { accept(it) }
|
||||
}
|
||||
|
||||
+14
-15
@@ -156,12 +156,11 @@ class ClassTranslator private constructor(
|
||||
}
|
||||
|
||||
private fun createEnumInitFunction(): JsFunction {
|
||||
val function = context().createRootScopedFunction(descriptor)
|
||||
val function = context().createRootScopedFunction(descriptor).withDefaultLocation()
|
||||
function.name = JsScope.declareTemporaryName(StaticContext.getSuggestedName(descriptor) + "_initFields")
|
||||
function.source = classDeclaration
|
||||
val emptyFunction = context().createRootScopedFunction(descriptor)
|
||||
val emptyFunction = context().createRootScopedFunction(descriptor).withDefaultLocation()
|
||||
function.body.statements += JsAstUtils.assignment(JsAstUtils.pureFqn(function.name, null), emptyFunction)
|
||||
.source(classDeclaration).makeStmt()
|
||||
.withDefaultLocation().makeStmt()
|
||||
context().addDeclarationStatement(function.makeStmt())
|
||||
return function
|
||||
}
|
||||
@@ -243,7 +242,7 @@ class ClassTranslator private constructor(
|
||||
val nameParamName = JsScope.declareTemporaryName("name")
|
||||
val ordinalParamName = JsScope.declareTemporaryName("ordinal")
|
||||
constructorInitializer.parameters.addAll(0, listOf(JsParameter(nameParamName), JsParameter(ordinalParamName)))
|
||||
leadingArgs += listOf(nameParamName.makeRef(), ordinalParamName.makeRef())
|
||||
leadingArgs += listOf(nameParamName.makeRef().withDefaultLocation(), ordinalParamName.makeRef().withDefaultLocation())
|
||||
}
|
||||
if (outerClassName != null) {
|
||||
constructorInitializer.parameters.add(0, JsParameter(outerClassName))
|
||||
@@ -287,7 +286,7 @@ class ClassTranslator private constructor(
|
||||
usageTracker.getNameForCapturedDescriptor(it)!!.makeRef()
|
||||
}
|
||||
it += JsInvocation(Namer.getFunctionCallRef(referenceToClass), listOf(thisNameRef.deepCopy()) + closure + leadingArgs)
|
||||
.source(classDeclaration).makeStmt()
|
||||
.withDefaultLocation().makeStmt()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,31 +447,29 @@ class ClassTranslator private constructor(
|
||||
|
||||
private fun addObjectCache(statements: MutableList<JsStatement>) {
|
||||
cachedInstanceName = JsScope.declareTemporaryName(StaticContext.getSuggestedName(descriptor) + Namer.OBJECT_INSTANCE_VAR_SUFFIX)
|
||||
statements += JsAstUtils.assignment(cachedInstanceName.makeRef(), JsThisRef()).source(classDeclaration).makeStmt()
|
||||
statements += JsAstUtils.assignment(cachedInstanceName.makeRef(), JsThisRef()).withDefaultLocation().makeStmt()
|
||||
}
|
||||
|
||||
private fun addObjectMethods() {
|
||||
context().addDeclarationStatement(JsAstUtils.newVar(cachedInstanceName, JsNullLiteral()))
|
||||
|
||||
val instanceFun = context().createRootScopedFunction("Instance function: " + descriptor)
|
||||
val instanceFun = context().createRootScopedFunction("Instance function: " + descriptor).withDefaultLocation()
|
||||
instanceFun.name = context().getNameForObjectInstance(descriptor)
|
||||
instanceFun.source = classDeclaration
|
||||
|
||||
if (enumInitializerName != null) {
|
||||
instanceFun.body.statements += JsInvocation(pureFqn(enumInitializerName, null)).source(classDeclaration).makeStmt()
|
||||
instanceFun.body.statements += JsInvocation(pureFqn(enumInitializerName, null)).withDefaultLocation().makeStmt()
|
||||
}
|
||||
if (descriptor.kind != ClassKind.ENUM_ENTRY) {
|
||||
val instanceCreatedCondition = JsAstUtils.equality(cachedInstanceName.makeRef(), JsNullLiteral())
|
||||
.source(classDeclaration)
|
||||
val instanceCreatedCondition = JsAstUtils.equality(cachedInstanceName.makeRef(), JsNullLiteral()).withDefaultLocation()
|
||||
val instanceCreationBlock = JsBlock()
|
||||
val instanceCreatedGuard = JsIf(instanceCreatedCondition, instanceCreationBlock)
|
||||
val instanceCreatedGuard = JsIf(instanceCreatedCondition, instanceCreationBlock).withDefaultLocation()
|
||||
instanceFun.body.statements += instanceCreatedGuard
|
||||
|
||||
val objectRef = context().getInnerReference(descriptor)
|
||||
instanceCreationBlock.statements += JsNew(objectRef).source(classDeclaration).makeStmt()
|
||||
instanceCreationBlock.statements += JsNew(objectRef).withDefaultLocation().makeStmt()
|
||||
}
|
||||
|
||||
instanceFun.body.statements += JsReturn(cachedInstanceName.makeRef().source(classDeclaration))
|
||||
instanceFun.body.statements += JsReturn(cachedInstanceName.makeRef().withDefaultLocation()).withDefaultLocation()
|
||||
|
||||
context().addDeclarationStatement(instanceFun.makeStmt())
|
||||
}
|
||||
@@ -498,6 +495,8 @@ class ClassTranslator private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T : JsNode> T.withDefaultLocation(): T = apply { source = classDeclaration }
|
||||
|
||||
companion object {
|
||||
@JvmStatic fun translate(classDeclaration: KtClassOrObject, context: TranslationContext, enumInitializerName: JsName?) {
|
||||
return ClassTranslator(classDeclaration, context, enumInitializerName, null).translate()
|
||||
|
||||
+3
-2
@@ -72,7 +72,8 @@ class DeclarationBodyVisitor(
|
||||
assert(supertypes.size == 1) { "Simple Enum entry must have one supertype" }
|
||||
val jsEnumEntryCreation = ClassInitializerTranslator.generateEnumEntryInstanceCreation(context, enumEntry, enumEntryOrdinal)
|
||||
context.addDeclarationStatement(JsAstUtils.newVar(enumInstanceName, null))
|
||||
enumInitializer.body.statements += JsAstUtils.assignment(pureFqn(enumInstanceName, null), jsEnumEntryCreation).makeStmt()
|
||||
enumInitializer.body.statements += JsAstUtils.assignment(pureFqn(enumInstanceName, null), jsEnumEntryCreation)
|
||||
.source(enumEntry).makeStmt()
|
||||
|
||||
val enumInstanceFunction = context.createRootScopedFunction(descriptor)
|
||||
enumInstanceFunction.source = enumEntry
|
||||
@@ -80,7 +81,7 @@ class DeclarationBodyVisitor(
|
||||
context.addDeclarationStatement(enumInstanceFunction.makeStmt())
|
||||
|
||||
enumInstanceFunction.body.statements += JsInvocation(pureFqn(enumInitializer.name, null)).source(enumEntry).makeStmt()
|
||||
enumInstanceFunction.body.statements += JsReturn(enumInstanceName.makeRef().source(enumEntry))
|
||||
enumInstanceFunction.body.statements += JsReturn(enumInstanceName.makeRef().source(enumEntry)).apply { source = enumEntry }
|
||||
}
|
||||
|
||||
context.export(descriptor)
|
||||
|
||||
+9
-5
@@ -44,7 +44,7 @@ class EnumTranslator(
|
||||
val values = entries.map {
|
||||
JsInvocation(JsAstUtils.pureFqn(context().getNameForObjectInstance(it), null)).source(psi)
|
||||
}
|
||||
function.body.statements += JsReturn(JsArrayLiteral(values))
|
||||
function.body.statements += JsReturn(JsArrayLiteral(values).source(psi)).apply { source = psi }
|
||||
}
|
||||
|
||||
private fun generateValueOfFunction() {
|
||||
@@ -55,8 +55,9 @@ class EnumTranslator(
|
||||
|
||||
val clauses = entries.map { entry ->
|
||||
JsCase().apply {
|
||||
caseExpression = JsStringLiteral(entry.name.asString())
|
||||
caseExpression = JsStringLiteral(entry.name.asString()).source(psi)
|
||||
statements += JsReturn(JsInvocation(JsAstUtils.pureFqn(context().getNameForObjectInstance(entry), null)).source(psi))
|
||||
.apply { source = psi }
|
||||
source = psi
|
||||
}
|
||||
}
|
||||
@@ -67,18 +68,21 @@ class EnumTranslator(
|
||||
val throwStatement = JsExpressionStatement(JsInvocation(Namer.throwIllegalStateExceptionFunRef(), message).source(psi))
|
||||
|
||||
if (clauses.isNotEmpty()) {
|
||||
val defaultCase = JsDefault().apply { statements += throwStatement }
|
||||
function.body.statements += JsSwitch(nameParam.makeRef().source(psi), clauses + defaultCase)
|
||||
val defaultCase = JsDefault().apply {
|
||||
statements += throwStatement
|
||||
source = psi
|
||||
}
|
||||
function.body.statements += JsSwitch(nameParam.makeRef().source(psi), clauses + defaultCase).apply { source = psi }
|
||||
}
|
||||
else {
|
||||
function.body.statements += throwStatement
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun createFunction(functionDescriptor: FunctionDescriptor): JsFunction {
|
||||
val function = context().getFunctionObject(functionDescriptor)
|
||||
function.name = context().getInnerNameForDescriptor(functionDescriptor)
|
||||
function.source = psi
|
||||
context().addDeclarationStatement(function.makeStmt())
|
||||
|
||||
val classRef = context().getInnerReference(descriptor)
|
||||
|
||||
@@ -356,7 +356,7 @@ public final class JsAstUtils {
|
||||
|
||||
@NotNull
|
||||
public static JsStatement assignmentToThisField(@NotNull String fieldName, @NotNull JsExpression right) {
|
||||
return assignment(new JsNameRef(fieldName, new JsThisRef()), right).makeStmt();
|
||||
return assignment(new JsNameRef(fieldName, new JsThisRef()), right).source(right.getSource()).makeStmt();
|
||||
}
|
||||
|
||||
public static JsStatement asSyntheticStatement(@NotNull JsExpression expression) {
|
||||
|
||||
@@ -6,4 +6,4 @@ enum class Foo {
|
||||
}
|
||||
}
|
||||
|
||||
// LINES: 1 1 1 1 1 1 2 4 * 2 2 2 4 4 5 5 * 4 4 4 4 4 * 1 1 * 1 1 1 1
|
||||
// LINES: 1 1 1 1 1 1 1 1 1 2 2 4 * 2 2 2 2 4 4 5 5 * 4 4 4 4 4 4 4 * 1 1 1 * 1 1 1 1 1 1
|
||||
+1
-1
@@ -10,4 +10,4 @@ enum class E {
|
||||
}
|
||||
}
|
||||
|
||||
// LINES: 1 1 1 1 1 1 2 4 8 * 2 2 2 4 4 4 5 5 5 * 4 4 4 8 8 8 9 9 9 * 8 8 8 * 1 1 * 1 1 1 1 1 1
|
||||
// LINES: 1 1 1 1 1 1 1 1 1 2 2 4 8 * 2 2 2 2 4 4 4 5 5 5 * 4 4 4 4 8 8 8 9 9 9 * 8 8 8 8 * 1 1 1 * 1 1 1 1 1 1 1 1 1 1
|
||||
@@ -12,4 +12,4 @@ object O {
|
||||
val y = 23
|
||||
}
|
||||
|
||||
// LINES: 9 2 2 3 3 4 4 6 6 7 7 8 8 11 11 12 12 * 11 11 11 11
|
||||
// LINES: 9 2 2 3 3 4 4 6 6 7 7 8 8 11 11 12 12 * 11 11 11 11 11 11
|
||||
|
||||
@@ -4,4 +4,4 @@ fun foo() {
|
||||
println("foo")
|
||||
}
|
||||
|
||||
// LINES: 1 1 * 1 1 1 1 5 4 4
|
||||
// LINES: 1 1 * 1 1 1 1 1 1 5 4 4
|
||||
@@ -25,4 +25,4 @@ class C : A {
|
||||
)
|
||||
}
|
||||
|
||||
// LINES: 1 1 3 * 1 1 1 1 7 * 9 10 11 * 15 15 16 17 14 19 15 15 22 22 23 24 22 22
|
||||
// LINES: 1 1 3 * 1 1 1 1 1 1 7 * 9 10 11 * 15 15 16 17 14 19 15 15 22 22 23 24 22 22
|
||||
@@ -8,4 +8,4 @@ enum class E {
|
||||
}
|
||||
}
|
||||
|
||||
// LINES: 1 1 1 1 1 1 2 3 4 * 2 2 2 * 3 3 3 4 4 4 6 6 * 4 4 4 * 1 1 * 1 1 1 1 1 1
|
||||
// LINES: 1 1 1 1 1 1 1 1 1 2 2 3 3 4 * 2 2 2 2 * 3 3 3 3 4 4 4 6 6 * 4 4 4 4 * 1 1 1 * 1 1 1 1 1 1 1 1 1 1
|
||||
Reference in New Issue
Block a user