Skip to content

Commit

Permalink
Note which standard all members in std are from
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Sep 17, 2021
1 parent dd88622 commit e345d28
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
Expand Up @@ -63,9 +63,9 @@ class Phase1ReadTypescript(
case s @ Source.TsHelperFile(file, inLib, _) =>
val L = logger.withContext(file)

def assertPartsOnly(m: SortedMap[Source, Phase1Res]): SortedMap[Source, LibraryPart] =
def assertFilesOnly(m: SortedMap[Source, Phase1Res]): SortedMap[Source, LibTsFile] =
m.map {
case (s, part: LibraryPart) => s -> part
case (s, part: LibTsFile) => s -> part
case (s, other) => sys.error(s"$s: Unexpected $other")
}

Expand Down Expand Up @@ -107,7 +107,7 @@ class Phase1ReadTypescript(
/* Ensure we resolved all modules referenced by a path directive */
pathRefs <- PhaseRes.sequenceSet(pathRefsR ++ libRefsR)
/* Assert all path directive referenced modules are files (not libraries) */
toInline <- getDeps(pathRefs.sorted).map(assertPartsOnly)
toInline <- getDeps(pathRefs.sorted).map(assertFilesOnly)

withoutDirectives = parsed.copy(directives = IArray.Empty)

Expand All @@ -129,7 +129,15 @@ class Phase1ReadTypescript(

/* look up all resulting dependencies */
deps <- getDeps((withExternals.resolvedDeps ++ typeReferencedDeps ++ inferredDeps).sorted)
} yield LibraryPart(FileAndInlinesRec(withExternals.rewritten, toInline), deps)
withOrigin = {
val file = withExternals.rewritten
lazy val shortName = s.file.path.last.split("\\.").drop(1).dropRight(2).mkString(".")
if (source.libName === TsIdent.std && shortName.nonEmpty) {
T.AddComments(Comments(List(Comment(s"/* standard $shortName */\n")))).visitTsParsedFile(())(file)
} else file
}

} yield LibTsFile(FileAndInlinesRec(withOrigin, toInline), deps)
}

case source: Source.TsLibSource =>
Expand Down
Expand Up @@ -23,9 +23,9 @@ object Phase1Res {
def name: TsIdentLibrary = source.libName
}

case class LibraryPart(file: FileAndInlinesRec, deps: SortedMap[Source, Phase1Res]) extends Phase1Res
case class LibTsFile(file: FileAndInlinesRec, deps: SortedMap[Source, Phase1Res]) extends Phase1Res

case class FileAndInlinesRec(file: TsParsedFile, toInline: SortedMap[Source, LibraryPart])
case class FileAndInlinesRec(file: TsParsedFile, toInline: SortedMap[Source, LibTsFile])

case class FileAndInlinesFlat(file: TsParsedFile, toInline: SortedMap[Source, TsParsedFile])

Expand Down Expand Up @@ -55,7 +55,7 @@ object Phase1Res {

def go(m: Map[Source, Phase1Res]): Unit =
m.foreach {
case (s: TsHelperFile, libPart: LibraryPart) =>
case (s: TsHelperFile, libPart: LibTsFile) =>
if (!libParts.contains(s)) {
def flatten(os: Option[Source], _f: FileAndInlinesRec): SortedMap[Source, TsParsedFile] = {
val first: SortedMap[Source, TsParsedFile] =
Expand All @@ -65,7 +65,7 @@ object Phase1Res {
}
val rest: SortedMap[Source, TsParsedFile] =
_f.toInline.flatMap {
case (s2, x: LibraryPart) =>
case (s2, x: LibTsFile) =>
go(x.deps)
flatten(Some(s2), x.file)
}
Expand Down
Expand Up @@ -3,7 +3,7 @@ package internal
package importer

import com.olvind.logging.Logger
import org.scalablytyped.converter.internal.importer.Phase1Res.{LibTs, LibraryPart}
import org.scalablytyped.converter.internal.importer.Phase1Res.{LibTs, LibTsFile}
import org.scalablytyped.converter.internal.maps._
import org.scalablytyped.converter.internal.phases.{GetDeps, IsCircular, Phase, PhaseRes}
import org.scalablytyped.converter.internal.scalajs.CastConversion.TypeRewriterCast
Expand Down Expand Up @@ -43,7 +43,7 @@ class Phase2ToScalaJs(
logger: Logger[Unit],
): PhaseRes[Source, LibScalaJs] =
current match {
case _: LibraryPart =>
case _: LibTsFile =>
PhaseRes.Ignore()

case lib: LibTs =>
Expand Down
@@ -0,0 +1,15 @@
package org.scalablytyped.converter.internal
package ts
package transforms

case class AddComments(newComments: Comments) extends TreeTransformationUnit {
override def enterTsMember(t: Unit)(x: TsMember): TsMember =
x match {
case x: TsMemberCall => x.copy(comments = x.comments ++ newComments)
case x: TsMemberCtor => x.copy(comments = x.comments ++ newComments)
case x: TsMemberFunction => x.copy(comments = x.comments ++ newComments)
case x: TsMemberIndex => x.copy(comments = x.comments ++ newComments)
case x: TsMemberTypeMapped => x.copy(comments = x.comments ++ newComments)
case x: TsMemberProperty => x.copy(comments = x.comments ++ newComments)
}
}

0 comments on commit e345d28

Please sign in to comment.