ScalablyTyped / Converter

Typescript to Scala.js converter
https://scalablytyped.org
GNU General Public License v3.0
218 stars 43 forks source link

Importing @stripe/stripe-js generates no types #638

Open steinybot opened 1 month ago

steinybot commented 1 month ago

importing "@stripe/stripe-js" -> "4.1.0" does not generate any types (besides stripeStripeJsRequire).

The package.json has:

  "types": "lib/index.d.ts",

and lib/index.d.ts has:

export * from '../dist';

It looks to me like the issue is here: https://github.com/ScalablyTyped/Converter/blob/02257bf3588da08deec5a3b07f306fcc6236642d/importer-portable/src/main/scala/org/scalablytyped/converter/internal/importer/LibraryResolver.scala#L76-L79

file.path is /Users/jason/src/ScalablyTyped/Converter/tests/stripe/in/dist/index.d.ts source.folder.path is /Users/jason/src/ScalablyTyped/Converter/tests/stripe/in/lib relativeTo returns ../dist/index.d.ts but the segments doesn't contain the .. it is only ["dist", "index.d.ts"] so the result is IArray(TsIdentModule(None,List(lib, dist))) which is wrong.

steinybot commented 1 month ago

I think it might need to be something like:

      val relative = file.path.relativeTo(source.folder.path)
      val ups = List.fill(relative.ups)("..")
      val downs = relative.segments.toList

      ModuleNameParser(
        source.libName.`__value` +: ups ++: downs,
        keepIndexPath,
      )

That is assuming that the .. segments are handled correctly.

steinybot commented 1 month ago

Ah nope that doesn't work.

Maybe this?

      val relative = file.path.relativeTo(source.folder.path)
      val downs = relative.segments.toList
      val fragments = relative.ups match {
        case 0 => source.libName.`__value` +: downs
        case 1 => downs
        case _ => ???
      }

      ModuleNameParser(fragments, keepIndexPath)

But now the scope.moduleScopes are wrong. They only have:

scope.moduleScopes = {Map$Map4@8516} size = 4
 0 = {Tuple2@8546} (TsIdentModule(None,List(lib)),TreeScope(TsParsedFile() / TsDeclModule(lib)))
 1 = {Tuple2@8547} (TsIdentModule(None,List(es, index)),TreeScope(TsParsedFile() / TsDeclModule(lib)))
 2 = {Tuple2@8548} (TsIdentModule(None,List(es)),TreeScope(TsParsedFile() / TsDeclModule(lib)))
 3 = {Tuple2@8549} (TsIdentModule(None,List(lib, index)),TreeScope(TsParsedFile() / TsDeclModule(lib)))
steinybot commented 1 month ago

Hmm lib and dist are being treated as different roots. I would have thought . would be the root of both of them.

steinybot commented 1 month ago

Oh wait is this issue with moduleScopes just an ImporterHarness problem? It is creating a separate LibTsSource and running the full phase on each one individually. I'll try publish the fix locally and see if it fixes my stripe import issue.

steinybot commented 1 month ago

Surely relative imports must work...

I tried:

import {loadStripe} from '../dist';
export {loadStripe};

but org.scalablytyped.converter.internal.ts.modules.Exports#expandExport fails for that.

steinybot commented 1 month ago

This also doesn't work:

import {StripeConstructor} from "../dist/stripe-js";

Local "modules" (they aren't separate modules) aren't considered dependencies? https://github.com/ScalablyTyped/Converter/blob/02257bf3588da08deec5a3b07f306fcc6236642d/importer-portable/src/main/scala/org/scalablytyped/converter/internal/importer/Phase1ReadTypescript.scala#L104-L111

steinybot commented 1 month ago

This also doesn't work:

import {StripeConstructor} from "../dist/stripe-js";

declare global {
  interface Window {
    // Stripe.js must be loaded directly from https://js.stripe.com/v3, which
    // places a `Stripe` object on the window
    Stripe?: StripeConstructor;
  }
}

org.scalablytyped.converter.internal.ts.modules.Imports#lookupFromImports has chosenImport as TsImport(false,IArray(Destructured(IArray((TsIdentSimple(StripeConstructor),None)))),From(TsIdentModule(None,List(stripe-js, dist, stripe-js)))) which is weird. Why stripe-js/dist/stripe-js?

I realise this makes little sense without the test I am using. It is in https://github.com/steinybot/Converter/commit/e3d39fbe69e66a391166cb9c4f00cb49b79ce1f9. Apologies for the ramblings. I'm attempting to document my process so that when I come back to it on Monday I know what I did.

steinybot commented 1 month ago

stripe-js/dist/stripe-js is correct but it is another star export

steinybot commented 1 month ago

This seems relevant. https://github.com/ScalablyTyped/Converter/blob/02257bf3588da08deec5a3b07f306fcc6236642d/importer-portable/src/main/scala/org/scalablytyped/converter/internal/importer/Phase1ReadTypescript.scala#L59-L61