Skip to content

Commit 56c271f

Browse files
committed
[javascript mode] Stop treating TS contextual keywords as regular keywords
Closes #5133
1 parent d9f05c9 commit 56c271f

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

mode/javascript/javascript.js

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
2626
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"), D = kw("keyword d");
2727
var operator = kw("operator"), atom = {type: "atom", style: "atom"};
2828

29-
var jsKeywords = {
29+
return {
3030
"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
3131
"return": D, "break": D, "continue": D, "new": kw("new"), "delete": C, "void": C, "throw": C,
3232
"debugger": kw("debugger"), "var": kw("var"), "const": kw("var"), "let": kw("var"),
@@ -38,33 +38,6 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
3838
"yield": C, "export": kw("export"), "import": kw("import"), "extends": C,
3939
"await": C
4040
};
41-
42-
// Extend the 'normal' keywords with the TypeScript language extensions
43-
if (isTS) {
44-
var type = {type: "variable", style: "type"};
45-
var tsKeywords = {
46-
// object-like things
47-
"interface": kw("class"),
48-
"implements": C,
49-
"namespace": C,
50-
51-
// scope modifiers
52-
"public": kw("modifier"),
53-
"private": kw("modifier"),
54-
"protected": kw("modifier"),
55-
"abstract": kw("modifier"),
56-
"readonly": kw("modifier"),
57-
58-
// types
59-
"string": type, "number": type, "boolean": type, "any": type
60-
};
61-
62-
for (var attr in tsKeywords) {
63-
jsKeywords[attr] = tsKeywords[attr];
64-
}
65-
}
66-
67-
return jsKeywords;
6841
}();
6942

7043
var isOperatorChar = /[+\-*&%=<>!?|~^@]/;
@@ -310,6 +283,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
310283
}
311284
}
312285

286+
function isModifier(name) {
287+
return name == "public" || name == "private" || name == "protected" || name == "abstract" || name == "readonly"
288+
}
289+
313290
// Combinators
314291

315292
var defaultVars = {name: "this", next: {name: "arguments"}};
@@ -366,6 +343,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
366343
}
367344
if (type == "function") return cont(functiondef);
368345
if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
346+
if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), className, poplex); }
369347
if (type == "variable") {
370348
if (isTS && value == "type") {
371349
cx.marked = "keyword"
@@ -376,6 +354,9 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
376354
} else if (isTS && (value == "module" || value == "enum") && cx.stream.match(/^\s*\w/, false)) {
377355
cx.marked = "keyword"
378356
return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
357+
} else if (isTS && value == "namespace") {
358+
cx.marked = "keyword"
359+
return cont(pushlex("form"), expression, block, poplex)
379360
} else {
380361
return cont(pushlex("stat"), maybelabel);
381362
}
@@ -386,24 +367,23 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
386367
if (type == "default") return cont(expect(":"));
387368
if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
388369
statement, poplex, popcontext);
389-
if (type == "class") return cont(pushlex("form"), className, poplex);
390370
if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
391371
if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
392372
if (type == "async") return cont(statement)
393373
if (value == "@") return cont(expression, statement)
394374
return pass(pushlex("stat"), expression, expect(";"), poplex);
395375
}
396-
function expression(type) {
397-
return expressionInner(type, false);
376+
function expression(type, value) {
377+
return expressionInner(type, value, false);
398378
}
399-
function expressionNoComma(type) {
400-
return expressionInner(type, true);
379+
function expressionNoComma(type, value) {
380+
return expressionInner(type, value, true);
401381
}
402382
function parenExpr(type) {
403383
if (type != "(") return pass()
404384
return cont(pushlex(")"), expression, expect(")"), poplex)
405385
}
406-
function expressionInner(type, noComma) {
386+
function expressionInner(type, value, noComma) {
407387
if (cx.state.fatArrowAt == cx.stream.start) {
408388
var body = noComma ? arrowBodyNoComma : arrowBody;
409389
if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
@@ -413,7 +393,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
413393
var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
414394
if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
415395
if (type == "function") return cont(functiondef, maybeop);
416-
if (type == "class") return cont(pushlex("form"), classExpression, poplex);
396+
if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); }
417397
if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
418398
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
419399
if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
@@ -511,10 +491,11 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
511491
return cont(afterprop);
512492
} else if (type == "jsonld-keyword") {
513493
return cont(afterprop);
514-
} else if (type == "modifier") {
494+
} else if (isTS && isModifier(value)) {
495+
cx.marked = "keyword"
515496
return cont(objprop)
516497
} else if (type == "[") {
517-
return cont(expression, expect("]"), afterprop);
498+
return cont(expression, maybetype, expect("]"), afterprop);
518499
} else if (type == "spread") {
519500
return cont(expressionNoComma, afterprop);
520501
} else if (value == "*") {
@@ -616,7 +597,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
616597
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
617598
if (value == "|" || type == ".") return cont(typeexpr)
618599
if (type == "[") return cont(expect("]"), afterType)
619-
if (value == "extends") return cont(typeexpr)
600+
if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) }
620601
}
621602
function maybeTypeArgs(_, value) {
622603
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
@@ -631,7 +612,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
631612
return pass(pattern, maybetype, maybeAssign, vardefCont);
632613
}
633614
function pattern(type, value) {
634-
if (type == "modifier") return cont(pattern)
615+
if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) }
635616
if (type == "variable") { register(value); return cont(); }
636617
if (type == "spread") return cont(pattern);
637618
if (type == "[") return contCommasep(pattern, "]");
@@ -685,7 +666,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
685666
}
686667
function funarg(type, value) {
687668
if (value == "@") cont(expression, funarg)
688-
if (type == "spread" || type == "modifier") return cont(funarg);
669+
if (type == "spread") return cont(funarg);
670+
if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); }
689671
return pass(pattern, maybetype, maybeAssign);
690672
}
691673
function classExpression(type, value) {
@@ -703,9 +685,9 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
703685
if (type == "{") return cont(pushlex("}"), classBody, poplex);
704686
}
705687
function classBody(type, value) {
706-
if (type == "modifier" || type == "async" ||
688+
if (type == "async" ||
707689
(type == "variable" &&
708-
(value == "static" || value == "get" || value == "set") &&
690+
(value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
709691
cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
710692
cx.marked = "keyword";
711693
return cont(classBody);
@@ -715,7 +697,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
715697
return cont(isTS ? classfield : functiondef, classBody);
716698
}
717699
if (type == "[")
718-
return cont(expression, expect("]"), isTS ? classfield : functiondef, classBody)
700+
return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody)
719701
if (value == "*") {
720702
cx.marked = "keyword";
721703
return cont(classBody);

0 commit comments

Comments
 (0)