Skip to content

Commit

Permalink
literals: split number into int and double on scala side. handle infi…
Browse files Browse the repository at this point in the history
…nity by widening to double
  • Loading branch information
oyvindberg committed Sep 29, 2022
1 parent b47f557 commit 7cb11a7
Show file tree
Hide file tree
Showing 31 changed files with 277 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ object Name {
val INTERSECTION: Name = Name("<intersection>")
val SINGLETON: Name = Name("<typeof>")
val STRING_LITERAL: Name = Name("<string_literal>")
val NUMBER_LITERAL: Name = Name("<number_literal>")
val DOUBLE_LITERAL: Name = Name("<double_literal>")
val INT_LITERAL: Name = Name("<int_literal>")
val BOOLEAN_LITERAL: Name = Name("<boolean_literal>")
val THIS: Name = Name("<this>")
val SUPER: Name = Name("<super>")
Expand All @@ -81,7 +82,8 @@ object Name {
INTERSECTION,
SINGLETON,
STRING_LITERAL,
NUMBER_LITERAL,
DOUBLE_LITERAL,
INT_LITERAL,
BOOLEAN_LITERAL,
THIS,
SUPER,
Expand All @@ -96,7 +98,8 @@ object Name {
case INTERSECTION => Suffix("Intersection")
case SINGLETON => Suffix("Singleton")
case BOOLEAN_LITERAL => Suffix("Boolean")
case NUMBER_LITERAL => Suffix("Number")
case DOUBLE_LITERAL => Suffix("Double")
case INT_LITERAL => Suffix("Int")
case STRING_LITERAL => Suffix("String")
case THIS => Suffix("This")
case WILDCARD => Suffix("Wildcard")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ object QualifiedName {
val UNION: QualifiedName = QualifiedName(IArray(Name.UNION))
val INTERSECTION: QualifiedName = QualifiedName(IArray(Name.INTERSECTION))
val STRING_LITERAL: QualifiedName = QualifiedName(IArray(Name.STRING_LITERAL))
val NUMBER_LITERAL: QualifiedName = QualifiedName(IArray(Name.NUMBER_LITERAL))
val DOUBLE_LITERAL: QualifiedName = QualifiedName(IArray(Name.DOUBLE_LITERAL))
val INT_LITERAL: QualifiedName = QualifiedName(IArray(Name.INT_LITERAL))
val BOOLEAN_LITERAL: QualifiedName = QualifiedName(IArray(Name.BOOLEAN_LITERAL))
val THIS: QualifiedName = QualifiedName(IArray(Name.THIS))
val WILDCARD: QualifiedName = QualifiedName(IArray(Name.WILDCARD))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,17 @@ object TypeRef {
}
}
object StringLiteral extends LiteralCompanion(QualifiedName.STRING_LITERAL)
object NumberLiteral extends LiteralCompanion(QualifiedName.NUMBER_LITERAL)
object DoubleLiteral extends LiteralCompanion(QualifiedName.DOUBLE_LITERAL)
object IntLiteral extends LiteralCompanion(QualifiedName.INT_LITERAL)
object BooleanLiteral extends LiteralCompanion(QualifiedName.BOOLEAN_LITERAL)
object Literal {
def unapply(typeRef: TypeRef): Boolean =
apply(typeRef.typeName)

def apply(typeName: QualifiedName): Boolean =
typeName === QualifiedName.STRING_LITERAL ||
typeName === QualifiedName.NUMBER_LITERAL ||
typeName === QualifiedName.DOUBLE_LITERAL ||
typeName === QualifiedName.INT_LITERAL ||
typeName === QualifiedName.BOOLEAN_LITERAL
}

Expand Down Expand Up @@ -587,7 +589,8 @@ object ExprTree {
}

case class BooleanLit(value: Boolean) extends Lit
case class NumberLit(value: String) extends Lit
case class IntLit(value: String) extends Lit
case class DoubleLit(value: String) extends Lit
case class StringLit(value: String) extends Lit
case object undefined extends Lit
case object Null extends Lit
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typings
*Mod/
global/
fooStrings.scala
fooNumbers.scala
fooInts.scala
fooBooleans.scala
fooRequire.scala
... top-level types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,13 @@ object ImportScalaDefinitions extends App {
val tps = toTypeParameterTrees(tparams)
go(inner, tps)
case x: ConstantType =>
(TypeRef.NumberLiteral(x.toString), Empty, Empty)
x.constant match {
case x: Double => (TypeRef.DoubleLiteral(x.toString), Empty, Empty)
case x: Int => (TypeRef.IntLiteral(x.toString), Empty, Empty)
case x: String => (TypeRef.StringLiteral(x), Empty, Empty)
case x: Boolean => (TypeRef.BooleanLiteral(x.toString), Empty, Empty)
case null => (TypeRef.Null, Empty, Empty)
}
case AnnotatedType(typeRef, _) =>
go(typeRef, tparams)
case NoType =>
Expand Down Expand Up @@ -498,8 +504,8 @@ object ImportScalaDefinitions extends App {
case ConstantType(constant) =>
constant match {
case x: String => Right(TypeRef.StringLiteral(x))
case x: Double => Right(TypeRef.NumberLiteral(x.toString))
case x: Int => Right(TypeRef.NumberLiteral(x.toString))
case x: Double => Right(TypeRef.DoubleLiteral(x.toString))
case x: Int => Right(TypeRef.IntLiteral(x.toString))
case x: Boolean => Right(TypeRef.BooleanLiteral(x.toString))
case null => Right(TypeRef.Null)
case _ => Left(tpe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.scalablytyped.converter.internal
package importer

import org.scalablytyped.converter.internal.scalajs.{ExprTree, QualifiedName}
import org.scalablytyped.converter.internal.scalajs.transforms.FakeLiterals
import org.scalablytyped.converter.internal.ts._

class ImportExpr(importType: ImportType, importName: AdaptiveNamingImport) {
Expand All @@ -13,11 +12,7 @@ class ImportExpr(importType: ImportType, importName: AdaptiveNamingImport) {

case TsExpr.Literal(value) =>
value match {
case TsLiteral.Num(value) =>
FakeLiterals.isTooBigForInt(value) match {
case Some(long) => ExprTree.NumberLit(long.toString + ".0")
case None => ExprTree.NumberLit(value)
}
case TsLiteral.Num(value) => ImportType.numberToExpr(value)
case TsLiteral.Str(value) => ExprTree.StringLit(value)
case TsLiteral.Bool(value) => ExprTree.BooleanLit(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.scalablytyped.converter.internal.ts.TsTreeScope.LoopDetector
import org.scalablytyped.converter.internal.ts._
import org.scalablytyped.converter.internal.ts.transforms.ExtractInterfaces

import scala.util.{Success, Try}

class ImportType(stdNames: QualifiedName.StdNames) {

def orAny(scope: TsTreeScope, importName: AdaptiveNamingImport)(ott: Option[TsType]): TypeRef =
Expand Down Expand Up @@ -272,7 +274,7 @@ class ImportType(stdNames: QualifiedName.StdNames) {

case TsTypeLiteral(lit) =>
lit match {
case TsLiteral.Num(value) => TypeRef.NumberLiteral(value)
case TsLiteral.Num(value) => ImportType.numberToTypeRef(value)
case TsLiteral.Str(value) => TypeRef.StringLiteral(value)
case TsLiteral.Bool(value) => TypeRef.BooleanLiteral(value.toString)
}
Expand Down Expand Up @@ -375,4 +377,38 @@ object ImportType {
case _ => Not
}
}

def numberToExpr(value: String): ExprTree = {
def validInt = Try(java.lang.Long.decode(value)) match {
case Success(int) if int < Int.MaxValue && int > Int.MinValue => Some(ExprTree.IntLit(value))
case _ => None
}

def validDouble = Try(java.lang.Double.parseDouble(value)) match {
case Success(double) if !double.isInfinite && !double.isNaN => Some(ExprTree.DoubleLit(value))
case _ => None
}

def other =
ExprTree.Ref(TypeRef(QualifiedName.Double).withComments(Comments(s"/* $value */ ")))

validInt.orElse(validDouble).getOrElse(other)
}

def numberToTypeRef(value: String): TypeRef = {
def validInt = Try(java.lang.Long.decode(value)) match {
case Success(int) if int < Int.MaxValue && int > Int.MinValue => Some(TypeRef.IntLiteral(value))
case _ => None
}

def validDouble = Try(java.lang.Double.parseDouble(value)) match {
case Success(double) if !double.isInfinite && !double.isNaN => Some(TypeRef.DoubleLiteral(value))
case _ => None
}

def other =
TypeRef(QualifiedName.Double).withComments(Comments(s"/* $value */ "))

validInt.orElse(validDouble).getOrElse(other)
}
}
2 changes: 1 addition & 1 deletion scalajs/src/main/resources/scalajs-definitions.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Erasure(scalaVersion: Versions.Scala) {
case QualifiedName.REPEATED => QualifiedName.Array
// the way we fake literal means these are true enough
case QualifiedName.STRING_LITERAL => tpe.targs.head.typeName
case QualifiedName.NUMBER_LITERAL => tpe.targs.head.typeName
case QualifiedName.DOUBLE_LITERAL => tpe.targs.head.typeName
case QualifiedName.INT_LITERAL => tpe.targs.head.typeName
case QualifiedName.BOOLEAN_LITERAL => tpe.targs.head.typeName

case QualifiedName.INTERSECTION if scalaVersion.is3 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ object Printer {
types.map(formatTypeRef(indent)).map(paramsIfNeeded).mkString(" | ")

case TypeRef.StringLiteral(underlying) => stringUtils.quote(underlying)
case TypeRef.NumberLiteral(underlying) => underlying
case TypeRef.DoubleLiteral(underlying) => underlying
case TypeRef.IntLiteral(underlying) => underlying
case TypeRef.BooleanLiteral(underlying) => underlying

case TypeRef.Repeated(underlying: TypeRef, _) =>
Expand Down Expand Up @@ -633,8 +634,10 @@ object Printer {
formatQN(value)
case ExprTree.StringLit(value) =>
stringUtils.quote(value)
case ExprTree.NumberLit(value) =>
case ExprTree.IntLit(value) =>
value
case ExprTree.DoubleLit(value) =>
if (value.contains("e")) value else value + "d"
case ExprTree.BooleanLit(value) =>
value.toString
case ExprTree.Unary(op, expr) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ class TreeTransformation { self =>
case ExprTree.Arg.Variable(expr) =>
ExprTree.Arg.Variable(visitExprTree(childrenScope)(expr))
case x: ExprTree.Ref => x
case x: ExprTree.NumberLit => x
case x: ExprTree.DoubleLit => x
case x: ExprTree.IntLit => x
case x: ExprTree.StringLit => x
case x: ExprTree.BooleanLit => x

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ class JapgollyGenComponents(
val members: IArray[MethodTree] =
props.flatMap {
case Prop.CompressedProp(name, tpe, asExpr, isRequired) =>
val args1 = Call(Ref(genStBuilder.args.name), IArray(IArray(NumberLit("1"))))
val args1 = Call(Ref(genStBuilder.args.name), IArray(IArray(IntLit("1"))))

val (default, update) =
if (isRequired) (NotImplemented, asExpr(args1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers

val impl = Block(
Call(
Select(Cast(Call(Ref(args.name), IArray(IArray(NumberLit("1")))), TypeRef.JsDynamic), Name("updateDynamic")),
Select(Cast(Call(Ref(args.name), IArray(IArray(IntLit("1")))), TypeRef.JsDynamic), Name("updateDynamic")),
IArray(IArray(Ref(keyParam.name)), IArray(Cast(Ref(valueParam.name), TypeRef.JsAny))),
),
Ref(QualifiedName.THIS),
Expand Down Expand Up @@ -89,8 +89,8 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers
Select(Ref(args.name), Name("update")),
IArray(
IArray(
NumberLit("0"),
Call(Ref(fParam.name), IArray(IArray(Call(Ref(args.name), IArray(IArray(NumberLit("0"))))))),
IntLit("0"),
Call(Ref(fParam.name), IArray(IArray(Call(Ref(args.name), IArray(IArray(IntLit("0"))))))),
),
),
),
Expand Down Expand Up @@ -121,7 +121,7 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers
Ref(QualifiedName.JsObject + Name("assign")),
IArray(
IArray(
Cast(Call(Ref(args.name), IArray(IArray(NumberLit("1")))), TypeRef.JsObject),
Cast(Call(Ref(args.name), IArray(IArray(IntLit("1")))), TypeRef.JsObject),
Cast(Ref(param.name), TypeRef.JsObject),
),
),
Expand Down Expand Up @@ -473,7 +473,7 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers
val compParam = ParamTree(Name("comp"), false, false, builderRef, NotImplemented, NoComments)
val name = Name("make")
val compArgs = Select(Ref(compParam.name), Name("args"))
val args0 = Call(compArgs, IArray(IArray(NumberLit("0"))))
val args0 = Call(compArgs, IArray(IArray(IntLit("0"))))
val reactRawName = Ref(ReactRaw.name)
val createElementApply = reactRawName.select("createElement", "applyDynamic")
val ret = Name("ret")
Expand Down Expand Up @@ -508,7 +508,7 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers
TypeRef(JapgollyNames.rawReact.Element),
),
),
inDevMode(Call(Select(compArgs, Name("update")), IArray(IArray(NumberLit("0"), Null)))),
inDevMode(Call(Select(compArgs, Name("update")), IArray(IArray(IntLit("0"), Null)))),
Call(Ref(JapgollyNames.vdom.ReactElement), IArray(IArray(Ref(ret)))),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ class SlinkyGenComponents(
val members: IArray[MethodTree] =
props.filterNot(_.isRequired).flatMap {
case Prop.CompressedProp(name, tpe, asExpr, isRequired) =>
val args1 = Call(Ref(genStBuilder.args.name), IArray(IArray(NumberLit("1"))))
val args1 = Call(Ref(genStBuilder.args.name), IArray(IArray(IntLit("1"))))

val (default, update) =
if (isRequired) (NotImplemented, asExpr(args1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio

val impl = Block(
Call(
Select(Cast(Call(Ref(args.name), IArray(IArray(NumberLit("1")))), TypeRef.JsDynamic), Name("updateDynamic")),
Select(Cast(Call(Ref(args.name), IArray(IArray(IntLit("1")))), TypeRef.JsDynamic), Name("updateDynamic")),
IArray(IArray(Ref(keyParam.name)), IArray(Cast(Ref(valueParam.name), TypeRef.JsAny))),
),
Ref(QualifiedName.THIS),
Expand Down Expand Up @@ -90,8 +90,8 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio
Select(Ref(args.name), Name("update")),
IArray(
IArray(
NumberLit("0"),
Call(Ref(fParam.name), IArray(IArray(Call(Ref(args.name), IArray(IArray(NumberLit("0"))))))),
IntLit("0"),
Call(Ref(fParam.name), IArray(IArray(Call(Ref(args.name), IArray(IArray(IntLit("0"))))))),
),
),
),
Expand Down Expand Up @@ -144,7 +144,7 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio
Ref(QualifiedName.JsObject + Name("assign")),
IArray(
IArray(
Cast(Call(Ref(args.name), IArray(IArray(NumberLit("1")))), TypeRef.JsObject),
Cast(Call(Ref(args.name), IArray(IArray(IntLit("1")))), TypeRef.JsObject),
Cast(Ref(param.name), TypeRef.JsObject),
),
),
Expand Down Expand Up @@ -413,7 +413,7 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio
val compParam = ParamTree(Name("comp"), false, false, builderRef, NotImplemented, NoComments)
val name = Name("make")
val compArgs = Select(Ref(compParam.name), Name("args"))
val args0 = Call(compArgs, IArray(IArray(NumberLit("0"))))
val args0 = Call(compArgs, IArray(IArray(IntLit("0"))))
val reactRawName = Ref(ReactRaw.name)
val createElementApply = reactRawName.select("createElement", "applyDynamic")
val ret = Name("ret")
Expand Down Expand Up @@ -448,7 +448,7 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio
TypeRef(SlinkyGenComponents.names.ReactElement),
),
),
inDevMode(Call(Select(compArgs, Name("update")), IArray(IArray(NumberLit("0"), Null)))),
inDevMode(Call(Select(compArgs, Name("update")), IArray(IArray(IntLit("0"), Null)))),
Ref(ret),
)

Expand Down
Loading

0 comments on commit 7cb11a7

Please sign in to comment.