Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Scala.js definitions for all libraries the encoding uses types from #310

Merged
merged 3 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ lazy val `sbt-converter` = project
libraryDependencies ++= Seq(Deps.awssdkS3, Deps.java8Compat),
)

lazy val `import-scalajs-definitions` = project
.configure(baseSettings)
.dependsOn(importer)
.settings(
libraryDependencies ++= List("org.scala-lang" % "scalap" % scalaVersion.value),
publish / skip := true,
)

lazy val root = project
.in(file("."))
.settings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ object Name {
val global: Name = Name("global")
val dummy: Name = Name("dummy")
val Any: Name = Name("Any")
val AnyRef: Name = Name("AnyRef")
val AnyVal: Name = Name("AnyVal")
val Double: Name = Name("Double")
val Short: Name = Name("Short")
val Byte: Name = Name("Byte")
val Float: Name = Name("Float")
val Int: Name = Name("Int")
val Long: Name = Name("Long")
val Boolean: Name = Name("Boolean")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,65 +27,60 @@ final case class QualifiedName(parts: IArray[Name]) {
object QualifiedName {
def apply(str: String): QualifiedName = QualifiedName(IArray.fromArray(str.split("\\.")).map(Name(_)))

val java_lang: QualifiedName = QualifiedName(IArray(Name.java, Name.lang))
val scala: QualifiedName = QualifiedName(IArray(Name.scala))
val scala_scalajs: QualifiedName = scala + Name.scalajs
val scalajs_runtime: QualifiedName = scala_scalajs + Name("runtime")
val linkingInfo: QualifiedName = scalajs_runtime + Name("linkingInfo")
val linkingInfo_productionMode: QualifiedName = linkingInfo + Name("productionMode")
val scala_js: QualifiedName = scala_scalajs + Name.js
val scala_js_ann: QualifiedName = scala_js + Name("annotation")
val Runtime: QualifiedName = QualifiedName(IArray(Name("org"), Name("scalablytyped"), Name("runtime")))
val ScalaJsDom: QualifiedName = QualifiedName(IArray(Name("org"), Name("scalajs"), Name("dom")))
val String: QualifiedName = java_lang + Name.String
val JArray: QualifiedName = java_lang + Name.Array
val ScalaAny: QualifiedName = scala + Name.Any
val AnyVal: QualifiedName = scala + Name.AnyVal
val Double: QualifiedName = scala + Name.Double
val Int: QualifiedName = scala + Name.Int
val Long: QualifiedName = scala + Name.Long
val Boolean: QualifiedName = scala + Name.Boolean
val Unit: QualifiedName = scala + Name.Unit
val Null: QualifiedName = scala + Name.Null
val Nothing: QualifiedName = scala + Name.Nothing
val Any: QualifiedName = scala_js + Name.Any
val Object: QualifiedName = scala_js + Name.Object
val Array: QualifiedName = scala_js + Name.Array
val BigInt: QualifiedName = scala_js + Name("BigInt")
val Thenable: QualifiedName = scala_js + Name("Thenable")
val Promise: QualifiedName = scala_js + Name("Promise")
val `|` : QualifiedName = scala_js + Name("|")
val Function: QualifiedName = scala_js + Name.Function
val Symbol: QualifiedName = scala_js + Name.Symbol
val UndefOr: QualifiedName = scala_js + Name.UndefOr
val constructorOf: QualifiedName = scala_js + Name("constructorOf")
val special: QualifiedName = scala_js + Name("special")
val isUndefined: QualifiedName = scala_js + Name("isUndefined")
val specialDelete: QualifiedName = special + Name("delete")
val Dictionary: QualifiedName = scala_js + Name("Dictionary")
val DictionaryEmpty: QualifiedName = Dictionary + Name("empty")
val Dynamic: QualifiedName = scala_js + Name.Dynamic
val DynamicLiteral: QualifiedName = Dynamic + Name("literal")
val DynamicGlobal: QualifiedName = Dynamic + Name("global")
val DynamicGlobalObject: QualifiedName = DynamicGlobal + Name("Object")
val DynamicGlobalObjectAssign: QualifiedName = DynamicGlobalObject + Name("assign")
val NumberDictionary: QualifiedName = Runtime + Name("NumberDictionary")
val StringDictionary: QualifiedName = Runtime + Name("StringDictionary")
val TopLevel: QualifiedName = Runtime + Name("TopLevel")
val Shortcut: QualifiedName = Runtime + Name("Shortcut")
val StObject: QualifiedName = Runtime + Name("StObject")
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 BOOLEAN_LITERAL: QualifiedName = QualifiedName(IArray(Name.BOOLEAN_LITERAL))
val THIS: QualifiedName = QualifiedName(IArray(Name.THIS))
val WILDCARD: QualifiedName = QualifiedName(IArray(Name.WILDCARD))
val REPEATED: QualifiedName = QualifiedName(IArray(Name.REPEATED))
val SINGLETON: QualifiedName = QualifiedName(IArray(Name.SINGLETON))
val UNDEFINED: QualifiedName = QualifiedName(IArray(Name.UNDEFINED))

def AnyFromFunction(n: Int): QualifiedName = Any + Name(s"fromFunction$n")
val java_lang: QualifiedName = QualifiedName(IArray(Name.java, Name.lang))
val String: QualifiedName = java_lang + Name.String
val Array: QualifiedName = java_lang + Name.Array

val scala: QualifiedName = QualifiedName(IArray(Name.scala))
val Any: QualifiedName = scala + Name.Any
val AnyRef: QualifiedName = scala + Name.AnyRef
val AnyVal: QualifiedName = scala + Name.AnyVal
val Byte: QualifiedName = scala + Name.Byte
val Short: QualifiedName = scala + Name.Short
val Float: QualifiedName = scala + Name.Float
val Double: QualifiedName = scala + Name.Double
val Int: QualifiedName = scala + Name.Int
val Long: QualifiedName = scala + Name.Long
val Boolean: QualifiedName = scala + Name.Boolean
val Unit: QualifiedName = scala + Name.Unit
val Null: QualifiedName = scala + Name.Null
val Nothing: QualifiedName = scala + Name.Nothing

val scala_scalajs: QualifiedName = scala + Name.scalajs
val scala_js: QualifiedName = scala_scalajs + Name.js
val JsAny: QualifiedName = scala_js + Name.Any
val JsObject: QualifiedName = scala_js + Name.Object
val JsArray: QualifiedName = scala_js + Name.Array
val JsBigInt: QualifiedName = scala_js + Name("BigInt")
val JsThenable: QualifiedName = scala_js + Name("Thenable")
val JsPromise: QualifiedName = scala_js + Name("Promise")
val JsFunction: QualifiedName = scala_js + Name.Function
val JsSymbol: QualifiedName = scala_js + Name.Symbol
val JsUndefOr: QualifiedName = scala_js + Name.UndefOr
val `|` : QualifiedName = scala_js + Name("|")
val isUndefined: QualifiedName = scala_js + Name("isUndefined")
val JsDictionary: QualifiedName = scala_js + Name("Dictionary")
val JsDynamic: QualifiedName = scala_js + Name.Dynamic

val Runtime: QualifiedName = QualifiedName(IArray(Name("org"), Name("scalablytyped"), Name("runtime")))
val NumberDictionary: QualifiedName = Runtime + Name("NumberDictionary")
val StringDictionary: QualifiedName = Runtime + Name("StringDictionary")
val TopLevel: QualifiedName = Runtime + Name("TopLevel")
val Shortcut: QualifiedName = Runtime + Name("Shortcut")
val StObject: QualifiedName = Runtime + Name("StObject")

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 BOOLEAN_LITERAL: QualifiedName = QualifiedName(IArray(Name.BOOLEAN_LITERAL))
val THIS: QualifiedName = QualifiedName(IArray(Name.THIS))
val WILDCARD: QualifiedName = QualifiedName(IArray(Name.WILDCARD))
val REPEATED: QualifiedName = QualifiedName(IArray(Name.REPEATED))
val SINGLETON: QualifiedName = QualifiedName(IArray(Name.SINGLETON))
val UNDEFINED: QualifiedName = QualifiedName(IArray(Name.UNDEFINED))

def AnyFromFunction(n: Int): QualifiedName = JsAny + Name(s"fromFunction$n")

class StdNames(outputPkg: Name) {
val lib: QualifiedName = QualifiedName(IArray(outputPkg, Name.std))
Expand All @@ -112,7 +107,7 @@ object QualifiedName {

def Tuple(arity: Int): QualifiedName =
arity match {
case 0 | 1 => Array
case 0 | 1 => JsArray
case n => scala_js + Name("Tuple" + n.toString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,36 @@ object TypeRef {
def apply(qn: QualifiedName): TypeRef =
TypeRef(qn, Empty, NoComments)

def stripTargs(tr: TypeRef): TypeRef = tr.copy(targs = tr.targs.map(_ => TypeRef.Any))

val Wildcard = TypeRef(QualifiedName.WILDCARD, Empty, NoComments)
val ScalaAny = TypeRef(QualifiedName.ScalaAny, Empty, NoComments)
val Any = TypeRef(QualifiedName.Any, Empty, NoComments)
val AnyVal = TypeRef(QualifiedName.AnyVal, Empty, NoComments)
val BigInt = TypeRef(QualifiedName.BigInt, Empty, NoComments)
val Boolean = TypeRef(QualifiedName.Boolean, Empty, NoComments)
val Double = TypeRef(QualifiedName.Double, Empty, NoComments)
val Dynamic = TypeRef(QualifiedName.Dynamic, Empty, NoComments)
val Int = TypeRef(QualifiedName.Int, Empty, NoComments)
val Long = TypeRef(QualifiedName.Long, Empty, NoComments)
val Nothing = TypeRef(QualifiedName.Nothing, Empty, NoComments)
val Null = TypeRef(QualifiedName.Null, Empty, NoComments)
val Object = TypeRef(QualifiedName.Object, Empty, NoComments)
val String = TypeRef(QualifiedName.String, Empty, NoComments)
val Symbol = TypeRef(QualifiedName.Symbol, Empty, NoComments)
val Unit = TypeRef(QualifiedName.Unit, Empty, NoComments)
val FunctionBase = TypeRef(QualifiedName.Function, Empty, NoComments)

/* we represent `js.UndefOr` as this fake type ref inside a union type. Note that t can also appear on it's own */
def stripTargs(tr: TypeRef): TypeRef = tr.copy(targs = tr.targs.map(_ => TypeRef.JsAny))

val Wildcard = TypeRef(QualifiedName.WILDCARD, Empty, NoComments)

val AnyRef = TypeRef(QualifiedName.AnyRef, Empty, NoComments)
val Any = TypeRef(QualifiedName.Any, Empty, NoComments)
val AnyVal = TypeRef(QualifiedName.AnyVal, Empty, NoComments)
val Boolean = TypeRef(QualifiedName.Boolean, Empty, NoComments)
val Byte = TypeRef(QualifiedName.Byte, Empty, NoComments)
val Double = TypeRef(QualifiedName.Double, Empty, NoComments)
val Float = TypeRef(QualifiedName.Float, Empty, NoComments)
val Int = TypeRef(QualifiedName.Int, Empty, NoComments)
val Long = TypeRef(QualifiedName.Long, Empty, NoComments)
val Nothing = TypeRef(QualifiedName.Nothing, Empty, NoComments)
val Null = TypeRef(QualifiedName.Null, Empty, NoComments)
val Short = TypeRef(QualifiedName.Short, Empty, NoComments)
val String = TypeRef(QualifiedName.String, Empty, NoComments)
val Unit = TypeRef(QualifiedName.Unit, Empty, NoComments)

val JsAny = TypeRef(QualifiedName.JsAny, Empty, NoComments)
val JsBigInt = TypeRef(QualifiedName.JsBigInt, Empty, NoComments)
val JsDynamic = TypeRef(QualifiedName.JsDynamic, Empty, NoComments)
val JsFunctionBase = TypeRef(QualifiedName.JsFunction, Empty, NoComments)
val JsObject = TypeRef(QualifiedName.JsObject, Empty, NoComments)
val JsSymbol = TypeRef(QualifiedName.JsSymbol, Empty, NoComments)

/* we represent `js.UndefOr` as this fake type ref inside a union type. Note that it can also appear on its own */
val undefined = TypeRef(QualifiedName.UNDEFINED, Empty, NoComments)

val Primitive = Set(Double, Int, Long, Boolean, Unit, Nothing)
val Primitive = Set(Boolean, Byte, Double, Float, Int, Long, Nothing, Null, Short, Unit)

def StringDictionary(typeParam: TypeRef, comments: Comments): TypeRef =
TypeRef(QualifiedName.StringDictionary, IArray(typeParam), comments)
Expand All @@ -286,7 +292,7 @@ object TypeRef {
}
}

object Function {
object JsFunction {
def apply(thisType: Option[TypeRef], typeParams: IArray[TypeRef], resType: TypeRef, comments: Comments): TypeRef = {
val rewriteRepeated: IArray[TypeRef] =
typeParams.lastOption match {
Expand Down Expand Up @@ -352,11 +358,11 @@ object TypeRef {
}
}

def Tuple(typeParams: IArray[TypeRef]): TypeRef =
def JsTuple(typeParams: IArray[TypeRef]): TypeRef =
typeParams match {
case IArray.Empty => TypeRef(QualifiedName.Array, IArray(TypeRef.Any), NoComments)
case IArray.exactlyOne(one) => TypeRef(QualifiedName.Array, IArray(one), NoComments)
case catch22 if catch22.length > 22 => TypeRef(QualifiedName.Array, IArray(TypeRef.Any), NoComments)
case IArray.Empty => TypeRef(QualifiedName.JsArray, IArray(TypeRef.JsAny), NoComments)
case IArray.exactlyOne(one) => TypeRef(QualifiedName.JsArray, IArray(one), NoComments)
case catch22 if catch22.length > 22 => TypeRef(QualifiedName.JsArray, IArray(TypeRef.JsAny), NoComments)
case _ => TypeRef(QualifiedName.Tuple(typeParams.length), typeParams, NoComments)
}

Expand Down Expand Up @@ -477,6 +483,12 @@ object TypeRef {
object StringLiteral extends LiteralCompanion(QualifiedName.STRING_LITERAL)
object NumberLiteral extends LiteralCompanion(QualifiedName.NUMBER_LITERAL)
object BooleanLiteral extends LiteralCompanion(QualifiedName.BOOLEAN_LITERAL)
object Literal {
def unapply(typeRef: TypeRef): Boolean =
typeRef.typeName === QualifiedName.STRING_LITERAL ||
typeRef.typeName === QualifiedName.NUMBER_LITERAL ||
typeRef.typeName === QualifiedName.BOOLEAN_LITERAL
}

object Repeated {
def apply(underlying: TypeRef, comments: Comments): TypeRef =
Expand Down Expand Up @@ -527,7 +539,10 @@ object ImplTree {
}

case object NotImplemented extends ImplTree
sealed trait ExprTree extends ImplTree
sealed trait ExprTree extends ImplTree {
def select(strings: String*): ExprTree =
strings.foldLeft(this)((acc, str) => ExprTree.Select(acc, Name(str)))
}

object ExprTree {
implicit val encodes: Encoder[ExprTree] = io.circe013.generic.semiauto.deriveEncoder
Expand Down
Loading