From 830041d502c633e0f70fa42e9e0cbe595bfb2adb Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Tue, 3 Dec 2019 16:25:18 +0100 Subject: [PATCH] Convert UnionType to string whenever it's possible --- compiler/test/data/typescript/aaa/aaa.d.kt | 26 ++++++++++++ compiler/test/data/typescript/aaa/aaa.d.ts | 9 ++++ .../interface/inheritance/withGeneric.d.kt | 2 +- .../objectType/asOptionalProperty.d.kt | 6 --- .../objectType/asOptionalProperty.d.ts | 2 - .../data/typescript/thisType/inNameSpace.d.kt | 8 +--- .../test/data/typescript/tuple/tuple.d.kt | 2 +- .../nodeLowering/lowerings/introduceModels.kt | 42 +++++++++++++------ 8 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 compiler/test/data/typescript/aaa/aaa.d.kt create mode 100644 compiler/test/data/typescript/aaa/aaa.d.ts diff --git a/compiler/test/data/typescript/aaa/aaa.d.kt b/compiler/test/data/typescript/aaa/aaa.d.kt new file mode 100644 index 000000000..45b9d7576 --- /dev/null +++ b/compiler/test/data/typescript/aaa/aaa.d.kt @@ -0,0 +1,26 @@ +@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", "EXTERNAL_DELEGATION") + +import kotlin.js.* +import kotlin.js.Json +import org.khronos.webgl.* +import org.w3c.dom.* +import org.w3c.dom.events.* +import org.w3c.dom.parsing.* +import org.w3c.dom.svg.* +import org.w3c.dom.url.* +import org.w3c.fetch.* +import org.w3c.files.* +import org.w3c.notifications.* +import org.w3c.performance.* +import org.w3c.workers.* +import org.w3c.xhr.* + +external interface RS { + fun unshift(chunk: String, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */ = definedExternally) + fun unshift(chunk: Array, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */ = definedExternally) +} + +external open class R : RS { + fun unshift(chunk: Any, encoding: String = definedExternally) + open fun unshift(chunk: Any) +} \ No newline at end of file diff --git a/compiler/test/data/typescript/aaa/aaa.d.ts b/compiler/test/data/typescript/aaa/aaa.d.ts new file mode 100644 index 000000000..4aeda11a3 --- /dev/null +++ b/compiler/test/data/typescript/aaa/aaa.d.ts @@ -0,0 +1,9 @@ +type Benc = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex"; + +interface RS { + unshift(chunk: string | Array, encoding?: Benc): void; +} + +declare class R implements RS { + unshift(chunk: any, encoding?: Benc): void; +} diff --git a/compiler/test/data/typescript/interface/inheritance/withGeneric.d.kt b/compiler/test/data/typescript/interface/inheritance/withGeneric.d.kt index a2b185cd4..4d33df130 100644 --- a/compiler/test/data/typescript/interface/inheritance/withGeneric.d.kt +++ b/compiler/test/data/typescript/interface/inheritance/withGeneric.d.kt @@ -22,7 +22,7 @@ external interface JQueryXHR : MyXMLHttpRequest, JQueryPromise { external interface Property -external interface PropertySpec : Property +external interface PropertySpec : Property external interface MyXMLHttpRequest diff --git a/compiler/test/data/typescript/objectType/asOptionalProperty.d.kt b/compiler/test/data/typescript/objectType/asOptionalProperty.d.kt index 57f3acc40..effba4bb3 100644 --- a/compiler/test/data/typescript/objectType/asOptionalProperty.d.kt +++ b/compiler/test/data/typescript/objectType/asOptionalProperty.d.kt @@ -25,12 +25,6 @@ external interface `T$0` { } external interface ClientConfig { - var mode: dynamic /* 'live' | 'rtc' */ - get() = definedExternally - set(value) = definedExternally - var codec: dynamic /* 'vp8' | 'h264' */ - get() = definedExternally - set(value) = definedExternally var proxyServer: String? get() = definedExternally set(value) = definedExternally diff --git a/compiler/test/data/typescript/objectType/asOptionalProperty.d.ts b/compiler/test/data/typescript/objectType/asOptionalProperty.d.ts index efa8c61f7..de9a1b331 100644 --- a/compiler/test/data/typescript/objectType/asOptionalProperty.d.ts +++ b/compiler/test/data/typescript/objectType/asOptionalProperty.d.ts @@ -1,7 +1,5 @@ // based on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/agora-rtc-sdk/index.d.ts export interface ClientConfig { - mode: 'live' | 'rtc'; - codec: 'vp8' | 'h264'; proxyServer?: string; turnServer?: { turnServerURL: string; diff --git a/compiler/test/data/typescript/thisType/inNameSpace.d.kt b/compiler/test/data/typescript/thisType/inNameSpace.d.kt index add824ba5..88e467db3 100644 --- a/compiler/test/data/typescript/thisType/inNameSpace.d.kt +++ b/compiler/test/data/typescript/thisType/inNameSpace.d.kt @@ -42,12 +42,8 @@ import org.w3c.xhr.* external interface Status { var current: Number var total: Number - var type: dynamic /* "begin" | "end" | "unknown" */ - get() = definedExternally - set(value) = definedExternally - var status: dynamic /* "ok" | "fail" */ - get() = definedExternally - set(value) = definedExternally + var type: String /* "begin" | "end" | "unknown" */ + var status: String /* "ok" | "fail" */ } external interface EventEmitter { diff --git a/compiler/test/data/typescript/tuple/tuple.d.kt b/compiler/test/data/typescript/tuple/tuple.d.kt index 1fc898954..68cd5baf7 100644 --- a/compiler/test/data/typescript/tuple/tuple.d.kt +++ b/compiler/test/data/typescript/tuple/tuple.d.kt @@ -16,7 +16,7 @@ import org.w3c.workers.* import org.w3c.xhr.* external interface Options { - var mode: dynamic /* JsTuple */ + var mode: dynamic /* JsTuple */ get() = definedExternally set(value) = definedExternally } diff --git a/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt b/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt index fe1a3b62b..d1ae8b48b 100644 --- a/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt +++ b/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt @@ -231,6 +231,20 @@ private class NodeConverter(private val uidToNameMapper: Map } } + private fun NameEntity.addLibPrefix() = IdentifierEntity("").appendLeft(this) + private fun UnionTypeNode.canBeTranslatedAsString(): Boolean { + return params.all { (it is TypeValueNode) && (it.value == IdentifierEntity("String")) } + } + + private fun UnionTypeNode.convertMeta(): String { + return params.joinToString(" | ") { unionMember -> + if (unionMember.meta is StringLiteralDeclaration) { + (unionMember.meta as StringLiteralDeclaration).token + } else { + unionMember.process().translate() + } + } + } private fun ParameterValueDeclaration.process(context: TranslationContext = TranslationContext.IRRELEVANT): TypeModel { val dynamicName = when (context) { @@ -238,18 +252,22 @@ private class NodeConverter(private val uidToNameMapper: Map else -> IdentifierEntity("dynamic") } return when (this) { - is UnionTypeNode -> TypeValueModel( - dynamicName, - emptyList(), - params.joinToString(" | ") { unionMember -> - if (unionMember.meta is StringLiteralDeclaration) { - (unionMember.meta as StringLiteralDeclaration).token - } else { - unionMember.process().translate() - } - }, - null - ) + is UnionTypeNode -> if (canBeTranslatedAsString()) { + val stringEntity = IdentifierEntity("String") + TypeValueModel( + stringEntity, + emptyList(), + convertMeta(), + stringEntity.addLibPrefix() + ) + } else { + TypeValueModel( + dynamicName, + emptyList(), + convertMeta(), + null + ) + } is TupleTypeNode -> TypeValueModel( dynamicName, emptyList(),