Skip to content

Commit

Permalink
parser: understand +readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Sep 29, 2022
1 parent aad3cd9 commit b47f557
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<O> = {
| +readonly [K in keyof O]: O[K] extends BuiltIn ? O[K] : ReadonlyDeep<O[K]>;
|}
|""".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,
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ object TsLexer extends Lexical with StdTokens with ParserHelpers with ImplicitCo
"{", "}", "(", ")", "[", "]", "<", ">",
".", ";", ",", "?", ":", "=", "|", "&", "*", "+", "-", "^", "/", "%",
// TypeScript-specific
"...", "=>", "-?", "+?", "-readonly",
"...", "=>", "-?", "+?", "-readonly", "+readonly",
)
// format: on
delimiters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit b47f557

Please sign in to comment.