From 4c38c4aac418acc6a1d61605322266e75334400a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Raddum=20Berg?= Date: Thu, 29 Sep 2022 00:39:37 +0200 Subject: [PATCH] parser: support type assertions for non-typeref --- .../converter/internal/ts/trees.scala | 2 +- .../internal/ts/parser/ParserTests.scala | 17 +++++++++++++++++ .../internal/ts/TreeTransformation.scala | 2 +- .../converter/internal/ts/parser/TsParser.scala | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala b/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala index 0c23b766e1..34dad3d67a 100644 --- a/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala +++ b/core/src/main/scala/org/scalablytyped/converter/internal/ts/trees.scala @@ -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) diff --git a/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala b/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala index a8bd2a811d..a127f65468 100644 --- a/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala +++ b/importer/src/test/scala/org/scalablytyped/converter/internal/ts/parser/ParserTests.scala @@ -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)))), + ), + ) + } } diff --git a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/TreeTransformation.scala b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/TreeTransformation.scala index 725a1d480f..a0826d0a35 100644 --- a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/TreeTransformation.scala +++ b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/TreeTransformation.scala @@ -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))) } } diff --git a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala index eecc0aa07f..7639dc4b39 100644 --- a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala +++ b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsParser.scala @@ -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