From b3c681ee42041f82ec3e5086efe130c7516080b9 Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Thu, 2 Sep 2021 19:37:05 +0300 Subject: [PATCH] [JS IR] Tests for TS declarations of sealed classes Also make sure we generate proper declarations for abstract and data classes. #KT-39364 Fixed #KT-47376 Fixed --- .../declarations/declarations.d.ts | 43 +++++++++++++ .../declarations/declarations.kt | 64 ++++++++++++++++++- .../declarations/declarations__main.ts | 15 +++++ 3 files changed, 120 insertions(+), 2 deletions(-) diff --git a/js/js.translator/testData/typescript-export/declarations/declarations.d.ts b/js/js.translator/testData/typescript-export/declarations/declarations.d.ts index 4439e2f71f3..e40a3a7d3a3 100644 --- a/js/js.translator/testData/typescript-export/declarations/declarations.d.ts +++ b/js/js.translator/testData/typescript-export/declarations/declarations.d.ts @@ -55,5 +55,48 @@ declare namespace JS_TESTS { readonly x: number; }; } + class TestSealed { + protected constructor(name: string); + readonly name: string; + } + namespace TestSealed { + class AA extends foo.TestSealed { + constructor(); + bar(): string; + } + class BB extends foo.TestSealed { + constructor(); + baz(): string; + } + } + abstract class TestAbstract { + constructor(name: string); + readonly name: string; + } + namespace TestAbstract { + class AA extends foo.TestAbstract { + constructor(); + bar(): string; + } + class BB extends foo.TestAbstract { + constructor(); + baz(): string; + } + } + class TestDataClass { + constructor(name: string); + readonly name: string; + component1(): string; + copy(name: string): foo.TestDataClass; + toString(): string; + hashCode(): number; + equals(other: Nullable): boolean; + } + namespace TestDataClass { + class Nested { + constructor(); + readonly prop: string; + } + } } } diff --git a/js/js.translator/testData/typescript-export/declarations/declarations.kt b/js/js.translator/testData/typescript-export/declarations/declarations.kt index 460b80fc12c..e16776a9a07 100644 --- a/js/js.translator/testData/typescript-export/declarations/declarations.kt +++ b/js/js.translator/testData/typescript-export/declarations/declarations.kt @@ -3,71 +3,98 @@ // SKIP_MINIFICATION // SKIP_NODE_JS -@file:JsExport - package foo // TODO: Test the same for member functions: +@JsExport fun sum(x: Int, y: Int): Int = x + y +@JsExport fun varargInt(vararg x: Int): Int = x.size +@JsExport fun varargNullableInt(vararg x: Int?): Int = x.size +@JsExport fun varargWithOtherParameters(x: String, vararg y: String, z: String): Int = x.length + y.size + z.length +@JsExport fun varargWithComplexType(vararg x: (Array) -> Array): Int = x.size +@JsExport fun sumNullable(x: Int?, y: Int?): Int = (x ?: 0) + (y ?: 0) +@JsExport fun defaultParameters(x: Int = 10, y: String = "OK"): String = x.toString() + y +@JsExport fun generic1(x: T): T = x +@JsExport fun generic2(x: T?): Boolean = (x == null) +@JsExport fun generic3(a: A, b: B, c: C, d: D): E? = null +@JsExport inline fun inlineFun(x: Int, callback: (Int) -> Unit) { callback(x) } // Properties +@JsExport const val _const_val: Int = 1 + +@JsExport val _val: Int = 1 + +@JsExport var _var: Int = 1 +@JsExport val _valCustom: Int get() = 1 +@JsExport val _valCustomWithField: Int = 1 get() = field + 1 +@JsExport var _varCustom: Int get() = 1 set(value) {} +@JsExport var _varCustomWithField: Int = 1 get() = field * 10 set(value) { field = value * 10 } // Classes +@JsExport class A + +@JsExport class A1(val x: Int) + +@JsExport class A2(val x: String, var y: Boolean) + +@JsExport class A3 { val x: Int = 100 } + +@JsExport class A4 { val _valCustom: Int get() = 1 @@ -85,19 +112,52 @@ class A4 { } +@JsExport object O0 +@JsExport object O { val x = 10 @JsName("foo") // TODO: Should work without JsName fun foo() = 20 } +@JsExport fun takesO(o: O): Int = O.x + O.foo() +@JsExport class KT_37829 { companion object { val x = 10 } +} + +// See KT-47376, KT-39364 +@JsExport +sealed class TestSealed(val name: String) { + class AA : TestSealed("AA") { + fun bar(): String = "bar" + } + class BB : TestSealed("BB") { + fun baz(): String = "baz" + } +} + +// See KT-39364 +@JsExport +abstract class TestAbstract(val name: String) { + class AA : TestAbstract("AA") { + fun bar(): String = "bar" + } + class BB : TestAbstract("BB") { + fun baz(): String = "baz" + } +} + +@JsExport +data class TestDataClass(val name: String) { + class Nested { + val prop: String = "hello" + } } \ No newline at end of file diff --git a/js/js.translator/testData/typescript-export/declarations/declarations__main.ts b/js/js.translator/testData/typescript-export/declarations/declarations__main.ts index 9fbf5937ce8..0b6713d542a 100644 --- a/js/js.translator/testData/typescript-export/declarations/declarations__main.ts +++ b/js/js.translator/testData/typescript-export/declarations/declarations__main.ts @@ -23,6 +23,9 @@ import A4 = JS_TESTS.foo.A4; import O = JS_TESTS.foo.O; import takesO = JS_TESTS.foo.takesO; import KT_37829 = JS_TESTS.foo.KT_37829; +import TestSealed = JS_TESTS.foo.TestSealed; +import TestAbstract = JS_TESTS.foo.TestAbstract; +import TestDataClass = JS_TESTS.foo.TestDataClass; function assert(condition: boolean) { if (!condition) { @@ -95,5 +98,17 @@ function box(): string { assert(KT_37829.Companion.x == 10); + assert(new TestSealed.AA().name == "AA"); + assert(new TestSealed.AA().bar() == "bar"); + assert(new TestSealed.BB().name == "BB"); + assert(new TestSealed.BB().baz() == "baz"); + + assert(new TestAbstract.AA().name == "AA"); + assert(new TestAbstract.AA().bar() == "bar"); + assert(new TestAbstract.BB().name == "BB"); + assert(new TestAbstract.BB().baz() == "baz"); + + assert(new TestDataClass.Nested().prop == "hello"); + return "OK"; } \ No newline at end of file