Skip to content

Commit

Permalink
infer shorter (index-based) names for destructured parameters which d…
Browse files Browse the repository at this point in the history
…o not have a name.

This is a follow-up from #485, to avoid too long class names on windows
  • Loading branch information
oyvindberg committed Oct 23, 2022
1 parent 2ae7282 commit 0b17669
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ object TsIdent {
val `this`: TsIdentSimple = TsIdent("this")
val Apply: TsIdentSimple = TsIdent("<apply>") // keep in sync with Name.necessaryRewrite
val Global: TsIdentSimple = TsIdent("<global>") // keep in sync with Name.necessaryRewrite
val Destructured: TsIdentSimple = TsIdent("<destructured>") // for parameters with no name.

val update: TsIdentSimple = TsIdent("update")
val prototype: TsIdentSimple = TsIdent("prototype")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3054,7 +3054,7 @@ export {};
shouldParseAs(content, TsParser.functionParam)(
TsFunParam(
NoComments,
TsIdentSimple("hasArr"),
TsIdent.Destructured,
Some(TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("Array"))), IArray(T))),
),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/recharts/check-3/r/recharts/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "recharts"
version := "0.0-unknown-46f738"
version := "0.0-unknown-f300ef"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object mod {
@js.native
val ^ : js.Any = js.native

inline def rectWithPoints(hasX1Y1: Coordinate, hasX2Y2: Coordinate): Height = (^.asInstanceOf[js.Dynamic].applyDynamic("rectWithPoints")(hasX1Y1.asInstanceOf[js.Any], hasX2Y2.asInstanceOf[js.Any])).asInstanceOf[Height]
inline def rectWithPoints(param0: Coordinate, param1: Coordinate): Height = (^.asInstanceOf[js.Dynamic].applyDynamic("rectWithPoints")(param0.asInstanceOf[js.Any], param1.asInstanceOf[js.Any])).asInstanceOf[Height]

trait Coordinate extends StObject {

Expand Down
4 changes: 2 additions & 2 deletions tests/vue/check-3/s/storybook__vue/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "storybook__vue"
version := "3.3-8970b4"
version := "3.3-d0a6e0"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "std" % "0.0-unknown-ddd099",
"org.scalablytyped" %%% "vue" % "2.5.13-597b3c",
"org.scalablytyped" %%% "vue" % "2.5.13-a9cbc6",
"org.scalablytyped" %%% "webpack-env" % "1.13-08f28b")
publishArtifact in packageDoc := false
scalacOptions ++= List("-encoding", "utf-8", "-feature", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-no-indent", "-source:future")
Expand Down
4 changes: 2 additions & 2 deletions tests/vue/check-3/v/vue-scrollto/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
organization := "org.scalablytyped"
name := "vue-scrollto"
version := "2.7-d31dd1"
version := "2.7-931a02"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
"com.olvind" %%% "scalablytyped-runtime" % "2.4.2",
"org.scalablytyped" %%% "std" % "0.0-unknown-ddd099",
"org.scalablytyped" %%% "vue" % "2.5.13-597b3c")
"org.scalablytyped" %%% "vue" % "2.5.13-a9cbc6")
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/vue/check-3/v/vue/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "vue"
version := "2.5.13-597b3c"
version := "2.5.13-a9cbc6"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object anon {
}

@js.native
trait FnCallObjectKey extends StObject {
trait FnCallCall extends StObject {

def apply(`object`: js.Object, key: String): Unit = js.native
def apply[T](array: js.Array[T], key: Double): Unit = js.native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import typings.std.Element
import typings.std.HTMLElement
import typings.std.Record
import typings.vue.anon.FnCall
import typings.vue.anon.FnCallObjectKey
import typings.vue.anon.FnCallCall
import typings.vue.anon.StaticRenderFns
import typings.vue.typesOptionsMod.AsyncComponent
import typings.vue.typesOptionsMod.Component
Expand Down Expand Up @@ -157,7 +157,7 @@ object typesVueMod {
@JSName("$delete")
def $delete[T](array: js.Array[T], key: Double): Unit = js.native
@JSName("$delete")
var $delete_Original: FnCallObjectKey = js.native
var $delete_Original: FnCallCall = js.native

@JSName("$destroy")
def $destroy(): Unit = js.native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,11 @@ class TsParser(path: Option[(os.Path, Int)]) extends StdTokenParsers with Parser
lazy val tsTypeParams: Parser[IArray[TsTypeParam]] =
"<" ~>! repsep_(typeParam, ",") <~! ",".? <~ comments.? <~! ">" | success(Empty)

val tsFunctionParams: Parser[IArray[TsFunParam]] = functionParam.** ^^ {

/** **/
case ps if ps.count(_.name.value === "has") > 1 =>
ps.zipWithIndex.map {
case (p @ TsFunParam(_, TsIdentSimple("has"), _), idx) => p.copy(name = TsIdent("has" + idx))
case (other, _) => other
}
case ok => ok
val tsFunctionParams: Parser[IArray[TsFunParam]] = functionParam.** ^^ { params =>
params.zipWithIndex.map {
case (p @ TsFunParam(_, TsIdent.Destructured, _), i) => p.copy(name = TsIdent(s"param$i"))
case (p, _) => p
}
}

lazy val functionSignature: Parser[TsFunSig] =
Expand All @@ -404,20 +400,16 @@ class TsParser(path: Option[(os.Path, Int)]) extends StdTokenParsers with Parser

/** Note: we don't care about the specifics of a destructured parameter. we just want a unique name and a type **/
lazy val destructuredObj: Parser[TsIdentSimple] =
"{" ~>! rep((tsIdentLiberal | ("..." ~> tsIdent)) ~ (":" ~> (tsIdent | destructured)).? <~ ",".?) <~ "}" ^^ {
tupled =>
val rendered = tupled.map {
case _ ~ Some(renamed) => renamed.value.capitalize;
case ident ~ None => ident.value.capitalize
}
TsIdent("has" + rendered.mkString(""))

}
"{" ~>! rep((tsIdentLiberal | ("..." ~> tsIdent)) ~ (":" ~> (tsIdent | destructured)).? <~ ",".?) <~ "}" ^^ (
_ =>
TsIdent.Destructured,
)
lazy val destructuredArray: Parser[TsIdentSimple] =
"[" ~>! ",".? ~> repsep("...".? ~> tsIdent <~ (":" <~ (tsIdent | destructured)).?, ",") <~ "]" ^^ (
ids =>
TsIdent("has" + ids.map(_.value.capitalize).mkString("")),
_ =>
TsIdent.Destructured,
)

lazy val destructured = destructuredArray | destructuredObj

comments ~ "...".isDefined ~ (tsIdent | destructured) ~ "?".isDefined ~ (typeAnnotationOpt <~ ("=" ~ expr).?) <~ ",".? ^^ {
Expand Down

0 comments on commit 0b17669

Please sign in to comment.