Skip to content

Commit f18e396

Browse files
committed
[javascript mode] Handle parenthesized typescript types
Closes codemirror#5332
1 parent 7a813dd commit f18e396

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

mode/javascript/javascript.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
572572
if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
573573
if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
574574
if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
575+
if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr)
575576
}
576577
function maybeReturnType(type) {
577578
if (type == "=>") return cont(typeexpr)
@@ -588,9 +589,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
588589
return cont(expression, maybetype, expect("]"), typeprop)
589590
}
590591
}
591-
function typearg(type) {
592-
if (type == "variable") return cont(typearg)
593-
else if (type == ":") return cont(typeexpr)
592+
function typearg(type, value) {
593+
if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg)
594+
if (type == ":") return cont(typeexpr)
595+
return pass(typeexpr)
594596
}
595597
function afterType(type, value) {
596598
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)

mode/javascript/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@
402402
" [def META],",
403403
"}")
404404

405+
TS("parenthesized type",
406+
"[keyword class] [def Foo] {",
407+
" [property x] [operator =] [keyword new] [variable A][operator <][type B], [type string][operator |](() [operator =>] [type void])[operator >]();",
408+
" [keyword private] [property bar]();",
409+
"}")
410+
405411
var jsonld_mode = CodeMirror.getMode(
406412
{indentUnit: 2},
407413
{name: "javascript", jsonld: true}

0 commit comments

Comments
 (0)