Skip to content

Commit

Permalink
Fix lookup (ScalablyTyped#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinybot committed Jul 31, 2024
1 parent 43a64aa commit ceca32f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
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 @@ -3,7 +3,8 @@ package scalajs

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

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

override lazy val hashCode = dropRoot(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 =>
dropRoot(parts) === dropRoot(other.parts)
dropRootPart(parts) === dropRootPart(other.parts)
case _ => false
}
}
Expand Down Expand Up @@ -118,6 +125,9 @@ object QualifiedName {
implicit val encodes: Encoder[QualifiedName] = deriveEncoder
implicit val decodes: Decoder[QualifiedName] = deriveDecoder

private def dropRoot(parts: IArray[Name]): IArray[Name] =
parts.dropWhile(_ == Name.root)
@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 @@ -52,7 +52,7 @@ object AdaptiveNamingImport {
// 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"_root_.${outputPkg.unescaped.toLowerCase}.node.mod") = IArray(TsIdentSimple("_____"))
lowercaseIndex(s"${outputPkg.unescaped.toLowerCase}.node.mod") = IArray(TsIdentSimple("_____"))
}

val illegalNames = (cleanIllegalNames.Illegal ++ ScalaJsClasses.jsObjectMembersByName.keys).map(_.value)
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 Expand Up @@ -489,7 +489,7 @@ object Printer {
else q.parts.map(formatName).mkString(".")

def formatName(name: Name): String = name match {
case `outputPackage` => s"_root_.${outputPackage.unescaped}" // this let's dots in chosen package name slip through
case `outputPackage` => outputPackage.unescaped // this let's dots in chosen package name slip through
case Name.APPLY => "apply"
case Name.THIS => "this"
case Name.SUPER => "super"
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

0 comments on commit ceca32f

Please sign in to comment.