Skip to content

Commit

Permalink
Generalize type references to classes to point to the parent class as…
Browse files Browse the repository at this point in the history
… long as name and type parameters are the same.
  • Loading branch information
oyvindberg committed Oct 5, 2020
1 parent 69157a7 commit 759d141
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 25 deletions.
Expand Up @@ -76,6 +76,7 @@ class Phase2ToScalaJs(
Adapter(scope)((tree, s) => S.UnionToInheritance(s, tree, scalaName, willBeExternalTypes)), // after FakeLiterals
S.LimitUnionLength.visitPackageTree(scope), // after UnionToInheritance
(S.AvoidMacroParadiseBug >> new S.RemoveMultipleInheritance(new ParentsResolver)).visitPackageTree(scope),
S.GeneralizeTypeRefs.visitPackageTree(scope), // after RemoveMultipleInheritance
S.CombineOverloads.visitPackageTree(scope), //must have stable types, so FakeLiterals run before
new S.FilterMemberOverrides(new ParentsResolver).visitPackageTree(scope), //
new S.InferMemberOverrides(new ParentsResolver)
Expand Down
@@ -0,0 +1,32 @@
package org.scalablytyped.converter.internal
package scalajs
package transforms

object GeneralizeTypeRefs extends TreeTransformation {
override def leaveTypeRef(scope: TreeScope)(tr: TypeRef): TypeRef =
generalize(scope)(tr)

def generalize(scope: TreeScope)(tr: TypeRef): TypeRef =
if (scope.isAbstract(tr)) tr
else
FollowAliases(scope)(tr) match {
case dealiasedTr if dealiasedTr.name === tr.name =>
scope
._lookup(dealiasedTr.typeName.parts)
.collectFirst {
case (cls: ClassTree, newScope) if cls.tparams.length === tr.targs.length =>
val newCls = FillInTParams(cls, newScope, dealiasedTr.targs, Empty)
newCls.parents match {
case IArray.exactlyOne(p) if p.name === tr.name =>
// commit at this point
generalize(newScope)(p)
case _ =>
tr
}
case _ => tr
}
.getOrElse(tr)

case _ => tr
}
}
2 changes: 1 addition & 1 deletion tests/aws-sdk/check/a/aws-sdk/build.sbt
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "aws-sdk"
version := "2.247.1-ea5354"
version := "2.247.1-659d53"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Expand Up @@ -21,14 +21,14 @@ object allMod extends js.Object {
object DynamoDB extends js.Object {
@js.native
class Converter ()
extends typings.awsSdk.dynamodbMod.Converter
extends typings.awsSdk.converterMod.Converter

@js.native
/**
* Creates a DynamoDB document client with a set of configuration options.
*/
class DocumentClient ()
extends typings.awsSdk.dynamodbMod.DocumentClient {
extends typings.awsSdk.documentClientMod.DocumentClient {
def this(options: DocumentClientOptions with ClientConfiguration) = this()
}

Expand Down
Expand Up @@ -23,14 +23,14 @@ object mod extends js.Object {
object DynamoDB extends js.Object {
@js.native
class Converter ()
extends typings.awsSdk.allMod.DynamoDB.Converter
extends typings.awsSdk.converterMod.Converter

@js.native
/**
* Creates a DynamoDB document client with a set of configuration options.
*/
class DocumentClient ()
extends typings.awsSdk.allMod.DynamoDB.DocumentClient {
extends typings.awsSdk.documentClientMod.DocumentClient {
def this(options: DocumentClientOptions with ClientConfiguration) = this()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/firebase-admin/check/f/firebase-admin/build.sbt
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "firebase-admin"
version := "8.2.0-e100e5"
version := "8.2.0-64d207"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Expand Up @@ -20,8 +20,8 @@ object mod extends js.Object {
def this(settings: Settings) = this()
}

def apply(): Firestore = js.native
def apply(str: String): Firestore = js.native
def apply(): typings.googleCloudFirestore.FirebaseFirestore.Firestore = js.native
def apply(str: String): typings.googleCloudFirestore.FirebaseFirestore.Firestore = js.native
}

}
Expand Down
2 changes: 1 addition & 1 deletion tests/sax/check/n/node/build.sbt
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "node"
version := "9.6.x-171ad7"
version := "9.6.x-b3c907"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Expand Up @@ -13,7 +13,7 @@ class EventEmitter ()
@JSImport("events", "EventEmitter")
@js.native
object EventEmitter extends js.Object {
def listenerCount(emitter: EventEmitter, event: String): Double = js.native
def listenerCount(emitter: EventEmitter, event: js.Symbol): Double = js.native
def listenerCount(emitter: typings.node.NodeJS.EventEmitter, event: String): Double = js.native
def listenerCount(emitter: typings.node.NodeJS.EventEmitter, event: js.Symbol): Double = js.native
}

@@ -1,8 +1,8 @@
package typings.node.streamMod

import typings.node.NodeJS.EventEmitter
import typings.node.NodeJS.WritableStream
import typings.node.anon.End
import typings.node.eventsMod.EventEmitter
import scala.scalajs.js
import scala.scalajs.js.`|`
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
Expand Down
4 changes: 2 additions & 2 deletions tests/sax/check/s/sax/build.sbt
@@ -1,11 +1,11 @@
organization := "org.scalablytyped"
name := "sax"
version := "1.x-cca7a4"
version := "1.x-ade742"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.1.0",
"org.scalablytyped" %%% "node" % "9.6.x-171ad7",
"org.scalablytyped" %%% "node" % "9.6.x-b3c907",
"org.scalablytyped" %%% "std" % "0.0-unknown-f7d52a")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "express-serve-static-core"
version := "0.0-unknown-577f29"
version := "0.0-unknown-b641a9"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Expand Up @@ -7,12 +7,12 @@ import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, J
@js.native
trait Response
extends typings.expressServeStaticCore.mod.global.Express.Response {
def location(url: String): Response = js.native
def location(url: String): typings.expressServeStaticCore.mod.global.Express.Response = js.native
}

object Response {
@scala.inline
def apply(location: String => Response): Response = {
def apply(location: String => typings.expressServeStaticCore.mod.global.Express.Response): Response = {
val __obj = js.Dynamic.literal(location = js.Any.fromFunction1(location))
__obj.asInstanceOf[Response]
}
Expand All @@ -28,7 +28,7 @@ object Response {
x
}
@scala.inline
def setLocation(value: String => Response): Self = this.set("location", js.Any.fromFunction1(value))
def setLocation(value: String => typings.expressServeStaticCore.mod.global.Express.Response): Self = this.set("location", js.Any.fromFunction1(value))
}

}
Expand Down
Expand Up @@ -8,8 +8,8 @@ package object mod {
type Handler = typings.expressServeStaticCore.mod.RequestHandler
type NextFunction = js.Function1[/* err */ js.UndefOr[js.Any], scala.Unit]
type RequestHandler = js.Function3[
/* req */ typings.expressServeStaticCore.mod.Request,
/* res */ typings.expressServeStaticCore.mod.Response,
typings.expressServeStaticCore.mod.global.Express.Request,
typings.expressServeStaticCore.mod.global.Express.Response,
/* next */ typings.expressServeStaticCore.mod.NextFunction,
js.Any
]
Expand Down
4 changes: 2 additions & 2 deletions tests/serve-static/check/s/serve-static/build.sbt
@@ -1,11 +1,11 @@
organization := "org.scalablytyped"
name := "serve-static"
version := "0.0-unknown-79d765"
version := "0.0-unknown-b8f141"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.1.0",
"org.scalablytyped" %%% "express-serve-static-core" % "0.0-unknown-577f29",
"org.scalablytyped" %%% "express-serve-static-core" % "0.0-unknown-b641a9",
"org.scalablytyped" %%% "mime" % "2.0-a17b50",
"org.scalablytyped" %%% "std" % "0.0-unknown-c6dcd1")
publishArtifact in packageDoc := false
Expand Down
@@ -1,13 +1,13 @@
package typings.serveStatic.mod

import typings.expressServeStaticCore.mod.Response
import typings.expressServeStaticCore.mod.global.Express.Response
import scala.scalajs.js
import scala.scalajs.js.`|`
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

@js.native
trait ServeStaticOptions extends js.Object {
var setHeaders: js.UndefOr[js.Function3[/* res */ Response, /* path */ String, /* stat */ js.Any, _]] = js.native
var setHeaders: js.UndefOr[js.Function3[Response, /* path */ String, /* stat */ js.Any, _]] = js.native
}

object ServeStaticOptions {
Expand All @@ -28,7 +28,7 @@ object ServeStaticOptions {
x
}
@scala.inline
def setSetHeaders(value: (/* res */ Response, /* path */ String, /* stat */ js.Any) => _): Self = this.set("setHeaders", js.Any.fromFunction3(value))
def setSetHeaders(value: (Response, /* path */ String, /* stat */ js.Any) => _): Self = this.set("setHeaders", js.Any.fromFunction3(value))
@scala.inline
def deleteSetHeaders: Self = this.set("setHeaders", js.undefined)
}
Expand Down

0 comments on commit 759d141

Please sign in to comment.