JS backend: support named class objects
Temporary solution until named objects in classes are supported in js backend
This commit is contained in:
@@ -43,4 +43,8 @@ public final class ClassObjectTest extends SingleFileTranslationTest {
|
|||||||
public void testAccessing() throws Exception {
|
public void testAccessing() throws Exception {
|
||||||
checkFooBoxIsOk();
|
checkFooBoxIsOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testNamedClassObject() throws Exception {
|
||||||
|
checkFooBoxIsOk();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-1
@@ -22,10 +22,12 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.jetbrains.kotlin.descriptors.*;
|
import org.jetbrains.kotlin.descriptors.*;
|
||||||
import org.jetbrains.kotlin.js.translate.context.Namer;
|
import org.jetbrains.kotlin.js.translate.context.Namer;
|
||||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils;
|
||||||
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
|
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
|
||||||
import org.jetbrains.kotlin.psi.JetExpression;
|
import org.jetbrains.kotlin.psi.JetExpression;
|
||||||
import org.jetbrains.kotlin.psi.JetQualifiedExpression;
|
import org.jetbrains.kotlin.psi.JetQualifiedExpression;
|
||||||
import org.jetbrains.kotlin.psi.JetSimpleNameExpression;
|
import org.jetbrains.kotlin.psi.JetSimpleNameExpression;
|
||||||
|
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
|
import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
|
||||||
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.setQualifier;
|
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.setQualifier;
|
||||||
@@ -51,8 +53,12 @@ public final class ReferenceTranslator {
|
|||||||
) {
|
) {
|
||||||
JsExpression simpleName = translateSimpleName(expression, context);
|
JsExpression simpleName = translateSimpleName(expression, context);
|
||||||
|
|
||||||
// Ignore qualifier if expression is EnumEntry and use always use FQ name.
|
// Ignore qualifier if expression is EnumEntry or default object reference and use always use FQ name.
|
||||||
DeclarationDescriptor descriptor = BindingUtils.getDescriptorForReferenceExpression(context.bindingContext(), expression);
|
DeclarationDescriptor descriptor = BindingUtils.getDescriptorForReferenceExpression(context.bindingContext(), expression);
|
||||||
|
//TODO: should go away when objects inside classes are supported
|
||||||
|
if (DescriptorUtils.isClassObject(descriptor) && !AnnotationsUtils.isNativeObject(descriptor)) {
|
||||||
|
return simpleName;
|
||||||
|
}
|
||||||
if (descriptor instanceof ClassDescriptor) {
|
if (descriptor instanceof ClassDescriptor) {
|
||||||
ClassDescriptor entryClass = (ClassDescriptor) descriptor;
|
ClassDescriptor entryClass = (ClassDescriptor) descriptor;
|
||||||
if (entryClass.getKind() == ClassKind.ENUM_ENTRY) {
|
if (entryClass.getKind() == ClassKind.ENUM_ENTRY) {
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
trait Named {
|
||||||
|
class object Bar {
|
||||||
|
val g = "a";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
class object {
|
||||||
|
val g = "b";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
assertEquals("a", Named.Bar.g, "Named.Bar.g")
|
||||||
|
assertEquals("a", Named.g, "Named.g")
|
||||||
|
|
||||||
|
assertEquals("b", Foo.Default.g, "Foo.Default.g")
|
||||||
|
assertEquals("b", Foo.g, "Foo.g")
|
||||||
|
|
||||||
|
assertEquals("b", foo(Foo), "foo(Foo)")
|
||||||
|
assertEquals("b", foo(Foo.Default), "foo(Foo.Default)")
|
||||||
|
|
||||||
|
assertEquals("c", Named.ext(), "Named.ext()")
|
||||||
|
assertEquals("c", Named.Bar.ext(), "Named.Bar.ext()")
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(f: Foo.Default) = f.g
|
||||||
|
|
||||||
|
fun Named.Bar.ext() = "c"
|
||||||
Reference in New Issue
Block a user