Skip to content

Commit

Permalink
Predictable module names (fixes #469)
Browse files Browse the repository at this point in the history
add `stShortModuleNames` (for sbt plugin) and `--shortModuleNames` for CLI which reverts to old behaviour.

Note that the old behaviour is deprecated
  • Loading branch information
oyvindberg committed Oct 13, 2022
1 parent 0b3581f commit b15041b
Show file tree
Hide file tree
Showing 272 changed files with 923 additions and 857 deletions.
41 changes: 24 additions & 17 deletions cli/src/main/scala/org/scalablytyped/converter/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ object Main {
}

val DefaultOptions = ConversionOptions(
useScalaJsDomTypes = true,
outputPackage = Name.typings,
enableScalaJsDefined = Selection.All,
flavour = Flavour.Normal,
ignored = SortedSet("typescript"),
stdLibs = SortedSet("es6"),
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
versions = Versions(Versions.Scala3, Versions.ScalaJs1),
organization = "org.scalablytyped",
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
useScalaJsDomTypes = true,
outputPackage = Name.typings,
enableScalaJsDefined = Selection.All,
flavour = Flavour.Normal,
ignored = SortedSet("typescript"),
stdLibs = SortedSet("es6"),
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
versions = Versions(Versions.Scala3, Versions.ScalaJs1),
organization = "org.scalablytyped",
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
useDeprecatedModuleNames = false,
)

case class Config(
Expand Down Expand Up @@ -166,6 +167,11 @@ object Main {
opt[Boolean]("enableLongApplyMethod")
.action((x, c) => c.mapConversion(_.copy(enableLongApplyMethod = x)))
.text(s"Enables long apply methods, instead of implicit ops builders"),
opt[Boolean]("shortModuleNames")
.action((x, c) => c.mapConversion(_.copy(useDeprecatedModuleNames = x)))
.text(
s"Enables short module names. This used to be the default, and is now deprecated since it's so difficult to navigate",
),
arg[Seq[TsIdentLibrary]]("libs")
.text("Libraries you want to convert from node_modules")
.unbounded()
Expand Down Expand Up @@ -263,11 +269,12 @@ object Main {
)
.next(
new Phase2ToScalaJs(
pedantic = false,
scalaVersion = conversion.versions.scala,
enableScalaJsDefined = conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = conversion.flavourImpl,
pedantic = false,
scalaVersion = conversion.versions.scala,
enableScalaJsDefined = conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = conversion.flavourImpl,
useDeprecatedModuleNames = conversion.useDeprecatedModuleNames,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ final class AdaptiveNamingImport(private val rewrites: Map[IArray[TsIdent], Qual

object AdaptiveNamingImport {
def apply(
outputPkg: Name,
libraryName: TsIdentLibrary,
library: TsParsedFile,
depsRewrites: IArray[AdaptiveNamingImport],
cleanIllegalNames: CleanIllegalNames,
outputPkg: Name,
libraryName: TsIdentLibrary,
library: TsParsedFile,
depsRewrites: IArray[AdaptiveNamingImport],
cleanIllegalNames: CleanIllegalNames,
useDeprecatedModuleNames: Boolean,
): AdaptiveNamingImport = {
val allReferences: IArray[IArray[TsIdent]] =
TsTreeTraverse
Expand Down Expand Up @@ -60,9 +61,15 @@ object AdaptiveNamingImport {
case IArray.Empty => ()
case whole @ IArray.initLast(parent, current) =>
val parentTranslated = registeredReferences(parent)
val variants = variantsFor(current, parent.exists(_.isInstanceOf[TsIdentModule]), illegalNames)
var continue = true
val iter = variants.iterator
val variants = variantsFor(
current,
parent.exists(_.isInstanceOf[TsIdentModule]),
parent.collectFirst { case x: TsIdentLibrary => x },
illegalNames,
useDeprecatedModuleNames,
)
var continue = true
val iter = variants.iterator
while (continue && iter.hasNext) {
val currentVariant = iter.next()
val possibleQname = QualifiedName(parentTranslated.parts :+ Name.necessaryRewrite(Name(currentVariant)))
Expand All @@ -88,7 +95,13 @@ object AdaptiveNamingImport {
new AdaptiveNamingImport(registeredReferences.toMap)
}

def variantsFor(tsIdent: TsIdent, hasModuleParent: Boolean, illegalNames: Set[String]): Stream[String] = {
def variantsFor(
tsIdent: TsIdent,
hasModuleParent: Boolean,
inLib: Option[TsIdentLibrary],
illegalNames: Set[String],
useDeprecatedModuleNames: Boolean,
): Stream[String] = {
val base = tsIdent match {
case TsIdent.namespaced => Stream(Name.namespaced.unescaped)
case TsIdent.Apply => Stream(Name.APPLY.unescaped)
Expand All @@ -100,7 +113,7 @@ object AdaptiveNamingImport {
/* todo: We should look up what the augmented module is called and reuse it. I don't care enough to do it now */
nameVariants(joinCamelCase(m.scopeOpt.toList ++ m.fragments)).map(_ + "AugmentingMod")

case m: TsIdentModule =>
case m: TsIdentModule if useDeprecatedModuleNames =>
val increasingLength: Stream[List[String]] = {
val (libraryBits, moduleBits) =
m match {
Expand All @@ -122,15 +135,28 @@ object AdaptiveNamingImport {

preferCamelCase.flatMap(frags => nameVariants(addMod(joinCamelCase(frags))))

case library: TsIdentLibrary =>
nameVariants(toCamelCase(library.value))
case m: TsIdentModule =>
val shortenedFragments = {
inLib match {
case Some(TsIdentLibrarySimple(value)) if m.fragments.head == value =>
m.fragments.drop(1)
case Some(TsIdentLibraryScoped(scope, name)) if m.scopeOpt.contains(scope) && m.fragments.head == name =>
m.fragments.drop(1)
case _ =>
m.fragments
}
}
Stream(addMod(joinCamelCase(shortenedFragments.map(toCamelCase))))

case _: TsIdentImport => sys.error("unexpected")
case library: TsIdentLibrary => variantsForLibName(library)
case _: TsIdentImport => sys.error("unexpected")
}

base #::: base.map(_ + "_") #::: base.map(_ + "__")
}

def variantsForLibName(library: TsIdentLibrary) =
nameVariants(toCamelCase(library.value))
private def addMod(str: String) = str match {
case "" => Name.mod.unescaped
case nonEmpty => nonEmpty + "Mod"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import org.scalablytyped.converter.internal.ts.TsIdentLibrary
import scala.collection.immutable.SortedSet

case class ConversionOptions(
useScalaJsDomTypes: Boolean,
flavour: Flavour,
outputPackage: Name,
enableScalaJsDefined: Selection[TsIdentLibrary],
stdLibs: SortedSet[String],
expandTypeMappings: Selection[TsIdentLibrary],
ignored: SortedSet[String],
versions: Versions,
organization: String,
enableReactTreeShaking: Selection[Name],
enableLongApplyMethod: Boolean,
privateWithin: Option[Name],
useScalaJsDomTypes: Boolean,
flavour: Flavour,
outputPackage: Name,
enableScalaJsDefined: Selection[TsIdentLibrary],
stdLibs: SortedSet[String],
expandTypeMappings: Selection[TsIdentLibrary],
ignored: SortedSet[String],
versions: Versions,
organization: String,
enableReactTreeShaking: Selection[Name],
enableLongApplyMethod: Boolean,
privateWithin: Option[Name],
useDeprecatedModuleNames: Boolean,
) {
val ignoredLibs: Set[TsIdentLibrary] =
ignored.map(TsIdentLibrary.apply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ImportName {
Name.necessaryRewrite(Name(i.value))

def apply(i: TsIdentLibrary): Name =
Name.necessaryRewrite(Name(AdaptiveNamingImport.variantsFor(i, hasModuleParent = false, Set()).head))
Name.necessaryRewrite(Name(AdaptiveNamingImport.variantsForLibName(i).head))

object withJsNameAnnotation {
def apply(original: TsIdentSimple): (Name, Option[Annotation.JsName]) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class ImportTree(
tparams(0).upperBound.exists {
case TsTypeKeyOf(_) => true
case TsTypeRef.string => true
case _ => false
case _ => false
} &&
TsTypeRef(tparams(0).name) == from &&
TsTypeRef(tparams(1).name) == to =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class ImportType(stdNames: QualifiedName.StdNames) {
case TsTypeObject(_, Empty) =>
TypeRef(QualifiedName.JsObject, Empty, NoComments)


case TsTypeObject(_, ms) if ExtractInterfaces.isDictionary(ms) =>
val (numbers, strings, Empty) = ms.partitionCollect2(
{ case x @ TsMemberIndex(_, _, _, Indexing.Dict(_, TsTypeRef.number), _) => x },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import scala.collection.immutable.SortedSet
* Then the phase itself implements a bunch of scala.js limitations, like ensuring no methods erase to the same signature
*/
class Phase2ToScalaJs(
pedantic: Boolean,
scalaVersion: Versions.Scala,
enableScalaJsDefined: Selection[TsIdentLibrary],
outputPkg: Name,
flavour: FlavourImpl,
pedantic: Boolean,
useDeprecatedModuleNames: Boolean,
scalaVersion: Versions.Scala,
enableScalaJsDefined: Selection[TsIdentLibrary],
outputPkg: Name,
flavour: FlavourImpl,
) extends Phase[LibTsSource, LibTs, LibScalaJs] {

override def apply(
Expand Down Expand Up @@ -90,6 +91,7 @@ class Phase2ToScalaJs(
tsLibrary.parsed,
scalaDeps.mapToIArray { case (_, v) => v.names },
cleanIllegalNames,
useDeprecatedModuleNames,
)

val importType = new ImportType(new StdNames(outputPkg))
Expand All @@ -100,7 +102,7 @@ class Phase2ToScalaJs(
cleanIllegalNames,
new ImportExpr(importType, importName),
enableScalaJsDefined(tsLibrary.name),
scalaVersion
scalaVersion,
)

val scalaTree = importTree(tsLibrary, logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ object Ci {
else Versions.Scala3,
Versions.ScalaJs1,
),
organization = organization,
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
organization = organization,
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
useDeprecatedModuleNames = false,
),
wantedLibs = wantedLibNames,
offline = flags contains "-offline",
Expand Down Expand Up @@ -272,10 +273,11 @@ class Ci(config: Ci.Config, paths: Ci.Paths, pool: ForkJoinPool, ec: ExecutionCo
.next(
new Phase2ToScalaJs(
config.pedantic,
scalaVersion = config.conversion.versions.scala,
enableScalaJsDefined = config.conversion.enableScalaJsDefined,
outputPkg = config.conversion.outputPackage,
flavour = config.conversion.flavourImpl,
scalaVersion = config.conversion.versions.scala,
enableScalaJsDefined = config.conversion.enableScalaJsDefined,
outputPkg = config.conversion.outputPackage,
flavour = config.conversion.flavourImpl,
useDeprecatedModuleNames = config.conversion.useDeprecatedModuleNames,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ trait ImporterHarness extends AnyFunSuite {
.next(
new Phase2ToScalaJs(
pedantic,
scalaVersion = version.scala,
enableScalaJsDefined = Selection.All,
outputPkg = flavour.outputPkg,
flavour = flavour,
scalaVersion = version.scala,
enableScalaJsDefined = Selection.All,
outputPkg = flavour.outputPkg,
flavour = flavour,
useDeprecatedModuleNames = false,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ object ImportTypings {
)
.next(
new Phase2ToScalaJs(
pedantic = false,
scalaVersion = input.conversion.versions.scala,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = input.conversion.outputPackage,
flavour = input.conversion.flavourImpl,
pedantic = false,
scalaVersion = input.conversion.versions.scala,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = input.conversion.outputPackage,
flavour = input.conversion.flavourImpl,
useDeprecatedModuleNames = input.conversion.useDeprecatedModuleNames,
),
"scala.js",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ object ImportTypingsGenSources {
)
.next(
new Phase2ToScalaJs(
pedantic = false,
scalaVersion = input.conversion.versions.scala,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = input.conversion.flavourImpl,
pedantic = false,
scalaVersion = input.conversion.versions.scala,
enableScalaJsDefined = input.conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = input.conversion.flavourImpl,
useDeprecatedModuleNames = input.conversion.useDeprecatedModuleNames,
),
"scala.js",
)
Expand Down Expand Up @@ -155,18 +156,19 @@ object ImportTypingsGenSources {
val outputName = Name.typings

val conversion = ConversionOptions(
useScalaJsDomTypes = true,
flavour = Flavour.Slinky,
outputPackage = outputName,
enableScalaJsDefined = Selection.All,
stdLibs = SortedSet("es5", "dom"),
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
ignored = SortedSet(),
versions = Versions(Versions.Scala213, Versions.ScalaJs1),
organization = "org.scalablytyped",
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
useScalaJsDomTypes = true,
flavour = Flavour.Slinky,
outputPackage = outputName,
enableScalaJsDefined = Selection.All,
stdLibs = SortedSet("es5", "dom"),
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
ignored = SortedSet(),
versions = Versions(Versions.Scala213, Versions.ScalaJs1),
organization = "org.scalablytyped",
enableReactTreeShaking = Selection.None,
enableLongApplyMethod = false,
privateWithin = None,
useDeprecatedModuleNames = false,
)

println(
Expand Down
Loading

0 comments on commit b15041b

Please sign in to comment.