Add module name as a prefix to declaration keys in JS translator

This is necessary due to different modules can have same
package declarations. When importing declarations from these
packages, we should distinguish from which module we are importing it.

See KT-18652
This commit is contained in:
Alexey Andreev
2017-06-26 12:06:23 +03:00
parent 3331be9cc8
commit 20842dcc44
3 changed files with 64 additions and 1 deletions
@@ -5618,6 +5618,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("samePackageNames.kt")
public void testSamePackageNames() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/multiModule/samePackageNames.kt");
doTest(fileName);
}
@TestMetadata("useElementsFromDefaultPackageInAnotherModule.kt") @TestMetadata("useElementsFromDefaultPackageInAnotherModule.kt")
public void testUseElementsFromDefaultPackageInAnotherModule() throws Exception { public void testUseElementsFromDefaultPackageInAnotherModule() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/multiModule/useElementsFromDefaultPackageInAnotherModule.kt"); String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/multiModule/useElementsFromDefaultPackageInAnotherModule.kt");
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.js.translate.utils
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.js.naming.encodeSignature import org.jetbrains.kotlin.js.naming.encodeSignature
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.module
// Unfortunately, our descriptor serializer can't serialize references to descriptors. // Unfortunately, our descriptor serializer can't serialize references to descriptors.
// We have to serialize entire class or package to serialize function or property. // We have to serialize entire class or package to serialize function or property.
@@ -50,7 +51,9 @@ fun generateSignature(descriptor: DeclarationDescriptor): String? {
parent + separator + escape(descriptor.name.asString()) + "|" + encodeSignature(descriptor) parent + separator + escape(descriptor.name.asString()) + "|" + encodeSignature(descriptor)
} }
is PackageFragmentDescriptor -> { is PackageFragmentDescriptor -> {
if (descriptor.fqName.isRoot) "" else escape(descriptor.fqName.pathSegments().map { escape(it.identifier) }.joinToString(".")) val module = descriptor.module.name.asString()
val parts = sequenceOf(module) + descriptor.fqName.pathSegments().map { it.identifier }
parts.joinToString(".") { escape(it) }
} }
is ClassDescriptor -> { is ClassDescriptor -> {
val parent = generateSignature(descriptor.containingDeclaration) ?: return null val parent = generateSignature(descriptor.containingDeclaration) ?: return null
@@ -0,0 +1,54 @@
// EXPECTED_REACHABLE_NODES: 511
// MODULE: lib1
// FILE: lib1.kt
package pkg
object O {
fun f() = "O.f"
}
fun foo() = "foo"
class A {
fun f() = "A.f"
}
// MODULE: lib2
// FILE: lib2.kt
package pkg
object P {
fun f() = "P.f"
}
fun bar() = "bar"
class B {
fun f() = "B.f"
}
// MODULE: main(lib1, lib2)
// FILE: main.kt
import pkg.*
fun box(): String {
var r = O.f()
if (r != "O.f") return "fail1: $r"
r = foo()
if (r != "foo") return "fail2: $r"
r = A().f()
if (r != "A.f") return "fail3: $r"
r = P.f()
if (r != "P.f") return "fail4: $r"
r = bar()
if (r != "bar") return "fail5: $r"
r = B().f()
if (r != "B.f") return "fail6: $r"
return "OK"
}