expression.js 28 KB

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