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 21ff172
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,10 @@ object TsIdent {
def unapply(ident: TsIdent): Some[String] =
Some(ident.value)

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 `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
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 21ff172

Please sign in to comment.