Skip to content

Commit 27400df

Browse files
committed
Generalize type references to classes to point to the parent class as long as name and type parameters are the same.
1 parent 69157a7 commit 27400df

File tree

16 files changed

+85
-25
lines changed

16 files changed

+85
-25
lines changed

importer-portable/src/main/scala/org/scalablytyped/converter/internal/importer/Phase2ToScalaJs.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Phase2ToScalaJs(
7676
Adapter(scope)((tree, s) => S.UnionToInheritance(s, tree, scalaName, willBeExternalTypes)), // after FakeLiterals
7777
S.LimitUnionLength.visitPackageTree(scope), // after UnionToInheritance
7878
(S.AvoidMacroParadiseBug >> new S.RemoveMultipleInheritance(new ParentsResolver)).visitPackageTree(scope),
79+
S.GeneralizeTypeRefs.visitPackageTree(scope), // after RemoveMultipleInheritance
7980
S.CombineOverloads.visitPackageTree(scope), //must have stable types, so FakeLiterals run before
8081
new S.FilterMemberOverrides(new ParentsResolver).visitPackageTree(scope), //
8182
new S.InferMemberOverrides(new ParentsResolver)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.scalablytyped.converter.internal
2+
package scalajs
3+
package transforms
4+
5+
/**
6+
* So far we've been pretty pedantic in the handling of things like re-exported types.
7+
* ```typescript
8+
* declare module "libfoo/impl/foo" {
9+
* export class Foo {}
10+
* }
11+
* declare module "libfoo" {
12+
* export import {Foo} from "libfoo/impl/foo";
13+
* export class Bar {
14+
* constructor(foo: Foo) {}
15+
* }
16+
* }
17+
* ```
18+
*
19+
* In most cases `Bar` has pointed to `libfoo.Foo` so far, while the intention was really something compatible with `libfoo/impl/foo.Foo`
20+
*
21+
* In the name of pragmatism, assert that if a class has a parent with the same name and type parameters, accept that in the place of the child class.
22+
*
23+
* Also, only do this for types in input position
24+
*/
25+
object GeneralizeTypeRefs extends TreeTransformation {
26+
override def leaveTypeRef(scope: TreeScope)(tr: TypeRef): TypeRef =
27+
if (shouldGeneralize(scope)) generalize(scope)(tr).copy(comments = tr.comments) else tr
28+
29+
def shouldGeneralize(scope: TreeScope): Boolean =
30+
scope.stack.filterNot(_.isInstanceOf[TypeRef]) match {
31+
case (_: MethodTree) :: _ => false // return type
32+
case (_: ClassTree) :: _ => false // parent
33+
case _ => true
34+
}
35+
36+
def generalize(scope: TreeScope)(tr: TypeRef): TypeRef =
37+
if (scope.isAbstract(tr)) tr
38+
else
39+
FollowAliases(scope)(tr) match {
40+
case dealiasedTr if dealiasedTr.name === tr.name && dealiasedTr.targs.length === tr.targs.length =>
41+
scope
42+
._lookup(dealiasedTr.typeName.parts)
43+
.collectFirst {
44+
case (cls: ClassTree, newScope) if cls.tparams.length === tr.targs.length =>
45+
val newCls = FillInTParams(cls, newScope, dealiasedTr.targs, Empty)
46+
newCls.parents match {
47+
case IArray.exactlyOne(p) if p.name === tr.name =>
48+
// commit at this point
49+
generalize(newScope)(p)
50+
case _ =>
51+
tr
52+
}
53+
case _ => tr
54+
}
55+
.getOrElse(tr)
56+
57+
case _ => tr
58+
}
59+
}

tests/aws-sdk/check/a/aws-sdk/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
organization := "org.scalablytyped"
22
name := "aws-sdk"
3-
version := "2.247.1-ea5354"
3+
version := "2.247.1-659d53"
44
scalaVersion := "2.13.3"
55
enablePlugins(ScalaJSPlugin)
66
libraryDependencies ++= Seq(

tests/aws-sdk/check/a/aws-sdk/src/main/scala/typings/awsSdk/allMod.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ object allMod extends js.Object {
2121
object DynamoDB extends js.Object {
2222
@js.native
2323
class Converter ()
24-
extends typings.awsSdk.dynamodbMod.Converter
24+
extends typings.awsSdk.converterMod.Converter
2525

2626
@js.native
2727
/**
2828
* Creates a DynamoDB document client with a set of configuration options.
2929
*/
3030
class DocumentClient ()
31-
extends typings.awsSdk.dynamodbMod.DocumentClient {
31+
extends typings.awsSdk.documentClientMod.DocumentClient {
3232
def this(options: DocumentClientOptions with ClientConfiguration) = this()
3333
}
3434

tests/aws-sdk/check/a/aws-sdk/src/main/scala/typings/awsSdk/mod.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ object mod extends js.Object {
2323
object DynamoDB extends js.Object {
2424
@js.native
2525
class Converter ()
26-
extends typings.awsSdk.allMod.DynamoDB.Converter
26+
extends typings.awsSdk.converterMod.Converter
2727

2828
@js.native
2929
/**
3030
* Creates a DynamoDB document client with a set of configuration options.
3131
*/
3232
class DocumentClient ()
33-
extends typings.awsSdk.allMod.DynamoDB.DocumentClient {
33+
extends typings.awsSdk.documentClientMod.DocumentClient {
3434
def this(options: DocumentClientOptions with ClientConfiguration) = this()
3535
}
3636

tests/firebase-admin/check/f/firebase-admin/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
organization := "org.scalablytyped"
22
name := "firebase-admin"
3-
version := "8.2.0-e100e5"
3+
version := "8.2.0-64d207"
44
scalaVersion := "2.13.3"
55
enablePlugins(ScalaJSPlugin)
66
libraryDependencies ++= Seq(

tests/firebase-admin/check/f/firebase-admin/src/main/scala/typings/firebaseAdmin/mod.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ object mod extends js.Object {
2020
def this(settings: Settings) = this()
2121
}
2222

23-
def apply(): Firestore = js.native
24-
def apply(str: String): Firestore = js.native
23+
def apply(): typings.googleCloudFirestore.FirebaseFirestore.Firestore = js.native
24+
def apply(str: String): typings.googleCloudFirestore.FirebaseFirestore.Firestore = js.native
2525
}
2626

2727
}

tests/sax/check/n/node/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
organization := "org.scalablytyped"
22
name := "node"
3-
version := "9.6.x-171ad7"
3+
version := "9.6.x-b3c907"
44
scalaVersion := "2.13.3"
55
enablePlugins(ScalaJSPlugin)
66
libraryDependencies ++= Seq(

tests/sax/check/n/node/src/main/scala/typings/node/eventsMod/EventEmitter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class EventEmitter ()
1313
@JSImport("events", "EventEmitter")
1414
@js.native
1515
object EventEmitter extends js.Object {
16-
def listenerCount(emitter: EventEmitter, event: String): Double = js.native
17-
def listenerCount(emitter: EventEmitter, event: js.Symbol): Double = js.native
16+
def listenerCount(emitter: typings.node.NodeJS.EventEmitter, event: String): Double = js.native
17+
def listenerCount(emitter: typings.node.NodeJS.EventEmitter, event: js.Symbol): Double = js.native
1818
}
1919

tests/sax/check/n/node/src/main/scala/typings/node/streamMod/internal.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package typings.node.streamMod
22

3+
import typings.node.NodeJS.EventEmitter
34
import typings.node.NodeJS.WritableStream
45
import typings.node.anon.End
5-
import typings.node.eventsMod.EventEmitter
66
import scala.scalajs.js
77
import scala.scalajs.js.`|`
88
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

0 commit comments

Comments
 (0)