Skip to content

Commit

Permalink
Convert UnionType to string whenever it's possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Schahen committed Dec 3, 2019
1 parent 6120312 commit 830041d
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 28 deletions.
26 changes: 26 additions & 0 deletions compiler/test/data/typescript/aaa/aaa.d.kt
Original file line number Diff line number Diff line change
@@ -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<String>, 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)
}
9 changes: 9 additions & 0 deletions compiler/test/data/typescript/aaa/aaa.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Benc = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex";

interface RS {
unshift(chunk: string | Array<string>, encoding?: Benc): void;
}

declare class R implements RS {
unshift(chunk: any, encoding?: Benc): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ external interface JQueryXHR : MyXMLHttpRequest, JQueryPromise<Any> {

external interface Property<T>

external interface PropertySpec : Property<dynamic /* "ping" | "pong" */>
external interface PropertySpec : Property<String /* "ping" | "pong" */>

external interface MyXMLHttpRequest

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 2 additions & 6 deletions compiler/test/data/typescript/thisType/inNameSpace.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/data/typescript/tuple/tuple.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.w3c.workers.*
import org.w3c.xhr.*

external interface Options {
var mode: dynamic /* JsTuple<dynamic> */
var mode: dynamic /* JsTuple<String> */
get() = definedExternally
set(value) = definedExternally
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,43 @@ private class NodeConverter(private val uidToNameMapper: Map<String, NameEntity>
}
}

private fun NameEntity.addLibPrefix() = IdentifierEntity("<LIBROOT>").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) {
TranslationContext.TYPE_CONSTRAINT -> IMPOSSIBLE_CONSTRAINT
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(),
Expand Down

0 comments on commit 830041d

Please sign in to comment.