Kotlin / dukat

Converter of <any kind of declarations> to Kotlin external declarations
548 stars 44 forks source link

Trying to auto-generate externals for `preact` creates 500+ errors in `lib.dom.kt` #433

Open lambrospetrou opened 3 years ago

lambrospetrou commented 3 years ago

I created a new Kotlin/JS project using Gradle > Kotlin/JS for browser from the Intellij wizard.

I modified my dependencies to the following:

repositories {
    maven("https://kotlin.bintray.com/kotlin-js-wrappers/")
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-js"))
    implementation("org.jetbrains:kotlin-extensions:1.0.1-pre.144-kotlin-1.4.30")
    implementation(npm("preact", "10.5.12", generateExternals = true))
}

Then I tried to build the project using the build task from Gradle, and it generated the following files:

➜  kotlin-preact git:(master) ✗ ll build/externals/kotlin-preact/src/
total 1072
drwxr-xr-x  12 petrou  staff     384 14 Feb 18:34 .
drwxr-xr-x   5 petrou  staff     160 14 Feb 18:34 ..
-rw-r--r--   1 petrou  staff   12776 14 Feb 18:34 index.preact.module_preact.kt
-rw-r--r--   1 petrou  staff   78866 14 Feb 18:34 jsx.JSXInternal.module_preact.kt
-rw-r--r--   1 petrou  staff     503 14 Feb 18:34 jsx.module_preact.kt
-rw-r--r--   1 petrou  staff  409432 14 Feb 18:34 lib.dom.kt
-rw-r--r--   1 petrou  staff     844 14 Feb 18:34 lib.es2015.collection.kt
-rw-r--r--   1 petrou  staff    2548 14 Feb 18:34 lib.es2015.iterable.kt
-rw-r--r--   1 petrou  staff    7336 14 Feb 18:34 lib.es5.Intl.module_dukat.kt
-rw-r--r--   1 petrou  staff   10062 14 Feb 18:34 lib.es5.kt
-rw-r--r--   1 petrou  staff     546 14 Feb 18:34 lib.scripthost.kt
-rw-r--r--   1 petrou  staff     559 14 Feb 18:34 nonDeclarations.preact.kt

Most of them are OK, but lib.dom.kt which is basically a dependency of all the rest has 521 errors (see screenshot).

Screenshot 2021-02-14 at 18 39 15

Preact has TypeScript definitions in its own package at https://github.com/preactjs/preact/blob/master/src/index.d.ts.

If it was an issue with the Preact typings I could have a look and fix them, but this seems to be an issue with the core compiler since lib.dom.kt contains generic definitions. Should these be even generated since they should have been part of Kotlin/JS anyway?

As a sidenote, if I remove the generateExternals = true from the preact dependency, and I manually define the following naive types I can use Preact but since this basically uses dynamic for everything it's quite bad, and I don't have type safety.

@file:JsModule("preact")
@file:JsNonModule
package preact

import org.w3c.dom.Element

external fun render(vnode: Any, container: Element): Unit
external fun h(type: String, props: dynamic, children: dynamic): dynamic

Is there anything else needed as configuration in order to make Dukat generate correct types?

Thanks

lambrospetrou commented 3 years ago

I also tried running dukat on its own through the command line, directly giving the Preact type definitions and the results are the same.

➜ npm init -y && npm i -D dukat preact
➜ npx dukat node_modules/preact/src/index.d.ts
nonDeclarations.preact.kt
index.preact.module_preact.kt
jsx.module_preact.kt
jsx.JSXInternal.module_preact.kt
lib.es5.kt
lib.es5.Intl.module_dukat.kt
lib.dom.kt
lib.es2015.iterable.kt
lib.es2015.collection.kt
lib.scripthost.kt

➜ ls
index.preact.module_preact.kt    jsx.module_preact.kt             lib.es2015.collection.kt         lib.es5.Intl.module_dukat.kt     lib.scripthost.kt                nonDeclarations.preact.kt        package.json
jsx.JSXInternal.module_preact.kt lib.dom.kt                       lib.es2015.iterable.kt           lib.es5.kt                       node_modules                     package-lock.json

➜ grep -A 5 -e "CacheQueryOptions" ./lib.dom.kt
external interface MultiCacheQueryOptions : CacheQueryOptions {
    var cacheName: String?
        get() = definedExternally
        set(value) = definedExternally
}

The grep-ed snippet errors with the following in Intellij (see original post):

e: /Users/petrou/dev/github/code-playground/kotlin-preact/build/externals/kotlin-preact/src/lib.dom.kt: (906, 9): 'cacheName' hides member of supertype 'CacheQueryOptions' and needs 'override' modifier
tradeJmark commented 2 years ago

Did you ever figure out a solution for this? I'm having essentially the same problem trying to use Chart.js.