Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _root_ prefix to output package #642

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ final class IArray[+A <: AnyRef](private val array: Array[AnyRef], val length: I
}

def drop(n: Int): IArray[A] = {
if (n == 0) return this
val newLength = math.max(0, length - n)
if (newLength == 0) return IArray.Empty
val ret = Array.ofDim[AnyRef](newLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object Name {
val underscore: Name = Name("_")
val org: Name = Name("org")
val com: Name = Name("com")
val root: Name = Name("_root_")

val APPLY: Name = Name("<apply>")
val CONSTRUCTOR: Name = Name("<init>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package scalajs

import io.circe013.{Decoder, Encoder}
import io.circe013.generic.semiauto.{deriveDecoder, deriveEncoder}
import org.scalablytyped.converter.internal.scalajs.QualifiedName.dropRootPart
import scala.annotation.tailrec

final case class QualifiedName(parts: IArray[Name]) {
def +(name: Name) =
Expand All @@ -14,12 +16,18 @@ final case class QualifiedName(parts: IArray[Name]) {
def startsWith(other: QualifiedName): Boolean =
parts.startsWith(other.parts)

override lazy val hashCode = parts.hashCode
def dropRoot: QualifiedName = {
val partsWithoutRoot = dropRootPart(parts)
if (partsWithoutRoot eq parts) this
else QualifiedName(partsWithoutRoot)
}

override lazy val hashCode = dropRootPart(parts).hashCode

override def equals(obj: Any): Boolean =
obj match {
case other: QualifiedName if other.hashCode == hashCode =>
parts === other.parts
dropRootPart(parts) === dropRootPart(other.parts)
case _ => false
}
}
Expand Down Expand Up @@ -85,7 +93,7 @@ object QualifiedName {
def AnyFromFunction(n: Int): QualifiedName = JsAny + Name(s"fromFunction$n")

class StdNames(outputPkg: Name) {
val lib: QualifiedName = QualifiedName(IArray(outputPkg, Name.std))
val lib: QualifiedName = QualifiedName(IArray(Name.root, outputPkg, Name.std))
val Array: QualifiedName = lib + Name.Array
val Boolean: QualifiedName = lib + Name.Boolean
val BigInt: QualifiedName = lib + Name("BigInt")
Expand Down Expand Up @@ -116,4 +124,10 @@ object QualifiedName {
implicit val suffix: ToSuffix[QualifiedName] = t => ToSuffix(t.parts.last)
implicit val encodes: Encoder[QualifiedName] = deriveEncoder
implicit val decodes: Decoder[QualifiedName] = deriveDecoder

@tailrec
private def dropRootPart(parts: IArray[Name]): IArray[Name] =
if (parts.isEmpty) parts
else if (parts(0) == Name.root) dropRootPart(parts.tail)
else parts
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object AdaptiveNamingImport {
.sorted(ShortestAndLowercaseFirst)

val registeredReferences =
mutable.Map[IArray[TsIdent], QualifiedName](IArray.Empty -> QualifiedName(IArray(outputPkg)))
mutable.Map[IArray[TsIdent], QualifiedName](IArray.Empty -> QualifiedName(IArray(Name.root, outputPkg)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the output package setting everywhere and anywhere it became a QualifiedName I prefixed it with Name.root.


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ImportTree(
Marker.ManglerLeaveAlone,
),
),
codePath = QualifiedName(IArray(outputPkg, libName, name)),
codePath = QualifiedName(IArray(Name.root, outputPkg, libName, name)),
isOverride = false,
)
}
Expand All @@ -61,7 +61,7 @@ class ImportTree(
outputPkg,
IArray(withRequire),
NoComments,
QualifiedName(IArray(outputPkg)),
QualifiedName(IArray(Name.root, outputPkg)),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ object Printer {
case (ScalaOutput.File(name), members: IArray[Tree]) =>
reg.write(targetFolder / os.RelPath(s"${name.unescaped}.scala")) { writer =>
val (imports, shortenedMembers) = ShortenNames(tree, scope, parentsResolver)(members)
writer.println(s"package ${formatQN(QualifiedName(packages))}")
writer.println(s"package ${formatQN(QualifiedName(packages).dropRoot)}")
writer.println("")
imports.foreach(i => writer.println(s"import ${formatQN(i.imported)}"))
writer.println(Imports)
Expand All @@ -155,7 +155,7 @@ object Printer {
reg.write(targetFolder / packageScalaFileName) { writer =>
val (imports, shortenedMembers) =
ShortenNames(tree, scope, parentsResolver)(members)
writer.println(s"package ${formatQN(QualifiedName(packages))}")
writer.println(s"package ${formatQN(QualifiedName(packages).dropRoot)}")
writer.println("")
imports.foreach(i => writer.println(s"import ${formatQN(i.imported)}"))
writer.println(Imports)
Expand All @@ -177,7 +177,7 @@ object Printer {
packages.dropRight(1) match {
case IArray.Empty => ()
case remaining =>
writer.println(s"package ${formatQN(QualifiedName(remaining))}")
writer.println(s"package ${formatQN(QualifiedName(remaining).dropRoot)}")
}

writer.println("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ object TreeScope {

override def _lookup(fragments: IArray[Name]): IArray[(Tree, TreeScope)] =
fragments match {
case IArray.headHeadTail(`outputPkg`, head, tail) =>
case IArray.headHeadTail(`outputPkg`, head, _) =>
dependencies.get(head) match {
case Some(dep) => dep.lookupNoBacktrack(outputPkg +: head +: tail)
case Some(dep) => dep.lookupNoBacktrack(fragments)
case None => Empty
}
case IArray.headTail(Name.root, tail) =>
_lookup(tail)
case _ => Empty
}

Expand Down Expand Up @@ -151,6 +153,9 @@ object TreeScope {

def lookupNoBacktrack(names: IArray[Name]): IArray[(Tree, TreeScope)] =
names match {
case IArray.headTail(Name.root, tail) =>
lookupNoBacktrack(tail)

case IArray.exactlyOne(current.name) =>
IArray((current, this))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class JapgollyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Vers
val enableAnyVal = if (scalaVersion.binVersion <= "2.12") None else Some(TypeRef.AnyVal)

val StBuildingComponent = Name("StBuildingComponent")
val builderCp = QualifiedName(IArray(outputPkg, StBuildingComponent))
val builderCp = QualifiedName(IArray(Name.root, outputPkg, StBuildingComponent))
val R = TypeParamTree(Name("R"), Empty, Some(TypeRef.JsObject), NoComments, ignoreBound = false)
val builderTparams = IArray(R)
val builderRef = TypeRef(builderCp, TypeParamTree.asTypeArgs(builderTparams), NoComments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package flavours

class ReactNames(val outputPkg: Name) {
val libName = Name("react")
val mod = QualifiedName(IArray(outputPkg, libName, Name.mod))
val static = QualifiedName(IArray(outputPkg, libName, Name.global, Name("React")))
val mod = QualifiedName(IArray(Name.root, outputPkg, libName, Name.mod))
val static = QualifiedName(IArray(Name.root, outputPkg, libName, Name.global, Name("React")))
val Ref = mod + Name("Ref")
val RefCallback = mod + Name("RefCallback")
val LegacyRef = mod + Name("LegacyRef")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SlinkyGenStBuildingComponent(val outputPkg: Name, val scalaVersion: Versio
val enableAnyVal = if (scalaVersion.binVersion <= "2.12") None else Some(TypeRef.AnyVal)

val StBuildingComponent = Name("StBuildingComponent")
val builderCp = QualifiedName(IArray(outputPkg, StBuildingComponent))
val builderCp = QualifiedName(IArray(Name.root, outputPkg, StBuildingComponent))
val E = TypeParamTree(Name("E"), Empty, None, NoComments, ignoreBound = false)
val R = TypeParamTree(Name("R"), Empty, Some(TypeRef.JsObject), NoComments, ignoreBound = false)
val builderTparams = IArray(E, R)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object FakeLiterals {
val lit = ExprTree.StringLit(underlying)
collectedStrings(name) = lit
TypeRef(
QualifiedName(IArray(outputPkg, tree.name, StringModuleName, name)),
QualifiedName(IArray(Name.root, outputPkg, tree.name, StringModuleName, name)),
Empty,
Comments(Marker.WasLiteral(lit)),
)
Expand All @@ -120,7 +120,7 @@ object FakeLiterals {
val lit = ExprTree.BooleanLit(underlying.toBoolean)
collectedBooleans(name) = lit
TypeRef(
QualifiedName(IArray(outputPkg, tree.name, BooleansModuleName, name)),
QualifiedName(IArray(Name.root, outputPkg, tree.name, BooleansModuleName, name)),
Empty,
Comments(Marker.WasLiteral(lit)),
)
Expand All @@ -130,7 +130,7 @@ object FakeLiterals {
val lit = ExprTree.DoubleLit(underlying)
collectedDoubles(name) = lit
TypeRef(
QualifiedName(IArray(outputPkg, tree.name, DoublesModuleName, name)),
QualifiedName(IArray(Name.root, outputPkg, tree.name, DoublesModuleName, name)),
Empty,
Comments(Marker.WasLiteral(lit)),
)
Expand All @@ -140,7 +140,7 @@ object FakeLiterals {
val lit = ExprTree.IntLit(underlying)
collectedInts(name) = lit
TypeRef(
QualifiedName(IArray(outputPkg, tree.name, IntsModuleName, name)),
QualifiedName(IArray(Name.root, outputPkg, tree.name, IntsModuleName, name)),
Empty,
Comments(Marker.WasLiteral(lit)),
)
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-b2de7b"
version := "4.14-cfe4eb"
scalaVersion := "3.3.1"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package typings.lodash

import _root_.typings.lodash.mod.CurriedFunction1
import _root_.typings.lodash.mod.CurriedFunction2
import org.scalablytyped.runtime.Shortcut
import typings.lodash.mod.CurriedFunction1
import typings.lodash.mod.CurriedFunction2
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,9 +1,9 @@
package typings.lodash

import _root_.typings.lodash.fpCurryMod.Curry
import _root_.typings.lodash.mod.CurriedFunction1
import _root_.typings.lodash.mod.CurriedFunction2
import org.scalablytyped.runtime.Shortcut
import typings.lodash.fpCurryMod.Curry
import typings.lodash.mod.CurriedFunction1
import typings.lodash.mod.CurriedFunction2
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,6 +1,6 @@
package typings.lodash

import typings.lodash.mod.LoDashStatic
import _root_.typings.lodash.mod.LoDashStatic
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,7 +1,7 @@
package typings.lodash

import _root_.typings.std.ArrayLike
import org.scalablytyped.runtime.Shortcut
import typings.std.ArrayLike
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
Expand Down Expand Up @@ -50,8 +50,6 @@ object mod extends Shortcut {
def at[T /* <: js.Object */](`object`: T, props: (Many[/* keyof T */ String])*): js.Array[
/* import warning: importer.ImportType#apply Failed type conversion: T[keyof T] */ js.Any
] = js.native
def at[T](`object`: Null, props: ((Many[/* keyof T */ String]) | PropertyPath)*): js.Array[T] = js.native
def at[T](`object`: Unit, props: ((Many[/* keyof T */ String]) | PropertyPath)*): js.Array[T] = js.native
/**
* Creates an array of elements corresponding to the given keys, or indexes, of collection. Keys may be
* specified as individual arguments or as arrays of keys.
Expand All @@ -60,7 +58,9 @@ object mod extends Shortcut {
* @param props The property names or indexes of elements to pick, specified individually or in arrays.
* @return Returns the new array of picked elements.
*/
def at[T](`object`: typings.lodash.mod.List[T], props: PropertyPath*): js.Array[T] = js.native
def at[T](`object`: _root_.typings.lodash.mod.List[T], props: PropertyPath*): js.Array[T] = js.native
def at[T](`object`: Null, props: ((Many[/* keyof T */ String]) | PropertyPath)*): js.Array[T] = js.native
def at[T](`object`: Unit, props: ((Many[/* keyof T */ String]) | PropertyPath)*): js.Array[T] = js.native
}

type Many[T] = T | js.Array[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package typings.lodash

import typings.std.Partial
import _root_.typings.std.Partial
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
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-b773ad"
version := "2.247.1-921b00"
scalaVersion := "3.3.1"
enablePlugins(ScalaJSPlugin)
libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package typings.awsSdk

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}

object awsSdkStrings {

@js.native
sealed trait ADD
extends StObject
with _AttributeAction
steinybot marked this conversation as resolved.
Show resolved Hide resolved
sealed trait ADD extends StObject
inline def ADD: ADD = "ADD".asInstanceOf[ADD]

@js.native
sealed trait DELETE
extends StObject
with _AttributeAction
sealed trait DELETE extends StObject
inline def DELETE: DELETE = "DELETE".asInstanceOf[DELETE]

@js.native
sealed trait PUT
extends StObject
with _AttributeAction
sealed trait PUT extends StObject
inline def PUT: PUT = "PUT".asInstanceOf[PUT]

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

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 _root_.typings.awsSdk.clientsDynamodbMod.AttributeValue
import _root_.typings.awsSdk.clientsDynamodbMod.ClientConfiguration
import _root_.typings.awsSdk.clientsDynamodbMod.^
import _root_.typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import _root_.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 All @@ -21,7 +21,7 @@ object clientsAllMod {
@JSImport("aws-sdk/clients/all", "DynamoDB.Converter")
@js.native
open class Converter ()
extends typings.awsSdk.clientsDynamodbMod.Converter
extends _root_.typings.awsSdk.clientsDynamodbMod.Converter
/* static members */
object Converter {

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

import typings.awsSdk.awsSdkStrings.latest
import typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import typings.awsSdk.libServicesDynamodbMod.DynamoDBCustomizations
import _root_.typings.awsSdk.awsSdkStrings.latest
import _root_.typings.awsSdk.libDynamodbConverterMod.Converter.ConverterOptions
import _root_.typings.awsSdk.libDynamodbDocumentClientMod.DocumentClient.DocumentClientOptions
import _root_.typings.awsSdk.libServicesDynamodbMod.DynamoDBCustomizations
import org.scalablytyped.runtime.StObject
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSGlobalScope, JSGlobal, JSImport, JSName, JSBracketAccess}
Expand All @@ -19,7 +19,7 @@ object clientsDynamodbMod {
@JSImport("aws-sdk/clients/dynamodb", "Converter")
@js.native
open class Converter ()
extends typings.awsSdk.libDynamodbConverterMod.Converter
extends _root_.typings.awsSdk.libDynamodbConverterMod.Converter
/* static members */
object Converter {

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

Expand Down
Loading
Loading