Skip to content

Commit

Permalink
import: give type aliases of conditional types same treatment as thos…
Browse files Browse the repository at this point in the history
…e with mapped types, see da701d2
  • Loading branch information
oyvindberg committed Oct 17, 2022
1 parent fbe142d commit 28eaaad
Show file tree
Hide file tree
Showing 82 changed files with 679 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,41 @@ class ImportTree(
),
)

/* Conditional types. Proper handling (of static) cases is done elsewhere, this just takes care of the dependencies */
case TsDeclTypeAlias(cs, _, _, tparams, tpe: TsTypeConditional, codePath) =>
val importedCp = importName(codePath)
val importedName = importedCp.parts.last
val importedTparams = tparams.map(typeParam(scope, importName))

def warning(explain: String) =
cs ++ Comments(
s"""/** NOTE: Conditional type definitions are impossible to translate to Scala.
| * See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
| * $explain.
| * TS definition: {{{
| ${TsTypeFormatter(tpe)}
| }}}
| */
|""".stripMargin,
)

IArray(
ClassTree(
isImplicit = false,
annotations = IArray(Annotation.JsNative),
level = ProtectionLevel.Public,
name = importedName,
tparams = importedTparams,
parents = Empty,
ctors = Empty,
members = Empty,
classType = ClassType.Trait,
isSealed = false,
comments = warning("You'll have to cast your way around this structure, unfortunately"),
codePath = importedCp,
),
)

/* Mapped types. Proper handling (of static) cases is done elsewhere, this just takes care of the dependencies */
case TsDeclTypeAlias(
cs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,6 @@ class ImportType(stdNames: QualifiedName.StdNames) {
case TsTypeThis() =>
TypeRef.ThisType(NoComments)

case x: TsTypeConditional =>
apply(scope, importName)(unify(IArray(x.ifFalse, x.ifTrue)))

case other =>
val msg = s"Failed type conversion: ${TsTypeFormatter(other)}"
scope.logger.info(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ object Phase1ReadTypescript {
if (expandTypeMappings(libName)) T.ExpandTypeMappings.visitTsParsedFile(scope.caching) else identity, // before ExtractInterfaces
if (expandTypeMappings(libName)) T.ExpandTypeMappings.After.visitTsParsedFile(scope.caching) else identity, // before ExtractInterfaces
(
T.SimplifyConditionals >> // after ExpandTypeMappings
T.TypeAliasToConstEnum >>
T.TypeAliasToConstEnum >>
T.ForwardCtors >>
T.ExpandTypeParams >>
T.UnionTypesFromKeyOf >>
Expand Down
4 changes: 2 additions & 2 deletions tests/punchcard/check-3/p/punchcard/build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
organization := "org.scalablytyped"
name := "punchcard"
version := "0.0-unknown-ae6eca"
version := "0.0-unknown-591208"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "std" % "0.0-unknown-29cbd9")
"org.scalablytyped" %%% "std" % "0.0-unknown-7dfdbf")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
2 changes: 1 addition & 1 deletion tests/punchcard/check-3/s/std/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "std"
version := "0.0-unknown-29cbd9"
version := "0.0-unknown-7dfdbf"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package typings.std

import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
T extends U ? T : never
}}}
*/
@js.native
trait Extract[T, U] extends StObject
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}


type Extract[T, U] = T

/** NOTE: Mapped type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/mapped-types.html for an intro.
* This translation is imprecise and ignores the effect of the type mapping.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "componentstest"
version := "0.0-unknown-192563"
version := "0.0-unknown-c856ab"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "react-bootstrap"
version := "0.32-917bae"
version := "0.32-6eac56"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "react-contextmenu"
version := "2.13.0-62c09e"
version := "2.13.0-e70b6a"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "react-dropzone"
version := "10.1.10-3f6e87"
version := "10.1.10-505392"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "react-markdown"
version := "0.0-unknown-7ebfca"
version := "0.0-unknown-a12fbc"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "react-native"
version := "0.0-unknown-0fb830"
version := "0.0-unknown-0c8e8e"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
organization := "org.scalablytyped"
name := "react-select"
version := "0.0-unknown-f830c1"
version := "0.0-unknown-685a3b"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "react" % "16.9.2-1a0f7f",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "react" % "16.9.2-89c3ae",
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "react"
version := "16.9.2-1a0f7f"
version := "16.9.2-89c3ae"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "2.1.1",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "std" % "0.0-unknown-57c945")
"org.scalablytyped" %%% "std" % "0.0-unknown-f06d0f")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, J

object anon {

trait `0` extends StObject {
trait `0`[R] extends StObject {

var ref: js.UndefOr[Exclude[Any, String]] = js.undefined
var ref: js.UndefOr[Exclude[R, String]] = js.undefined
}
object `0` {

inline def apply(): `0` = {
inline def apply[R](): `0`[R] = {
val __obj = js.Dynamic.literal()
__obj.asInstanceOf[`0`]
__obj.asInstanceOf[`0`[R]]
}

extension [Self <: `0`](x: Self) {
extension [Self <: `0`[?], R](x: Self & `0`[R]) {

inline def setRef(value: Exclude[Any, String]): Self = StObject.set(x, "ref", value.asInstanceOf[js.Any])
inline def setRef(value: Exclude[R, String]): Self = StObject.set(x, "ref", value.asInstanceOf[js.Any])

inline def setRefUndefined: Self = StObject.set(x, "ref", js.undefined)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package typingsJapgolly.react.mod

import typingsJapgolly.react.reactStrings.a_
import typingsJapgolly.react.reactStrings.abbr
import typingsJapgolly.react.reactStrings.address
import typingsJapgolly.react.reactStrings.area
import typingsJapgolly.react.reactStrings.article
import typingsJapgolly.react.reactStrings.aside
import typingsJapgolly.react.reactStrings.audio
import typingsJapgolly.react.reactStrings.b
import typingsJapgolly.react.reactStrings.base
import typingsJapgolly.react.reactStrings.bdi
import typingsJapgolly.react.reactStrings.bdo
import typingsJapgolly.react.reactStrings.big
import typingsJapgolly.react.reactStrings.view
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

/**
* NOTE: prefer ComponentPropsWithRef, if the ref is forwarded,
* or ComponentPropsWithoutRef when refs are not supported.
*/
/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
T extends react.react.JSXElementConstructor<infer P> ? P : T extends 'a' | 'abbr' | 'address' | 'area' | 'article' | 'aside' | 'audio' | 'b' | 'base' | 'bdi' | 'bdo' | 'big' | 'view' ? react.react.<global>.JSX.IntrinsicElements[T] : {}
}}}
*/
@js.native
trait ComponentProps[T /* <: a_ | abbr | address | area | article | aside | audio | b | base | bdi | bdo | big | view | JSXElementConstructor[Any] */] extends StObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package typingsJapgolly.react.mod

import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
T extends react.react.ComponentClass<infer P, react.react.ComponentState> ? react.react.PropsWithoutRef<P> & react.react.RefAttributes</ * import warning: transforms.QualifyReferences#resolveTypeRef many Couldn't qualify InstanceType<T> * / any> : react.react.PropsWithRef<react.react.ComponentProps<T>>
}}}
*/
@js.native
trait ComponentPropsWithRef[T /* <: japgolly.scalajs.react.facade.React.ElementType */] extends StObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package typingsJapgolly.react.mod

import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
C extends react.react.Context<infer T> ? T : never
}}}
*/
@js.native
trait ContextType[C /* <: Context[Any] */] extends StObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package typingsJapgolly.react.mod

import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

// Any prop that has a default prop becomes optional, but its type is unchanged
// Undeclared default props are augmented into the resulting allowable attributes
// If declared props have indexed properties, ignore default props entirely as keyof gets widened
// Wrap in an outer-level conditional type to allow distribution over props that are unions
/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
P extends any ? string extends keyof P ? P : std.Pick<P, std.Exclude<keyof P, keyof D>> & std.Partial<std.Pick<P, / * import warning: transforms.QualifyReferences#resolveTypeRef many Couldn't qualify Extract<keyof P, keyof D> * / any>> & std.Partial<std.Pick<D, std.Exclude<keyof D, keyof P>>> : never
}}}
*/
@js.native
trait Defaultize[P, D] extends StObject
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ trait FunctionComponentElement[P]
extends StObject
with ReactElement {

var ref: js.UndefOr[Any] = js.undefined
var ref: js.UndefOr[
/* import warning: importer.ImportType#apply Failed type conversion: 'ref' extends keyof P ? P extends react.anon.Ref ? R : never : never */ js.Any
] = js.undefined
}
object FunctionComponentElement {

Expand All @@ -20,7 +22,9 @@ object FunctionComponentElement {

extension [Self <: FunctionComponentElement[?], P](x: Self & FunctionComponentElement[P]) {

inline def setRef(value: Any): Self = StObject.set(x, "ref", value.asInstanceOf[js.Any])
inline def setRef(
value: /* import warning: importer.ImportType#apply Failed type conversion: 'ref' extends keyof P ? P extends react.anon.Ref ? R : never : never */ js.Any
): Self = StObject.set(x, "ref", value.asInstanceOf[js.Any])

inline def setRefUndefined: Self = StObject.set(x, "ref", js.undefined)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, J

// naked 'any' type in a conditional type will short circuit and union both the then/else branches
// so boolean is only resolved for T = any
/* Rewritten from type alias, can be one of:
- typingsJapgolly.react.reactBooleans.`false`
- typingsJapgolly.react.reactBooleans.`true`
*/
/** NOTE: Conditional type definitions are impossible to translate to Scala.
* See https://www.typescriptlang.org/docs/handbook/2/conditional-types.html for an intro.
* You'll have to cast your way around this structure, unfortunately.
* TS definition: {{{
boolean extends T extends never ? true : false ? true : false
}}}
*/
@js.native
trait IsExactlyAny[T] extends StObject
Loading

0 comments on commit 28eaaad

Please sign in to comment.