javascript.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // TODO actually recognize syntax of TypeScript constructs
  2. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  3. var indentUnit = config.indentUnit;
  4. var statementIndent = parserConfig.statementIndent;
  5. var jsonMode = parserConfig.json;
  6. var isTS = parserConfig.typescript;
  7. // Tokenizer
  8. var keywords = function(){
  9. function kw(type) {return {type: type, style: "keyword"};}
  10. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  11. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  12. var jsKeywords = {
  13. "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  14. "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
  15. "var": kw("var"), "const": kw("var"), "let": kw("var"),
  16. "function": kw("function"), "catch": kw("catch"),
  17. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  18. "in": operator, "typeof": operator, "instanceof": operator,
  19. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  20. "this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"),
  21. "yield": C, "export": kw("export"), "import": kw("import"), "extends": C
  22. };
  23. // Extend the 'normal' keywords with the TypeScript language extensions
  24. if (isTS) {
  25. var type = {type: "variable", style: "variable-3"};
  26. var tsKeywords = {
  27. // object-like things
  28. "interface": kw("interface"),
  29. "extends": kw("extends"),
  30. "constructor": kw("constructor"),
  31. // scope modifiers
  32. "public": kw("public"),
  33. "private": kw("private"),
  34. "protected": kw("protected"),
  35. "static": kw("static"),
  36. // types
  37. "string": type, "number": type, "bool": type, "any": type
  38. };
  39. for (var attr in tsKeywords) {
  40. jsKeywords[attr] = tsKeywords[attr];
  41. }
  42. }
  43. return jsKeywords;
  44. }();
  45. var isOperatorChar = /[+\-*&%=<>!?|~^]/;
  46. function nextUntilUnescaped(stream, end) {
  47. var escaped = false, next;
  48. while ((next = stream.next()) != null) {
  49. if (next == end && !escaped)
  50. return false;
  51. escaped = !escaped && next == "\\";
  52. }
  53. return escaped;
  54. }
  55. // Used as scratch variables to communicate multiple values without
  56. // consing up tons of objects.
  57. var type, content;
  58. function ret(tp, style, cont) {
  59. type = tp; content = cont;
  60. return style;
  61. }
  62. function tokenBase(stream, state) {
  63. var ch = stream.next();
  64. if (ch == '"' || ch == "'") {
  65. state.tokenize = tokenString(ch);
  66. return state.tokenize(stream, state);
  67. } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {
  68. return ret("number", "number");
  69. } else if (ch == "." && stream.match("..")) {
  70. return ret("spread", "meta");
  71. } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  72. return ret(ch);
  73. } else if (ch == "=" && stream.eat(">")) {
  74. return ret("=>");
  75. } else if (ch == "0" && stream.eat(/x/i)) {
  76. stream.eatWhile(/[\da-f]/i);
  77. return ret("number", "number");
  78. } else if (/\d/.test(ch)) {
  79. stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
  80. return ret("number", "number");
  81. } else if (ch == "/") {
  82. if (stream.eat("*")) {
  83. state.tokenize = tokenComment;
  84. return tokenComment(stream, state);
  85. } else if (stream.eat("/")) {
  86. stream.skipToEnd();
  87. return ret("comment", "comment");
  88. } else if (state.lastType == "operator" || state.lastType == "keyword c" ||
  89. state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {
  90. nextUntilUnescaped(stream, "/");
  91. stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
  92. return ret("regexp", "string-2");
  93. } else {
  94. stream.eatWhile(isOperatorChar);
  95. return ret("operator", null, stream.current());
  96. }
  97. } else if (ch == "`") {
  98. state.tokenize = tokenQuasi;
  99. return tokenQuasi(stream, state);
  100. } else if (ch == "#") {
  101. stream.skipToEnd();
  102. return ret("error", "error");
  103. } else if (isOperatorChar.test(ch)) {
  104. stream.eatWhile(isOperatorChar);
  105. return ret("operator", null, stream.current());
  106. } else {
  107. stream.eatWhile(/[\w\$_]/);
  108. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  109. return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
  110. ret("variable", "variable", word);
  111. }
  112. }
  113. function tokenString(quote) {
  114. return function(stream, state) {
  115. if (!nextUntilUnescaped(stream, quote))
  116. state.tokenize = tokenBase;
  117. return ret("string", "string");
  118. };
  119. }
  120. function tokenComment(stream, state) {
  121. var maybeEnd = false, ch;
  122. while (ch = stream.next()) {
  123. if (ch == "/" && maybeEnd) {
  124. state.tokenize = tokenBase;
  125. break;
  126. }
  127. maybeEnd = (ch == "*");
  128. }
  129. return ret("comment", "comment");
  130. }
  131. function tokenQuasi(stream, state) {
  132. var escaped = false, next;
  133. while ((next = stream.next()) != null) {
  134. if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {
  135. state.tokenize = tokenBase;
  136. break;
  137. }
  138. escaped = !escaped && next == "\\";
  139. }
  140. return ret("quasi", "string-2", stream.current());
  141. }
  142. var brackets = "([{}])";
  143. // This is a crude lookahead trick to try and notice that we're
  144. // parsing the argument patterns for a fat-arrow function before we
  145. // actually hit the arrow token. It only works if the arrow is on
  146. // the same line as the arguments and there's no strange noise
  147. // (comments) in between. Fallback is to only notice when we hit the
  148. // arrow, and not declare the arguments as locals for the arrow
  149. // body.
  150. function findFatArrow(stream, state) {
  151. if (state.fatArrowAt) state.fatArrowAt = null;
  152. var arrow = stream.string.indexOf("=>", stream.start);
  153. if (arrow < 0) return;
  154. var depth = 0, sawSomething = false;
  155. for (var pos = arrow - 1; pos >= 0; --pos) {
  156. var ch = stream.string.charAt(pos);
  157. var bracket = brackets.indexOf(ch);
  158. if (bracket >= 0 && bracket < 3) {
  159. if (!depth) { ++pos; break; }
  160. if (--depth == 0) break;
  161. } else if (bracket >= 3 && bracket < 6) {
  162. ++depth;
  163. } else if (/[$\w]/.test(ch)) {
  164. sawSomething = true;
  165. } else if (sawSomething && !depth) {
  166. ++pos;
  167. break;
  168. }
  169. }
  170. if (sawSomething && !depth) state.fatArrowAt = pos;
  171. }
  172. // Parser
  173. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};
  174. function JSLexical(indented, column, type, align, prev, info) {
  175. this.indented = indented;
  176. this.column = column;
  177. this.type = type;
  178. this.prev = prev;
  179. this.info = info;
  180. if (align != null) this.align = align;
  181. }
  182. function inScope(state, varname) {
  183. for (var v = state.localVars; v; v = v.next)
  184. if (v.name == varname) return true;
  185. for (var cx = state.context; cx; cx = cx.prev) {
  186. for (var v = cx.vars; v; v = v.next)
  187. if (v.name == varname) return true;
  188. }
  189. }
  190. function parseJS(state, style, type, content, stream) {
  191. var cc = state.cc;
  192. // Communicate our context to the combinators.
  193. // (Less wasteful than consing up a hundred closures on every call.)
  194. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  195. if (!state.lexical.hasOwnProperty("align"))
  196. state.lexical.align = true;
  197. while(true) {
  198. var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  199. if (combinator(type, content)) {
  200. while(cc.length && cc[cc.length - 1].lex)
  201. cc.pop()();
  202. if (cx.marked) return cx.marked;
  203. if (type == "variable" && inScope(state, content)) return "variable-2";
  204. return style;
  205. }
  206. }
  207. }
  208. // Combinator utils
  209. var cx = {state: null, column: null, marked: null, cc: null};
  210. function pass() {
  211. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  212. }
  213. function cont() {
  214. pass.apply(null, arguments);
  215. return true;
  216. }
  217. function register(varname) {
  218. function inList(list) {
  219. for (var v = list; v; v = v.next)
  220. if (v.name == varname) return true;
  221. return false;
  222. }
  223. var state = cx.state;
  224. if (state.context) {
  225. cx.marked = "def";
  226. if (inList(state.localVars)) return;
  227. state.localVars = {name: varname, next: state.localVars};
  228. } else {
  229. if (inList(state.globalVars)) return;
  230. if (parserConfig.globalVars)
  231. state.globalVars = {name: varname, next: state.globalVars};
  232. }
  233. }
  234. // Combinators
  235. var defaultVars = {name: "this", next: {name: "arguments"}};
  236. function pushcontext() {
  237. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  238. cx.state.localVars = defaultVars;
  239. }
  240. function popcontext() {
  241. cx.state.localVars = cx.state.context.vars;
  242. cx.state.context = cx.state.context.prev;
  243. }
  244. function pushlex(type, info) {
  245. var result = function() {
  246. var state = cx.state, indent = state.indented;
  247. if (state.lexical.type == "stat") indent = state.lexical.indented;
  248. state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
  249. };
  250. result.lex = true;
  251. return result;
  252. }
  253. function poplex() {
  254. var state = cx.state;
  255. if (state.lexical.prev) {
  256. if (state.lexical.type == ")")
  257. state.indented = state.lexical.indented;
  258. state.lexical = state.lexical.prev;
  259. }
  260. }
  261. poplex.lex = true;
  262. function expect(wanted) {
  263. return function(type) {
  264. if (type == wanted) return cont();
  265. else if (wanted == ";") return pass();
  266. else return cont(arguments.callee);
  267. };
  268. }
  269. function statement(type, value) {
  270. if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
  271. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  272. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  273. if (type == "{") return cont(pushlex("}"), block, poplex);
  274. if (type == ";") return cont();
  275. if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse);
  276. if (type == "function") return cont(functiondef);
  277. if (type == "for") return cont(pushlex("form"), forspec, poplex, statement, poplex);
  278. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  279. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  280. block, poplex, poplex);
  281. if (type == "case") return cont(expression, expect(":"));
  282. if (type == "default") return cont(expect(":"));
  283. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  284. statement, poplex, popcontext);
  285. if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex);
  286. if (type == "class") return cont(pushlex("form"), className, objlit, poplex);
  287. if (type == "export") return cont(pushlex("form"), afterExport, poplex);
  288. if (type == "import") return cont(pushlex("form"), afterImport, poplex);
  289. return pass(pushlex("stat"), expression, expect(";"), poplex);
  290. }
  291. function expression(type) {
  292. return expressionInner(type, false);
  293. }
  294. function expressionNoComma(type) {
  295. return expressionInner(type, true);
  296. }
  297. function expressionInner(type, noComma) {
  298. if (cx.state.fatArrowAt == cx.stream.start) {
  299. var body = noComma ? arrowBodyNoComma : arrowBody;
  300. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);
  301. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  302. }
  303. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  304. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  305. if (type == "function") return cont(functiondef);
  306. if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
  307. if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop);
  308. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  309. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  310. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  311. return cont();
  312. }
  313. function maybeexpression(type) {
  314. if (type.match(/[;\}\)\],]/)) return pass();
  315. return pass(expression);
  316. }
  317. function maybeexpressionNoComma(type) {
  318. if (type.match(/[;\}\)\],]/)) return pass();
  319. return pass(expressionNoComma);
  320. }
  321. function maybeoperatorComma(type, value) {
  322. if (type == ",") return cont(expression);
  323. return maybeoperatorNoComma(type, value, false);
  324. }
  325. function maybeoperatorNoComma(type, value, noComma) {
  326. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  327. var expr = noComma == false ? expression : expressionNoComma;
  328. if (value == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  329. if (type == "operator") {
  330. if (/\+\+|--/.test(value)) return cont(me);
  331. if (value == "?") return cont(expression, expect(":"), expr);
  332. return cont(expr);
  333. }
  334. if (type == "quasi") { cx.cc.push(me); return quasi(value); }
  335. if (type == ";") return;
  336. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  337. if (type == ".") return cont(property, me);
  338. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  339. }
  340. function quasi(value) {
  341. if (!value) debugger;
  342. if (value.slice(value.length - 2) != "${") return cont();
  343. return cont(expression, continueQuasi);
  344. }
  345. function continueQuasi(type) {
  346. if (type == "}") {
  347. cx.marked = "string-2";
  348. cx.state.tokenize = tokenQuasi;
  349. return cont();
  350. }
  351. }
  352. function arrowBody(type) {
  353. findFatArrow(cx.stream, cx.state);
  354. if (type == "{") return pass(statement);
  355. return pass(expression);
  356. }
  357. function arrowBodyNoComma(type) {
  358. findFatArrow(cx.stream, cx.state);
  359. if (type == "{") return pass(statement);
  360. return pass(expressionNoComma);
  361. }
  362. function maybelabel(type) {
  363. if (type == ":") return cont(poplex, statement);
  364. return pass(maybeoperatorComma, expect(";"), poplex);
  365. }
  366. function property(type) {
  367. if (type == "variable") {cx.marked = "property"; return cont();}
  368. }
  369. function objprop(type, value) {
  370. if (type == "variable") {
  371. cx.marked = "property";
  372. if (value == "get" || value == "set") return cont(getterSetter);
  373. } else if (type == "number" || type == "string") {
  374. cx.marked = type + " property";
  375. } else if (type == "[") {
  376. return cont(expression, expect("]"), afterprop);
  377. }
  378. if (atomicTypes.hasOwnProperty(type)) return cont(afterprop);
  379. }
  380. function getterSetter(type) {
  381. if (type != "variable") return pass(afterprop);
  382. cx.marked = "property";
  383. return cont(functiondef);
  384. }
  385. function afterprop(type) {
  386. if (type == ":") return cont(expressionNoComma);
  387. if (type == "(") return pass(functiondef);
  388. }
  389. function commasep(what, end) {
  390. function proceed(type) {
  391. if (type == ",") {
  392. var lex = cx.state.lexical;
  393. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  394. return cont(what, proceed);
  395. }
  396. if (type == end) return cont();
  397. return cont(expect(end));
  398. }
  399. return function(type) {
  400. if (type == end) return cont();
  401. return pass(what, proceed);
  402. };
  403. }
  404. function contCommasep(what, end, info) {
  405. for (var i = 3; i < arguments.length; i++)
  406. cx.cc.push(arguments[i]);
  407. return cont(pushlex(end, info), commasep(what, end), poplex);
  408. }
  409. function block(type) {
  410. if (type == "}") return cont();
  411. return pass(statement, block);
  412. }
  413. function maybetype(type) {
  414. if (isTS && type == ":") return cont(typedef);
  415. }
  416. function typedef(type) {
  417. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  418. }
  419. function vardef() {
  420. return pass(pattern, maybetype, maybeAssign, vardefCont);
  421. }
  422. function pattern(type, value) {
  423. if (type == "variable") { register(value); return cont(); }
  424. if (type == "[") return contCommasep(pattern, "]");
  425. if (type == "{") return contCommasep(proppattern, "}");
  426. }
  427. function proppattern(type, value) {
  428. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  429. register(value);
  430. return cont(maybeAssign);
  431. }
  432. if (type == "variable") cx.marked = "property";
  433. return cont(expect(":"), pattern, maybeAssign);
  434. }
  435. function maybeAssign(_type, value) {
  436. if (value == "=") return cont(expressionNoComma);
  437. }
  438. function vardefCont(type) {
  439. if (type == ",") return cont(vardef);
  440. }
  441. function maybeelse(type, value) {
  442. if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex);
  443. }
  444. function forspec(type) {
  445. if (type == "(") return cont(pushlex(")"), forspec1, expect(")"));
  446. }
  447. function forspec1(type) {
  448. if (type == "var") return cont(vardef, expect(";"), forspec2);
  449. if (type == ";") return cont(forspec2);
  450. if (type == "variable") return cont(formaybeinof);
  451. return pass(expression, expect(";"), forspec2);
  452. }
  453. function formaybeinof(_type, value) {
  454. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  455. return cont(maybeoperatorComma, forspec2);
  456. }
  457. function forspec2(type, value) {
  458. if (type == ";") return cont(forspec3);
  459. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  460. return pass(expression, expect(";"), forspec3);
  461. }
  462. function forspec3(type) {
  463. if (type != ")") cont(expression);
  464. }
  465. function functiondef(type, value) {
  466. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  467. if (type == "variable") {register(value); return cont(functiondef);}
  468. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);
  469. }
  470. function funarg(type) {
  471. if (type == "spread") return cont(funarg);
  472. return pass(pattern, maybetype);
  473. }
  474. function className(type, value) {
  475. if (type == "variable") {register(value); return cont(classNameAfter);}
  476. }
  477. function classNameAfter(_type, value) {
  478. if (value == "extends") return cont(expression);
  479. }
  480. function objlit(type) {
  481. if (type == "{") return contCommasep(objprop, "}");
  482. }
  483. function afterModule(type, value) {
  484. if (type == "string") return cont(statement);
  485. if (type == "variable") { register(value); return cont(maybeFrom); }
  486. }
  487. function afterExport(_type, value) {
  488. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  489. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  490. return pass(statement);
  491. }
  492. function afterImport(type) {
  493. if (type == "string") return cont();
  494. return pass(importSpec, maybeFrom);
  495. }
  496. function importSpec(type, value) {
  497. if (type == "{") return contCommasep(importSpec, "}");
  498. if (type == "variable") register(value);
  499. return cont();
  500. }
  501. function maybeFrom(_type, value) {
  502. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  503. }
  504. function arrayLiteral(type) {
  505. if (type == "]") return cont();
  506. return pass(expressionNoComma, maybeArrayComprehension);
  507. }
  508. function maybeArrayComprehension(type) {
  509. if (type == "for") return pass(comprehension);
  510. if (type == ",") return cont(commasep(expressionNoComma, "]"));
  511. return pass(commasep(expressionNoComma, "]"));
  512. }
  513. function comprehension(type) {
  514. if (type == "for") return cont(forspec, comprehension);
  515. if (type == "if") return cont(expression, comprehension);
  516. }
  517. // Interface
  518. return {
  519. startState: function(basecolumn) {
  520. var state = {
  521. tokenize: tokenBase,
  522. lastType: "sof",
  523. cc: [],
  524. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  525. localVars: parserConfig.localVars,
  526. context: parserConfig.localVars && {vars: parserConfig.localVars},
  527. indented: 0
  528. };
  529. if (parserConfig.globalVars) state.globalVars = parserConfig.globalVars;
  530. return state;
  531. },
  532. token: function(stream, state) {
  533. if (stream.sol()) {
  534. if (!state.lexical.hasOwnProperty("align"))
  535. state.lexical.align = false;
  536. state.indented = stream.indentation();
  537. findFatArrow(stream, state);
  538. }
  539. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  540. var style = state.tokenize(stream, state);
  541. if (type == "comment") return style;
  542. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  543. return parseJS(state, style, type, content, stream);
  544. },
  545. indent: function(state, textAfter) {
  546. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  547. if (state.tokenize != tokenBase) return 0;
  548. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  549. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  550. for (var i = state.cc.length - 1; i >= 0; --i) {
  551. var c = state.cc[i];
  552. if (c == poplex) lexical = lexical.prev;
  553. else if (c != maybeelse) break;
  554. }
  555. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  556. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  557. lexical = lexical.prev;
  558. var type = lexical.type, closing = firstChar == type;
  559. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
  560. else if (type == "form" && firstChar == "{") return lexical.indented;
  561. else if (type == "form") return lexical.indented + indentUnit;
  562. else if (type == "stat")
  563. return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0);
  564. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  565. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  566. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  567. else return lexical.indented + (closing ? 0 : indentUnit);
  568. },
  569. electricChars: ":{}",
  570. blockCommentStart: jsonMode ? null : "/*",
  571. blockCommentEnd: jsonMode ? null : "*/",
  572. lineComment: jsonMode ? null : "//",
  573. fold: "brace",
  574. helperType: jsonMode ? "json" : "javascript",
  575. jsonMode: jsonMode
  576. };
  577. });
  578. CodeMirror.defineMIME("text/javascript", "javascript");
  579. CodeMirror.defineMIME("text/ecmascript", "javascript");
  580. CodeMirror.defineMIME("application/javascript", "javascript");
  581. CodeMirror.defineMIME("application/ecmascript", "javascript");
  582. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  583. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  584. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  585. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });