| Index: third_party/WebKit/Source/devtools/front_end/cm/javascript.js
|
| diff --git a/third_party/WebKit/Source/devtools/front_end/cm/javascript.js b/third_party/WebKit/Source/devtools/front_end/cm/javascript.js
|
| index 67f4448d030c63b162d48540739c713f0314da38..a717745897415d1cc465d8c9382dce0a620a00b2 100644
|
| --- a/third_party/WebKit/Source/devtools/front_end/cm/javascript.js
|
| +++ b/third_party/WebKit/Source/devtools/front_end/cm/javascript.js
|
| @@ -1,8 +1,6 @@
|
| // CodeMirror, copyright (c) by Marijn Haverbeke and others
|
| // Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
| -// TODO actually recognize syntax of TypeScript constructs
|
| -
|
| (function(mod) {
|
| if (typeof exports == "object" && typeof module == "object") // CommonJS
|
| mod(require("../../lib/codemirror"));
|
| @@ -56,6 +54,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| "namespace": C,
|
| "module": kw("module"),
|
| "enum": kw("module"),
|
| + "type": kw("type"),
|
|
|
| // scope modifiers
|
| "public": kw("modifier"),
|
| @@ -210,13 +209,18 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| var arrow = stream.string.indexOf("=>", stream.start);
|
| if (arrow < 0) return;
|
|
|
| + if (isTS) { // Try to skip TypeScript return type declarations after the arguments
|
| + var m = /:\s*(?:\w+(?:<[^>]*>|\[\])?|\{[^}]*\})\s*$/.exec(stream.string.slice(stream.start, arrow))
|
| + if (m) arrow = m.index
|
| + }
|
| +
|
| var depth = 0, sawSomething = false;
|
| for (var pos = arrow - 1; pos >= 0; --pos) {
|
| var ch = stream.string.charAt(pos);
|
| var bracket = brackets.indexOf(ch);
|
| if (bracket >= 0 && bracket < 3) {
|
| if (!depth) { ++pos; break; }
|
| - if (--depth == 0) break;
|
| + if (--depth == 0) { if (ch == "(") sawSomething = true; break; }
|
| } else if (bracket >= 3 && bracket < 6) {
|
| ++depth;
|
| } else if (wordRE.test(ch)) {
|
| @@ -345,19 +349,19 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
|
|
| function statement(type, value) {
|
| if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
|
| - if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
|
| + if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
|
| if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
|
| if (type == "{") return cont(pushlex("}"), block, poplex);
|
| if (type == ";") return cont();
|
| if (type == "if") {
|
| if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)
|
| cx.state.cc.pop()();
|
| - return cont(pushlex("form"), expression, statement, poplex, maybeelse);
|
| + return cont(pushlex("form"), parenExpr, statement, poplex, maybeelse);
|
| }
|
| if (type == "function") return cont(functiondef);
|
| if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
|
| if (type == "variable") return cont(pushlex("stat"), maybelabel);
|
| - if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
|
| + if (type == "switch") return cont(pushlex("form"), parenExpr, pushlex("}", "switch"), expect("{"),
|
| block, poplex, poplex);
|
| if (type == "case") return cont(expression, expect(":"));
|
| if (type == "default") return cont(expect(":"));
|
| @@ -367,6 +371,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
|
| if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
|
| if (type == "module") return cont(pushlex("form"), pattern, pushlex("}"), expect("{"), block, poplex, poplex)
|
| + if (type == "type") return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
|
| if (type == "async") return cont(statement)
|
| return pass(pushlex("stat"), expression, expect(";"), poplex);
|
| }
|
| @@ -376,6 +381,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| function expressionNoComma(type) {
|
| return expressionInner(type, true);
|
| }
|
| + function parenExpr(type) {
|
| + if (type != "(") return pass()
|
| + return cont(pushlex(")"), expression, expect(")"), poplex)
|
| + }
|
| function expressionInner(type, noComma) {
|
| if (cx.state.fatArrowAt == cx.stream.start) {
|
| var body = noComma ? arrowBodyNoComma : arrowBody;
|
| @@ -386,6 +395,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
|
| if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
|
| if (type == "function") return cont(functiondef, maybeop);
|
| + if (type == "class") return cont(pushlex("form"), classExpression, poplex);
|
| if (type == "keyword c" || type == "async") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
|
| if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
|
| if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
|
| @@ -463,8 +473,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| if (type == "variable") {cx.marked = "property"; return cont();}
|
| }
|
| function objprop(type, value) {
|
| - if (type == "async") return cont(objprop);
|
| - if (type == "variable" || cx.style == "keyword") {
|
| + if (type == "async") {
|
| + cx.marked = "property";
|
| + return cont(objprop);
|
| + } else if (type == "variable" || cx.style == "keyword") {
|
| cx.marked = "property";
|
| if (value == "get" || value == "set") return cont(getterSetter);
|
| return cont(afterprop);
|
| @@ -479,6 +491,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| return cont(expression, expect("]"), afterprop);
|
| } else if (type == "spread") {
|
| return cont(expression);
|
| + } else if (type == ":") {
|
| + return pass(afterprop)
|
| }
|
| }
|
| function getterSetter(type) {
|
| @@ -517,14 +531,31 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| if (type == "}") return cont();
|
| return pass(statement, block);
|
| }
|
| - function maybetype(type) {
|
| - if (isTS && type == ":") return cont(typeexpr);
|
| - }
|
| - function maybedefault(_, value) {
|
| - if (value == "=") return cont(expressionNoComma);
|
| + function maybetype(type, value) {
|
| + if (isTS) {
|
| + if (type == ":") return cont(typeexpr);
|
| + if (value == "?") return cont(maybetype);
|
| + }
|
| }
|
| function typeexpr(type) {
|
| if (type == "variable") {cx.marked = "variable-3"; return cont(afterType);}
|
| + if (type == "{") return cont(commasep(typeprop, "}"))
|
| + if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
|
| + }
|
| + function maybeReturnType(type) {
|
| + if (type == "=>") return cont(typeexpr)
|
| + }
|
| + function typeprop(type) {
|
| + if (type == "variable" || cx.style == "keyword") {
|
| + cx.marked = "property"
|
| + return cont(typeprop)
|
| + } else if (type == ":") {
|
| + return cont(typeexpr)
|
| + }
|
| + }
|
| + function typearg(type) {
|
| + if (type == "variable") return cont(typearg)
|
| + else if (type == ":") return cont(typeexpr)
|
| }
|
| function afterType(type, value) {
|
| if (value == "<") return cont(commasep(typeexpr, ">"), afterType)
|
| @@ -587,24 +618,30 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| }
|
| function funarg(type) {
|
| if (type == "spread") return cont(funarg);
|
| - return pass(pattern, maybetype, maybedefault);
|
| + return pass(pattern, maybetype, maybeAssign);
|
| + }
|
| + function classExpression(type, value) {
|
| + // Class expressions may have an optional name.
|
| + if (type == "variable") return className(type, value);
|
| + return classNameAfter(type, value);
|
| }
|
| function className(type, value) {
|
| if (type == "variable") {register(value); return cont(classNameAfter);}
|
| }
|
| function classNameAfter(type, value) {
|
| - if (value == "extends") return cont(expression, classNameAfter);
|
| + if (value == "extends" || value == "implements") return cont(isTS ? typeexpr : expression, classNameAfter);
|
| if (type == "{") return cont(pushlex("}"), classBody, poplex);
|
| }
|
| function classBody(type, value) {
|
| if (type == "variable" || cx.style == "keyword") {
|
| - if (value == "static") {
|
| + if ((value == "static" || value == "get" || value == "set" ||
|
| + (isTS && (value == "public" || value == "private" || value == "protected" || value == "readonly" || value == "abstract"))) &&
|
| + cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false)) {
|
| cx.marked = "keyword";
|
| return cont(classBody);
|
| }
|
| cx.marked = "property";
|
| - if (value == "get" || value == "set") return cont(classGetterSetter, functiondef, classBody);
|
| - return cont(functiondef, classBody);
|
| + return cont(isTS ? classfield : functiondef, classBody);
|
| }
|
| if (value == "*") {
|
| cx.marked = "keyword";
|
| @@ -613,10 +650,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| if (type == ";") return cont(classBody);
|
| if (type == "}") return cont();
|
| }
|
| - function classGetterSetter(type) {
|
| - if (type != "variable") return pass();
|
| - cx.marked = "property";
|
| - return cont();
|
| + function classfield(type, value) {
|
| + if (value == "?") return cont(classfield)
|
| + if (type == ":") return cont(typeexpr, maybeAssign)
|
| + return pass(functiondef)
|
| }
|
| function afterExport(_type, value) {
|
| if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
|
| @@ -641,7 +678,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| }
|
| function arrayLiteral(type) {
|
| if (type == "]") return cont();
|
| - return pass(expressionNoComma, commasep(expressionNoComma, "]"));
|
| + return pass(commasep(expressionNoComma, "]"));
|
| }
|
|
|
| function isContinuedStatement(state, textAfter) {
|
| @@ -685,14 +722,18 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
| indent: function(state, textAfter) {
|
| if (state.tokenize == tokenComment) return CodeMirror.Pass;
|
| if (state.tokenize != tokenBase) return 0;
|
| - var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
|
| + var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top
|
| // Kludge to prevent 'maybelse' from blocking lexical scope pops
|
| if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
|
| var c = state.cc[i];
|
| if (c == poplex) lexical = lexical.prev;
|
| else if (c != maybeelse) break;
|
| }
|
| - if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
|
| + while ((lexical.type == "stat" || lexical.type == "form") &&
|
| + (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) &&
|
| + (top == maybeoperatorComma || top == maybeoperatorNoComma) &&
|
| + !/^[,\.=+\-*:?[\(]/.test(textAfter))))
|
| + lexical = lexical.prev;
|
| if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
|
| lexical = lexical.prev;
|
| var type = lexical.type, closing = firstChar == type;
|
| @@ -740,4 +781,4 @@ CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true})
|
| CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
|
| CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
|
|
|
| -});
|
| +});
|
|
|