Skip to content

Commit

Permalink
Dont rewrite type unions to inheritance when the types will be replac…
Browse files Browse the repository at this point in the history
…ed with external types.

This is a stopgap solution until the new union type alias encoding is done
  • Loading branch information
oyvindberg committed Jun 17, 2020
1 parent d866af1 commit c0ec940
Show file tree
Hide file tree
Showing 36 changed files with 101 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ object Main {
pedantic = false,
enableScalaJsDefined = conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = conversion.flavourImpl,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import com.olvind.logging.Logger
import org.scalablytyped.converter.internal.importer.Phase1Res.{LibTs, LibraryPart}
import org.scalablytyped.converter.internal.maps._
import org.scalablytyped.converter.internal.phases.{GetDeps, IsCircular, Phase, PhaseRes}
import org.scalablytyped.converter.internal.scalajs.flavours.FlavourImpl
import org.scalablytyped.converter.internal.scalajs.transforms.{Adapter, CleanIllegalNames}
import org.scalablytyped.converter.internal.scalajs.{Name, PackageTree, ParentsResolver, TreeScope, transforms => S}
import org.scalablytyped.converter.internal.scalajs.{
Name,
PackageTree,
ParentsResolver,
QualifiedName,
TreeScope,
transforms => S,
}
import org.scalablytyped.converter.internal.ts.{TsIdentLibrary, TsTreeTraverse}

import scala.collection.immutable.SortedSet
Expand All @@ -16,8 +24,17 @@ import scala.collection.immutable.SortedSet
* This phase starts by going from the typescript AST to the scala AST.
* Then the phase itself implements a bunch of scala.js limitations, like ensuring no methods erase to the same signature
*/
class Phase2ToScalaJs(pedantic: Boolean, enableScalaJsDefined: Selection[TsIdentLibrary], outputPkg: Name)
extends Phase[Source, Phase1Res, LibScalaJs] {
class Phase2ToScalaJs(
pedantic: Boolean,
enableScalaJsDefined: Selection[TsIdentLibrary],
outputPkg: Name,
flavour: FlavourImpl,
) extends Phase[Source, Phase1Res, LibScalaJs] {

val willBeExternalTypes: Set[QualifiedName] = flavour.rewritesOpt match {
case Some(rewrites) => rewrites.conversionsForTypeName.keys.to[Set]
case None => Set()
}

override def apply(
source: Source,
Expand Down Expand Up @@ -56,7 +73,7 @@ class Phase2ToScalaJs(pedantic: Boolean, enableScalaJsDefined: Selection[TsIdent
cleanIllegalNames >>
S.Deduplicator visitPackageTree scope,
Adapter(scope)((tree, s) => S.FakeLiterals(outputPkg, s, cleanIllegalNames)(tree)),
Adapter(scope)((tree, s) => S.UnionToInheritance(s, tree, scalaName)), // after FakeLiterals
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.CombineOverloads visitPackageTree scope, //must have stable types, so FakeLiterals run before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class Ci(config: Ci.Config, paths: Ci.Paths, publisher: Publisher, pool: ForkJoi
config.pedantic,
enableScalaJsDefined = config.conversion.enableScalaJsDefined,
outputPkg = config.conversion.outputPackage,
flavour = config.conversion.flavourImpl,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ trait ImporterHarness extends AnyFunSuite {
"typescript",
)
.next(
new Phase2ToScalaJs(pedantic, enableScalaJsDefined = Selection.None, outputPkg = flavour.outputPkg),
new Phase2ToScalaJs(
pedantic,
enableScalaJsDefined = Selection.None,
outputPkg = flavour.outputPkg,
flavour = flavour,
),
"scala.js",
)
.next(new PhaseFlavour(flavour), flavour.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ object ImportTypings {
pedantic = false,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = input.conversion.outputPackage,
flavour = input.conversion.flavourImpl,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ object ImportTypingsGenSources {
)
.next(
new Phase2ToScalaJs(
pedantic = false,
input.conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
pedantic = false,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = input.conversion.flavourImpl,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ trait FlavourImpl {
def dependencies: Set[Dep]
val outputPkg: Name

val rewritesOpt: Option[CastConversion.TypeRewriterCast]

override val toString = getClass.getSimpleName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ case class JapgollyFlavour(outputPkg: Name, enableImplicitOps: Boolean) extends

rewriter.visitPackageTree(scope)(withComponents)
}

override val rewritesOpt: Option[CastConversion.TypeRewriterCast] = Some(rewriter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ case class NormalFlavour(

override val dependencies =
if (shouldUseScalaJsDomTypes) Set(Versions.scalaJsDom, Versions.runtime) else Set(Versions.runtime)
val rewriterOpt = if (shouldUseScalaJsDomTypes) Some(new TypeRewriterCast(scalaJsDomNames.All)) else None
val memberToProp = new MemberToProp.Default(rewriterOpt)
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToProp, parentsResolver)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)
override val rewritesOpt = if (shouldUseScalaJsDomTypes) Some(new TypeRewriterCast(scalaJsDomNames.All)) else None
val memberToProp = new MemberToProp.Default(rewritesOpt)
val findProps = new FindProps(new CleanIllegalNames(outputPkg), memberToProp, parentsResolver)
val genCompanions = new GenCompanions(findProps, enableImplicitOps)

final override def rewrittenTree(scope: TreeScope, tree: PackageTree): PackageTree = {
val withCompanions = genCompanions.visitPackageTree(scope)(tree)

rewriterOpt match {
rewritesOpt match {
case Some(rewriter) => rewriter.visitPackageTree(scope)(withCompanions)
case _ => withCompanions
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ case class SlinkyFlavour(outputPkg: Name, enableImplicitOps: Boolean) extends Fl

rewriter.visitPackageTree(scope)(withComponents)
}

override val rewritesOpt: Option[CastConversion.TypeRewriterCast] = Some(rewriter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ case class SlinkyNativeFlavour(outputPkg: Name, enableImplicitOps: Boolean) exte

rewriter.visitPackageTree(scope)(withComponents)
}

override val rewritesOpt: Option[CastConversion.TypeRewriterCast] = Some(rewriter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ package transforms
object UnionToInheritance {
final case class WasUnion(related: IArray[TypeRef]) extends Comment.Data

def apply(scope: TreeScope, tree: ContainerTree, inLib: Name): ContainerTree = {
val rewrites = Rewrite.identify(inLib, tree, scope / tree)
def apply(
scope: TreeScope,
tree: ContainerTree,
inLib: Name,
willBeExternalTypes: Set[QualifiedName],
): ContainerTree = {
val rewrites = Rewrite.identify(inLib, tree, scope / tree, willBeExternalTypes)

val newParentsByCodePath: Map[QualifiedName, IArray[InvertingTypeParamRef]] =
rewrites
Expand Down Expand Up @@ -181,16 +186,22 @@ object UnionToInheritance {
final case class Rewrite(original: TypeAliasTree, asInheritance: IArray[TypeRef], unchanged: IArray[TypeRef])

object Rewrite {
def identify(inLib: Name, p: ContainerTree, scope: TreeScope): IArray[Rewrite] = {
def identify(
inLib: Name,
p: ContainerTree,
scope: TreeScope,
willBeExternalTypes: Set[QualifiedName],
): IArray[Rewrite] = {
def go(p: ContainerTree, scope: TreeScope): IArray[Rewrite] = {
def legalClassName(name: Name): Boolean =
p index name forall {
case _: PackageTree => false
case _ => true
}
p.members.flatMap {
case p: ContainerTree => identify(inLib, p, scope / p)
case ta: TypeAliasTree if legalClassName(ta.name) => IArray.fromOption(canRewrite(inLib, ta, scope / ta))
case p: ContainerTree => identify(inLib, p, scope / p, willBeExternalTypes)
case ta: TypeAliasTree if legalClassName(ta.name) =>
IArray.fromOption(canRewrite(inLib, ta, scope / ta, willBeExternalTypes))
case _ => Empty
}
}
Expand All @@ -213,15 +224,22 @@ object UnionToInheritance {
all.map(r => r.copy(unchanged = r.unchanged ++ r.asInheritance.map(_.typeName).flatMap(go).distinct))
}

def canRewrite(inLib: Name, ta: TypeAliasTree, scope: TreeScope): Option[Rewrite] =
def canRewrite(
inLib: Name,
ta: TypeAliasTree,
scope: TreeScope,
willBeExternalTypes: Set[QualifiedName],
): Option[Rewrite] =
ta.alias match {
case TypeRef.Union(types, _) =>
def legalTarget(tr: TypeRef): Boolean =
scope.lookup(tr.typeName).exists {
case (_: ClassTree, _) => true
case (ta: TypeAliasTree, _) => canRewrite(inLib, ta, scope).isDefined
case _ => false
}
if (willBeExternalTypes(tr.typeName)) false
else
scope.lookup(tr.typeName).exists {
case (_: ClassTree, _) => true
case (ta: TypeAliasTree, _) => canRewrite(inLib, ta, scope, willBeExternalTypes).isDefined
case _ => false
}

val InLibrary: PartialFunction[TypeRef, TypeRef] = {
case tr @ TypeRef(QualifiedName(parts), _, _)
Expand Down
4 changes: 2 additions & 2 deletions tests/material-ui/check-japgolly/m/material-ui/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "material-ui"
version := "0.0-unknown-1631b3"
version := "0.0-unknown-ff1022"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "1.7.0",
"com.olvind" %%% "scalablytyped-runtime" % "2.1.0",
"org.scalablytyped" %%% "react" % "0.0-unknown-fdf042",
"org.scalablytyped" %%% "react" % "0.0-unknown-1c8110",
"org.scalablytyped" %%% "std" % "0.0-unknown-7244e3")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
2 changes: 1 addition & 1 deletion tests/material-ui/check-japgolly/r/react/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "react"
version := "0.0-unknown-fdf042"
version := "0.0-unknown-1c8110"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import scala.scalajs.js.annotation._

@js.native
trait ComponentClass[P]
extends ComponentType[P]
with Instantiable1[
extends Instantiable1[
/* props */ P,
japgolly.scalajs.react.raw.React.Component[P with js.Object, js.Object]
]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.scalajs.js.`|`
import scala.scalajs.js.annotation._

@js.native
trait StatelessComponent[P] extends ComponentType[P] {
trait StatelessComponent[P] extends js.Object {
var defaultProps: js.UndefOr[Partial[P]] = js.native
var displayName: js.UndefOr[String] = js.native
def apply(props: P with Children): Element | Null = js.native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.scalajs.js.annotation._

package object mod {
type ComponentState = js.Object
type ComponentType[P] = (japgolly.scalajs.react.raw.React.ComponentClassP[P with js.Object]) | typingsJapgolly.react.mod.StatelessComponent[P]
type Key = java.lang.String | scala.Double
type ReactNode = js.UndefOr[java.lang.String | scala.Double | scala.Boolean]
type SFC[P] = typingsJapgolly.react.mod.StatelessComponent[P]
Expand Down
4 changes: 2 additions & 2 deletions tests/material-ui/check-slinky/m/material-ui/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "material-ui"
version := "0.0-unknown-9a6db6"
version := "0.0-unknown-3ac7af"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.1.0",
"me.shadaj" %%% "slinky-web" % "0.6.5",
"org.scalablytyped" %%% "react" % "0.0-unknown-726a4c",
"org.scalablytyped" %%% "react" % "0.0-unknown-c6c500",
"org.scalablytyped" %%% "std" % "0.0-unknown-e37381")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
2 changes: 1 addition & 1 deletion tests/material-ui/check-slinky/r/react/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "react"
version := "0.0-unknown-726a4c"
version := "0.0-unknown-c6c500"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import scala.scalajs.js.annotation._

@js.native
trait ComponentClass[P]
extends ComponentType[P]
with Instantiable1[/* props */ P, ReactComponentClass[P]]
extends Instantiable1[/* props */ P, ReactComponentClass[P]]
with Instantiable2[/* props */ P, /* context */ js.Any, ReactComponentClass[P]] {
var defaultProps: js.UndefOr[Partial[P]] = js.native
var displayName: js.UndefOr[String] = js.native
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.scalajs.js.`|`
import scala.scalajs.js.annotation._

@js.native
trait StatelessComponent[P] extends ComponentType[P] {
trait StatelessComponent[P] extends js.Object {
var defaultProps: js.UndefOr[Partial[P]] = js.native
var displayName: js.UndefOr[String] = js.native
def apply(props: P with Children): slinky.core.facade.ReactElement | Null = js.native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.scalajs.js.annotation._

package object mod {
type ComponentState = js.Object
type ComponentType[P] = slinky.core.ReactComponentClass[P]
type Key = java.lang.String | scala.Double
type ReactNode = js.UndefOr[java.lang.String | scala.Double | scala.Boolean]
type SFC[P] = slinky.core.ReactComponentClass[P]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "react-transition-group"
version := "2.0-6ecb64"
version := "2.0-d7d8f4"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "1.7.0",
"com.olvind" %%% "scalablytyped-runtime" % "2.1.0",
"org.scalablytyped" %%% "react" % "0.0-unknown-267232",
"org.scalablytyped" %%% "react" % "0.0-unknown-f1f1a5",
"org.scalablytyped" %%% "std" % "0.0-unknown-a4120e")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "react"
version := "0.0-unknown-267232"
version := "0.0-unknown-f1f1a5"
scalaVersion := "2.13.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import scala.scalajs.js.annotation._

@js.native
trait ComponentClass[P]
extends ComponentType[P]
with Instantiable1[
extends Instantiable1[
/* props */ P,
japgolly.scalajs.react.raw.React.Component[P with js.Object, js.Object]
]
Expand Down

This file was deleted.

Loading

0 comments on commit c0ec940

Please sign in to comment.