Skip to content

Commit 871fb95

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

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

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

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

339339
is IrClass -> {
340-
if (klass.isInterface) {
341-
nestedClasses.addIfNotNull(klass.companionObject()?.let { exportClass(it) as? ExportedClass }?.withAttributesFor(candidate))
340+
if (klass.isInterface && !candidate.isCompanion) continue
341+
val ec = exportClass(candidate)?.withAttributesFor(candidate)
342+
if (ec is ExportedClass) {
343+
nestedClasses.add(ec)
342344
} else {
343-
val ec = exportClass(candidate)?.withAttributesFor(candidate)
344-
if (ec is ExportedClass) {
345-
nestedClasses.add(ec)
346-
} else {
347-
members.addIfNotNull(ec)
348-
}
345+
members.addIfNotNull(ec)
349346
}
350347
}
351348

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

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

194194
private fun ExportedObject.generateTypeScriptString(indent: String, prefix: String): String {
195-
val shouldGenerateObjectWithGetInstance = isEsModules && !isExternal && !(isInsideInterface && isCompanion)
196-
195+
val shouldGenerateObjectWithGetInstance = isEsModules && !isExternal
197196
val constructorTypeReference =
198197
if (shouldGenerateObjectWithGetInstance) MetadataConstructor else "$name.$Metadata.$MetadataConstructor"
199198

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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
export declare namespace KtList {
@@ -16,6 +17,9 @@ export declare namespace KtList {
1617
fromJsArray<E>(array: ReadonlyArray<E>): KtList<E>;
1718
private constructor();
1819
}
20+
abstract class type<E> extends KtSingleton<constructor>() {
21+
private constructor();
22+
}
1923
}
2024
}
2125
export declare const value: { get(): number; };
@@ -80,11 +84,15 @@ export declare interface AnInterfaceWithCompanion {
8084
readonly "foo.AnInterfaceWithCompanion": unique symbol;
8185
};
8286
}
83-
export declare abstract class AnInterfaceWithCompanion extends KtSingleton<AnInterfaceWithCompanion.$metadata$.constructor>() {
87+
export declare abstract class AnInterfaceWithCompanion {
88+
static readonly getInstance: () => typeof AnInterfaceWithCompanion.$metadata$.type;
8489
private constructor();
8590
}
8691
/** @deprecated $metadata$ is used for internal purposes, please don't use it in your code, because it can be removed at any moment */
8792
export declare namespace AnInterfaceWithCompanion.$metadata$ {
93+
abstract class type extends KtSingleton<constructor>() {
94+
private constructor();
95+
}
8896
abstract class constructor {
8997
get someValue(): string;
9098
private constructor();

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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
export declare namespace KtList {
@@ -16,6 +17,9 @@ export declare namespace KtList {
1617
fromJsArray<E>(array: ReadonlyArray<E>): KtList<E>;
1718
private constructor();
1819
}
20+
abstract class type<E> extends KtSingleton<constructor>() {
21+
private constructor();
22+
}
1923
}
2024
}
2125
export declare const value: { get(): number; };
@@ -80,11 +84,15 @@ export declare interface AnInterfaceWithCompanion {
8084
readonly "foo.AnInterfaceWithCompanion": unique symbol;
8185
};
8286
}
83-
export declare abstract class AnInterfaceWithCompanion extends KtSingleton<AnInterfaceWithCompanion.$metadata$.constructor>() {
87+
export declare abstract class AnInterfaceWithCompanion {
88+
static readonly getInstance: () => typeof AnInterfaceWithCompanion.$metadata$.type;
8489
private constructor();
8590
}
8691
/** @deprecated $metadata$ is used for internal purposes, please don't use it in your code, because it can be removed at any moment */
8792
export declare namespace AnInterfaceWithCompanion.$metadata$ {
93+
abstract class type extends KtSingleton<constructor>() {
94+
private constructor();
95+
}
8896
abstract class constructor {
8997
get someValue(): string;
9098
private constructor();

0 commit comments

Comments
 (0)