Skip to content

Commit 900eaca

Browse files
JSMonkSpace Team
authored andcommitted
[K/JS] Fix exporting interfaces with companions to ES modules
^KT-79926 Fixed
1 parent d2b081d commit 900eaca

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,12 @@ class ExportModelGenerator(val context: JsIrBackendContext, val generateNamespac
348348
members.addIfNotNull(exportProperty(candidate)?.withAttributesFor(candidate))
349349

350350
is IrClass -> {
351-
if (klass.isInterface) {
352-
nestedClasses.addIfNotNull(klass.companionObject()?.let { exportClass(it) as? ExportedClass }?.withAttributesFor(candidate))
351+
if (klass.isInterface && !candidate.isCompanion) continue
352+
val ec = exportClass(candidate)?.withAttributesFor(candidate)
353+
if (ec is ExportedClass) {
354+
nestedClasses.add(ec)
353355
} else {
354-
val ec = exportClass(candidate)?.withAttributesFor(candidate)
355-
if (ec is ExportedClass) {
356-
nestedClasses.add(ec)
357-
} else {
358-
members.addIfNotNull(ec)
359-
}
356+
members.addIfNotNull(ec)
360357
}
361358
}
362359

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToTsDeclarations.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ class ExportModelToTsDeclarations(private val moduleKind: ModuleKind) {
196196
}
197197

198198
private fun ExportedObject.generateTypeScriptString(indent: String, prefix: String): String {
199-
val shouldGenerateObjectWithGetInstance =
200-
isEsModules && !ir.isEffectivelyExternal() && (ir.parent as? IrClass).let { it == null || (it.isInterface && !ir.isCompanion) }
199+
val shouldGenerateObjectWithGetInstance = isEsModules && !ir.isExternal
201200

202201
val constructorTypeReference =
203202
if (shouldGenerateObjectWithGetInstance) MetadataConstructor else "$name.$Metadata.$MetadataConstructor"

js/js.translator/testData/typescript-export/js/module-systems-in-exported-file/esm.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ export declare interface KtList<E> /* extends Collection<E> */ {
66
readonly "kotlin.collections.KtList": unique symbol;
77
};
88
}
9-
export declare abstract class KtList<E> extends KtSingleton<KtList.$metadata$.constructor>() {
9+
export declare abstract class KtList<E> {
10+
static readonly getInstance: () => typeof KtList.$metadata$.type;
1011
private constructor();
1112
}
1213
/** @deprecated $metadata$ is used for internal purposes, please don't use it in your code, because it can be removed at any moment */
1314
export declare namespace KtList.$metadata$ {
15+
abstract class type<E> extends KtSingleton<constructor>() {
16+
private constructor();
17+
}
1418
abstract class constructor {
1519
fromJsArray<E>(array: ReadonlyArray<E>): KtList<E>;
1620
private constructor();

js/js.translator/testData/typescript-export/js/module-systems/esm.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ export declare interface KtList<E> /* extends Collection<E> */ {
66
readonly "kotlin.collections.KtList": unique symbol;
77
};
88
}
9-
export declare abstract class KtList<E> extends KtSingleton<KtList.$metadata$.constructor>() {
9+
export declare abstract class KtList<E> {
10+
static readonly getInstance: () => typeof KtList.$metadata$.type;
1011
private constructor();
1112
}
1213
/** @deprecated $metadata$ is used for internal purposes, please don't use it in your code, because it can be removed at any moment */
1314
export declare namespace KtList.$metadata$ {
15+
abstract class type<E> extends KtSingleton<constructor>() {
16+
private constructor();
17+
}
1418
abstract class constructor {
1519
fromJsArray<E>(array: ReadonlyArray<E>): KtList<E>;
1620
private constructor();

0 commit comments

Comments
 (0)