expression.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. "use strict";
  2. var _create = require("babel-runtime/core-js/object/create");
  3. var _create2 = _interopRequireDefault(_create);
  4. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  5. var _getIterator3 = _interopRequireDefault(_getIterator2);
  6. var _types = require("../tokenizer/types");
  7. var _index = require("./index");
  8. var _index2 = _interopRequireDefault(_index);
  9. var _identifier = require("../util/identifier");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var pp = _index2.default.prototype;
  12. // Check if property name clashes with already added.
  13. // Object/class getters and setters are not allowed to clash —
  14. // either with each other or with an init property — and in
  15. // strict mode, init properties are also not allowed to be repeated.
  16. /* eslint indent: 0 */
  17. /* eslint max-len: 0 */
  18. // A recursive descent parser operates by defining functions for all
  19. // syntactic elements, and recursively calling those, each function
  20. // advancing the input stream and returning an AST node. Precedence
  21. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  22. // instead of `(!x)[1]` is handled by the fact that the parser
  23. // function that parses unary prefix operators is called first, and
  24. // in turn calls the function that parses `[]` subscripts — that
  25. // way, it'll receive the node for `x[1]` already parsed, and wraps
  26. // *that* in the unary operator node.
  27. //
  28. // Acorn uses an [operator precedence parser][opp] to handle binary
  29. // operator precedence, because it is much more compact than using
  30. // the technique outlined above, which uses different, nesting
  31. // functions to specify precedence, for all of the ten binary
  32. // precedence levels that JavaScript defines.
  33. //
  34. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  35. pp.checkPropClash = function (prop, propHash) {
  36. if (prop.computed) return;
  37. var key = prop.key;
  38. var name = void 0;
  39. switch (key.type) {
  40. case "Identifier":
  41. name = key.name;
  42. break;
  43. case "StringLiteral":
  44. case "NumericLiteral":
  45. name = String(key.value);
  46. break;
  47. default:
  48. return;
  49. }
  50. if (name === "__proto__" && prop.kind === "init") {
  51. if (propHash.proto) this.raise(key.start, "Redefinition of __proto__ property");
  52. propHash.proto = true;
  53. }
  54. };
  55. // ### Expression parsing
  56. // These nest, from the most general expression type at the top to
  57. // 'atomic', nondivisible expression types at the bottom. Most of
  58. // the functions will simply let the function (s) below them parse,
  59. // and, *if* the syntactic construct they handle is present, wrap
  60. // the AST node that the inner parser gave them in another node.
  61. // Parse a full expression. The optional arguments are used to
  62. // forbid the `in` operator (in for loops initalization expressions)
  63. // and provide reference for storing '=' operator inside shorthand
  64. // property assignment in contexts where both object expression
  65. // and object pattern might appear (so it's possible to raise
  66. // delayed syntax error at correct position).
  67. pp.parseExpression = function (noIn, refShorthandDefaultPos) {
  68. var startPos = this.state.start,
  69. startLoc = this.state.startLoc;
  70. var expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
  71. if (this.match(_types.types.comma)) {
  72. var node = this.startNodeAt(startPos, startLoc);
  73. node.expressions = [expr];
  74. while (this.eat(_types.types.comma)) {
  75. node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
  76. }
  77. this.toReferencedList(node.expressions);
  78. return this.finishNode(node, "SequenceExpression");
  79. }
  80. return expr;
  81. };
  82. // Parse an assignment expression. This includes applications of
  83. // operators like `+=`.
  84. pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  85. if (this.match(_types.types._yield) && this.state.inGenerator) {
  86. return this.parseYield();
  87. }
  88. var failOnShorthandAssign = void 0;
  89. if (refShorthandDefaultPos) {
  90. failOnShorthandAssign = false;
  91. } else {
  92. refShorthandDefaultPos = { start: 0 };
  93. failOnShorthandAssign = true;
  94. }
  95. var startPos = this.state.start;
  96. var startLoc = this.state.startLoc;
  97. if (this.match(_types.types.parenL) || this.match(_types.types.name)) {
  98. this.state.potentialArrowAt = this.state.start;
  99. }
  100. var left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
  101. if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc);
  102. if (this.state.type.isAssign) {
  103. var node = this.startNodeAt(startPos, startLoc);
  104. node.operator = this.state.value;
  105. node.left = this.match(_types.types.eq) ? this.toAssignable(left) : left;
  106. refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly
  107. this.checkLVal(left);
  108. if (left.extra && left.extra.parenthesized) {
  109. var errorMsg = void 0;
  110. if (left.type === "ObjectPattern") {
  111. errorMsg = "`({a}) = 0` use `({a} = 0)`";
  112. } else if (left.type === "ArrayPattern") {
  113. errorMsg = "`([a]) = 0` use `([a] = 0)`";
  114. }
  115. if (errorMsg) {
  116. this.raise(left.start, "You're trying to assign to a parenthesized expression, eg. instead of " + errorMsg);
  117. }
  118. }
  119. this.next();
  120. node.right = this.parseMaybeAssign(noIn);
  121. return this.finishNode(node, "AssignmentExpression");
  122. } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
  123. this.unexpected(refShorthandDefaultPos.start);
  124. }
  125. return left;
  126. };
  127. // Parse a ternary conditional (`?:`) operator.
  128. pp.parseMaybeConditional = function (noIn, refShorthandDefaultPos, refNeedsArrowPos) {
  129. var startPos = this.state.start,
  130. startLoc = this.state.startLoc;
  131. var expr = this.parseExprOps(noIn, refShorthandDefaultPos);
  132. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  133. return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  134. };
  135. pp.parseConditional = function (expr, noIn, startPos, startLoc) {
  136. if (this.eat(_types.types.question)) {
  137. var node = this.startNodeAt(startPos, startLoc);
  138. node.test = expr;
  139. node.consequent = this.parseMaybeAssign();
  140. this.expect(_types.types.colon);
  141. node.alternate = this.parseMaybeAssign(noIn);
  142. return this.finishNode(node, "ConditionalExpression");
  143. }
  144. return expr;
  145. };
  146. // Start the precedence parser.
  147. pp.parseExprOps = function (noIn, refShorthandDefaultPos) {
  148. var startPos = this.state.start,
  149. startLoc = this.state.startLoc;
  150. var expr = this.parseMaybeUnary(refShorthandDefaultPos);
  151. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  152. return expr;
  153. } else {
  154. return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
  155. }
  156. };
  157. // Parse binary operators with the operator precedence parsing
  158. // algorithm. `left` is the left-hand side of the operator.
  159. // `minPrec` provides context that allows the function to stop and
  160. // defer further parser to one of its callers when it encounters an
  161. // operator that has a lower precedence than the set it is parsing.
  162. pp.parseExprOp = function (left, leftStartPos, leftStartLoc, minPrec, noIn) {
  163. var prec = this.state.type.binop;
  164. if (prec != null && (!noIn || !this.match(_types.types._in))) {
  165. if (prec > minPrec) {
  166. var node = this.startNodeAt(leftStartPos, leftStartLoc);
  167. node.left = left;
  168. node.operator = this.state.value;
  169. if (node.operator === "**" && left.type === "UnaryExpression" && left.extra && !left.extra.parenthesizedArgument && !left.extra.parenthesized) {
  170. this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
  171. }
  172. var op = this.state.type;
  173. this.next();
  174. var startPos = this.state.start;
  175. var startLoc = this.state.startLoc;
  176. node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
  177. this.finishNode(node, op === _types.types.logicalOR || op === _types.types.logicalAND ? "LogicalExpression" : "BinaryExpression");
  178. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  179. }
  180. }
  181. return left;
  182. };
  183. // Parse unary operators, both prefix and postfix.
  184. pp.parseMaybeUnary = function (refShorthandDefaultPos) {
  185. if (this.state.type.prefix) {
  186. var node = this.startNode();
  187. var update = this.match(_types.types.incDec);
  188. node.operator = this.state.value;
  189. node.prefix = true;
  190. this.next();
  191. var argType = this.state.type;
  192. node.argument = this.parseMaybeUnary();
  193. this.addExtra(node, "parenthesizedArgument", argType === _types.types.parenL && (!node.argument.extra || !node.argument.extra.parenthesized));
  194. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  195. this.unexpected(refShorthandDefaultPos.start);
  196. }
  197. if (update) {
  198. this.checkLVal(node.argument);
  199. } else if (this.state.strict && node.operator === "delete" && node.argument.type === "Identifier") {
  200. this.raise(node.start, "Deleting local variable in strict mode");
  201. }
  202. return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  203. }
  204. var startPos = this.state.start,
  205. startLoc = this.state.startLoc;
  206. var expr = this.parseExprSubscripts(refShorthandDefaultPos);
  207. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  208. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  209. var _node = this.startNodeAt(startPos, startLoc);
  210. _node.operator = this.state.value;
  211. _node.prefix = false;
  212. _node.argument = expr;
  213. this.checkLVal(expr);
  214. this.next();
  215. expr = this.finishNode(_node, "UpdateExpression");
  216. }
  217. return expr;
  218. };
  219. // Parse call, dot, and `[]`-subscript expressions.
  220. pp.parseExprSubscripts = function (refShorthandDefaultPos) {
  221. var startPos = this.state.start,
  222. startLoc = this.state.startLoc;
  223. var potentialArrowAt = this.state.potentialArrowAt;
  224. var expr = this.parseExprAtom(refShorthandDefaultPos);
  225. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  226. return expr;
  227. }
  228. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  229. return expr;
  230. }
  231. return this.parseSubscripts(expr, startPos, startLoc);
  232. };
  233. pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
  234. for (;;) {
  235. if (!noCalls && this.eat(_types.types.doubleColon)) {
  236. var node = this.startNodeAt(startPos, startLoc);
  237. node.object = base;
  238. node.callee = this.parseNoCallExpr();
  239. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  240. } else if (this.eat(_types.types.dot)) {
  241. var _node2 = this.startNodeAt(startPos, startLoc);
  242. _node2.object = base;
  243. _node2.property = this.parseIdentifier(true);
  244. _node2.computed = false;
  245. base = this.finishNode(_node2, "MemberExpression");
  246. } else if (this.eat(_types.types.bracketL)) {
  247. var _node3 = this.startNodeAt(startPos, startLoc);
  248. _node3.object = base;
  249. _node3.property = this.parseExpression();
  250. _node3.computed = true;
  251. this.expect(_types.types.bracketR);
  252. base = this.finishNode(_node3, "MemberExpression");
  253. } else if (!noCalls && this.match(_types.types.parenL)) {
  254. var possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon();
  255. this.next();
  256. var _node4 = this.startNodeAt(startPos, startLoc);
  257. _node4.callee = base;
  258. _node4.arguments = this.parseCallExpressionArguments(_types.types.parenR, possibleAsync);
  259. base = this.finishNode(_node4, "CallExpression");
  260. if (possibleAsync && this.shouldParseAsyncArrow()) {
  261. return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), _node4);
  262. } else {
  263. this.toReferencedList(_node4.arguments);
  264. }
  265. } else if (this.match(_types.types.backQuote)) {
  266. var _node5 = this.startNodeAt(startPos, startLoc);
  267. _node5.tag = base;
  268. _node5.quasi = this.parseTemplate();
  269. base = this.finishNode(_node5, "TaggedTemplateExpression");
  270. } else {
  271. return base;
  272. }
  273. }
  274. };
  275. pp.parseCallExpressionArguments = function (close, possibleAsyncArrow) {
  276. var innerParenStart = void 0;
  277. var elts = [],
  278. first = true;
  279. while (!this.eat(close)) {
  280. if (first) {
  281. first = false;
  282. } else {
  283. this.expect(_types.types.comma);
  284. if (this.eat(close)) break;
  285. }
  286. // we need to make sure that if this is an async arrow functions, that we don't allow inner parens inside the params
  287. if (this.match(_types.types.parenL) && !innerParenStart) {
  288. innerParenStart = this.state.start;
  289. }
  290. elts.push(this.parseExprListItem(undefined, possibleAsyncArrow ? { start: 0 } : undefined));
  291. }
  292. // we found an async arrow function so let's not allow any inner parens
  293. if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
  294. this.unexpected();
  295. }
  296. return elts;
  297. };
  298. pp.shouldParseAsyncArrow = function () {
  299. return this.match(_types.types.arrow);
  300. };
  301. pp.parseAsyncArrowFromCallExpression = function (node, call) {
  302. this.expect(_types.types.arrow);
  303. return this.parseArrowExpression(node, call.arguments, true);
  304. };
  305. // Parse a no-call expression (like argument of `new` or `::` operators).
  306. pp.parseNoCallExpr = function () {
  307. var startPos = this.state.start,
  308. startLoc = this.state.startLoc;
  309. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  310. };
  311. // Parse an atomic expression — either a single token that is an
  312. // expression, an expression started by a keyword like `function` or
  313. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  314. // or `{}`.
  315. pp.parseExprAtom = function (refShorthandDefaultPos) {
  316. var node = void 0,
  317. canBeArrow = this.state.potentialArrowAt === this.state.start;
  318. switch (this.state.type) {
  319. case _types.types._super:
  320. if (!this.state.inMethod && !this.options.allowSuperOutsideMethod) {
  321. this.raise(this.state.start, "'super' outside of function or class");
  322. }
  323. node = this.startNode();
  324. this.next();
  325. if (!this.match(_types.types.parenL) && !this.match(_types.types.bracketL) && !this.match(_types.types.dot)) {
  326. this.unexpected();
  327. }
  328. if (this.match(_types.types.parenL) && this.state.inMethod !== "constructor" && !this.options.allowSuperOutsideMethod) {
  329. this.raise(node.start, "super() outside of class constructor");
  330. }
  331. return this.finishNode(node, "Super");
  332. case _types.types._this:
  333. node = this.startNode();
  334. this.next();
  335. return this.finishNode(node, "ThisExpression");
  336. case _types.types._yield:
  337. if (this.state.inGenerator) this.unexpected();
  338. case _types.types.name:
  339. node = this.startNode();
  340. var allowAwait = this.state.value === "await" && this.state.inAsync;
  341. var allowYield = this.shouldAllowYieldIdentifier();
  342. var id = this.parseIdentifier(allowAwait || allowYield);
  343. if (id.name === "await") {
  344. if (this.state.inAsync || this.inModule) {
  345. return this.parseAwait(node);
  346. }
  347. } else if (id.name === "async" && this.match(_types.types._function) && !this.canInsertSemicolon()) {
  348. this.next();
  349. return this.parseFunction(node, false, false, true);
  350. } else if (canBeArrow && id.name === "async" && this.match(_types.types.name)) {
  351. var params = [this.parseIdentifier()];
  352. this.expect(_types.types.arrow);
  353. // let foo = bar => {};
  354. return this.parseArrowExpression(node, params, true);
  355. }
  356. if (canBeArrow && !this.canInsertSemicolon() && this.eat(_types.types.arrow)) {
  357. return this.parseArrowExpression(node, [id]);
  358. }
  359. return id;
  360. case _types.types._do:
  361. if (this.hasPlugin("doExpressions")) {
  362. var _node6 = this.startNode();
  363. this.next();
  364. var oldInFunction = this.state.inFunction;
  365. var oldLabels = this.state.labels;
  366. this.state.labels = [];
  367. this.state.inFunction = false;
  368. _node6.body = this.parseBlock(false, true);
  369. this.state.inFunction = oldInFunction;
  370. this.state.labels = oldLabels;
  371. return this.finishNode(_node6, "DoExpression");
  372. }
  373. case _types.types.regexp:
  374. var value = this.state.value;
  375. node = this.parseLiteral(value.value, "RegExpLiteral");
  376. node.pattern = value.pattern;
  377. node.flags = value.flags;
  378. return node;
  379. case _types.types.num:
  380. return this.parseLiteral(this.state.value, "NumericLiteral");
  381. case _types.types.string:
  382. return this.parseLiteral(this.state.value, "StringLiteral");
  383. case _types.types._null:
  384. node = this.startNode();
  385. this.next();
  386. return this.finishNode(node, "NullLiteral");
  387. case _types.types._true:case _types.types._false:
  388. node = this.startNode();
  389. node.value = this.match(_types.types._true);
  390. this.next();
  391. return this.finishNode(node, "BooleanLiteral");
  392. case _types.types.parenL:
  393. return this.parseParenAndDistinguishExpression(null, null, canBeArrow);
  394. case _types.types.bracketL:
  395. node = this.startNode();
  396. this.next();
  397. node.elements = this.parseExprList(_types.types.bracketR, true, refShorthandDefaultPos);
  398. this.toReferencedList(node.elements);
  399. return this.finishNode(node, "ArrayExpression");
  400. case _types.types.braceL:
  401. return this.parseObj(false, refShorthandDefaultPos);
  402. case _types.types._function:
  403. return this.parseFunctionExpression();
  404. case _types.types.at:
  405. this.parseDecorators();
  406. case _types.types._class:
  407. node = this.startNode();
  408. this.takeDecorators(node);
  409. return this.parseClass(node, false);
  410. case _types.types._new:
  411. return this.parseNew();
  412. case _types.types.backQuote:
  413. return this.parseTemplate();
  414. case _types.types.doubleColon:
  415. node = this.startNode();
  416. this.next();
  417. node.object = null;
  418. var callee = node.callee = this.parseNoCallExpr();
  419. if (callee.type === "MemberExpression") {
  420. return this.finishNode(node, "BindExpression");
  421. } else {
  422. this.raise(callee.start, "Binding should be performed on object property.");
  423. }
  424. default:
  425. this.unexpected();
  426. }
  427. };
  428. pp.parseFunctionExpression = function () {
  429. var node = this.startNode();
  430. var meta = this.parseIdentifier(true);
  431. if (this.state.inGenerator && this.eat(_types.types.dot) && this.hasPlugin("functionSent")) {
  432. return this.parseMetaProperty(node, meta, "sent");
  433. } else {
  434. return this.parseFunction(node, false);
  435. }
  436. };
  437. pp.parseMetaProperty = function (node, meta, propertyName) {
  438. node.meta = meta;
  439. node.property = this.parseIdentifier(true);
  440. if (node.property.name !== propertyName) {
  441. this.raise(node.property.start, "The only valid meta property for new is " + meta.name + "." + propertyName);
  442. }
  443. return this.finishNode(node, "MetaProperty");
  444. };
  445. pp.parseLiteral = function (value, type) {
  446. var node = this.startNode();
  447. this.addExtra(node, "rawValue", value);
  448. this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
  449. node.value = value;
  450. this.next();
  451. return this.finishNode(node, type);
  452. };
  453. pp.parseParenExpression = function () {
  454. this.expect(_types.types.parenL);
  455. var val = this.parseExpression();
  456. this.expect(_types.types.parenR);
  457. return val;
  458. };
  459. pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow, isAsync) {
  460. startPos = startPos || this.state.start;
  461. startLoc = startLoc || this.state.startLoc;
  462. var val = void 0;
  463. this.expect(_types.types.parenL);
  464. var innerStartPos = this.state.start,
  465. innerStartLoc = this.state.startLoc;
  466. var exprList = [],
  467. first = true;
  468. var refShorthandDefaultPos = { start: 0 },
  469. spreadStart = void 0,
  470. optionalCommaStart = void 0;
  471. var refNeedsArrowPos = { start: 0 };
  472. while (!this.match(_types.types.parenR)) {
  473. if (first) {
  474. first = false;
  475. } else {
  476. this.expect(_types.types.comma, refNeedsArrowPos.start || null);
  477. if (this.match(_types.types.parenR)) {
  478. optionalCommaStart = this.state.start;
  479. break;
  480. }
  481. }
  482. if (this.match(_types.types.ellipsis)) {
  483. var spreadNodeStartPos = this.state.start,
  484. spreadNodeStartLoc = this.state.startLoc;
  485. spreadStart = this.state.start;
  486. exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartLoc, spreadNodeStartPos));
  487. break;
  488. } else {
  489. exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
  490. }
  491. }
  492. var innerEndPos = this.state.start;
  493. var innerEndLoc = this.state.startLoc;
  494. this.expect(_types.types.parenR);
  495. var arrowNode = this.startNodeAt(startPos, startLoc);
  496. if (canBeArrow && !this.canInsertSemicolon() && (arrowNode = this.parseArrow(arrowNode))) {
  497. var _iteratorNormalCompletion = true;
  498. var _didIteratorError = false;
  499. var _iteratorError = undefined;
  500. try {
  501. for (var _iterator = (0, _getIterator3.default)(exprList), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  502. var param = _step.value;
  503. if (param.extra && param.extra.parenthesized) this.unexpected(param.extra.parenStart);
  504. }
  505. } catch (err) {
  506. _didIteratorError = true;
  507. _iteratorError = err;
  508. } finally {
  509. try {
  510. if (!_iteratorNormalCompletion && _iterator.return) {
  511. _iterator.return();
  512. }
  513. } finally {
  514. if (_didIteratorError) {
  515. throw _iteratorError;
  516. }
  517. }
  518. }
  519. return this.parseArrowExpression(arrowNode, exprList, isAsync);
  520. }
  521. if (!exprList.length) {
  522. if (isAsync) {
  523. return;
  524. } else {
  525. this.unexpected(this.state.lastTokStart);
  526. }
  527. }
  528. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  529. if (spreadStart) this.unexpected(spreadStart);
  530. if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start);
  531. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  532. if (exprList.length > 1) {
  533. val = this.startNodeAt(innerStartPos, innerStartLoc);
  534. val.expressions = exprList;
  535. this.toReferencedList(val.expressions);
  536. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  537. } else {
  538. val = exprList[0];
  539. }
  540. this.addExtra(val, "parenthesized", true);
  541. this.addExtra(val, "parenStart", startPos);
  542. return val;
  543. };
  544. pp.parseArrow = function (node) {
  545. if (this.eat(_types.types.arrow)) {
  546. return node;
  547. }
  548. };
  549. pp.parseParenItem = function (node) {
  550. return node;
  551. };
  552. // New's precedence is slightly tricky. It must allow its argument
  553. // to be a `[]` or dot subscript expression, but not a call — at
  554. // least, not without wrapping it in parentheses. Thus, it uses the
  555. pp.parseNew = function () {
  556. var node = this.startNode();
  557. var meta = this.parseIdentifier(true);
  558. if (this.eat(_types.types.dot)) {
  559. return this.parseMetaProperty(node, meta, "target");
  560. }
  561. node.callee = this.parseNoCallExpr();
  562. if (this.eat(_types.types.parenL)) {
  563. node.arguments = this.parseExprList(_types.types.parenR);
  564. this.toReferencedList(node.arguments);
  565. } else {
  566. node.arguments = [];
  567. }
  568. return this.finishNode(node, "NewExpression");
  569. };
  570. // Parse template expression.
  571. pp.parseTemplateElement = function () {
  572. var elem = this.startNode();
  573. elem.value = {
  574. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  575. cooked: this.state.value
  576. };
  577. this.next();
  578. elem.tail = this.match(_types.types.backQuote);
  579. return this.finishNode(elem, "TemplateElement");
  580. };
  581. pp.parseTemplate = function () {
  582. var node = this.startNode();
  583. this.next();
  584. node.expressions = [];
  585. var curElt = this.parseTemplateElement();
  586. node.quasis = [curElt];
  587. while (!curElt.tail) {
  588. this.expect(_types.types.dollarBraceL);
  589. node.expressions.push(this.parseExpression());
  590. this.expect(_types.types.braceR);
  591. node.quasis.push(curElt = this.parseTemplateElement());
  592. }
  593. this.next();
  594. return this.finishNode(node, "TemplateLiteral");
  595. };
  596. // Parse an object literal or binding pattern.
  597. pp.parseObj = function (isPattern, refShorthandDefaultPos) {
  598. var decorators = [];
  599. var propHash = (0, _create2.default)(null);
  600. var first = true;
  601. var node = this.startNode();
  602. node.properties = [];
  603. this.next();
  604. while (!this.eat(_types.types.braceR)) {
  605. if (first) {
  606. first = false;
  607. } else {
  608. this.expect(_types.types.comma);
  609. if (this.eat(_types.types.braceR)) break;
  610. }
  611. while (this.match(_types.types.at)) {
  612. decorators.push(this.parseDecorator());
  613. }
  614. var prop = this.startNode(),
  615. isGenerator = false,
  616. isAsync = false,
  617. startPos = void 0,
  618. startLoc = void 0;
  619. if (decorators.length) {
  620. prop.decorators = decorators;
  621. decorators = [];
  622. }
  623. if (this.hasPlugin("objectRestSpread") && this.match(_types.types.ellipsis)) {
  624. prop = this.parseSpread();
  625. prop.type = isPattern ? "RestProperty" : "SpreadProperty";
  626. node.properties.push(prop);
  627. continue;
  628. }
  629. prop.method = false;
  630. prop.shorthand = false;
  631. if (isPattern || refShorthandDefaultPos) {
  632. startPos = this.state.start;
  633. startLoc = this.state.startLoc;
  634. }
  635. if (!isPattern) {
  636. isGenerator = this.eat(_types.types.star);
  637. }
  638. if (!isPattern && this.isContextual("async")) {
  639. if (isGenerator) this.unexpected();
  640. var asyncId = this.parseIdentifier();
  641. if (this.match(_types.types.colon) || this.match(_types.types.parenL) || this.match(_types.types.braceR)) {
  642. prop.key = asyncId;
  643. } else {
  644. isAsync = true;
  645. if (this.hasPlugin("asyncGenerators")) isGenerator = this.eat(_types.types.star);
  646. this.parsePropertyName(prop);
  647. }
  648. } else {
  649. this.parsePropertyName(prop);
  650. }
  651. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos);
  652. this.checkPropClash(prop, propHash);
  653. if (prop.shorthand) {
  654. this.addExtra(prop, "shorthand", true);
  655. }
  656. node.properties.push(prop);
  657. }
  658. if (decorators.length) {
  659. this.raise(this.state.start, "You have trailing decorators with no property");
  660. }
  661. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
  662. };
  663. pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos) {
  664. if (isAsync || isGenerator || this.match(_types.types.parenL)) {
  665. if (isPattern) this.unexpected();
  666. prop.kind = "method";
  667. prop.method = true;
  668. this.parseMethod(prop, isGenerator, isAsync);
  669. return this.finishNode(prop, "ObjectMethod");
  670. }
  671. if (this.eat(_types.types.colon)) {
  672. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
  673. return this.finishNode(prop, "ObjectProperty");
  674. }
  675. if (!prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && !this.match(_types.types.comma) && !this.match(_types.types.braceR)) {
  676. if (isGenerator || isAsync || isPattern) this.unexpected();
  677. prop.kind = prop.key.name;
  678. this.parsePropertyName(prop);
  679. this.parseMethod(prop, false);
  680. var paramCount = prop.kind === "get" ? 0 : 1;
  681. if (prop.params.length !== paramCount) {
  682. var start = prop.start;
  683. if (prop.kind === "get") {
  684. this.raise(start, "getter should have no params");
  685. } else {
  686. this.raise(start, "setter should have exactly one param");
  687. }
  688. }
  689. return this.finishNode(prop, "ObjectMethod");
  690. }
  691. if (!prop.computed && prop.key.type === "Identifier") {
  692. if (isPattern) {
  693. var illegalBinding = this.isKeyword(prop.key.name);
  694. if (!illegalBinding && this.state.strict) {
  695. illegalBinding = _identifier.reservedWords.strictBind(prop.key.name) || _identifier.reservedWords.strict(prop.key.name);
  696. }
  697. if (illegalBinding) {
  698. this.raise(prop.key.start, "Binding " + prop.key.name);
  699. }
  700. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  701. } else if (this.match(_types.types.eq) && refShorthandDefaultPos) {
  702. if (!refShorthandDefaultPos.start) {
  703. refShorthandDefaultPos.start = this.state.start;
  704. }
  705. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  706. } else {
  707. prop.value = prop.key.__clone();
  708. }
  709. prop.shorthand = true;
  710. return this.finishNode(prop, "ObjectProperty");
  711. }
  712. this.unexpected();
  713. };
  714. pp.parsePropertyName = function (prop) {
  715. if (this.eat(_types.types.bracketL)) {
  716. prop.computed = true;
  717. prop.key = this.parseMaybeAssign();
  718. this.expect(_types.types.bracketR);
  719. return prop.key;
  720. } else {
  721. prop.computed = false;
  722. return prop.key = this.match(_types.types.num) || this.match(_types.types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  723. }
  724. };
  725. // Initialize empty function node.
  726. pp.initFunction = function (node, isAsync) {
  727. node.id = null;
  728. node.generator = false;
  729. node.expression = false;
  730. node.async = !!isAsync;
  731. };
  732. // Parse object or class method.
  733. pp.parseMethod = function (node, isGenerator, isAsync) {
  734. var oldInMethod = this.state.inMethod;
  735. this.state.inMethod = node.kind || true;
  736. this.initFunction(node, isAsync);
  737. this.expect(_types.types.parenL);
  738. node.params = this.parseBindingList(_types.types.parenR);
  739. node.generator = isGenerator;
  740. this.parseFunctionBody(node);
  741. this.state.inMethod = oldInMethod;
  742. return node;
  743. };
  744. // Parse arrow function expression with given parameters.
  745. pp.parseArrowExpression = function (node, params, isAsync) {
  746. this.initFunction(node, isAsync);
  747. node.params = this.toAssignableList(params, true);
  748. this.parseFunctionBody(node, true);
  749. return this.finishNode(node, "ArrowFunctionExpression");
  750. };
  751. // Parse function body and check parameters.
  752. pp.parseFunctionBody = function (node, allowExpression) {
  753. var isExpression = allowExpression && !this.match(_types.types.braceL);
  754. var oldInAsync = this.state.inAsync;
  755. this.state.inAsync = node.async;
  756. if (isExpression) {
  757. node.body = this.parseMaybeAssign();
  758. node.expression = true;
  759. } else {
  760. // Start a new scope with regard to labels and the `inFunction`
  761. // flag (restore them to their old value afterwards).
  762. var oldInFunc = this.state.inFunction,
  763. oldInGen = this.state.inGenerator,
  764. oldLabels = this.state.labels;
  765. this.state.inFunction = true;this.state.inGenerator = node.generator;this.state.labels = [];
  766. node.body = this.parseBlock(true);
  767. node.expression = false;
  768. this.state.inFunction = oldInFunc;this.state.inGenerator = oldInGen;this.state.labels = oldLabels;
  769. }
  770. this.state.inAsync = oldInAsync;
  771. // If this is a strict mode function, verify that argument names
  772. // are not repeated, and it does not try to bind the words `eval`
  773. // or `arguments`.
  774. var checkLVal = this.state.strict;
  775. var checkLValStrict = false;
  776. var isStrict = false;
  777. // arrow function
  778. if (allowExpression) checkLVal = true;
  779. // normal function
  780. if (!isExpression && node.body.directives.length) {
  781. var _iteratorNormalCompletion2 = true;
  782. var _didIteratorError2 = false;
  783. var _iteratorError2 = undefined;
  784. try {
  785. for (var _iterator2 = (0, _getIterator3.default)(node.body.directives), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  786. var directive = _step2.value;
  787. if (directive.value.value === "use strict") {
  788. isStrict = true;
  789. checkLVal = true;
  790. checkLValStrict = true;
  791. break;
  792. }
  793. }
  794. } catch (err) {
  795. _didIteratorError2 = true;
  796. _iteratorError2 = err;
  797. } finally {
  798. try {
  799. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  800. _iterator2.return();
  801. }
  802. } finally {
  803. if (_didIteratorError2) {
  804. throw _iteratorError2;
  805. }
  806. }
  807. }
  808. }
  809. //
  810. if (isStrict && node.id && node.id.type === "Identifier" && node.id.name === "yield") {
  811. this.raise(node.id.start, "Binding yield in strict mode");
  812. }
  813. if (checkLVal) {
  814. var nameHash = (0, _create2.default)(null);
  815. var oldStrict = this.state.strict;
  816. if (checkLValStrict) this.state.strict = true;
  817. if (node.id) {
  818. this.checkLVal(node.id, true);
  819. }
  820. var _iteratorNormalCompletion3 = true;
  821. var _didIteratorError3 = false;
  822. var _iteratorError3 = undefined;
  823. try {
  824. for (var _iterator3 = (0, _getIterator3.default)(node.params), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  825. var param = _step3.value;
  826. this.checkLVal(param, true, nameHash);
  827. }
  828. } catch (err) {
  829. _didIteratorError3 = true;
  830. _iteratorError3 = err;
  831. } finally {
  832. try {
  833. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  834. _iterator3.return();
  835. }
  836. } finally {
  837. if (_didIteratorError3) {
  838. throw _iteratorError3;
  839. }
  840. }
  841. }
  842. this.state.strict = oldStrict;
  843. }
  844. };
  845. // Parses a comma-separated list of expressions, and returns them as
  846. // an array. `close` is the token type that ends the list, and
  847. // `allowEmpty` can be turned on to allow subsequent commas with
  848. // nothing in between them to be parsed as `null` (which is needed
  849. // for array literals).
  850. pp.parseExprList = function (close, allowEmpty, refShorthandDefaultPos) {
  851. var elts = [],
  852. first = true;
  853. while (!this.eat(close)) {
  854. if (first) {
  855. first = false;
  856. } else {
  857. this.expect(_types.types.comma);
  858. if (this.eat(close)) break;
  859. }
  860. elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
  861. }
  862. return elts;
  863. };
  864. pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos) {
  865. var elt = void 0;
  866. if (allowEmpty && this.match(_types.types.comma)) {
  867. elt = null;
  868. } else if (this.match(_types.types.ellipsis)) {
  869. elt = this.parseSpread(refShorthandDefaultPos);
  870. } else {
  871. elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem);
  872. }
  873. return elt;
  874. };
  875. // Parse the next token as an identifier. If `liberal` is true (used
  876. // when parsing properties), it will also convert keywords into
  877. // identifiers.
  878. pp.parseIdentifier = function (liberal) {
  879. var node = this.startNode();
  880. if (this.match(_types.types.name)) {
  881. if (!liberal && this.state.strict && _identifier.reservedWords.strict(this.state.value)) {
  882. this.raise(this.state.start, "The keyword '" + this.state.value + "' is reserved");
  883. }
  884. node.name = this.state.value;
  885. } else if (liberal && this.state.type.keyword) {
  886. node.name = this.state.type.keyword;
  887. } else {
  888. this.unexpected();
  889. }
  890. if (!liberal && node.name === "await" && this.state.inAsync) {
  891. this.raise(node.start, "invalid use of await inside of an async function");
  892. }
  893. node.loc.identifierName = node.name;
  894. this.next();
  895. return this.finishNode(node, "Identifier");
  896. };
  897. // Parses await expression inside async function.
  898. pp.parseAwait = function (node) {
  899. if (!this.state.inAsync) {
  900. this.unexpected();
  901. }
  902. if (this.match(_types.types.star)) {
  903. this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
  904. }
  905. node.argument = this.parseMaybeUnary();
  906. return this.finishNode(node, "AwaitExpression");
  907. };
  908. // Parses yield expression inside generator.
  909. pp.parseYield = function () {
  910. var node = this.startNode();
  911. this.next();
  912. if (this.match(_types.types.semi) || this.canInsertSemicolon() || !this.match(_types.types.star) && !this.state.type.startsExpr) {
  913. node.delegate = false;
  914. node.argument = null;
  915. } else {
  916. node.delegate = this.eat(_types.types.star);
  917. node.argument = this.parseMaybeAssign();
  918. }
  919. return this.finishNode(node, "YieldExpression");
  920. };