expression.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
  2. // A recursive descent parser operates by defining functions for all
  3. // syntactic elements, and recursively calling those, each function
  4. // advancing the input stream and returning an AST node. Precedence
  5. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  6. // instead of `(!x)[1]` is handled by the fact that the parser
  7. // function that parses unary prefix operators is called first, and
  8. // in turn calls the function that parses `[]` subscripts — that
  9. // way, it'll receive the node for `x[1]` already parsed, and wraps
  10. // *that* in the unary operator node.
  11. //
  12. // Acorn uses an [operator precedence parser][opp] to handle binary
  13. // operator precedence, because it is much more compact than using
  14. // the technique outlined above, which uses different, nesting
  15. // functions to specify precedence, for all of the ten binary
  16. // precedence levels that JavaScript defines.
  17. //
  18. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  19. var _flow = require('../plugins/flow');
  20. var _index = require('../plugins/jsx/index');
  21. var _types = require('../plugins/types');
  22. var _typescript = require('../plugins/typescript');
  23. var _index3 = require('../tokenizer/index');
  24. var _keywords = require('../tokenizer/keywords');
  25. var _state = require('../tokenizer/state');
  26. var _types3 = require('../tokenizer/types');
  27. var _charcodes = require('../util/charcodes');
  28. var _identifier = require('../util/identifier');
  29. var _base = require('./base');
  30. var _lval = require('./lval');
  31. var _statement = require('./statement');
  32. var _util = require('./util');
  33. class StopState {
  34. constructor(stop) {
  35. this.stop = stop;
  36. }
  37. } exports.StopState = StopState;
  38. // ### Expression parsing
  39. // These nest, from the most general expression type at the top to
  40. // 'atomic', nondivisible expression types at the bottom. Most of
  41. // the functions will simply let the function (s) below them parse,
  42. // and, *if* the syntactic construct they handle is present, wrap
  43. // the AST node that the inner parser gave them in another node.
  44. function parseExpression(noIn = false) {
  45. parseMaybeAssign(noIn);
  46. if (_index3.match.call(void 0, _types3.TokenType.comma)) {
  47. while (_index3.eat.call(void 0, _types3.TokenType.comma)) {
  48. parseMaybeAssign(noIn);
  49. }
  50. }
  51. } exports.parseExpression = parseExpression;
  52. /**
  53. * noIn is used when parsing a for loop so that we don't interpret a following "in" as the binary
  54. * operatior.
  55. * isWithinParens is used to indicate that we're parsing something that might be a comma expression
  56. * or might be an arrow function or might be a Flow type assertion (which requires explicit parens).
  57. * In these cases, we should allow : and ?: after the initial "left" part.
  58. */
  59. function parseMaybeAssign(noIn = false, isWithinParens = false) {
  60. if (_base.isTypeScriptEnabled) {
  61. return _typescript.tsParseMaybeAssign.call(void 0, noIn, isWithinParens);
  62. } else if (_base.isFlowEnabled) {
  63. return _flow.flowParseMaybeAssign.call(void 0, noIn, isWithinParens);
  64. } else {
  65. return baseParseMaybeAssign(noIn, isWithinParens);
  66. }
  67. } exports.parseMaybeAssign = parseMaybeAssign;
  68. // Parse an assignment expression. This includes applications of
  69. // operators like `+=`.
  70. // Returns true if the expression was an arrow function.
  71. function baseParseMaybeAssign(noIn, isWithinParens) {
  72. if (_index3.match.call(void 0, _types3.TokenType._yield)) {
  73. parseYield();
  74. return false;
  75. }
  76. if (_index3.match.call(void 0, _types3.TokenType.parenL) || _index3.match.call(void 0, _types3.TokenType.name) || _index3.match.call(void 0, _types3.TokenType._yield)) {
  77. _base.state.potentialArrowAt = _base.state.start;
  78. }
  79. const wasArrow = parseMaybeConditional(noIn);
  80. if (isWithinParens) {
  81. parseParenItem();
  82. }
  83. if (_base.state.type & _types3.TokenType.IS_ASSIGN) {
  84. _index3.next.call(void 0, );
  85. parseMaybeAssign(noIn);
  86. return false;
  87. }
  88. return wasArrow;
  89. } exports.baseParseMaybeAssign = baseParseMaybeAssign;
  90. // Parse a ternary conditional (`?:`) operator.
  91. // Returns true if the expression was an arrow function.
  92. function parseMaybeConditional(noIn) {
  93. const wasArrow = parseExprOps(noIn);
  94. if (wasArrow) {
  95. return true;
  96. }
  97. parseConditional(noIn);
  98. return false;
  99. }
  100. function parseConditional(noIn) {
  101. if (_base.isTypeScriptEnabled || _base.isFlowEnabled) {
  102. _types.typedParseConditional.call(void 0, noIn);
  103. } else {
  104. baseParseConditional(noIn);
  105. }
  106. }
  107. function baseParseConditional(noIn) {
  108. if (_index3.eat.call(void 0, _types3.TokenType.question)) {
  109. parseMaybeAssign();
  110. _util.expect.call(void 0, _types3.TokenType.colon);
  111. parseMaybeAssign(noIn);
  112. }
  113. } exports.baseParseConditional = baseParseConditional;
  114. // Start the precedence parser.
  115. // Returns true if this was an arrow function
  116. function parseExprOps(noIn) {
  117. const startTokenIndex = _base.state.tokens.length;
  118. const wasArrow = parseMaybeUnary();
  119. if (wasArrow) {
  120. return true;
  121. }
  122. parseExprOp(startTokenIndex, -1, noIn);
  123. return false;
  124. }
  125. // Parse binary operators with the operator precedence parsing
  126. // algorithm. `left` is the left-hand side of the operator.
  127. // `minPrec` provides context that allows the function to stop and
  128. // defer further parser to one of its callers when it encounters an
  129. // operator that has a lower precedence than the set it is parsing.
  130. function parseExprOp(startTokenIndex, minPrec, noIn) {
  131. if (
  132. _base.isTypeScriptEnabled &&
  133. (_types3.TokenType._in & _types3.TokenType.PRECEDENCE_MASK) > minPrec &&
  134. !_util.hasPrecedingLineBreak.call(void 0, ) &&
  135. (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as) || _util.eatContextual.call(void 0, _keywords.ContextualKeyword._satisfies))
  136. ) {
  137. const oldIsType = _index3.pushTypeContext.call(void 0, 1);
  138. _typescript.tsParseType.call(void 0, );
  139. _index3.popTypeContext.call(void 0, oldIsType);
  140. _index3.rescan_gt.call(void 0, );
  141. parseExprOp(startTokenIndex, minPrec, noIn);
  142. return;
  143. }
  144. const prec = _base.state.type & _types3.TokenType.PRECEDENCE_MASK;
  145. if (prec > 0 && (!noIn || !_index3.match.call(void 0, _types3.TokenType._in))) {
  146. if (prec > minPrec) {
  147. const op = _base.state.type;
  148. _index3.next.call(void 0, );
  149. if (op === _types3.TokenType.nullishCoalescing) {
  150. _base.state.tokens[_base.state.tokens.length - 1].nullishStartIndex = startTokenIndex;
  151. }
  152. const rhsStartTokenIndex = _base.state.tokens.length;
  153. parseMaybeUnary();
  154. // Extend the right operand of this operator if possible.
  155. parseExprOp(rhsStartTokenIndex, op & _types3.TokenType.IS_RIGHT_ASSOCIATIVE ? prec - 1 : prec, noIn);
  156. if (op === _types3.TokenType.nullishCoalescing) {
  157. _base.state.tokens[startTokenIndex].numNullishCoalesceStarts++;
  158. _base.state.tokens[_base.state.tokens.length - 1].numNullishCoalesceEnds++;
  159. }
  160. // Continue with any future operator holding this expression as the left operand.
  161. parseExprOp(startTokenIndex, minPrec, noIn);
  162. }
  163. }
  164. }
  165. // Parse unary operators, both prefix and postfix.
  166. // Returns true if this was an arrow function.
  167. function parseMaybeUnary() {
  168. if (_base.isTypeScriptEnabled && !_base.isJSXEnabled && _index3.eat.call(void 0, _types3.TokenType.lessThan)) {
  169. _typescript.tsParseTypeAssertion.call(void 0, );
  170. return false;
  171. }
  172. if (
  173. _util.isContextual.call(void 0, _keywords.ContextualKeyword._module) &&
  174. _index3.lookaheadCharCode.call(void 0, ) === _charcodes.charCodes.leftCurlyBrace &&
  175. !_util.hasFollowingLineBreak.call(void 0, )
  176. ) {
  177. parseModuleExpression();
  178. return false;
  179. }
  180. if (_base.state.type & _types3.TokenType.IS_PREFIX) {
  181. _index3.next.call(void 0, );
  182. parseMaybeUnary();
  183. return false;
  184. }
  185. const wasArrow = parseExprSubscripts();
  186. if (wasArrow) {
  187. return true;
  188. }
  189. while (_base.state.type & _types3.TokenType.IS_POSTFIX && !_util.canInsertSemicolon.call(void 0, )) {
  190. // The tokenizer calls everything a preincrement, so make it a postincrement when
  191. // we see it in that context.
  192. if (_base.state.type === _types3.TokenType.preIncDec) {
  193. _base.state.type = _types3.TokenType.postIncDec;
  194. }
  195. _index3.next.call(void 0, );
  196. }
  197. return false;
  198. } exports.parseMaybeUnary = parseMaybeUnary;
  199. // Parse call, dot, and `[]`-subscript expressions.
  200. // Returns true if this was an arrow function.
  201. function parseExprSubscripts() {
  202. const startTokenIndex = _base.state.tokens.length;
  203. const wasArrow = parseExprAtom();
  204. if (wasArrow) {
  205. return true;
  206. }
  207. parseSubscripts(startTokenIndex);
  208. // If there was any optional chain operation, the start token would be marked
  209. // as such, so also mark the end now.
  210. if (_base.state.tokens.length > startTokenIndex && _base.state.tokens[startTokenIndex].isOptionalChainStart) {
  211. _base.state.tokens[_base.state.tokens.length - 1].isOptionalChainEnd = true;
  212. }
  213. return false;
  214. } exports.parseExprSubscripts = parseExprSubscripts;
  215. function parseSubscripts(startTokenIndex, noCalls = false) {
  216. if (_base.isFlowEnabled) {
  217. _flow.flowParseSubscripts.call(void 0, startTokenIndex, noCalls);
  218. } else {
  219. baseParseSubscripts(startTokenIndex, noCalls);
  220. }
  221. }
  222. function baseParseSubscripts(startTokenIndex, noCalls = false) {
  223. const stopState = new StopState(false);
  224. do {
  225. parseSubscript(startTokenIndex, noCalls, stopState);
  226. } while (!stopState.stop && !_base.state.error);
  227. } exports.baseParseSubscripts = baseParseSubscripts;
  228. function parseSubscript(startTokenIndex, noCalls, stopState) {
  229. if (_base.isTypeScriptEnabled) {
  230. _typescript.tsParseSubscript.call(void 0, startTokenIndex, noCalls, stopState);
  231. } else if (_base.isFlowEnabled) {
  232. _flow.flowParseSubscript.call(void 0, startTokenIndex, noCalls, stopState);
  233. } else {
  234. baseParseSubscript(startTokenIndex, noCalls, stopState);
  235. }
  236. }
  237. /** Set 'state.stop = true' to indicate that we should stop parsing subscripts. */
  238. function baseParseSubscript(
  239. startTokenIndex,
  240. noCalls,
  241. stopState,
  242. ) {
  243. if (!noCalls && _index3.eat.call(void 0, _types3.TokenType.doubleColon)) {
  244. parseNoCallExpr();
  245. stopState.stop = true;
  246. // Propagate startTokenIndex so that `a::b?.()` will keep `a` as the first token. We may want
  247. // to revisit this in the future when fully supporting bind syntax.
  248. parseSubscripts(startTokenIndex, noCalls);
  249. } else if (_index3.match.call(void 0, _types3.TokenType.questionDot)) {
  250. _base.state.tokens[startTokenIndex].isOptionalChainStart = true;
  251. if (noCalls && _index3.lookaheadType.call(void 0, ) === _types3.TokenType.parenL) {
  252. stopState.stop = true;
  253. return;
  254. }
  255. _index3.next.call(void 0, );
  256. _base.state.tokens[_base.state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
  257. if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
  258. parseExpression();
  259. _util.expect.call(void 0, _types3.TokenType.bracketR);
  260. } else if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
  261. parseCallExpressionArguments();
  262. } else {
  263. parseMaybePrivateName();
  264. }
  265. } else if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
  266. _base.state.tokens[_base.state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
  267. parseMaybePrivateName();
  268. } else if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
  269. _base.state.tokens[_base.state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
  270. parseExpression();
  271. _util.expect.call(void 0, _types3.TokenType.bracketR);
  272. } else if (!noCalls && _index3.match.call(void 0, _types3.TokenType.parenL)) {
  273. if (atPossibleAsync()) {
  274. // We see "async", but it's possible it's a usage of the name "async". Parse as if it's a
  275. // function call, and if we see an arrow later, backtrack and re-parse as a parameter list.
  276. const snapshot = _base.state.snapshot();
  277. const asyncStartTokenIndex = _base.state.tokens.length;
  278. _index3.next.call(void 0, );
  279. _base.state.tokens[_base.state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
  280. const callContextId = _base.getNextContextId.call(void 0, );
  281. _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
  282. parseCallExpressionArguments();
  283. _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
  284. if (shouldParseAsyncArrow()) {
  285. // We hit an arrow, so backtrack and start again parsing function parameters.
  286. _base.state.restoreFromSnapshot(snapshot);
  287. stopState.stop = true;
  288. _base.state.scopeDepth++;
  289. _statement.parseFunctionParams.call(void 0, );
  290. parseAsyncArrowFromCallExpression(asyncStartTokenIndex);
  291. }
  292. } else {
  293. _index3.next.call(void 0, );
  294. _base.state.tokens[_base.state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
  295. const callContextId = _base.getNextContextId.call(void 0, );
  296. _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
  297. parseCallExpressionArguments();
  298. _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
  299. }
  300. } else if (_index3.match.call(void 0, _types3.TokenType.backQuote)) {
  301. // Tagged template expression.
  302. parseTemplate();
  303. } else {
  304. stopState.stop = true;
  305. }
  306. } exports.baseParseSubscript = baseParseSubscript;
  307. function atPossibleAsync() {
  308. // This was made less strict than the original version to avoid passing around nodes, but it
  309. // should be safe to have rare false positives here.
  310. return (
  311. _base.state.tokens[_base.state.tokens.length - 1].contextualKeyword === _keywords.ContextualKeyword._async &&
  312. !_util.canInsertSemicolon.call(void 0, )
  313. );
  314. } exports.atPossibleAsync = atPossibleAsync;
  315. function parseCallExpressionArguments() {
  316. let first = true;
  317. while (!_index3.eat.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
  318. if (first) {
  319. first = false;
  320. } else {
  321. _util.expect.call(void 0, _types3.TokenType.comma);
  322. if (_index3.eat.call(void 0, _types3.TokenType.parenR)) {
  323. break;
  324. }
  325. }
  326. parseExprListItem(false);
  327. }
  328. } exports.parseCallExpressionArguments = parseCallExpressionArguments;
  329. function shouldParseAsyncArrow() {
  330. return _index3.match.call(void 0, _types3.TokenType.colon) || _index3.match.call(void 0, _types3.TokenType.arrow);
  331. }
  332. function parseAsyncArrowFromCallExpression(startTokenIndex) {
  333. if (_base.isTypeScriptEnabled) {
  334. _typescript.tsStartParseAsyncArrowFromCallExpression.call(void 0, );
  335. } else if (_base.isFlowEnabled) {
  336. _flow.flowStartParseAsyncArrowFromCallExpression.call(void 0, );
  337. }
  338. _util.expect.call(void 0, _types3.TokenType.arrow);
  339. parseArrowExpression(startTokenIndex);
  340. }
  341. // Parse a no-call expression (like argument of `new` or `::` operators).
  342. function parseNoCallExpr() {
  343. const startTokenIndex = _base.state.tokens.length;
  344. parseExprAtom();
  345. parseSubscripts(startTokenIndex, true);
  346. }
  347. // Parse an atomic expression — either a single token that is an
  348. // expression, an expression started by a keyword like `function` or
  349. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  350. // or `{}`.
  351. // Returns true if the parsed expression was an arrow function.
  352. function parseExprAtom() {
  353. if (_index3.eat.call(void 0, _types3.TokenType.modulo)) {
  354. // V8 intrinsic expression. Just parse the identifier, and the function invocation is parsed
  355. // naturally.
  356. parseIdentifier();
  357. return false;
  358. }
  359. if (_index3.match.call(void 0, _types3.TokenType.jsxText) || _index3.match.call(void 0, _types3.TokenType.jsxEmptyText)) {
  360. parseLiteral();
  361. return false;
  362. } else if (_index3.match.call(void 0, _types3.TokenType.lessThan) && _base.isJSXEnabled) {
  363. _base.state.type = _types3.TokenType.jsxTagStart;
  364. _index.jsxParseElement.call(void 0, );
  365. _index3.next.call(void 0, );
  366. return false;
  367. }
  368. const canBeArrow = _base.state.potentialArrowAt === _base.state.start;
  369. switch (_base.state.type) {
  370. case _types3.TokenType.slash:
  371. case _types3.TokenType.assign:
  372. _index3.retokenizeSlashAsRegex.call(void 0, );
  373. // Fall through.
  374. case _types3.TokenType._super:
  375. case _types3.TokenType._this:
  376. case _types3.TokenType.regexp:
  377. case _types3.TokenType.num:
  378. case _types3.TokenType.bigint:
  379. case _types3.TokenType.decimal:
  380. case _types3.TokenType.string:
  381. case _types3.TokenType._null:
  382. case _types3.TokenType._true:
  383. case _types3.TokenType._false:
  384. _index3.next.call(void 0, );
  385. return false;
  386. case _types3.TokenType._import:
  387. _index3.next.call(void 0, );
  388. if (_index3.match.call(void 0, _types3.TokenType.dot)) {
  389. // import.meta
  390. _base.state.tokens[_base.state.tokens.length - 1].type = _types3.TokenType.name;
  391. _index3.next.call(void 0, );
  392. parseIdentifier();
  393. }
  394. return false;
  395. case _types3.TokenType.name: {
  396. const startTokenIndex = _base.state.tokens.length;
  397. const functionStart = _base.state.start;
  398. const contextualKeyword = _base.state.contextualKeyword;
  399. parseIdentifier();
  400. if (contextualKeyword === _keywords.ContextualKeyword._await) {
  401. parseAwait();
  402. return false;
  403. } else if (
  404. contextualKeyword === _keywords.ContextualKeyword._async &&
  405. _index3.match.call(void 0, _types3.TokenType._function) &&
  406. !_util.canInsertSemicolon.call(void 0, )
  407. ) {
  408. _index3.next.call(void 0, );
  409. _statement.parseFunction.call(void 0, functionStart, false);
  410. return false;
  411. } else if (
  412. canBeArrow &&
  413. contextualKeyword === _keywords.ContextualKeyword._async &&
  414. !_util.canInsertSemicolon.call(void 0, ) &&
  415. _index3.match.call(void 0, _types3.TokenType.name)
  416. ) {
  417. _base.state.scopeDepth++;
  418. _lval.parseBindingIdentifier.call(void 0, false);
  419. _util.expect.call(void 0, _types3.TokenType.arrow);
  420. // let foo = async bar => {};
  421. parseArrowExpression(startTokenIndex);
  422. return true;
  423. } else if (_index3.match.call(void 0, _types3.TokenType._do) && !_util.canInsertSemicolon.call(void 0, )) {
  424. _index3.next.call(void 0, );
  425. _statement.parseBlock.call(void 0, );
  426. return false;
  427. }
  428. if (canBeArrow && !_util.canInsertSemicolon.call(void 0, ) && _index3.match.call(void 0, _types3.TokenType.arrow)) {
  429. _base.state.scopeDepth++;
  430. _lval.markPriorBindingIdentifier.call(void 0, false);
  431. _util.expect.call(void 0, _types3.TokenType.arrow);
  432. parseArrowExpression(startTokenIndex);
  433. return true;
  434. }
  435. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.Access;
  436. return false;
  437. }
  438. case _types3.TokenType._do: {
  439. _index3.next.call(void 0, );
  440. _statement.parseBlock.call(void 0, );
  441. return false;
  442. }
  443. case _types3.TokenType.parenL: {
  444. const wasArrow = parseParenAndDistinguishExpression(canBeArrow);
  445. return wasArrow;
  446. }
  447. case _types3.TokenType.bracketL:
  448. _index3.next.call(void 0, );
  449. parseExprList(_types3.TokenType.bracketR, true);
  450. return false;
  451. case _types3.TokenType.braceL:
  452. parseObj(false, false);
  453. return false;
  454. case _types3.TokenType._function:
  455. parseFunctionExpression();
  456. return false;
  457. case _types3.TokenType.at:
  458. _statement.parseDecorators.call(void 0, );
  459. // Fall through.
  460. case _types3.TokenType._class:
  461. _statement.parseClass.call(void 0, false);
  462. return false;
  463. case _types3.TokenType._new:
  464. parseNew();
  465. return false;
  466. case _types3.TokenType.backQuote:
  467. parseTemplate();
  468. return false;
  469. case _types3.TokenType.doubleColon: {
  470. _index3.next.call(void 0, );
  471. parseNoCallExpr();
  472. return false;
  473. }
  474. case _types3.TokenType.hash: {
  475. const code = _index3.lookaheadCharCode.call(void 0, );
  476. if (_identifier.IS_IDENTIFIER_START[code] || code === _charcodes.charCodes.backslash) {
  477. parseMaybePrivateName();
  478. } else {
  479. _index3.next.call(void 0, );
  480. }
  481. // Smart pipeline topic reference.
  482. return false;
  483. }
  484. default:
  485. _util.unexpected.call(void 0, );
  486. return false;
  487. }
  488. } exports.parseExprAtom = parseExprAtom;
  489. function parseMaybePrivateName() {
  490. _index3.eat.call(void 0, _types3.TokenType.hash);
  491. parseIdentifier();
  492. }
  493. function parseFunctionExpression() {
  494. const functionStart = _base.state.start;
  495. parseIdentifier();
  496. if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
  497. // function.sent
  498. parseIdentifier();
  499. }
  500. _statement.parseFunction.call(void 0, functionStart, false);
  501. }
  502. function parseLiteral() {
  503. _index3.next.call(void 0, );
  504. } exports.parseLiteral = parseLiteral;
  505. function parseParenExpression() {
  506. _util.expect.call(void 0, _types3.TokenType.parenL);
  507. parseExpression();
  508. _util.expect.call(void 0, _types3.TokenType.parenR);
  509. } exports.parseParenExpression = parseParenExpression;
  510. // Returns true if this was an arrow expression.
  511. function parseParenAndDistinguishExpression(canBeArrow) {
  512. // Assume this is a normal parenthesized expression, but if we see an arrow, we'll bail and
  513. // start over as a parameter list.
  514. const snapshot = _base.state.snapshot();
  515. const startTokenIndex = _base.state.tokens.length;
  516. _util.expect.call(void 0, _types3.TokenType.parenL);
  517. let first = true;
  518. while (!_index3.match.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
  519. if (first) {
  520. first = false;
  521. } else {
  522. _util.expect.call(void 0, _types3.TokenType.comma);
  523. if (_index3.match.call(void 0, _types3.TokenType.parenR)) {
  524. break;
  525. }
  526. }
  527. if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
  528. _lval.parseRest.call(void 0, false /* isBlockScope */);
  529. parseParenItem();
  530. break;
  531. } else {
  532. parseMaybeAssign(false, true);
  533. }
  534. }
  535. _util.expect.call(void 0, _types3.TokenType.parenR);
  536. if (canBeArrow && shouldParseArrow()) {
  537. const wasArrow = parseArrow();
  538. if (wasArrow) {
  539. // It was an arrow function this whole time, so start over and parse it as params so that we
  540. // get proper token annotations.
  541. _base.state.restoreFromSnapshot(snapshot);
  542. _base.state.scopeDepth++;
  543. // Don't specify a context ID because arrow functions don't need a context ID.
  544. _statement.parseFunctionParams.call(void 0, );
  545. parseArrow();
  546. parseArrowExpression(startTokenIndex);
  547. if (_base.state.error) {
  548. // Nevermind! This must have been something that looks very much like an
  549. // arrow function but where its "parameter list" isn't actually a valid
  550. // parameter list. Force non-arrow parsing.
  551. // See https://github.com/alangpierce/sucrase/issues/666 for an example.
  552. _base.state.restoreFromSnapshot(snapshot);
  553. parseParenAndDistinguishExpression(false);
  554. return false;
  555. }
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. function shouldParseArrow() {
  562. return _index3.match.call(void 0, _types3.TokenType.colon) || !_util.canInsertSemicolon.call(void 0, );
  563. }
  564. // Returns whether there was an arrow token.
  565. function parseArrow() {
  566. if (_base.isTypeScriptEnabled) {
  567. return _typescript.tsParseArrow.call(void 0, );
  568. } else if (_base.isFlowEnabled) {
  569. return _flow.flowParseArrow.call(void 0, );
  570. } else {
  571. return _index3.eat.call(void 0, _types3.TokenType.arrow);
  572. }
  573. } exports.parseArrow = parseArrow;
  574. function parseParenItem() {
  575. if (_base.isTypeScriptEnabled || _base.isFlowEnabled) {
  576. _types.typedParseParenItem.call(void 0, );
  577. }
  578. }
  579. // New's precedence is slightly tricky. It must allow its argument to
  580. // be a `[]` or dot subscript expression, but not a call — at least,
  581. // not without wrapping it in parentheses. Thus, it uses the noCalls
  582. // argument to parseSubscripts to prevent it from consuming the
  583. // argument list.
  584. function parseNew() {
  585. _util.expect.call(void 0, _types3.TokenType._new);
  586. if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
  587. // new.target
  588. parseIdentifier();
  589. return;
  590. }
  591. parseNewCallee();
  592. if (_base.isFlowEnabled) {
  593. _flow.flowStartParseNewArguments.call(void 0, );
  594. }
  595. if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
  596. parseExprList(_types3.TokenType.parenR);
  597. }
  598. }
  599. function parseNewCallee() {
  600. parseNoCallExpr();
  601. _index3.eat.call(void 0, _types3.TokenType.questionDot);
  602. }
  603. function parseTemplate() {
  604. // Finish `, read quasi
  605. _index3.nextTemplateToken.call(void 0, );
  606. // Finish quasi, read ${
  607. _index3.nextTemplateToken.call(void 0, );
  608. while (!_index3.match.call(void 0, _types3.TokenType.backQuote) && !_base.state.error) {
  609. _util.expect.call(void 0, _types3.TokenType.dollarBraceL);
  610. parseExpression();
  611. // Finish }, read quasi
  612. _index3.nextTemplateToken.call(void 0, );
  613. // Finish quasi, read either ${ or `
  614. _index3.nextTemplateToken.call(void 0, );
  615. }
  616. _index3.next.call(void 0, );
  617. } exports.parseTemplate = parseTemplate;
  618. // Parse an object literal or binding pattern.
  619. function parseObj(isPattern, isBlockScope) {
  620. // Attach a context ID to the object open and close brace and each object key.
  621. const contextId = _base.getNextContextId.call(void 0, );
  622. let first = true;
  623. _index3.next.call(void 0, );
  624. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  625. while (!_index3.eat.call(void 0, _types3.TokenType.braceR) && !_base.state.error) {
  626. if (first) {
  627. first = false;
  628. } else {
  629. _util.expect.call(void 0, _types3.TokenType.comma);
  630. if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
  631. break;
  632. }
  633. }
  634. let isGenerator = false;
  635. if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
  636. const previousIndex = _base.state.tokens.length;
  637. _lval.parseSpread.call(void 0, );
  638. if (isPattern) {
  639. // Mark role when the only thing being spread over is an identifier.
  640. if (_base.state.tokens.length === previousIndex + 2) {
  641. _lval.markPriorBindingIdentifier.call(void 0, isBlockScope);
  642. }
  643. if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
  644. break;
  645. }
  646. }
  647. continue;
  648. }
  649. if (!isPattern) {
  650. isGenerator = _index3.eat.call(void 0, _types3.TokenType.star);
  651. }
  652. if (!isPattern && _util.isContextual.call(void 0, _keywords.ContextualKeyword._async)) {
  653. if (isGenerator) _util.unexpected.call(void 0, );
  654. parseIdentifier();
  655. if (
  656. _index3.match.call(void 0, _types3.TokenType.colon) ||
  657. _index3.match.call(void 0, _types3.TokenType.parenL) ||
  658. _index3.match.call(void 0, _types3.TokenType.braceR) ||
  659. _index3.match.call(void 0, _types3.TokenType.eq) ||
  660. _index3.match.call(void 0, _types3.TokenType.comma)
  661. ) {
  662. // This is a key called "async" rather than an async function.
  663. } else {
  664. if (_index3.match.call(void 0, _types3.TokenType.star)) {
  665. _index3.next.call(void 0, );
  666. isGenerator = true;
  667. }
  668. parsePropertyName(contextId);
  669. }
  670. } else {
  671. parsePropertyName(contextId);
  672. }
  673. parseObjPropValue(isPattern, isBlockScope, contextId);
  674. }
  675. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  676. } exports.parseObj = parseObj;
  677. function isGetterOrSetterMethod(isPattern) {
  678. // We go off of the next and don't bother checking if the node key is actually "get" or "set".
  679. // This lets us avoid generating a node, and should only make the validation worse.
  680. return (
  681. !isPattern &&
  682. (_index3.match.call(void 0, _types3.TokenType.string) || // get "string"() {}
  683. _index3.match.call(void 0, _types3.TokenType.num) || // get 1() {}
  684. _index3.match.call(void 0, _types3.TokenType.bracketL) || // get ["string"]() {}
  685. _index3.match.call(void 0, _types3.TokenType.name) || // get foo() {}
  686. !!(_base.state.type & _types3.TokenType.IS_KEYWORD)) // get debugger() {}
  687. );
  688. }
  689. // Returns true if this was a method.
  690. function parseObjectMethod(isPattern, objectContextId) {
  691. // We don't need to worry about modifiers because object methods can't have optional bodies, so
  692. // the start will never be used.
  693. const functionStart = _base.state.start;
  694. if (_index3.match.call(void 0, _types3.TokenType.parenL)) {
  695. if (isPattern) _util.unexpected.call(void 0, );
  696. parseMethod(functionStart, /* isConstructor */ false);
  697. return true;
  698. }
  699. if (isGetterOrSetterMethod(isPattern)) {
  700. parsePropertyName(objectContextId);
  701. parseMethod(functionStart, /* isConstructor */ false);
  702. return true;
  703. }
  704. return false;
  705. }
  706. function parseObjectProperty(isPattern, isBlockScope) {
  707. if (_index3.eat.call(void 0, _types3.TokenType.colon)) {
  708. if (isPattern) {
  709. _lval.parseMaybeDefault.call(void 0, isBlockScope);
  710. } else {
  711. parseMaybeAssign(false);
  712. }
  713. return;
  714. }
  715. // Since there's no colon, we assume this is an object shorthand.
  716. // If we're in a destructuring, we've now discovered that the key was actually an assignee, so
  717. // we need to tag it as a declaration with the appropriate scope. Otherwise, we might need to
  718. // transform it on access, so mark it as a normal object shorthand.
  719. let identifierRole;
  720. if (isPattern) {
  721. if (_base.state.scopeDepth === 0) {
  722. identifierRole = _index3.IdentifierRole.ObjectShorthandTopLevelDeclaration;
  723. } else if (isBlockScope) {
  724. identifierRole = _index3.IdentifierRole.ObjectShorthandBlockScopedDeclaration;
  725. } else {
  726. identifierRole = _index3.IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
  727. }
  728. } else {
  729. identifierRole = _index3.IdentifierRole.ObjectShorthand;
  730. }
  731. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = identifierRole;
  732. // Regardless of whether we know this to be a pattern or if we're in an ambiguous context, allow
  733. // parsing as if there's a default value.
  734. _lval.parseMaybeDefault.call(void 0, isBlockScope, true);
  735. }
  736. function parseObjPropValue(
  737. isPattern,
  738. isBlockScope,
  739. objectContextId,
  740. ) {
  741. if (_base.isTypeScriptEnabled) {
  742. _typescript.tsStartParseObjPropValue.call(void 0, );
  743. } else if (_base.isFlowEnabled) {
  744. _flow.flowStartParseObjPropValue.call(void 0, );
  745. }
  746. const wasMethod = parseObjectMethod(isPattern, objectContextId);
  747. if (!wasMethod) {
  748. parseObjectProperty(isPattern, isBlockScope);
  749. }
  750. }
  751. function parsePropertyName(objectContextId) {
  752. if (_base.isFlowEnabled) {
  753. _flow.flowParseVariance.call(void 0, );
  754. }
  755. if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
  756. _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
  757. parseMaybeAssign();
  758. _util.expect.call(void 0, _types3.TokenType.bracketR);
  759. _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
  760. } else {
  761. if (_index3.match.call(void 0, _types3.TokenType.num) || _index3.match.call(void 0, _types3.TokenType.string) || _index3.match.call(void 0, _types3.TokenType.bigint) || _index3.match.call(void 0, _types3.TokenType.decimal)) {
  762. parseExprAtom();
  763. } else {
  764. parseMaybePrivateName();
  765. }
  766. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.ObjectKey;
  767. _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
  768. }
  769. } exports.parsePropertyName = parsePropertyName;
  770. // Parse object or class method.
  771. function parseMethod(functionStart, isConstructor) {
  772. const funcContextId = _base.getNextContextId.call(void 0, );
  773. _base.state.scopeDepth++;
  774. const startTokenIndex = _base.state.tokens.length;
  775. const allowModifiers = isConstructor; // For TypeScript parameter properties
  776. _statement.parseFunctionParams.call(void 0, allowModifiers, funcContextId);
  777. parseFunctionBodyAndFinish(functionStart, funcContextId);
  778. const endTokenIndex = _base.state.tokens.length;
  779. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
  780. _base.state.scopeDepth--;
  781. } exports.parseMethod = parseMethod;
  782. // Parse arrow function expression.
  783. // If the parameters are provided, they will be converted to an
  784. // assignable list.
  785. function parseArrowExpression(startTokenIndex) {
  786. parseFunctionBody(true);
  787. const endTokenIndex = _base.state.tokens.length;
  788. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
  789. _base.state.scopeDepth--;
  790. } exports.parseArrowExpression = parseArrowExpression;
  791. function parseFunctionBodyAndFinish(functionStart, funcContextId = 0) {
  792. if (_base.isTypeScriptEnabled) {
  793. _typescript.tsParseFunctionBodyAndFinish.call(void 0, functionStart, funcContextId);
  794. } else if (_base.isFlowEnabled) {
  795. _flow.flowParseFunctionBodyAndFinish.call(void 0, funcContextId);
  796. } else {
  797. parseFunctionBody(false, funcContextId);
  798. }
  799. } exports.parseFunctionBodyAndFinish = parseFunctionBodyAndFinish;
  800. function parseFunctionBody(allowExpression, funcContextId = 0) {
  801. const isExpression = allowExpression && !_index3.match.call(void 0, _types3.TokenType.braceL);
  802. if (isExpression) {
  803. parseMaybeAssign();
  804. } else {
  805. _statement.parseBlock.call(void 0, true /* isFunctionScope */, funcContextId);
  806. }
  807. } exports.parseFunctionBody = parseFunctionBody;
  808. // Parses a comma-separated list of expressions, and returns them as
  809. // an array. `close` is the token type that ends the list, and
  810. // `allowEmpty` can be turned on to allow subsequent commas with
  811. // nothing in between them to be parsed as `null` (which is needed
  812. // for array literals).
  813. function parseExprList(close, allowEmpty = false) {
  814. let first = true;
  815. while (!_index3.eat.call(void 0, close) && !_base.state.error) {
  816. if (first) {
  817. first = false;
  818. } else {
  819. _util.expect.call(void 0, _types3.TokenType.comma);
  820. if (_index3.eat.call(void 0, close)) break;
  821. }
  822. parseExprListItem(allowEmpty);
  823. }
  824. }
  825. function parseExprListItem(allowEmpty) {
  826. if (allowEmpty && _index3.match.call(void 0, _types3.TokenType.comma)) {
  827. // Empty item; nothing more to parse for this item.
  828. } else if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
  829. _lval.parseSpread.call(void 0, );
  830. parseParenItem();
  831. } else if (_index3.match.call(void 0, _types3.TokenType.question)) {
  832. // Partial function application proposal.
  833. _index3.next.call(void 0, );
  834. } else {
  835. parseMaybeAssign(false, true);
  836. }
  837. }
  838. // Parse the next token as an identifier.
  839. function parseIdentifier() {
  840. _index3.next.call(void 0, );
  841. _base.state.tokens[_base.state.tokens.length - 1].type = _types3.TokenType.name;
  842. } exports.parseIdentifier = parseIdentifier;
  843. // Parses await expression inside async function.
  844. function parseAwait() {
  845. parseMaybeUnary();
  846. }
  847. // Parses yield expression inside generator.
  848. function parseYield() {
  849. _index3.next.call(void 0, );
  850. if (!_index3.match.call(void 0, _types3.TokenType.semi) && !_util.canInsertSemicolon.call(void 0, )) {
  851. _index3.eat.call(void 0, _types3.TokenType.star);
  852. parseMaybeAssign();
  853. }
  854. }
  855. // https://github.com/tc39/proposal-js-module-blocks
  856. function parseModuleExpression() {
  857. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._module);
  858. _util.expect.call(void 0, _types3.TokenType.braceL);
  859. // For now, just call parseBlockBody to parse the block. In the future when we
  860. // implement full support, we'll want to emit scopes and possibly other
  861. // information.
  862. _statement.parseBlockBody.call(void 0, _types3.TokenType.braceR);
  863. }