Skip to content

Commit

Permalink
Be more tolerant with missing dependencies (inferred or from directiv…
Browse files Browse the repository at this point in the history
…es) and warn instead of failing conversion
  • Loading branch information
oyvindberg committed Apr 30, 2021
1 parent ba921f8 commit dd3dd10
Showing 1 changed file with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ class Phase1ReadTypescript(
case s @ Source.TsHelperFile(file, inLib, _) =>
val L = logger.withContext(file)

val resolveDep = (value: String) =>
PhaseRes.fromOption(source, resolve.module(s, value).map(_._1: Source), Right(s"Couldn't resolve $value"))

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

def orWarn(ot: Option[Source])(onEmpty: String): PhaseRes[Source, Source] =
ot match {
case Some(value) => PhaseRes.Ok(value)
case None =>
L.warn(onEmpty)
PhaseRes.Ignore()
}

PhaseRes.fromEither(source, parser(file)).flatMap { parsed: TsParsedFile =>
val (
pathRefsR: Set[PhaseRes[Source, Source]],
Expand All @@ -84,13 +89,16 @@ class Phase1ReadTypescript(
case r @ Directive.PathRef(value) =>
val maybeSource: Option[Source] =
LibraryResolver.file(file.folder, value).map(Source.helperFile(inLib))
PhaseRes.fromOption(source, maybeSource, Right(s"Couldn't resolve $r"))
},
{ case Directive.TypesRef(value) => resolveDep(value) }, {
orWarn(maybeSource)(s"Couldn't resolve $r")
}, {
case Directive.TypesRef(value) =>
val maybeSource: Option[Source] = resolve.module(s, value).map(_._1)
orWarn(maybeSource)(s"Couldn't resolve $value")
}, {
case r @ Directive.LibRef(value) if inLib.libName === TsIdent.std =>
val maybeSource: Option[Source] =
LibraryResolver.file(resolve.stdLib.folder, s"lib.$value.d.ts").map(Source.helperFile(inLib))
PhaseRes.fromOption(source, maybeSource, Right(s"Couldn't resolve $r"))
orWarn(maybeSource)(s"Couldn't resolve $r")
},
)

Expand All @@ -114,7 +122,9 @@ class Phase1ReadTypescript(
withExternals.unresolvedDeps,
L,
)
inferredDeps <- PhaseRes.sequenceSet(inferredDepNames.map(n => resolveDep(n.value)))
inferredDeps <- PhaseRes.sequenceSet(
inferredDepNames.map(n => orWarn(resolve.module(s, n.value).map(_._1))(s"Couldn't resolve ${n.value}")),
)

/* look up all resulting dependencies */
deps <- getDeps((withExternals.resolvedDeps ++ typeReferencedDeps ++ inferredDeps).sorted)
Expand Down

0 comments on commit dd3dd10

Please sign in to comment.