[JS] Support secondary constructors in JsExport

This commit is contained in:
Svyatoslav Kuzmich
2020-05-20 18:00:03 +03:00
parent f128e5222a
commit 695d383ed1
14 changed files with 233 additions and 17 deletions
@@ -29,6 +29,24 @@ public class IrJsTypeScriptExportTestGenerated extends AbstractIrJsTypeScriptExp
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("js/js.translator/testData/typescript-export/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Constructors extends AbstractIrJsTypeScriptExportTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInConstructors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export/constructors"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("constructors.kt")
public void testConstructors() throws Exception {
runTest("js/js.translator/testData/typescript-export/constructors/constructors.kt");
}
}
@TestMetadata("js/js.translator/testData/typescript-export/declarations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -29,6 +29,19 @@ public class LegacyJsTypeScriptExportTestGenerated extends AbstractLegacyJsTypeS
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS, true);
}
@TestMetadata("js/js.translator/testData/typescript-export/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Constructors extends AbstractLegacyJsTypeScriptExportTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInConstructors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export/constructors"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS, true);
}
}
@TestMetadata("js/js.translator/testData/typescript-export/declarations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1,5 +1,8 @@
// EXPECTED_REACHABLE_NODES: 1290
// TODO: Support JsExport on object declarations: KT-39117
// IGNORE_BACKEND: JS_IR
@JsExport
object A {
@JsName("js_method") fun f() = "method"
+3
View File
@@ -1,5 +1,8 @@
// EXPECTED_REACHABLE_NODES: 1291
// TODO: Support JsExport on object declarations: KT-39117
// IGNORE_BACKEND: JS_IR
@JsExport
object A {
@JsName("js_f") fun f(x: Int) = "f($x)"
@@ -0,0 +1,30 @@
declare namespace JS_TESTS {
type Nullable<T> = T | null | undefined
class ClassWithDefaultCtor {
constructor()
readonly x: string;
}
class ClassWithPrimaryCtor {
constructor(x: string)
readonly x: string;
}
class ClassWithSecondaryCtor {
readonly x: string;
static create(y: string): ClassWithSecondaryCtor
}
class ClassWithMultipleSecondaryCtors {
readonly x: string;
static createFromString(y: string): ClassWithMultipleSecondaryCtors
static createFromInts(y: number, z: number): ClassWithMultipleSecondaryCtors
}
class OpenClassWithMixedConstructors {
constructor(x: string)
readonly x: string;
static createFromStrings(y: string, z: string): OpenClassWithMixedConstructors
static createFromInts(y: number, z: number): OpenClassWithMixedConstructors
}
class DerivedClassWithSecondaryCtor extends OpenClassWithMixedConstructors {
static delegateToPrimary(y: string): DerivedClassWithSecondaryCtor
static delegateToCreateFromInts(y: number, z: number): DerivedClassWithSecondaryCtor
}
}
@@ -0,0 +1,56 @@
// TARGET_BACKEND: JS_IR
// CHECK_TYPESCRIPT_DECLARATIONS
// RUN_PLAIN_BOX_FUNCTION
// SKIP_MINIFICATION
// SKIP_NODE_JS
// TODO fix statics export in DCE-driven mode
// SKIP_DCE_DRIVEN
@file:JsExport
class ClassWithDefaultCtor {
val x = "ClassWithDefaultCtor::x"
}
class ClassWithPrimaryCtor(
val x: String
)
class ClassWithSecondaryCtor {
val x: String
@JsName("create")
constructor(y: String) {
x = y
}
}
class ClassWithMultipleSecondaryCtors {
val x: String
@JsName("createFromString")
constructor(y: String) {
x = "fromString:$y"
}
@JsName("createFromInts")
constructor(y: Int, z: Int) {
x = "fromInts:$y:$z"
}
}
open class OpenClassWithMixedConstructors(val x: String) {
@JsName("createFromStrings")
constructor(y: String, z: String) : this("fromStrings:$y:$z")
@JsName("createFromInts")
constructor(y: Int, z: Int) : this(y.toString(), z.toString())
}
class DerivedClassWithSecondaryCtor : OpenClassWithMixedConstructors {
@JsName("delegateToPrimary")
constructor(y: String) : super(y)
@JsName("delegateToCreateFromInts")
constructor(y: Int, z: Int) : super(y, z)
}
@@ -0,0 +1,35 @@
"use strict";
var ClassWithDefaultCtor = JS_TESTS.ClassWithDefaultCtor, ClassWithPrimaryCtor = JS_TESTS.ClassWithPrimaryCtor, ClassWithSecondaryCtor = JS_TESTS.ClassWithSecondaryCtor, ClassWithMultipleSecondaryCtors = JS_TESTS.ClassWithMultipleSecondaryCtors, DerivedClassWithSecondaryCtor = JS_TESTS.DerivedClassWithSecondaryCtor, OpenClassWithMixedConstructors = JS_TESTS.OpenClassWithMixedConstructors;
function box() {
var o1 = new ClassWithDefaultCtor();
if (o1.x !== "ClassWithDefaultCtor::x")
return "Fail: ClassWithDefaultCtor";
var o2 = new ClassWithPrimaryCtor("foo");
if (o2.x !== "foo")
return "Fail: ClassWithPrimaryCtor";
var o3 = ClassWithSecondaryCtor.create("foo2");
if (o3.x !== "foo2")
return "Fail: ClassWithSecondaryCtor.create";
var o4 = ClassWithMultipleSecondaryCtors.createFromString("foo3");
if (o4.x !== "fromString:foo3")
return "Fail: ClassWithMultipleSecondaryCtors.createFromString";
var o5 = ClassWithMultipleSecondaryCtors.createFromInts(1, 2);
if (o5.x !== "fromInts:1:2")
return "Fail: ClassWithMultipleSecondaryCtors.createFromInts";
var o6 = new OpenClassWithMixedConstructors("foo4");
if (o6.x !== "foo4")
return "Fail: OpenClassWithMixedConstructors";
var o7 = OpenClassWithMixedConstructors.createFromStrings("foo", "bar");
if (o7.x !== "fromStrings:foo:bar")
return "Fail: OpenClassWithMixedConstructors.createFromStrings";
var o8 = OpenClassWithMixedConstructors.createFromInts(10, -20);
if (o8.x !== "fromStrings:10:-20")
return "Fail: OpenClassWithMixedConstructors.createFromInts";
var o9 = DerivedClassWithSecondaryCtor.delegateToPrimary("foo6");
if (o9.x !== "foo6")
return "Fail: DerivedClassWithSecondaryCtor.delegateToPrimary";
var o10 = DerivedClassWithSecondaryCtor.delegateToCreateFromInts(-10, 20);
if (o10.x !== "fromStrings:-10:20")
return "Fail: DerivedClassWithSecondaryCtor.delegateToCreateFromInts";
return "OK";
}
@@ -0,0 +1,47 @@
const {
ClassWithDefaultCtor,
ClassWithPrimaryCtor,
ClassWithSecondaryCtor,
ClassWithMultipleSecondaryCtors,
DerivedClassWithSecondaryCtor,
OpenClassWithMixedConstructors
} = JS_TESTS;
function box(): string {
const o1 = new ClassWithDefaultCtor();
if (o1.x !== "ClassWithDefaultCtor::x") return "Fail: ClassWithDefaultCtor";
const o2 = new ClassWithPrimaryCtor("foo");
if (o2.x !== "foo") return "Fail: ClassWithPrimaryCtor";
const o3 = ClassWithSecondaryCtor.create("foo2");
if (o3.x !== "foo2") return "Fail: ClassWithSecondaryCtor.create";
const o4 = ClassWithMultipleSecondaryCtors.createFromString("foo3");
if (o4.x !== "fromString:foo3") return "Fail: ClassWithMultipleSecondaryCtors.createFromString";
const o5 = ClassWithMultipleSecondaryCtors.createFromInts(1, 2);
if (o5.x !== "fromInts:1:2") return "Fail: ClassWithMultipleSecondaryCtors.createFromInts";
const o6 = new OpenClassWithMixedConstructors("foo4");
if (o6.x !== "foo4") return "Fail: OpenClassWithMixedConstructors";
const o7 = OpenClassWithMixedConstructors.createFromStrings("foo", "bar");
if (o7.x !== "fromStrings:foo:bar") return "Fail: OpenClassWithMixedConstructors.createFromStrings";
const o8 = OpenClassWithMixedConstructors.createFromInts(10, -20);
if (o8.x !== "fromStrings:10:-20") return "Fail: OpenClassWithMixedConstructors.createFromInts";
const o9 = DerivedClassWithSecondaryCtor.delegateToPrimary("foo6");
if (o9.x !== "foo6") return "Fail: DerivedClassWithSecondaryCtor.delegateToPrimary";
const o10 = DerivedClassWithSecondaryCtor.delegateToCreateFromInts(-10, 20);
if (o10.x !== "fromStrings:-10:20") return "Fail: DerivedClassWithSecondaryCtor.delegateToCreateFromInts";
return "OK";
}
@@ -0,0 +1,4 @@
{
"include": [ "./*" ],
"extends": "../common.tsconfig.json"
}