Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala.js 1.0.0 unnecessary generates imports for parent classes #4001

Closed
oyvindberg opened this issue Mar 27, 2020 · 4 comments · Fixed by #4009
Closed

Scala.js 1.0.0 unnecessary generates imports for parent classes #4001

oyvindberg opened this issue Mar 27, 2020 · 4 comments · Fixed by #4009
Assignees
Labels
bug Confirmed bug. Needs to be fixed.
Milestone

Comments

@oyvindberg
Copy link
Contributor

As mentioned in ScalablyTyped/Converter#131 Scala.js 1.0.0 ends up including imports of superclasses unnecessarily when referencing a class. I'll copy in the example:

object Main {
  @JSImport("rsocket-websocket-client/RSocketWebSocketClient", JSImport.Default)
  @js.native
  class Parent extends js.Object

  @JSImport("rsocket-websocket-client", JSImport.Default)
  @js.native
  class Child extends Parent

  def main(args: Array[String]): Unit = 
    println(new Child)
}

With Scala.js 0.6, this would only import from the module rsocket-websocket-client, while Scala.js 1.0.0 will also import from rsocket-websocket-client/RSocketWebSocketClient.

This is a problem because the latter doesn't exist, so bundling won't succeed.

There are a range of reasons why Typescript definitions may end up mentioning module names which don't have a corresponding .js file at bundle-time. For this particular example the third party typings are imprecise.

It's not impossible for me to find a different encoding for re-exporting classes such that this would be safe, but it would end up quite a bit uglier.

Any chance we can revert this behaviour?

@mushtaq
Copy link

mushtaq commented Apr 3, 2020

I want to upvote this in the hope it is given priority.

ScalablyTypes is a key project in making use of existing TS ecosystem. This issue will unblock projects consuming ScalablyTyped to move to Scala.js 1.x.

@gzm0
Copy link
Contributor

gzm0 commented Apr 3, 2020

Implementation note:

The reason this happens is that when we analyze, we always load the super class, even for native classes which usually do not need this information (I'm unsure whether there is a case it is needed). This is probably a good idea anyways to have a consistent LinkingUnit.

However, when emitting imports, we cannot distinguish between classes that are present because they are used or because merely something declares them in their "metadata". As such we import all of them.

@sjrd the refinement / linking stages could drop the jsNativeLoadSpec if it is not needed. But that in turn would make the IR checker fail / make it impossible for us to check that a native class in principle has a load spec.
Alternatively, we can add relevant analyzer metadata to the LinkedClass. But that would be kind of inconsistent with other refinement.

@sjrd
Copy link
Member

sjrd commented Apr 3, 2020

I think we should just not generate imports for native JS classes that are not instantiated (i.e., its LinkedClass.hasInstances field is false). We don't call Analyzer#ClassInfo.instantiated() for the superclass of a native JS class (only for the superclass of a non-native JS class, or of course when we use LoadJSConstructor() of the class).

@gzm0 gzm0 self-assigned this Apr 8, 2020
@gzm0 gzm0 added the bug Confirmed bug. Needs to be fixed. label Apr 8, 2020
@gzm0 gzm0 added this to the v1.1.0 milestone Apr 8, 2020
gzm0 added a commit to gzm0/scala-js that referenced this issue Apr 9, 2020
@gzm0 gzm0 linked a pull request Apr 9, 2020 that will close this issue
gzm0 added a commit to gzm0/scala-js that referenced this issue Apr 9, 2020
@gzm0 gzm0 closed this as completed in #4009 Apr 9, 2020
gzm0 added a commit that referenced this issue Apr 9, 2020
Fix #4001: Only import JS classes that are needed
@mushtaq
Copy link

mushtaq commented Apr 9, 2020

Hey, thank you so much @gzm0 for being so responsive!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug. Needs to be fixed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants