From b47f557fcad3536b0b82c4676ebb7c3daf6a9426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Raddum=20Berg?= Date: Thu, 29 Sep 2022 11:44:06 +0200 Subject: [PATCH] parser: understand +readonly --- .../internal/ts/parser/ParserTests.scala | 41 +++++++++++++++++++ .../internal/ts/parser/TsLexer.scala | 2 +- .../internal/ts/parser/TsParser.scala | 8 ++-- 3 files changed, 46 insertions(+), 5 deletions(-) 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 493b681218..41c38ae360 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 @@ -3109,4 +3109,45 @@ export {}; TsTypeLiteral(TsLiteral.Str("${Head}.${FixPathSquareBrackets<[${Middle}]${Tail}>}")), ) } + test("flaff") { + val O = TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("O"))), IArray()) + val K = TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("K"))), IArray()) + shouldParseAs( + """declare type ReadonlyDeep = { + | +readonly [K in keyof O]: O[K] extends BuiltIn ? O[K] : ReadonlyDeep; + |} + |""".stripMargin, + TsParser.tsDeclTypeAlias, + )( + TsDeclTypeAlias( + NoComments, + true, + TsIdentSimple("ReadonlyDeep"), + IArray(TsTypeParam(NoComments, TsIdentSimple("O"), None, None)), + TsTypeObject( + NoComments, + IArray( + TsMemberTypeMapped( + NoComments, + TsProtectionLevel.Default, + ReadonlyModifier.Yes, + TsIdentSimple("K"), + TsTypeKeyOf(O), + None, + Noop, + TsTypeConditional( + TsTypeExtends( + TsTypeLookup(O, K), + TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("BuiltIn"))), IArray()), + ), + TsTypeLookup(O, K), + TsTypeRef(NoComments, TsQIdent(IArray(TsIdentSimple("ReadonlyDeep"))), IArray(TsTypeLookup(O, K))), + ), + ), + ), + ), + CodePath.NoPath, + ), + ) + } } diff --git a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsLexer.scala b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsLexer.scala index 9aa7a267e3..7d5e23a5ae 100644 --- a/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsLexer.scala +++ b/ts/src/main/scala/org/scalablytyped/converter/internal/ts/parser/TsLexer.scala @@ -148,7 +148,7 @@ object TsLexer extends Lexical with StdTokens with ParserHelpers with ImplicitCo "{", "}", "(", ")", "[", "]", "<", ">", ".", ";", ",", "?", ":", "=", "|", "&", "*", "+", "-", "^", "/", "%", // TypeScript-specific - "...", "=>", "-?", "+?", "-readonly", + "...", "=>", "-?", "+?", "-readonly", "+readonly", ) // format: on delimiters 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 d9973da620..b89fc46124 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 @@ -583,10 +583,10 @@ class TsParser(path: Option[(os.Path, Int)]) extends StdTokenParsers with Parser } val readonly: Parser[ReadonlyModifier] = { - ("readonly" | "-readonly").? ^^ { - case Some("readonly") => ReadonlyModifier.Yes - case Some("-readonly") => ReadonlyModifier.No - case _ => ReadonlyModifier.Noop + ("readonly" | "-readonly" | "+readonly").? ^^ { + case Some("readonly" | "+readonly") => ReadonlyModifier.Yes + case Some("-readonly") => ReadonlyModifier.No + case _ => ReadonlyModifier.Noop } }