Kotlin / dukat

Converter of <any kind of declarations> to Kotlin external declarations
557 stars 42 forks source link

ts2kt: "StringLiteral" kind unsupported yet here #157

Open apomelov opened 6 years ago

apomelov commented 6 years ago

ts2kt version: 0.1.3

A fragment from phaser.d.ts (https://github.com/photonstorm/phaser3-docs/blob/master/typescript/phaser.d.ts):

declare type GameConfig = {
    input?: boolean | object;
    "input.keyboard"?: boolean;
    ...
}

results into ts2kt: "StringLiteral" kind unsupported yet here! in the stderr.

epabst commented 6 years ago

It looks like the latest phaser no longer uses that string literal. It now has:

declare type KeyboardInputConfig = {
    target?: any;
};
declare type InputConfig = {
    keyboard?: boolean | KeyboardInputConfig;
    ...
}
declare type GameConfig = {
    input?: boolean | InputConfig;
    ...
}

which successfully creates this:

external interface KeyboardInputConfig {
    var target: Any? get() = definedExternally; set(value) = definedExternally
}
external interface InputConfig {
    var keyboard: dynamic /* Boolean | KeyboardInputConfig */ get() = definedExternally; set(value) = definedExternally
}
external interface GameConfig {
    var input: dynamic /* Boolean | InputConfig */ get() = definedExternally; set(value) = definedExternally
}

Can this be closed?

apomelov commented 6 years ago

Just tried the last versions, got a list of warnings (maybe it is different from the first one I get):

Converting phaser.d.ts
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:780:21 to 784:15)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:784:25 to 788:15)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:788:25 to 792:19)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:792:29 to 796:20)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:932:25 to 936:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:936:17 to 940:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:940:17 to 944:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:944:17 to 948:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:948:17 to 952:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:952:17 to 956:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:956:17 to 960:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:960:17 to 964:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:964:17 to 968:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:968:17 to 972:8)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1635:19 to 1639:14)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1639:23 to 1643:14)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1647:20 to 1651:15)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1651:24 to 1655:15)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1865:20 to 1869:14)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1869:24 to 1873:14)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1873:24 to 1877:18)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1877:28 to 1881:18)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1882:26 to 1886:24)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1886:34 to 1890:23)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1891:23 to 1895:17)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1895:27 to 1899:17)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1899:27 to 1903:21)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1903:31 to 1907:21)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1908:23 to 1912:21)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:1912:31 to 1916:20)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:2349:27 to 2353:21)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:2353:31 to 2357:21)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:10487:31 to 10491:27)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:10491:36 to 10495:27)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:10511:37 to 10515:28)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:10515:37 to 10519:28)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:15194:22 to 15194:36)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:15407:22 to 15407:36)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:51984:46 to 51988:30)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:51988:40 to 51992:30)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:51992:40 to 51996:34)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:51996:44 to 52000:35)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:52000:45 to 52004:38)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:52004:48 to 52008:33)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:52008:44 to 52012:34)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:52012:45 to 52016:32)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:52016:43 to 52020:35)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67717:22 to 67721:16)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67721:26 to 67725:16)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67745:29 to 67749:24)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67749:35 to 67753:26)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67753:37 to 67757:26)
ts2kt: "StringLiteral" kind unsupported yet here! (phaser.d.ts:67757:37 to 67761:27)

This issue is not about converting phaser, it's about supporting string literals if they are allowed by typescript spec. Probaly should be marked as enhancement, not bug.

epabst commented 6 years ago

I can still see string literals in phaser. Examples:

declare type ArcadeWorldConfig = {
    checkCollision?: object;
    "checkCollision.up"?: boolean;
    "checkCollision.down"?: boolean;
    ...
}

What should the Kotlin look like?

apomelov commented 6 years ago

I'm not familiar with TS enough, but it seems to be like some autogenerated intermediate type.

joffrey-bion commented 5 years ago

I believe this is the TypeScript equivalent of Kotlin's backquoted identifiers, which allow special characters. This is a way to write a dot, dash or space as part of an identifier, for instance. Am I missing something or could it simply be translated to Kotlin backquoted identifiers?

epabst commented 5 years ago

A StringLiteral is a type in TypeScript which is like an enum class in what values are allowed, except that it is still a String.

joffrey-bion commented 5 years ago

@epabst The issue at hand is not about string literal types. What ts2kt fails to process here is a StringLiteral token in the position of a property name in a type declaration. ts2kt does not seem to support property names declared as string literals (as opposed to regular identifiers).

This kind of string literals used as property names is a way to define properties that would otherwise have illegal characters if declared using regular identifiers (such as the dash -).

For instance, the following declaration:

export interface Headers {
  'content-length'?: string;
}

allows this kind of property access in a type-safe way: headers["content-length"].

Just as a note for future readers, this is supported by Dukat, which is AFAIU the successor of ts2kt, but the generated Kotlin code is not actually valid because of this issue.