Skip to content

Commit

Permalink
parser: support type assertions for non-typeref
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Sep 29, 2022
1 parent ad2b976 commit 4c38c4a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ final case class TsTypeConstructor(isAbstract: Boolean, signature: TsTypeFunctio

final case class TsTypeIs(ident: TsIdent, tpe: TsType) extends TsType

final case class TsTypeAsserts(ident: TsIdentSimple, isOpt: Option[TsTypeRef]) extends TsType
final case class TsTypeAsserts(ident: TsIdentSimple, isOpt: Option[TsType]) extends TsType

final case class TsTupleElement(label: Option[TsIdent], tpe: TsType)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3083,4 +3083,21 @@ export {};
),
)
}

test("asserts type is not typeref") {
val a = TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("a"))), IArray())
val repeatedA = TsTypeRepeated(
TsTypeRef(
NoComments,
TsQIdent(IArray(TsIdentSimple("Array"))),
IArray(a),
),
)
shouldParseAs("asserts value is [a, ...a[]]", TsParser.tsType)(
TsTypeAsserts(
TsIdentSimple("value"),
Some(TsTypeTuple(IArray(TsTupleElement(None, a), TsTupleElement(None, repeatedA)))),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ trait TreeTransformation[T] { self =>
val xx = enterTsTypeAsserts(withTree(t, x))(x)
val tt = withTree(t, xx)
xx match {
case TsTypeAsserts(_1, _2) => TsTypeAsserts(_1, _2.map(visitTsTypeRef(tt)))
case TsTypeAsserts(_1, _2) => TsTypeAsserts(_1, _2.map(visitTsType(tt)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class TsParser(path: Option[(os.Path, Int)]) extends StdTokenParsers with Parser
| "(" ~> tsType <~ ")"
| tsLiteral ^^ TsTypeLiteral
| "this" ~> success(TsTypeThis())
| "asserts" ~> tsIdent ~ ("is" ~> tsTypeRef).? ^^ TsTypeAsserts
| "asserts" ~> tsIdent ~ ("is" ~> tsType).? ^^ TsTypeAsserts
| tsTypeKeyOf
| "infer" ~> typeParam ^^ TsTypeInfer
| "readonly" ~> tsType
Expand Down

0 comments on commit 4c38c4a

Please sign in to comment.