|
@@ -1,5 +1,5 @@
|
1
|
1
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
2
|
|
-// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
2
|
+// Distributed under an MIT license: https://codemirror.net/LICENSE
|
3
|
3
|
|
4
|
4
|
(function(mod) {
|
5
|
5
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
@@ -11,11 +11,6 @@
|
11
|
11
|
})(function(CodeMirror) {
|
12
|
12
|
"use strict";
|
13
|
13
|
|
14
|
|
-function expressionAllowed(stream, state, backUp) {
|
15
|
|
- return /^(?:operator|sof|keyword c|case|new|export|default|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
|
16
|
|
- (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
|
17
|
|
-}
|
18
|
|
-
|
19
|
14
|
CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
20
|
15
|
var indentUnit = config.indentUnit;
|
21
|
16
|
var statementIndent = parserConfig.statementIndent;
|
|
@@ -28,53 +23,21 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
28
|
23
|
|
29
|
24
|
var keywords = function(){
|
30
|
25
|
function kw(type) {return {type: type, style: "keyword"};}
|
31
|
|
- var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
|
|
26
|
+ var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"), D = kw("keyword d");
|
32
|
27
|
var operator = kw("operator"), atom = {type: "atom", style: "atom"};
|
33
|
28
|
|
34
|
|
- var jsKeywords = {
|
|
29
|
+ return {
|
35
|
30
|
"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
|
36
|
|
- "return": C, "break": C, "continue": C, "new": kw("new"), "delete": C, "throw": C, "debugger": C,
|
37
|
|
- "var": kw("var"), "const": kw("var"), "let": kw("var"),
|
|
31
|
+ "return": D, "break": D, "continue": D, "new": kw("new"), "delete": C, "void": C, "throw": C,
|
|
32
|
+ "debugger": kw("debugger"), "var": kw("var"), "const": kw("var"), "let": kw("var"),
|
38
|
33
|
"function": kw("function"), "catch": kw("catch"),
|
39
|
34
|
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
|
40
|
35
|
"in": operator, "typeof": operator, "instanceof": operator,
|
41
|
36
|
"true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
|
42
|
37
|
"this": kw("this"), "class": kw("class"), "super": kw("atom"),
|
43
|
38
|
"yield": C, "export": kw("export"), "import": kw("import"), "extends": C,
|
44
|
|
- "await": C, "async": kw("async")
|
|
39
|
+ "await": C
|
45
|
40
|
};
|
46
|
|
-
|
47
|
|
- // Extend the 'normal' keywords with the TypeScript language extensions
|
48
|
|
- if (isTS) {
|
49
|
|
- var type = {type: "variable", style: "variable-3"};
|
50
|
|
- var tsKeywords = {
|
51
|
|
- // object-like things
|
52
|
|
- "interface": kw("class"),
|
53
|
|
- "implements": C,
|
54
|
|
- "namespace": C,
|
55
|
|
- "module": kw("module"),
|
56
|
|
- "enum": kw("module"),
|
57
|
|
- "type": kw("type"),
|
58
|
|
-
|
59
|
|
- // scope modifiers
|
60
|
|
- "public": kw("modifier"),
|
61
|
|
- "private": kw("modifier"),
|
62
|
|
- "protected": kw("modifier"),
|
63
|
|
- "abstract": kw("modifier"),
|
64
|
|
-
|
65
|
|
- // operators
|
66
|
|
- "as": operator,
|
67
|
|
-
|
68
|
|
- // types
|
69
|
|
- "string": type, "number": type, "boolean": type, "any": type
|
70
|
|
- };
|
71
|
|
-
|
72
|
|
- for (var attr in tsKeywords) {
|
73
|
|
- jsKeywords[attr] = tsKeywords[attr];
|
74
|
|
- }
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- return jsKeywords;
|
78
|
41
|
}();
|
79
|
42
|
|
80
|
43
|
var isOperatorChar = /[+\-*&%=<>!?|~^@]/;
|
|
@@ -112,17 +75,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
112
|
75
|
return ret(ch);
|
113
|
76
|
} else if (ch == "=" && stream.eat(">")) {
|
114
|
77
|
return ret("=>", "operator");
|
115
|
|
- } else if (ch == "0" && stream.eat(/x/i)) {
|
116
|
|
- stream.eatWhile(/[\da-f]/i);
|
117
|
|
- return ret("number", "number");
|
118
|
|
- } else if (ch == "0" && stream.eat(/o/i)) {
|
119
|
|
- stream.eatWhile(/[0-7]/i);
|
120
|
|
- return ret("number", "number");
|
121
|
|
- } else if (ch == "0" && stream.eat(/b/i)) {
|
122
|
|
- stream.eatWhile(/[01]/i);
|
|
78
|
+ } else if (ch == "0" && stream.match(/^(?:x[\da-f]+|o[0-7]+|b[01]+)n?/i)) {
|
123
|
79
|
return ret("number", "number");
|
124
|
80
|
} else if (/\d/.test(ch)) {
|
125
|
|
- stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
|
|
81
|
+ stream.match(/^\d*(?:n|(?:\.\d*)?(?:[eE][+\-]?\d+)?)?/);
|
126
|
82
|
return ret("number", "number");
|
127
|
83
|
} else if (ch == "/") {
|
128
|
84
|
if (stream.eat("*")) {
|
|
@@ -133,10 +89,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
133
|
89
|
return ret("comment", "comment");
|
134
|
90
|
} else if (expressionAllowed(stream, state, 1)) {
|
135
|
91
|
readRegexp(stream);
|
136
|
|
- stream.match(/^\b(([gimyu])(?![gimyu]*\2))+\b/);
|
|
92
|
+ stream.match(/^\b(([gimyus])(?![gimyus]*\2))+\b/);
|
137
|
93
|
return ret("regexp", "string-2");
|
138
|
94
|
} else {
|
139
|
|
- stream.eatWhile(isOperatorChar);
|
|
95
|
+ stream.eat("=");
|
140
|
96
|
return ret("operator", "operator", stream.current());
|
141
|
97
|
}
|
142
|
98
|
} else if (ch == "`") {
|
|
@@ -146,14 +102,27 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
146
|
102
|
stream.skipToEnd();
|
147
|
103
|
return ret("error", "error");
|
148
|
104
|
} else if (isOperatorChar.test(ch)) {
|
149
|
|
- if (ch != ">" || !state.lexical || state.lexical.type != ">")
|
150
|
|
- stream.eatWhile(isOperatorChar);
|
|
105
|
+ if (ch != ">" || !state.lexical || state.lexical.type != ">") {
|
|
106
|
+ if (stream.eat("=")) {
|
|
107
|
+ if (ch == "!" || ch == "=") stream.eat("=")
|
|
108
|
+ } else if (/[<>*+\-]/.test(ch)) {
|
|
109
|
+ stream.eat(ch)
|
|
110
|
+ if (ch == ">") stream.eat(ch)
|
|
111
|
+ }
|
|
112
|
+ }
|
151
|
113
|
return ret("operator", "operator", stream.current());
|
152
|
114
|
} else if (wordRE.test(ch)) {
|
153
|
115
|
stream.eatWhile(wordRE);
|
154
|
|
- var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
|
155
|
|
- return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
|
156
|
|
- ret("variable", "variable", word);
|
|
116
|
+ var word = stream.current()
|
|
117
|
+ if (state.lastType != ".") {
|
|
118
|
+ if (keywords.propertyIsEnumerable(word)) {
|
|
119
|
+ var kw = keywords[word]
|
|
120
|
+ return ret(kw.type, kw.style, word)
|
|
121
|
+ }
|
|
122
|
+ if (word == "async" && stream.match(/^(\s|\/\*.*?\*\/)*[\[\(\w]/, false))
|
|
123
|
+ return ret("async", "keyword", word)
|
|
124
|
+ }
|
|
125
|
+ return ret("variable", "variable", word)
|
157
|
126
|
}
|
158
|
127
|
}
|
159
|
128
|
|
|
@@ -289,35 +258,68 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
289
|
258
|
pass.apply(null, arguments);
|
290
|
259
|
return true;
|
291
|
260
|
}
|
|
261
|
+ function inList(name, list) {
|
|
262
|
+ for (var v = list; v; v = v.next) if (v.name == name) return true
|
|
263
|
+ return false;
|
|
264
|
+ }
|
292
|
265
|
function register(varname) {
|
293
|
|
- function inList(list) {
|
294
|
|
- for (var v = list; v; v = v.next)
|
295
|
|
- if (v.name == varname) return true;
|
296
|
|
- return false;
|
297
|
|
- }
|
298
|
266
|
var state = cx.state;
|
299
|
267
|
cx.marked = "def";
|
300
|
268
|
if (state.context) {
|
301
|
|
- if (inList(state.localVars)) return;
|
302
|
|
- state.localVars = {name: varname, next: state.localVars};
|
|
269
|
+ if (state.lexical.info == "var" && state.context && state.context.block) {
|
|
270
|
+ // FIXME function decls are also not block scoped
|
|
271
|
+ var newContext = registerVarScoped(varname, state.context)
|
|
272
|
+ if (newContext != null) {
|
|
273
|
+ state.context = newContext
|
|
274
|
+ return
|
|
275
|
+ }
|
|
276
|
+ } else if (!inList(varname, state.localVars)) {
|
|
277
|
+ state.localVars = new Var(varname, state.localVars)
|
|
278
|
+ return
|
|
279
|
+ }
|
|
280
|
+ }
|
|
281
|
+ // Fall through means this is global
|
|
282
|
+ if (parserConfig.globalVars && !inList(varname, state.globalVars))
|
|
283
|
+ state.globalVars = new Var(varname, state.globalVars)
|
|
284
|
+ }
|
|
285
|
+ function registerVarScoped(varname, context) {
|
|
286
|
+ if (!context) {
|
|
287
|
+ return null
|
|
288
|
+ } else if (context.block) {
|
|
289
|
+ var inner = registerVarScoped(varname, context.prev)
|
|
290
|
+ if (!inner) return null
|
|
291
|
+ if (inner == context.prev) return context
|
|
292
|
+ return new Context(inner, context.vars, true)
|
|
293
|
+ } else if (inList(varname, context.vars)) {
|
|
294
|
+ return context
|
303
|
295
|
} else {
|
304
|
|
- if (inList(state.globalVars)) return;
|
305
|
|
- if (parserConfig.globalVars)
|
306
|
|
- state.globalVars = {name: varname, next: state.globalVars};
|
|
296
|
+ return new Context(context.prev, new Var(varname, context.vars), false)
|
307
|
297
|
}
|
308
|
298
|
}
|
309
|
299
|
|
|
300
|
+ function isModifier(name) {
|
|
301
|
+ return name == "public" || name == "private" || name == "protected" || name == "abstract" || name == "readonly"
|
|
302
|
+ }
|
|
303
|
+
|
310
|
304
|
// Combinators
|
311
|
305
|
|
312
|
|
- var defaultVars = {name: "this", next: {name: "arguments"}};
|
|
306
|
+ function Context(prev, vars, block) { this.prev = prev; this.vars = vars; this.block = block }
|
|
307
|
+ function Var(name, next) { this.name = name; this.next = next }
|
|
308
|
+
|
|
309
|
+ var defaultVars = new Var("this", new Var("arguments", null))
|
313
|
310
|
function pushcontext() {
|
314
|
|
- cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
|
315
|
|
- cx.state.localVars = defaultVars;
|
|
311
|
+ cx.state.context = new Context(cx.state.context, cx.state.localVars, false)
|
|
312
|
+ cx.state.localVars = defaultVars
|
|
313
|
+ }
|
|
314
|
+ function pushblockcontext() {
|
|
315
|
+ cx.state.context = new Context(cx.state.context, cx.state.localVars, true)
|
|
316
|
+ cx.state.localVars = null
|
316
|
317
|
}
|
317
|
318
|
function popcontext() {
|
318
|
|
- cx.state.localVars = cx.state.context.vars;
|
319
|
|
- cx.state.context = cx.state.context.prev;
|
|
319
|
+ cx.state.localVars = cx.state.context.vars
|
|
320
|
+ cx.state.context = cx.state.context.prev
|
320
|
321
|
}
|
|
322
|
+ popcontext.lex = true
|
321
|
323
|
function pushlex(type, info) {
|
322
|
324
|
var result = function() {
|
323
|
325
|
var state = cx.state, indent = state.indented;
|
|
@@ -342,17 +344,19 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
342
|
344
|
function expect(wanted) {
|
343
|
345
|
function exp(type) {
|
344
|
346
|
if (type == wanted) return cont();
|
345
|
|
- else if (wanted == ";") return pass();
|
|
347
|
+ else if (wanted == ";" || type == "}" || type == ")" || type == "]") return pass();
|
346
|
348
|
else return cont(exp);
|
347
|
349
|
};
|
348
|
350
|
return exp;
|
349
|
351
|
}
|
350
|
352
|
|
351
|
353
|
function statement(type, value) {
|
352
|
|
- if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
|
|
354
|
+ if (type == "var") return cont(pushlex("vardef", value), vardef, expect(";"), poplex);
|
353
|
355
|
if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
|
354
|
356
|
if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
|
355
|
|
- if (type == "{") return cont(pushlex("}"), block, poplex);
|
|
357
|
+ if (type == "keyword d") return cx.stream.match(/^\s*$/, false) ? cont() : cont(pushlex("stat"), maybeexpression, expect(";"), poplex);
|
|
358
|
+ if (type == "debugger") return cont(expect(";"));
|
|
359
|
+ if (type == "{") return cont(pushlex("}"), pushblockcontext, block, poplex, popcontext);
|
356
|
360
|
if (type == ";") return cont();
|
357
|
361
|
if (type == "if") {
|
358
|
362
|
if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)
|
|
@@ -361,60 +365,75 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
361
|
365
|
}
|
362
|
366
|
if (type == "function") return cont(functiondef);
|
363
|
367
|
if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
|
364
|
|
- if (type == "variable") return cont(pushlex("stat"), maybelabel);
|
365
|
|
- if (type == "switch") return cont(pushlex("form"), parenExpr, pushlex("}", "switch"), expect("{"),
|
366
|
|
- block, poplex, poplex);
|
|
368
|
+ if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), className, poplex); }
|
|
369
|
+ if (type == "variable") {
|
|
370
|
+ if (isTS && value == "declare") {
|
|
371
|
+ cx.marked = "keyword"
|
|
372
|
+ return cont(statement)
|
|
373
|
+ } else if (isTS && (value == "module" || value == "enum" || value == "type") && cx.stream.match(/^\s*\w/, false)) {
|
|
374
|
+ cx.marked = "keyword"
|
|
375
|
+ if (value == "enum") return cont(enumdef);
|
|
376
|
+ else if (value == "type") return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
|
|
377
|
+ else return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
|
|
378
|
+ } else if (isTS && value == "namespace") {
|
|
379
|
+ cx.marked = "keyword"
|
|
380
|
+ return cont(pushlex("form"), expression, block, poplex)
|
|
381
|
+ } else if (isTS && value == "abstract") {
|
|
382
|
+ cx.marked = "keyword"
|
|
383
|
+ return cont(statement)
|
|
384
|
+ } else {
|
|
385
|
+ return cont(pushlex("stat"), maybelabel);
|
|
386
|
+ }
|
|
387
|
+ }
|
|
388
|
+ if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"), pushblockcontext,
|
|
389
|
+ block, poplex, poplex, popcontext);
|
367
|
390
|
if (type == "case") return cont(expression, expect(":"));
|
368
|
391
|
if (type == "default") return cont(expect(":"));
|
369
|
|
- if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
|
370
|
|
- statement, poplex, popcontext);
|
371
|
|
- if (type == "class") return cont(pushlex("form"), className, poplex);
|
|
392
|
+ if (type == "catch") return cont(pushlex("form"), pushcontext, maybeCatchBinding, statement, poplex, popcontext);
|
372
|
393
|
if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
|
373
|
394
|
if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
|
374
|
|
- if (type == "module") return cont(pushlex("form"), pattern, pushlex("}"), expect("{"), block, poplex, poplex)
|
375
|
|
- if (type == "type") return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
|
376
|
395
|
if (type == "async") return cont(statement)
|
377
|
396
|
if (value == "@") return cont(expression, statement)
|
378
|
397
|
return pass(pushlex("stat"), expression, expect(";"), poplex);
|
379
|
398
|
}
|
380
|
|
- function expression(type) {
|
381
|
|
- return expressionInner(type, false);
|
|
399
|
+ function maybeCatchBinding(type) {
|
|
400
|
+ if (type == "(") return cont(funarg, expect(")"))
|
|
401
|
+ }
|
|
402
|
+ function expression(type, value) {
|
|
403
|
+ return expressionInner(type, value, false);
|
382
|
404
|
}
|
383
|
|
- function expressionNoComma(type) {
|
384
|
|
- return expressionInner(type, true);
|
|
405
|
+ function expressionNoComma(type, value) {
|
|
406
|
+ return expressionInner(type, value, true);
|
385
|
407
|
}
|
386
|
408
|
function parenExpr(type) {
|
387
|
409
|
if (type != "(") return pass()
|
388
|
410
|
return cont(pushlex(")"), expression, expect(")"), poplex)
|
389
|
411
|
}
|
390
|
|
- function expressionInner(type, noComma) {
|
|
412
|
+ function expressionInner(type, value, noComma) {
|
391
|
413
|
if (cx.state.fatArrowAt == cx.stream.start) {
|
392
|
414
|
var body = noComma ? arrowBodyNoComma : arrowBody;
|
393
|
|
- if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);
|
|
415
|
+ if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
|
394
|
416
|
else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
|
395
|
417
|
}
|
396
|
418
|
|
397
|
419
|
var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
|
398
|
420
|
if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
|
399
|
421
|
if (type == "function") return cont(functiondef, maybeop);
|
400
|
|
- if (type == "class") return cont(pushlex("form"), classExpression, poplex);
|
401
|
|
- if (type == "keyword c" || type == "async") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
|
|
422
|
+ if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); }
|
|
423
|
+ if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
|
402
|
424
|
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
|
403
|
425
|
if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
|
404
|
426
|
if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
|
405
|
427
|
if (type == "{") return contCommasep(objprop, "}", null, maybeop);
|
406
|
428
|
if (type == "quasi") return pass(quasi, maybeop);
|
407
|
429
|
if (type == "new") return cont(maybeTarget(noComma));
|
|
430
|
+ if (type == "import") return cont(expression);
|
408
|
431
|
return cont();
|
409
|
432
|
}
|
410
|
433
|
function maybeexpression(type) {
|
411
|
434
|
if (type.match(/[;\}\)\],]/)) return pass();
|
412
|
435
|
return pass(expression);
|
413
|
436
|
}
|
414
|
|
- function maybeexpressionNoComma(type) {
|
415
|
|
- if (type.match(/[;\}\)\],]/)) return pass();
|
416
|
|
- return pass(expressionNoComma);
|
417
|
|
- }
|
418
|
437
|
|
419
|
438
|
function maybeoperatorComma(type, value) {
|
420
|
439
|
if (type == ",") return cont(expression);
|
|
@@ -425,7 +444,9 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
425
|
444
|
var expr = noComma == false ? expression : expressionNoComma;
|
426
|
445
|
if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
|
427
|
446
|
if (type == "operator") {
|
428
|
|
- if (/\+\+|--/.test(value)) return cont(me);
|
|
447
|
+ if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me);
|
|
448
|
+ if (isTS && value == "<" && cx.stream.match(/^([^>]|<.*?>)*>\s*\(/, false))
|
|
449
|
+ return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, me);
|
429
|
450
|
if (value == "?") return cont(expression, expect(":"), expr);
|
430
|
451
|
return cont(expr);
|
431
|
452
|
}
|
|
@@ -434,6 +455,12 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
434
|
455
|
if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
|
435
|
456
|
if (type == ".") return cont(property, me);
|
436
|
457
|
if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
|
|
458
|
+ if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) }
|
|
459
|
+ if (type == "regexp") {
|
|
460
|
+ cx.state.lastType = cx.marked = "operator"
|
|
461
|
+ cx.stream.backUp(cx.stream.pos - cx.stream.start - 1)
|
|
462
|
+ return cont(expr)
|
|
463
|
+ }
|
437
|
464
|
}
|
438
|
465
|
function quasi(type, value) {
|
439
|
466
|
if (type != "quasi") return pass();
|
|
@@ -458,6 +485,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
458
|
485
|
function maybeTarget(noComma) {
|
459
|
486
|
return function(type) {
|
460
|
487
|
if (type == ".") return cont(noComma ? targetNoComma : target);
|
|
488
|
+ else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma)
|
461
|
489
|
else return pass(noComma ? expressionNoComma : expression);
|
462
|
490
|
};
|
463
|
491
|
}
|
|
@@ -481,18 +509,25 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
481
|
509
|
} else if (type == "variable" || cx.style == "keyword") {
|
482
|
510
|
cx.marked = "property";
|
483
|
511
|
if (value == "get" || value == "set") return cont(getterSetter);
|
|
512
|
+ var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params
|
|
513
|
+ if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false)))
|
|
514
|
+ cx.state.fatArrowAt = cx.stream.pos + m[0].length
|
484
|
515
|
return cont(afterprop);
|
485
|
516
|
} else if (type == "number" || type == "string") {
|
486
|
517
|
cx.marked = jsonldMode ? "property" : (cx.style + " property");
|
487
|
518
|
return cont(afterprop);
|
488
|
519
|
} else if (type == "jsonld-keyword") {
|
489
|
520
|
return cont(afterprop);
|
490
|
|
- } else if (type == "modifier") {
|
|
521
|
+ } else if (isTS && isModifier(value)) {
|
|
522
|
+ cx.marked = "keyword"
|
491
|
523
|
return cont(objprop)
|
492
|
524
|
} else if (type == "[") {
|
493
|
|
- return cont(expression, expect("]"), afterprop);
|
|
525
|
+ return cont(expression, maybetype, expect("]"), afterprop);
|
494
|
526
|
} else if (type == "spread") {
|
495
|
|
- return cont(expression);
|
|
527
|
+ return cont(expressionNoComma, afterprop);
|
|
528
|
+ } else if (value == "*") {
|
|
529
|
+ cx.marked = "keyword";
|
|
530
|
+ return cont(objprop);
|
496
|
531
|
} else if (type == ":") {
|
497
|
532
|
return pass(afterprop)
|
498
|
533
|
}
|
|
@@ -539,11 +574,32 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
539
|
574
|
if (value == "?") return cont(maybetype);
|
540
|
575
|
}
|
541
|
576
|
}
|
542
|
|
- function typeexpr(type) {
|
543
|
|
- if (type == "variable") {cx.marked = "variable-3"; return cont(afterType);}
|
|
577
|
+ function mayberettype(type) {
|
|
578
|
+ if (isTS && type == ":") {
|
|
579
|
+ if (cx.stream.match(/^\s*\w+\s+is\b/, false)) return cont(expression, isKW, typeexpr)
|
|
580
|
+ else return cont(typeexpr)
|
|
581
|
+ }
|
|
582
|
+ }
|
|
583
|
+ function isKW(_, value) {
|
|
584
|
+ if (value == "is") {
|
|
585
|
+ cx.marked = "keyword"
|
|
586
|
+ return cont()
|
|
587
|
+ }
|
|
588
|
+ }
|
|
589
|
+ function typeexpr(type, value) {
|
|
590
|
+ if (value == "keyof" || value == "typeof") {
|
|
591
|
+ cx.marked = "keyword"
|
|
592
|
+ return cont(value == "keyof" ? typeexpr : expressionNoComma)
|
|
593
|
+ }
|
|
594
|
+ if (type == "variable" || value == "void") {
|
|
595
|
+ cx.marked = "type"
|
|
596
|
+ return cont(afterType)
|
|
597
|
+ }
|
544
|
598
|
if (type == "string" || type == "number" || type == "atom") return cont(afterType);
|
545
|
|
- if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex)
|
|
599
|
+ if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
|
|
600
|
+ if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
|
546
|
601
|
if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
|
|
602
|
+ if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr)
|
547
|
603
|
}
|
548
|
604
|
function maybeReturnType(type) {
|
549
|
605
|
if (type == "=>") return cont(typeexpr)
|
|
@@ -556,25 +612,39 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
556
|
612
|
return cont(typeprop)
|
557
|
613
|
} else if (type == ":") {
|
558
|
614
|
return cont(typeexpr)
|
|
615
|
+ } else if (type == "[") {
|
|
616
|
+ return cont(expression, maybetype, expect("]"), typeprop)
|
559
|
617
|
}
|
560
|
618
|
}
|
561
|
|
- function typearg(type) {
|
562
|
|
- if (type == "variable") return cont(typearg)
|
563
|
|
- else if (type == ":") return cont(typeexpr)
|
|
619
|
+ function typearg(type, value) {
|
|
620
|
+ if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg)
|
|
621
|
+ if (type == ":") return cont(typeexpr)
|
|
622
|
+ return pass(typeexpr)
|
564
|
623
|
}
|
565
|
624
|
function afterType(type, value) {
|
566
|
625
|
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
|
567
|
|
- if (value == "|" || type == ".") return cont(typeexpr)
|
|
626
|
+ if (value == "|" || type == "." || value == "&") return cont(typeexpr)
|
568
|
627
|
if (type == "[") return cont(expect("]"), afterType)
|
|
628
|
+ if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) }
|
|
629
|
+ }
|
|
630
|
+ function maybeTypeArgs(_, value) {
|
|
631
|
+ if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
|
569
|
632
|
}
|
570
|
|
- function vardef() {
|
|
633
|
+ function typeparam() {
|
|
634
|
+ return pass(typeexpr, maybeTypeDefault)
|
|
635
|
+ }
|
|
636
|
+ function maybeTypeDefault(_, value) {
|
|
637
|
+ if (value == "=") return cont(typeexpr)
|
|
638
|
+ }
|
|
639
|
+ function vardef(_, value) {
|
|
640
|
+ if (value == "enum") {cx.marked = "keyword"; return cont(enumdef)}
|
571
|
641
|
return pass(pattern, maybetype, maybeAssign, vardefCont);
|
572
|
642
|
}
|
573
|
643
|
function pattern(type, value) {
|
574
|
|
- if (type == "modifier") return cont(pattern)
|
|
644
|
+ if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) }
|
575
|
645
|
if (type == "variable") { register(value); return cont(); }
|
576
|
646
|
if (type == "spread") return cont(pattern);
|
577
|
|
- if (type == "[") return contCommasep(pattern, "]");
|
|
647
|
+ if (type == "[") return contCommasep(eltpattern, "]");
|
578
|
648
|
if (type == "{") return contCommasep(proppattern, "}");
|
579
|
649
|
}
|
580
|
650
|
function proppattern(type, value) {
|
|
@@ -587,6 +657,9 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
587
|
657
|
if (type == "}") return pass();
|
588
|
658
|
return cont(expect(":"), pattern, maybeAssign);
|
589
|
659
|
}
|
|
660
|
+ function eltpattern() {
|
|
661
|
+ return pass(pattern, maybeAssign)
|
|
662
|
+ }
|
590
|
663
|
function maybeAssign(_type, value) {
|
591
|
664
|
if (value == "=") return cont(expressionNoComma);
|
592
|
665
|
}
|
|
@@ -596,7 +669,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
596
|
669
|
function maybeelse(type, value) {
|
597
|
670
|
if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
|
598
|
671
|
}
|
599
|
|
- function forspec(type) {
|
|
672
|
+ function forspec(type, value) {
|
|
673
|
+ if (value == "await") return cont(forspec);
|
600
|
674
|
if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
|
601
|
675
|
}
|
602
|
676
|
function forspec1(type) {
|
|
@@ -620,11 +694,13 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
620
|
694
|
function functiondef(type, value) {
|
621
|
695
|
if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
|
622
|
696
|
if (type == "variable") {register(value); return cont(functiondef);}
|
623
|
|
- if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, maybetype, statement, popcontext);
|
624
|
|
- if (isTS && value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, functiondef)
|
|
697
|
+ if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext);
|
|
698
|
+ if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef)
|
625
|
699
|
}
|
626
|
|
- function funarg(type) {
|
|
700
|
+ function funarg(type, value) {
|
|
701
|
+ if (value == "@") cont(expression, funarg)
|
627
|
702
|
if (type == "spread") return cont(funarg);
|
|
703
|
+ if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); }
|
628
|
704
|
return pass(pattern, maybetype, maybeAssign);
|
629
|
705
|
}
|
630
|
706
|
function classExpression(type, value) {
|
|
@@ -636,24 +712,27 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
636
|
712
|
if (type == "variable") {register(value); return cont(classNameAfter);}
|
637
|
713
|
}
|
638
|
714
|
function classNameAfter(type, value) {
|
639
|
|
- if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, classNameAfter)
|
640
|
|
- if (value == "extends" || value == "implements" || (isTS && type == ","))
|
|
715
|
+ if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter)
|
|
716
|
+ if (value == "extends" || value == "implements" || (isTS && type == ",")) {
|
|
717
|
+ if (value == "implements") cx.marked = "keyword";
|
641
|
718
|
return cont(isTS ? typeexpr : expression, classNameAfter);
|
|
719
|
+ }
|
642
|
720
|
if (type == "{") return cont(pushlex("}"), classBody, poplex);
|
643
|
721
|
}
|
644
|
722
|
function classBody(type, value) {
|
|
723
|
+ if (type == "async" ||
|
|
724
|
+ (type == "variable" &&
|
|
725
|
+ (value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
|
|
726
|
+ cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
|
|
727
|
+ cx.marked = "keyword";
|
|
728
|
+ return cont(classBody);
|
|
729
|
+ }
|
645
|
730
|
if (type == "variable" || cx.style == "keyword") {
|
646
|
|
- if ((value == "async" || value == "static" || value == "get" || value == "set" ||
|
647
|
|
- (isTS && (value == "public" || value == "private" || value == "protected" || value == "readonly" || value == "abstract"))) &&
|
648
|
|
- cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false)) {
|
649
|
|
- cx.marked = "keyword";
|
650
|
|
- return cont(classBody);
|
651
|
|
- }
|
652
|
731
|
cx.marked = "property";
|
653
|
732
|
return cont(isTS ? classfield : functiondef, classBody);
|
654
|
733
|
}
|
655
|
734
|
if (type == "[")
|
656
|
|
- return cont(expression, expect("]"), isTS ? classfield : functiondef, classBody)
|
|
735
|
+ return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody)
|
657
|
736
|
if (value == "*") {
|
658
|
737
|
cx.marked = "keyword";
|
659
|
738
|
return cont(classBody);
|
|
@@ -680,6 +759,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
680
|
759
|
}
|
681
|
760
|
function afterImport(type) {
|
682
|
761
|
if (type == "string") return cont();
|
|
762
|
+ if (type == "(") return pass(expression);
|
683
|
763
|
return pass(importSpec, maybeMoreImports, maybeFrom);
|
684
|
764
|
}
|
685
|
765
|
function importSpec(type, value) {
|
|
@@ -701,6 +781,12 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
701
|
781
|
if (type == "]") return cont();
|
702
|
782
|
return pass(commasep(expressionNoComma, "]"));
|
703
|
783
|
}
|
|
784
|
+ function enumdef() {
|
|
785
|
+ return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex)
|
|
786
|
+ }
|
|
787
|
+ function enummember() {
|
|
788
|
+ return pass(pattern, maybeAssign);
|
|
789
|
+ }
|
704
|
790
|
|
705
|
791
|
function isContinuedStatement(state, textAfter) {
|
706
|
792
|
return state.lastType == "operator" || state.lastType == "," ||
|
|
@@ -708,6 +794,12 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
708
|
794
|
/[,.]/.test(textAfter.charAt(0));
|
709
|
795
|
}
|
710
|
796
|
|
|
797
|
+ function expressionAllowed(stream, state, backUp) {
|
|
798
|
+ return state.tokenize == tokenBase &&
|
|
799
|
+ /^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
|
|
800
|
+ (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
|
|
801
|
+ }
|
|
802
|
+
|
711
|
803
|
// Interface
|
712
|
804
|
|
713
|
805
|
return {
|
|
@@ -718,7 +810,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
718
|
810
|
cc: [],
|
719
|
811
|
lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
|
720
|
812
|
localVars: parserConfig.localVars,
|
721
|
|
- context: parserConfig.localVars && {vars: parserConfig.localVars},
|
|
813
|
+ context: parserConfig.localVars && new Context(null, null, false),
|
722
|
814
|
indented: basecolumn || 0
|
723
|
815
|
};
|
724
|
816
|
if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
|
|
@@ -759,7 +851,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
759
|
851
|
lexical = lexical.prev;
|
760
|
852
|
var type = lexical.type, closing = firstChar == type;
|
761
|
853
|
|
762
|
|
- if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
|
|
854
|
+ if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0);
|
763
|
855
|
else if (type == "form" && firstChar == "{") return lexical.indented;
|
764
|
856
|
else if (type == "form") return lexical.indented + indentUnit;
|
765
|
857
|
else if (type == "stat")
|
|
@@ -773,6 +865,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
773
|
865
|
electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
|
774
|
866
|
blockCommentStart: jsonMode ? null : "/*",
|
775
|
867
|
blockCommentEnd: jsonMode ? null : "*/",
|
|
868
|
+ blockCommentContinue: jsonMode ? null : " * ",
|
776
|
869
|
lineComment: jsonMode ? null : "//",
|
777
|
870
|
fold: "brace",
|
778
|
871
|
closeBrackets: "()[]{}''\"\"``",
|
|
@@ -782,6 +875,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
782
|
875
|
jsonMode: jsonMode,
|
783
|
876
|
|
784
|
877
|
expressionAllowed: expressionAllowed,
|
|
878
|
+
|
785
|
879
|
skipExpression: function(state) {
|
786
|
880
|
var top = state.cc[state.cc.length - 1]
|
787
|
881
|
if (top == expression || top == expressionNoComma) state.cc.pop()
|