From 57a4d09ad25cd29f4883b50d06faa628d6b8b88c Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Mon, 22 Jan 2024 11:55:09 +0000 Subject: [PATCH] [K/Wasm] Add tests for nested external declarations + fix them --- .../wasm/export/ExportModelGenerator.kt | 14 +++++++--- .../externalDeclarations.d.ts | 25 +++++++++++++++--- .../externalDeclarations.kt | 26 +++++++++++++++++-- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/export/ExportModelGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/export/ExportModelGenerator.kt index 7fc5b68c229..ad6bf260355 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/export/ExportModelGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/export/ExportModelGenerator.kt @@ -21,8 +21,8 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid -import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid +import org.jetbrains.kotlin.name.parentOrNull import org.jetbrains.kotlin.serialization.js.ModuleKind import org.jetbrains.kotlin.utils.addToStdlib.runIf import org.jetbrains.kotlin.utils.memoryOptimizedFilter @@ -62,7 +62,13 @@ class ExportModelGenerator(val context: WasmBackendContext) { override fun visitClass(declaration: IrClass) { declaration.superTypes.forEach(::visitType) - declaration.acceptChildrenVoid(this) + declaration.typeParameters.forEach(::visitTypeParameter) + declaration.declarations.forEach { it.acceptVoid(this) } + } + + override fun visitProperty(declaration: IrProperty) { + declaration.backingField?.let(::visitField) + declaration.getter?.let(::visitFunction) } override fun visitField(declaration: IrField) { @@ -306,8 +312,10 @@ class ExportModelGenerator(val context: WasmBackendContext) { ) } + val parentFqName = declaration.getFqNameWithJsNameWhenAvailable(shouldIncludePackage = true).parentOrNull() + return ExportedNamespace( - name = "$NOT_EXPORTED_NAMESPACE${declaration.packageFqName?.asString()?.takeIf { it.isNotEmpty() }?.let { ".$it" }.orEmpty()}", + name = "$NOT_EXPORTED_NAMESPACE${parentFqName?.asString()?.takeIf { it.isNotEmpty() }?.let { ".$it" }.orEmpty()}", declarations = listOf(exportedDeclaration), isPrivate = true ) diff --git a/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.d.ts b/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.d.ts index 56ed17170e4..4bfb2fc3dfa 100644 --- a/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.d.ts +++ b/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.d.ts @@ -12,18 +12,37 @@ declare namespace not.exported.org.second { } declare namespace not.exported.org.second { const Foo: { - get bar(): number; + get bar(): not.exported.Parent.OneMoreLayer.MentionedNested; get baz(): string; + get oneMore(): not.exported.Parent.Companion.AnotherMentionedNested; } & not.exported.Baz; } declare namespace not.exported { interface Baz extends not.exported.Bar { readonly baz?: T; - readonly bar: number; + readonly bar: not.exported.Parent.OneMoreLayer.MentionedNested; + readonly oneMore: not.exported.Parent.Companion.AnotherMentionedNested; + } +} +declare namespace not.exported.Parent.OneMoreLayer { + interface MentionedNested { + readonly value: typeof not.exported.MentionedParent; + } +} +declare namespace not.exported.Parent.Companion { + class AnotherMentionedNested { + constructor(); + get value(): string; } } declare namespace not.exported { interface Bar { - readonly bar: number; + readonly bar: not.exported.Parent.OneMoreLayer.MentionedNested; + readonly oneMore: not.exported.Parent.Companion.AnotherMentionedNested; } +} +declare namespace not.exported { + const MentionedParent: { + get value(): string; + }; } \ No newline at end of file diff --git a/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.kt b/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.kt index 9586f7f2945..7ea3ab2b8fd 100644 --- a/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.kt +++ b/compiler/testData/codegen/boxWasmJsInterop/typeScriptDeclarations/externalDeclarations.kt @@ -3,8 +3,29 @@ // MODULE: main // FILE: first.kt +external object MentionedParent { + val value: String + interface Nested { + val value: Int + } +} + +external class Parent { + object OneMoreLayer { + interface MentionedNested { + val value: MentionedParent + } + } + companion object { + class AnotherMentionedNested { + val value: String + } + } +} + external interface Bar { - val bar: Int + val bar: Parent.OneMoreLayer.MentionedNested + val oneMore: Parent.Companion.AnotherMentionedNested } external interface Baz : Bar { @@ -18,8 +39,9 @@ import Bar import Baz external object Foo : Baz { - override val bar: Int + override val bar: Parent.OneMoreLayer.MentionedNested override val baz: JsString + override val oneMore: Parent.Companion.AnotherMentionedNested } external abstract class BaseResult(foo: Foo)