Skip to content

Commit

Permalink
FollowAliases: Recover from more circular things by catching `StackOv…
Browse files Browse the repository at this point in the history
…erflowError`
  • Loading branch information
oyvindberg committed Oct 13, 2023
1 parent 51dc956 commit ae10c35
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package org.scalablytyped.converter.internal
package ts

object FollowAliases {
def apply(scope: TsTreeScope, skipValidation: Boolean = false)(tpe: TsType): TsType = {
tpe match {
def apply(scope: TsTreeScope, skipValidation: Boolean = false)(tpe: TsType): TsType =
try tpe match {
case typeRef: TsTypeRef =>
scope
.lookupType(typeRef.name, skipValidation)
.collectFirst {
case ta: TsDeclTypeAlias =>
val next = FillInTParams(ta, typeRef.tparams).alias
// this is a consequence of interface augmentation. see for instance `Locals` in express
if (next == tpe) TsTypeRef.any.copy(comments = Comments(Comment.warning(s"circular ${TsTypeFormatter(tpe)}")))
else apply(scope, skipValidation)(next)
apply(scope, skipValidation)(next)
/* see through thin interfaces which might be translated into type aliases */
case i @ TsDeclInterface(_, _, _, _, IArray.exactlyOne(_), Empty, _) =>
apply(scope, skipValidation)(FillInTParams(i, typeRef.tparams).inheritance.head)
Expand All @@ -23,8 +21,12 @@ object FollowAliases {
case TsTypeUnion(types) =>
TsTypeUnion.simplified(types.map(FollowAliases(scope, skipValidation)))
case other => other
} catch {
case _: StackOverflowError =>
val formatted = TsTypeFormatter(tpe)
scope.logger.error(s"Recovered from SOE while following type alias $formatted")
TsTypeRef.any.copy(comments = Comments(Comment.warning(s"circular $formatted")))
}
}

def typeRef(scope: TsTreeScope)(tr: TsTypeRef): TsTypeRef =
scope
Expand Down

0 comments on commit ae10c35

Please sign in to comment.