Skip to content

Commit

Permalink
Predictable module names (fixes #469)
Browse files Browse the repository at this point in the history
todo: config so users can fall back to old behaviour
  • Loading branch information
oyvindberg committed Sep 20, 2022
1 parent 5908a72 commit 4e74743
Show file tree
Hide file tree
Showing 263 changed files with 806 additions and 771 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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,
Expand All @@ -48,21 +47,20 @@ object AdaptiveNamingImport {

val lowercaseIndex = mutable.Map.empty[String, IArray[TsIdent]]

// very obviously a hack. node is the only library seen so far where the shortest module name (`assert`)
// doesnt correspond to the library name
if (libraryName.value === "node") {
lowercaseIndex(s"${outputPkg.unescaped.toLowerCase}.node.mod") = IArray(TsIdentSimple("_____"))
}

val illegalNames = (cleanIllegalNames.Illegal ++ ScalaJsClasses.jsObjectMembersByName.keys).map(_.value)

allReferences.foreach {
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,
)
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 @@ -86,7 +84,12 @@ 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],
): Stream[String] = {
val base = tsIdent match {
case TsIdent.namespaced => Stream(Name.namespaced.unescaped)
case TsIdent.Apply => Stream(Name.APPLY.unescaped)
Expand All @@ -98,6 +101,19 @@ 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 if true =>
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 m: TsIdentModule =>
val increasingLength: Stream[List[String]] = {
val (libraryBits, moduleBits) =
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.variantsFor(i, hasModuleParent = false, inLib = None, Set()).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 @@ -86,7 +86,6 @@ class Phase2ToScalaJs(

val importName = AdaptiveNamingImport(
outputPkg,
tsLibrary.name,
tsLibrary.parsed,
scalaDeps.mapToIArray { case (_, v) => v.names },
cleanIllegalNames,
Expand Down
2 changes: 1 addition & 1 deletion tests/antd/check-3/a/antd/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "antd"
version := "4.3.1-c51621"
version := "4.3.1-5a356c"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object mod {
object libFormFormMod {

/* Inlined parent std.Omit<rc-field-form.rc-field-form/es/Form.FormProps, 'form'> */
trait FormProps extends StObject {
Expand Down
2 changes: 1 addition & 1 deletion tests/antd/check-3/r/rc-field-form/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "rc-field-form"
version := "1.4.4-0045e3"
version := "1.4.4-ead491"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object mod {
object esFormMod {

trait FormProps extends StObject {

Expand Down
2 changes: 1 addition & 1 deletion tests/augment-module/check-3/l/lodash/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "lodash"
version := "4.14-313289"
version := "4.14-fee813"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object curryMod extends Shortcut {
object fpCurryMod extends Shortcut {

@JSImport("lodash/fp/curry", JSImport.Namespace)
@js.native
Expand All @@ -22,6 +22,6 @@ object curryMod extends Shortcut {

type _To = Curry

/* This means you don't have to write `^`, but can instead just say `curryMod.foo` */
/* This means you don't have to write `^`, but can instead just say `fpCurryMod.foo` */
override def _to: Curry = ^
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package typings.lodash

import org.scalablytyped.runtime.Shortcut
import typings.lodash.curryMod.Curry
import typings.lodash.fpCurryMod.Curry
import typings.lodash.mod.CurriedFunction1
import typings.lodash.mod.CurriedFunction2
import org.scalablytyped.runtime.StObject
Expand Down
2 changes: 1 addition & 1 deletion tests/aws-sdk/check-3/a/aws-sdk/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "org.scalablytyped"
name := "aws-sdk"
version := "2.247.1-bd92aa"
version := "2.247.1-23037d"
scalaVersion := "3.1.2"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package typings.awsSdk

import typings.awsSdk.documentClientMod.DocumentClient._AttributeAction
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient._AttributeAction
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package typings.awsSdk

import typings.awsSdk.converterMod.Converter.ConverterOptions
import typings.awsSdk.documentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.dynamodbMod.AttributeValue
import typings.awsSdk.dynamodbMod.ClientConfiguration
import typings.awsSdk.dynamodbMod.^
import typings.awsSdk.clientsDynamodbMod.AttributeValue
import typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import typings.awsSdk.clientsDynamodbMod.^
import typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object allMod {
object clientsAllMod {

@JSImport("aws-sdk/clients/all", "DynamoDB")
@js.native
Expand All @@ -21,7 +21,7 @@ object allMod {
@JSImport("aws-sdk/clients/all", "DynamoDB.Converter")
@js.native
open class Converter ()
extends typings.awsSdk.dynamodbMod.Converter
extends typings.awsSdk.clientsDynamodbMod.Converter
/* static members */
object Converter {

Expand All @@ -39,7 +39,7 @@ object allMod {
* Creates a DynamoDB document client with a set of configuration options.
*/
open class DocumentClient ()
extends typings.awsSdk.dynamodbMod.DocumentClient {
extends typings.awsSdk.clientsDynamodbMod.DocumentClient {
def this(options: DocumentClientOptions & ClientConfiguration) = this()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package typings.awsSdk

import typings.awsSdk.awsSdkStrings.latest
import typings.awsSdk.converterMod.Converter.ConverterOptions
import typings.awsSdk.documentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.servicesDynamodbMod.DynamoDBCustomizations
import typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.libServicesDynamodbMod.DynamoDBCustomizations
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object dynamodbMod {
object clientsDynamodbMod {

@JSImport("aws-sdk/clients/dynamodb", JSImport.Namespace)
@js.native
Expand All @@ -19,7 +19,7 @@ object dynamodbMod {
@JSImport("aws-sdk/clients/dynamodb", "Converter")
@js.native
open class Converter ()
extends typings.awsSdk.converterMod.Converter
extends typings.awsSdk.libDynamodbConverterMod.Converter
/* static members */
object Converter {

Expand All @@ -37,7 +37,7 @@ object dynamodbMod {
* Creates a DynamoDB document client with a set of configuration options.
*/
open class DocumentClient ()
extends typings.awsSdk.documentClientMod.DocumentClient {
extends typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient {
def this(options: DocumentClientOptions & ClientConfiguration) = this()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package typings.awsSdk

import typings.awsSdk.configMod.Config
import typings.awsSdk.converterMod.Converter.ConverterOptions
import typings.awsSdk.documentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.dynamodbMod.AttributeValue
import typings.awsSdk.dynamodbMod.ClientConfiguration
import typings.awsSdk.clientsDynamodbMod.AttributeValue
import typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import typings.awsSdk.libConfigMod.Config
import typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package typings.awsSdk

import typings.awsSdk.awsSdkStrings.latest
import typings.awsSdk.configServicePlaceholdersMod.ConfigurationServiceApiVersions
import typings.awsSdk.configServicePlaceholdersMod.ConfigurationServicePlaceholders
import typings.awsSdk.libConfigServicePlaceholdersMod.ConfigurationServiceApiVersions
import typings.awsSdk.libConfigServicePlaceholdersMod.ConfigurationServicePlaceholders
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object configMod {
object libConfigMod {

@JSImport("aws-sdk/lib/config", "Config")
@js.native
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package typings.awsSdk

import typings.awsSdk.dynamodbMod.ClientConfiguration
import typings.awsSdk.dynamodbMod.apiVersion
import typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import typings.awsSdk.clientsDynamodbMod.apiVersion
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object configServicePlaceholdersMod {
object libConfigServicePlaceholdersMod {

@JSImport("aws-sdk/lib/config_service_placeholders", "ConfigurationServicePlaceholders")
@js.native
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package typings.awsSdk

import typings.awsSdk.dynamodbMod.AttributeValue
import typings.awsSdk.clientsDynamodbMod.AttributeValue
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object converterMod {
object libDynamodbConverterMod {

@JSImport("aws-sdk/lib/dynamodb/converter", "Converter")
@js.native
Expand All @@ -20,6 +20,6 @@ object converterMod {
inline def input(data: Any): AttributeValue = ^.asInstanceOf[js.Dynamic].applyDynamic("input")(data.asInstanceOf[js.Any]).asInstanceOf[AttributeValue]
inline def input(data: Any, options: ConverterOptions): AttributeValue = (^.asInstanceOf[js.Dynamic].applyDynamic("input")(data.asInstanceOf[js.Any], options.asInstanceOf[js.Any])).asInstanceOf[AttributeValue]

type ConverterOptions = typings.awsSdk.documentClientMod.DocumentClient.ConverterOptions
type ConverterOptions = typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.ConverterOptions
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package typings.awsSdk

import org.scalablytyped.runtime.StringDictionary
import typings.awsSdk.documentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.dynamodbMod.ClientConfiguration
import typings.awsSdk.dynamodbMod.^
import typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import typings.awsSdk.clientsDynamodbMod.^
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object documentClientMod {
object libDynamodbDocumentClientMod {

@JSImport("aws-sdk/lib/dynamodb/document_client", "DocumentClient")
@js.native
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package typings.awsSdk

import org.scalablytyped.runtime.Instantiable1
import typings.awsSdk.documentClientMod.DocumentClient
import typings.awsSdk.documentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.dynamodbMod.ClientConfiguration
import typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}

object servicesDynamodbMod {
object libServicesDynamodbMod {

@JSImport("aws-sdk/lib/services/dynamodb", "DynamoDBCustomizations")
@js.native
Expand All @@ -28,7 +28,7 @@ object servicesDynamodbMod {
@js.native
def DocumentClient: Instantiable1[
/* options */ js.UndefOr[DocumentClientOptions & ClientConfiguration],
typings.awsSdk.documentClientMod.DocumentClient
typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient
] = js.native
inline def DocumentClient_=(
x: Instantiable1[
Expand Down
Loading

0 comments on commit 4e74743

Please sign in to comment.