feat(Inner Classes): generate typescript type definitions and JS for Inner Classes.

This commit is contained in:
Artem Kobzar
2021-10-28 17:33:41 +00:00
committed by Space
parent 744191099c
commit 7f7aa9a921
13 changed files with 315 additions and 12 deletions
+19 -2
View File
@@ -170,6 +170,20 @@ val unzipV8 by task<Copy> {
into(unpackedDir)
}
val testDataDir = project(":js:js.translator").projectDir.resolve("testData")
val installTsDependencies by task<NpmTask> {
workingDir.set(testDataDir)
args.set(listOf("install"))
}
val generateTypeScriptTests by task<NpmTask> {
dependsOn(installTsDependencies)
workingDir.set(testDataDir)
args.set(listOf("run", "generateTypeScriptTests"))
}
fun Test.setupV8() {
dependsOn(unzipV8)
val v8Path = unzipV8.get().destinationDir
@@ -190,6 +204,11 @@ fun Test.setUpJsBoxTests(jsEnabled: Boolean, jsIrEnabled: Boolean) {
inputs.files(rootDir.resolve("js/js.engines/src/org/jetbrains/kotlin/js/engine/repl.js"))
dependsOn(":dist")
if (!project.hasProperty("teamcity")) {
dependsOn(generateTypeScriptTests)
}
if (jsEnabled) {
dependsOn(testJsRuntime)
inputs.files(testJsRuntime)
@@ -241,8 +260,6 @@ fun Test.setUpBoxTests() {
}
}
val testDataDir = project(":js:js.translator").projectDir.resolve("testData")
projectTest(parallel = true, jUnitMode = JUnitMode.Mix) {
setUpJsBoxTests(jsEnabled = true, jsIrEnabled = true)
systemProperty("kotlin.js.ir.pir", "false")
@@ -30,6 +30,24 @@ public class IrJsTypeScriptExportES6TestGenerated extends AbstractIrJsTypeScript
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("js/js.translator/testData/typescript-export/classes")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Classes extends AbstractIrJsTypeScriptExportES6Test {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath);
}
public void testAllFilesPresentInClasses() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export/classes"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("inner-class.kt")
public void testInner_class() throws Exception {
runTest("js/js.translator/testData/typescript-export/classes/inner-class.kt");
}
}
@TestMetadata("js/js.translator/testData/typescript-export/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -30,6 +30,24 @@ public class IrJsTypeScriptExportTestGenerated extends AbstractIrJsTypeScriptExp
KtTestUtil.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/classes")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Classes extends AbstractIrJsTypeScriptExportTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInClasses() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/typescript-export/classes"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("inner-class.kt")
public void testInner_class() throws Exception {
runTest("js/js.translator/testData/typescript-export/classes/inner-class.kt");
}
}
@TestMetadata("js/js.translator/testData/typescript-export/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,30 @@
declare namespace JS_TESTS {
type Nullable<T> = T | null | undefined
namespace foo {
class TestInner {
constructor(a: string);
readonly a: string;
readonly Inner: {
new(a: string): TestInner.Inner;
} & typeof TestInner.Inner;
}
namespace TestInner {
class Inner {
protected constructor($outer: foo.TestInner, a: string);
readonly a: string;
readonly concat: string;
static fromNumber(a: number): foo.TestInner.Inner;
readonly SecondLayerInner: {
new(a: string): TestInner.Inner.SecondLayerInner;
} & typeof TestInner.Inner.SecondLayerInner;
}
namespace Inner {
class SecondLayerInner {
protected constructor($outer: foo.TestInner.Inner, a: string);
readonly a: string;
readonly concat: string;
}
}
}
}
}
@@ -0,0 +1,25 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// RUN_PLAIN_BOX_FUNCTION
// SKIP_MINIFICATION
// SKIP_NODE_JS
// IGNORE_BACKEND: JS
// INFER_MAIN_MODULE
// MODULE: JS_TESTS
// FILE: inner-class.kt
package foo
@JsExport
class TestInner(val a: String) {
inner class Inner(val a: String) {
val concat: String = this@TestInner.a + this.a
@JsName("fromNumber")
constructor(a: Int): this(a.toString())
@JsName("SecondLayerInner")
inner class InnerInner(val a: String) {
val concat: String = this@TestInner.a + this@Inner.a + this.a
}
}
}
@@ -0,0 +1,24 @@
"use strict";
var TestInner = JS_TESTS.foo.TestInner;
function assert(condition) {
if (!condition) {
throw "Assertion failed";
}
}
function box() {
var outer = new TestInner("Hello ");
var inner = new outer.Inner("World");
var innerFromNumber = outer.Inner.fromNumber(1654);
var secondInner = new inner.SecondLayerInner("!");
assert(outer instanceof TestInner);
assert(inner instanceof TestInner.Inner);
assert(innerFromNumber instanceof TestInner.Inner);
assert(secondInner instanceof TestInner.Inner.SecondLayerInner);
assert(inner.a == "World");
assert(inner.concat == "Hello World");
assert(innerFromNumber.a == "1654");
assert(innerFromNumber.concat == "Hello 1654");
assert(secondInner.a == "!");
assert(secondInner.concat == "Hello World!");
return "OK";
}
@@ -0,0 +1,30 @@
import TestInner = JS_TESTS.foo.TestInner;
function assert(condition: boolean) {
if (!condition) {
throw "Assertion failed";
}
}
function box(): string {
const outer = new TestInner("Hello ")
const inner = new outer.Inner("World")
const innerFromNumber = outer.Inner.fromNumber(1654)
const secondInner = new inner.SecondLayerInner("!")
assert(outer instanceof TestInner)
assert(inner instanceof TestInner.Inner)
assert(innerFromNumber instanceof TestInner.Inner)
assert(secondInner instanceof TestInner.Inner.SecondLayerInner)
assert(inner.a == "World")
assert(inner.concat == "Hello World")
assert(innerFromNumber.a == "1654")
assert(innerFromNumber.concat == "Hello 1654")
assert(secondInner.a == "!")
assert(secondInner.concat == "Hello World!")
return "OK";
}
@@ -0,0 +1,4 @@
{
"include": [ "./*" ],
"extends": "../common.tsconfig.json"
}