Skip to content

Commit

Permalink
Builders: initialize null correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Mar 5, 2021
1 parent 6de2dc9 commit 710eff0
Show file tree
Hide file tree
Showing 58 changed files with 190 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ final class GenCompanions(findProps: FindProps, enableLongApplyMethod: Boolean)
Empty

case Res.One(_, props) if props.isEmpty => Empty
case Res.One(_, props) =>
case Res.One(_, props: IArray[Prop]) =>
val requiredProps =
if (enableLongApplyMethod) props
else props.filter(_.optionality === Optionality.No)
else props.filter(p => p.optionality === Optionality.No || p.optionality === Optionality.Null)

IArray.fromOptions(
Some(generateCreator(Name.APPLY, requiredProps, cls.codePath, cls.tparams))
Expand Down Expand Up @@ -150,6 +150,7 @@ final class GenCompanions(findProps: FindProps, enableLongApplyMethod: Boolean)
val impl: ExprTree = {
val objName = Name("__obj")
Block.flatten(
interpretedProps.collect { case (_, Left(valDef)) => valDef },
IArray(Val(objName, Call(Ref(QualifiedName.DynamicLiteral), IArray(initializers.map(_.value))))),
mutators.map(f => f.value(Ref(objName))),
IArray(Cast(Ref(QualifiedName(IArray(objName))), ret)),
Expand All @@ -161,7 +162,7 @@ final class GenCompanions(findProps: FindProps, enableLongApplyMethod: Boolean)
level = ProtectionLevel.Default,
name = name,
tparams = typeTparams,
params = IArray(interpretedProps.map(_._2)),
params = IArray(interpretedProps.collect { case (_, Right(param)) => param }),
impl = impl,
resultType = ret,
isOverride = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ class JapgollyGenComponents(
)

Block.flatten(
interpretedProps.collect { case (_, Left(valDef)) => valDef },
IArray(Val(objName, Call(Ref(QualifiedName.DynamicLiteral), IArray(initializers.map(_.value))))),
mutators.map(f => f.value(Ref(objName))),
IArray(newed),
)
}

val paramsOpt: Option[IArray[IArray[ParamTree]]] =
interpretedProps.map(_._2) match {
interpretedProps.collect { case (_, Right(param)) => param } match {
case Empty if tparams.nonEmpty => Some(IArray(IArray())) // allow nullary apply if there are type parameters
case Empty => None
case nonEmpty => Some(IArray(nonEmpty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,15 @@ class SlinkyGenComponents(
)

Block.flatten(
interpretedProps.collect { case (_, Left(valDef)) => valDef },
IArray(Val(objName, Call(Ref(QualifiedName.DynamicLiteral), IArray(initializers.map(_.value))))),
mutators.map(f => f.value(Ref(objName))),
IArray(newed),
)
}

val paramsOpt: Option[IArray[IArray[ParamTree]]] =
interpretedProps.map(_._2) match {
interpretedProps.collect { case (_, Right(param)) => param } match {
case Empty if tparams.nonEmpty => Some(IArray(IArray())) // allow nullary apply if there are type parameters
case Empty => None
case nonEmpty => Some(IArray(nonEmpty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ case class Mutator(value: ExprTree => ExprTree) extends ObjectUpdater
* Note that this is not the only possible way of doing it, [[GenBuilderOpsClass]] does it differently
*/
object defaultInterpretation {
def apply(prop: Prop): (ObjectUpdater, ParamTree) =
def apply(prop: Prop): (ObjectUpdater, Either[ExprTree.Val, ParamTree]) =
prop match {
case Prop.CompressedProp(name, tpe, asExpr, isRequired) =>
val default = if (isRequired) NotImplemented else Null
Expand All @@ -23,7 +23,7 @@ object defaultInterpretation {
val updater =
Mutator(ref => if (isRequired) asExpr(ref) else If(BinaryOp(Ref(name), "!=", Null), asExpr(ref), None))

(updater, param)
(updater, Right(param))

case prop @ Prop.Normal(Prop.Variant(tpe, asExpr, _, _), _, optionality, _, _) =>
def updateObj(value: ExprTree): Mutator =
Expand All @@ -34,74 +34,73 @@ object defaultInterpretation {
),
)

val (default, newType: TypeRef, namedArgOpt) =
optionality match {
case Optionality.No =>
val updater =
if (prop.canBeInitializer) Initializer(Arg.Named(prop.name, asExpr(Ref(prop.name))))
else updateObj(asExpr(Ref(prop.name)))

(NotImplemented, tpe, updater)

case Optionality.Null =>
val default = if (prop.main.extendsAnyVal) Cast(Null, tpe) else Null

val updater =
if (prop.canBeInitializer) Initializer(Arg.Named(prop.name, asExpr(Ref(prop.name))))
else updateObj(asExpr(Ref(prop.name)))

(default, tpe, updater)

case Optionality.Undef if prop.main.extendsAnyVal =>
val updater = Mutator { ref =>
If(
pred = Unary("!", Call(Ref(QualifiedName.isUndefined), IArray(IArray(Ref(prop.name))))),
ifTrue = updateObj(asExpr(Select(Ref(prop.name), Name("get")))).value(ref),
ifFalse = None,
)
}
(undefined, TypeRef.UndefOr(tpe), updater)

case Optionality.Undef =>
val updater = Mutator { ref =>
If(
pred = BinaryOp(Ref(prop.name), "!=", Null),
ifTrue = updateObj(asExpr(Ref(prop.name))).value(ref),
ifFalse = None,
)
}
(Null, tpe, updater)

case Optionality.NullOrUndef =>
val updater = Mutator { ref =>
val shortedDefaultImplementation =
asExpr(Null) match {
// default implementation, save boilerplate
case Cast(Null, TypeRef.Any) =>
updateObj(asExpr(Ref(prop.name)))
case _ =>
updateObj(
If(
pred = BinaryOp(Ref(prop.name), "!=", Null),
ifTrue = asExpr(Cast(Ref(prop.name), tpe)),
ifFalse = Some(Null),
),
)
}

If(
pred = Unary("!", Call(Ref(QualifiedName.isUndefined), IArray(IArray(Ref(prop.name))))),
ifTrue = shortedDefaultImplementation.value(ref),
ifFalse = None,
)
}

val newType = TypeRef.Union(IArray(TypeRef.undefined, TypeRef.Null, tpe), NoComments, sort = false)

(undefined, newType, updater)
}

val param = ParamTree(prop.name, isImplicit = false, isVal = false, newType, default, NoComments)
(namedArgOpt, param)
optionality match {
case Optionality.No =>
val updater =
if (prop.canBeInitializer) Initializer(Arg.Named(prop.name, asExpr(Ref(prop.name))))
else updateObj(asExpr(Ref(prop.name)))

(updater, Right(ParamTree(prop.name, isImplicit = false, isVal = false, tpe, NotImplemented, NoComments)))

case Optionality.Null =>
val updater =
if (prop.canBeInitializer) Initializer(Arg.Named(prop.name, asExpr(Ref(prop.name))))
else updateObj(asExpr(Ref(prop.name)))

(updater, Left(Val(prop.name, if (prop.main.extendsAnyVal) Cast(Null, tpe) else Null)))

case Optionality.Undef if prop.main.extendsAnyVal =>
val updater = Mutator { ref =>
If(
pred = Unary("!", Call(Ref(QualifiedName.isUndefined), IArray(IArray(Ref(prop.name))))),
ifTrue = updateObj(asExpr(Select(Ref(prop.name), Name("get")))).value(ref),
ifFalse = None,
)
}
val param =
ParamTree(prop.name, isImplicit = false, isVal = false, TypeRef.UndefOr(tpe), undefined, NoComments)
(updater, Right(param))

case Optionality.Undef =>
val updater = Mutator { ref =>
If(
pred = BinaryOp(Ref(prop.name), "!=", Null),
ifTrue = updateObj(asExpr(Ref(prop.name))).value(ref),
ifFalse = None,
)
}
val param = ParamTree(prop.name, isImplicit = false, isVal = false, tpe, Null, NoComments)
(updater, Right(param))

case Optionality.NullOrUndef =>
val updater = Mutator { ref =>
val shortedDefaultImplementation =
asExpr(Null) match {
// default implementation, save boilerplate
case Cast(Null, TypeRef.Any) =>
updateObj(asExpr(Ref(prop.name)))
case _ =>
updateObj(
If(
pred = BinaryOp(Ref(prop.name), "!=", Null),
ifTrue = asExpr(Cast(Ref(prop.name), tpe)),
ifFalse = Some(Null),
),
)
}

If(
pred = Unary("!", Call(Ref(QualifiedName.isUndefined), IArray(IArray(Ref(prop.name))))),
ifTrue = shortedDefaultImplementation.value(ref),
ifFalse = None,
)
}

val newType = TypeRef.Union(IArray(TypeRef.undefined, TypeRef.Null, tpe), NoComments, sort = false)

val param = ParamTree(prop.name, isImplicit = false, isVal = false, newType, undefined, NoComments)
(updater, Right(param))
}

}
}
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-28d847"
version := "0.0-unknown-f78bcc"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "1.7.5",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"org.scalablytyped" %%% "react" % "0.0-unknown-be04ee",
"org.scalablytyped" %%% "react" % "0.0-unknown-bcfda0",
"org.scalablytyped" %%% "std" % "0.0-unknown-310cd4")
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-be04ee"
version := "0.0-unknown-bcfda0"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ object mod {

@scala.inline
def apply(props: js.Any, `type`: String | ComponentClassP[js.Object] | SFC[_]): Element = {
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any])
val key = null
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], key = key.asInstanceOf[js.Any])
__obj.updateDynamic("type")(`type`.asInstanceOf[js.Any])
__obj.asInstanceOf[Element]
}
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-6d54e7"
version := "0.0-unknown-4475ac"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"me.shadaj" %%% "slinky-web" % "0.6.6",
"org.scalablytyped" %%% "react" % "0.0-unknown-09c72a",
"org.scalablytyped" %%% "react" % "0.0-unknown-eed1bf",
"org.scalablytyped" %%% "std" % "0.0-unknown-da7a30")
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-09c72a"
version := "0.0-unknown-eed1bf"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ object mod {

@scala.inline
def apply(props: js.Any, `type`: String | ReactComponentClass[_]): slinky.core.facade.ReactElement = {
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any])
val key = null
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], key = key.asInstanceOf[js.Any])
__obj.updateDynamic("type")(`type`.asInstanceOf[js.Any])
__obj.asInstanceOf[slinky.core.facade.ReactElement]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/react-icons/check/r/react-icon-base/build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
organization := "org.scalablytyped"
name := "react-icon-base"
version := "2.1-3e9841"
version := "2.1-65cec9"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"org.scalablytyped" %%% "react" % "0.0-unknown-f7b5df",
"org.scalablytyped" %%% "react" % "0.0-unknown-db50fc",
"org.scalablytyped" %%% "std" % "0.0-unknown-dc26fd")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
6 changes: 3 additions & 3 deletions tests/react-icons/check/r/react-icons/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "react-icons"
version := "2.2-834c51"
version := "2.2-8273f6"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"org.scalablytyped" %%% "react" % "0.0-unknown-f7b5df",
"org.scalablytyped" %%% "react-icon-base" % "2.1-3e9841",
"org.scalablytyped" %%% "react" % "0.0-unknown-db50fc",
"org.scalablytyped" %%% "react-icon-base" % "2.1-65cec9",
"org.scalablytyped" %%% "std" % "0.0-unknown-dc26fd")
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/react-icons/check/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-f7b5df"
version := "0.0-unknown-db50fc"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ object mod {

@scala.inline
def apply[P /* <: HTMLAttributes[T] | SVGAttributes[T] */, T /* <: Element */](props: js.Any, ref: Ref[T], `type`: String): DOMElement[P, T] = {
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], ref = ref.asInstanceOf[js.Any])
val key = null
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], ref = ref.asInstanceOf[js.Any], key = key.asInstanceOf[js.Any])
__obj.updateDynamic("type")(`type`.asInstanceOf[js.Any])
__obj.asInstanceOf[DOMElement[P, T]]
}
Expand Down Expand Up @@ -231,7 +232,8 @@ object mod {

@scala.inline
def apply(props: js.Any, `type`: String | ComponentClass[_]): ReactElement = {
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any])
val key = null
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], key = key.asInstanceOf[js.Any])
__obj.updateDynamic("type")(`type`.asInstanceOf[js.Any])
__obj.asInstanceOf[ReactElement]
}
Expand Down Expand Up @@ -297,7 +299,8 @@ object mod {

@scala.inline
def apply(props: js.Any, ref: Ref[SVGElement], `type`: animate | circle | clipPath): ReactSVGElement = {
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], ref = ref.asInstanceOf[js.Any])
val key = null
val __obj = js.Dynamic.literal(props = props.asInstanceOf[js.Any], ref = ref.asInstanceOf[js.Any], key = key.asInstanceOf[js.Any])
__obj.updateDynamic("type")(`type`.asInstanceOf[js.Any])
__obj.asInstanceOf[ReactSVGElement]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "componentstest"
version := "0.0-unknown-b3aac1"
version := "0.0-unknown-c62cb6"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "1.7.5",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"org.scalablytyped" %%% "react" % "16.9.2-5cfa3c",
"org.scalablytyped" %%% "react" % "16.9.2-0e03be",
"org.scalablytyped" %%% "std" % "0.0-unknown-43ed0a")
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,12 +1,12 @@
organization := "org.scalablytyped"
name := "react-bootstrap"
version := "0.32-a43133"
version := "0.32-9c84e7"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "core" % "1.7.5",
"com.olvind" %%% "scalablytyped-runtime" % "2.4.0",
"org.scalablytyped" %%% "react" % "16.9.2-5cfa3c",
"org.scalablytyped" %%% "react" % "16.9.2-0e03be",
"org.scalablytyped" %%% "std" % "0.0-unknown-43ed0a")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-g:notailcalls", "-language:implicitConversions", "-language:higherKinds", "-language:existentials")
Expand Down
Loading

0 comments on commit 710eff0

Please sign in to comment.