From 21ff172c40c7d4fa05fc0b3e78818ac4db66d26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Raddum=20Berg?= Date: Sun, 23 Oct 2022 21:00:48 +0200 Subject: [PATCH] infer shorter (index-based) names for destructured parameters which do not have a name. This is a follow-up from #485, to avoid too long class names on windows --- .../converter/internal/ts/trees.scala | 7 ++-- .../internal/ts/parser/ParserTests.scala | 2 +- tests/recharts/check-3/r/recharts/build.sbt | 2 +- .../src/main/scala/typings/recharts/mod.scala | 2 +- .../internal/ts/parser/TsParser.scala | 32 +++++++------------ 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala b/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala index 2c10fa94f6..f747eacd02 100644 --- a/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala +++ b/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala @@ -441,9 +441,10 @@ object TsIdent { def unapply(ident: TsIdent): Some[String] = Some(ident.value) - val `this`: TsIdentSimple = TsIdent("this") - val Apply: TsIdentSimple = TsIdent("") // keep in sync with Name.necessaryRewrite - val Global: TsIdentSimple = TsIdent("") // keep in sync with Name.necessaryRewrite + val `this`: TsIdentSimple = TsIdent("this") + val Apply: TsIdentSimple = TsIdent("") // keep in sync with Name.necessaryRewrite + val Global: TsIdentSimple = TsIdent("") // keep in sync with Name.necessaryRewrite + val Destructured: TsIdentSimple = TsIdent("") // for parameters with no name. val update: TsIdentSimple = TsIdent("update") val prototype: TsIdentSimple = TsIdent("prototype") diff --git a/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala b/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala index 232d0382fe..f20b53bf4a 100644 --- a/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala +++ b/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala @@ -3054,7 +3054,7 @@ export {}; shouldParseAs(content, TsParser.functionParam)( TsFunParam( NoComments, - TsIdentSimple("hasArr"), + TsIdent.Destructured, Some(TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("Array"))), IArray(T))), ), ) diff --git a/tests/recharts/check-3/r/recharts/build.sbt b/tests/recharts/check-3/r/recharts/build.sbt index f95010a0ce..e6d5a195f0 100644 --- a/tests/recharts/check-3/r/recharts/build.sbt +++ b/tests/recharts/check-3/r/recharts/build.sbt @@ -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( diff --git a/tests/recharts/check-3/r/recharts/src/main/scala/typings/recharts/mod.scala b/tests/recharts/check-3/r/recharts/src/main/scala/typings/recharts/mod.scala index ebd38cfdb1..22164f3ca3 100644 --- a/tests/recharts/check-3/r/recharts/src/main/scala/typings/recharts/mod.scala +++ b/tests/recharts/check-3/r/recharts/src/main/scala/typings/recharts/mod.scala @@ -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 { diff --git a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala index 3ec1b4a30a..800fb42a36 100644 --- a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala +++ b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala @@ -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] = @@ -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).?) <~ ",".? ^^ {