javascript.js 23 KB

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