statement.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
  2. var _index = require('../index');
  3. var _flow = require('../plugins/flow');
  4. var _typescript = require('../plugins/typescript');
  5. var _tokenizer = require('../tokenizer');
  6. var _keywords = require('../tokenizer/keywords');
  7. var _state = require('../tokenizer/state');
  8. var _types = require('../tokenizer/types');
  9. var _charcodes = require('../util/charcodes');
  10. var _base = require('./base');
  11. var _expression = require('./expression');
  12. var _lval = require('./lval');
  13. var _util = require('./util');
  14. function parseTopLevel() {
  15. parseBlockBody(_types.TokenType.eof);
  16. _base.state.scopes.push(new (0, _state.Scope)(0, _base.state.tokens.length, true));
  17. if (_base.state.scopeDepth !== 0) {
  18. throw new Error(`Invalid scope depth at end of file: ${_base.state.scopeDepth}`);
  19. }
  20. return new (0, _index.File)(_base.state.tokens, _base.state.scopes);
  21. } exports.parseTopLevel = parseTopLevel;
  22. // Parse a single statement.
  23. //
  24. // If expecting a statement and finding a slash operator, parse a
  25. // regular expression literal. This is to handle cases like
  26. // `if (foo) /blah/.exec(foo)`, where looking at the previous token
  27. // does not help.
  28. function parseStatement(declaration) {
  29. if (_base.isFlowEnabled) {
  30. if (_flow.flowTryParseStatement.call(void 0, )) {
  31. return;
  32. }
  33. }
  34. if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
  35. parseDecorators();
  36. }
  37. parseStatementContent(declaration);
  38. } exports.parseStatement = parseStatement;
  39. function parseStatementContent(declaration) {
  40. if (_base.isTypeScriptEnabled) {
  41. if (_typescript.tsTryParseStatementContent.call(void 0, )) {
  42. return;
  43. }
  44. }
  45. const starttype = _base.state.type;
  46. // Most types of statements are recognized by the keyword they
  47. // start with. Many are trivial to parse, some require a bit of
  48. // complexity.
  49. switch (starttype) {
  50. case _types.TokenType._break:
  51. case _types.TokenType._continue:
  52. parseBreakContinueStatement();
  53. return;
  54. case _types.TokenType._debugger:
  55. parseDebuggerStatement();
  56. return;
  57. case _types.TokenType._do:
  58. parseDoStatement();
  59. return;
  60. case _types.TokenType._for:
  61. parseForStatement();
  62. return;
  63. case _types.TokenType._function:
  64. if (_tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.dot) break;
  65. if (!declaration) _util.unexpected.call(void 0, );
  66. parseFunctionStatement();
  67. return;
  68. case _types.TokenType._class:
  69. if (!declaration) _util.unexpected.call(void 0, );
  70. parseClass(true);
  71. return;
  72. case _types.TokenType._if:
  73. parseIfStatement();
  74. return;
  75. case _types.TokenType._return:
  76. parseReturnStatement();
  77. return;
  78. case _types.TokenType._switch:
  79. parseSwitchStatement();
  80. return;
  81. case _types.TokenType._throw:
  82. parseThrowStatement();
  83. return;
  84. case _types.TokenType._try:
  85. parseTryStatement();
  86. return;
  87. case _types.TokenType._let:
  88. case _types.TokenType._const:
  89. if (!declaration) _util.unexpected.call(void 0, ); // NOTE: falls through to _var
  90. case _types.TokenType._var:
  91. parseVarStatement(starttype !== _types.TokenType._var);
  92. return;
  93. case _types.TokenType._while:
  94. parseWhileStatement();
  95. return;
  96. case _types.TokenType.braceL:
  97. parseBlock();
  98. return;
  99. case _types.TokenType.semi:
  100. parseEmptyStatement();
  101. return;
  102. case _types.TokenType._export:
  103. case _types.TokenType._import: {
  104. const nextType = _tokenizer.lookaheadType.call(void 0, );
  105. if (nextType === _types.TokenType.parenL || nextType === _types.TokenType.dot) {
  106. break;
  107. }
  108. _tokenizer.next.call(void 0, );
  109. if (starttype === _types.TokenType._import) {
  110. parseImport();
  111. } else {
  112. parseExport();
  113. }
  114. return;
  115. }
  116. case _types.TokenType.name:
  117. if (_base.state.contextualKeyword === _keywords.ContextualKeyword._async) {
  118. const functionStart = _base.state.start;
  119. // peek ahead and see if next token is a function
  120. const snapshot = _base.state.snapshot();
  121. _tokenizer.next.call(void 0, );
  122. if (_tokenizer.match.call(void 0, _types.TokenType._function) && !_util.canInsertSemicolon.call(void 0, )) {
  123. _util.expect.call(void 0, _types.TokenType._function);
  124. parseFunction(functionStart, true);
  125. return;
  126. } else {
  127. _base.state.restoreFromSnapshot(snapshot);
  128. }
  129. } else if (
  130. _base.state.contextualKeyword === _keywords.ContextualKeyword._using &&
  131. !_util.hasFollowingLineBreak.call(void 0, ) &&
  132. // Statements like `using[0]` and `using in foo` aren't actual using
  133. // declarations.
  134. _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.name
  135. ) {
  136. parseVarStatement(true);
  137. return;
  138. } else if (startsAwaitUsing()) {
  139. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._await);
  140. parseVarStatement(true);
  141. return;
  142. }
  143. default:
  144. // Do nothing.
  145. break;
  146. }
  147. // If the statement does not start with a statement keyword or a
  148. // brace, it's an ExpressionStatement or LabeledStatement. We
  149. // simply start parsing an expression, and afterwards, if the
  150. // next token is a colon and the expression was a simple
  151. // Identifier node, we switch to interpreting it as a label.
  152. const initialTokensLength = _base.state.tokens.length;
  153. _expression.parseExpression.call(void 0, );
  154. let simpleName = null;
  155. if (_base.state.tokens.length === initialTokensLength + 1) {
  156. const token = _base.state.tokens[_base.state.tokens.length - 1];
  157. if (token.type === _types.TokenType.name) {
  158. simpleName = token.contextualKeyword;
  159. }
  160. }
  161. if (simpleName == null) {
  162. _util.semicolon.call(void 0, );
  163. return;
  164. }
  165. if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
  166. parseLabeledStatement();
  167. } else {
  168. // This was an identifier, so we might want to handle flow/typescript-specific cases.
  169. parseIdentifierStatement(simpleName);
  170. }
  171. }
  172. /**
  173. * Determine if we're positioned at an `await using` declaration.
  174. *
  175. * Note that this can happen either in place of a regular variable declaration
  176. * or in a loop body, and in both places, there are similar-looking cases where
  177. * we need to return false.
  178. *
  179. * Examples returning true:
  180. * await using foo = bar();
  181. * for (await using a of b) {}
  182. *
  183. * Examples returning false:
  184. * await using
  185. * await using + 1
  186. * await using instanceof T
  187. * for (await using;;) {}
  188. *
  189. * For now, we early return if we don't see `await`, then do a simple
  190. * backtracking-based lookahead for the `using` and identifier tokens. In the
  191. * future, this could be optimized with a character-based approach.
  192. */
  193. function startsAwaitUsing() {
  194. if (!_util.isContextual.call(void 0, _keywords.ContextualKeyword._await)) {
  195. return false;
  196. }
  197. const snapshot = _base.state.snapshot();
  198. // await
  199. _tokenizer.next.call(void 0, );
  200. if (!_util.isContextual.call(void 0, _keywords.ContextualKeyword._using) || _util.hasPrecedingLineBreak.call(void 0, )) {
  201. _base.state.restoreFromSnapshot(snapshot);
  202. return false;
  203. }
  204. // using
  205. _tokenizer.next.call(void 0, );
  206. if (!_tokenizer.match.call(void 0, _types.TokenType.name) || _util.hasPrecedingLineBreak.call(void 0, )) {
  207. _base.state.restoreFromSnapshot(snapshot);
  208. return false;
  209. }
  210. _base.state.restoreFromSnapshot(snapshot);
  211. return true;
  212. }
  213. function parseDecorators() {
  214. while (_tokenizer.match.call(void 0, _types.TokenType.at)) {
  215. parseDecorator();
  216. }
  217. } exports.parseDecorators = parseDecorators;
  218. function parseDecorator() {
  219. _tokenizer.next.call(void 0, );
  220. if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
  221. _expression.parseExpression.call(void 0, );
  222. _util.expect.call(void 0, _types.TokenType.parenR);
  223. } else {
  224. _expression.parseIdentifier.call(void 0, );
  225. while (_tokenizer.eat.call(void 0, _types.TokenType.dot)) {
  226. _expression.parseIdentifier.call(void 0, );
  227. }
  228. parseMaybeDecoratorArguments();
  229. }
  230. }
  231. function parseMaybeDecoratorArguments() {
  232. if (_base.isTypeScriptEnabled) {
  233. _typescript.tsParseMaybeDecoratorArguments.call(void 0, );
  234. } else {
  235. baseParseMaybeDecoratorArguments();
  236. }
  237. }
  238. function baseParseMaybeDecoratorArguments() {
  239. if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
  240. _expression.parseCallExpressionArguments.call(void 0, );
  241. }
  242. } exports.baseParseMaybeDecoratorArguments = baseParseMaybeDecoratorArguments;
  243. function parseBreakContinueStatement() {
  244. _tokenizer.next.call(void 0, );
  245. if (!_util.isLineTerminator.call(void 0, )) {
  246. _expression.parseIdentifier.call(void 0, );
  247. _util.semicolon.call(void 0, );
  248. }
  249. }
  250. function parseDebuggerStatement() {
  251. _tokenizer.next.call(void 0, );
  252. _util.semicolon.call(void 0, );
  253. }
  254. function parseDoStatement() {
  255. _tokenizer.next.call(void 0, );
  256. parseStatement(false);
  257. _util.expect.call(void 0, _types.TokenType._while);
  258. _expression.parseParenExpression.call(void 0, );
  259. _tokenizer.eat.call(void 0, _types.TokenType.semi);
  260. }
  261. function parseForStatement() {
  262. _base.state.scopeDepth++;
  263. const startTokenIndex = _base.state.tokens.length;
  264. parseAmbiguousForStatement();
  265. const endTokenIndex = _base.state.tokens.length;
  266. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
  267. _base.state.scopeDepth--;
  268. }
  269. /**
  270. * Determine if this token is a `using` declaration (explicit resource
  271. * management) as part of a loop.
  272. * https://github.com/tc39/proposal-explicit-resource-management
  273. */
  274. function isUsingInLoop() {
  275. if (!_util.isContextual.call(void 0, _keywords.ContextualKeyword._using)) {
  276. return false;
  277. }
  278. // This must be `for (using of`, where `using` is the name of the loop
  279. // variable.
  280. if (_util.isLookaheadContextual.call(void 0, _keywords.ContextualKeyword._of)) {
  281. return false;
  282. }
  283. return true;
  284. }
  285. // Disambiguating between a `for` and a `for`/`in` or `for`/`of`
  286. // loop is non-trivial. Basically, we have to parse the init `var`
  287. // statement or expression, disallowing the `in` operator (see
  288. // the second parameter to `parseExpression`), and then check
  289. // whether the next token is `in` or `of`. When there is no init
  290. // part (semicolon immediately after the opening parenthesis), it
  291. // is a regular `for` loop.
  292. function parseAmbiguousForStatement() {
  293. _tokenizer.next.call(void 0, );
  294. let forAwait = false;
  295. if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._await)) {
  296. forAwait = true;
  297. _tokenizer.next.call(void 0, );
  298. }
  299. _util.expect.call(void 0, _types.TokenType.parenL);
  300. if (_tokenizer.match.call(void 0, _types.TokenType.semi)) {
  301. if (forAwait) {
  302. _util.unexpected.call(void 0, );
  303. }
  304. parseFor();
  305. return;
  306. }
  307. const isAwaitUsing = startsAwaitUsing();
  308. if (isAwaitUsing || _tokenizer.match.call(void 0, _types.TokenType._var) || _tokenizer.match.call(void 0, _types.TokenType._let) || _tokenizer.match.call(void 0, _types.TokenType._const) || isUsingInLoop()) {
  309. if (isAwaitUsing) {
  310. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._await);
  311. }
  312. _tokenizer.next.call(void 0, );
  313. parseVar(true, _base.state.type !== _types.TokenType._var);
  314. if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
  315. parseForIn(forAwait);
  316. return;
  317. }
  318. parseFor();
  319. return;
  320. }
  321. _expression.parseExpression.call(void 0, true);
  322. if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
  323. parseForIn(forAwait);
  324. return;
  325. }
  326. if (forAwait) {
  327. _util.unexpected.call(void 0, );
  328. }
  329. parseFor();
  330. }
  331. function parseFunctionStatement() {
  332. const functionStart = _base.state.start;
  333. _tokenizer.next.call(void 0, );
  334. parseFunction(functionStart, true);
  335. }
  336. function parseIfStatement() {
  337. _tokenizer.next.call(void 0, );
  338. _expression.parseParenExpression.call(void 0, );
  339. parseStatement(false);
  340. if (_tokenizer.eat.call(void 0, _types.TokenType._else)) {
  341. parseStatement(false);
  342. }
  343. }
  344. function parseReturnStatement() {
  345. _tokenizer.next.call(void 0, );
  346. // In `return` (and `break`/`continue`), the keywords with
  347. // optional arguments, we eagerly look for a semicolon or the
  348. // possibility to insert one.
  349. if (!_util.isLineTerminator.call(void 0, )) {
  350. _expression.parseExpression.call(void 0, );
  351. _util.semicolon.call(void 0, );
  352. }
  353. }
  354. function parseSwitchStatement() {
  355. _tokenizer.next.call(void 0, );
  356. _expression.parseParenExpression.call(void 0, );
  357. _base.state.scopeDepth++;
  358. const startTokenIndex = _base.state.tokens.length;
  359. _util.expect.call(void 0, _types.TokenType.braceL);
  360. // Don't bother validation; just go through any sequence of cases, defaults, and statements.
  361. while (!_tokenizer.match.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
  362. if (_tokenizer.match.call(void 0, _types.TokenType._case) || _tokenizer.match.call(void 0, _types.TokenType._default)) {
  363. const isCase = _tokenizer.match.call(void 0, _types.TokenType._case);
  364. _tokenizer.next.call(void 0, );
  365. if (isCase) {
  366. _expression.parseExpression.call(void 0, );
  367. }
  368. _util.expect.call(void 0, _types.TokenType.colon);
  369. } else {
  370. parseStatement(true);
  371. }
  372. }
  373. _tokenizer.next.call(void 0, ); // Closing brace
  374. const endTokenIndex = _base.state.tokens.length;
  375. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
  376. _base.state.scopeDepth--;
  377. }
  378. function parseThrowStatement() {
  379. _tokenizer.next.call(void 0, );
  380. _expression.parseExpression.call(void 0, );
  381. _util.semicolon.call(void 0, );
  382. }
  383. function parseCatchClauseParam() {
  384. _lval.parseBindingAtom.call(void 0, true /* isBlockScope */);
  385. if (_base.isTypeScriptEnabled) {
  386. _typescript.tsTryParseTypeAnnotation.call(void 0, );
  387. }
  388. }
  389. function parseTryStatement() {
  390. _tokenizer.next.call(void 0, );
  391. parseBlock();
  392. if (_tokenizer.match.call(void 0, _types.TokenType._catch)) {
  393. _tokenizer.next.call(void 0, );
  394. let catchBindingStartTokenIndex = null;
  395. if (_tokenizer.match.call(void 0, _types.TokenType.parenL)) {
  396. _base.state.scopeDepth++;
  397. catchBindingStartTokenIndex = _base.state.tokens.length;
  398. _util.expect.call(void 0, _types.TokenType.parenL);
  399. parseCatchClauseParam();
  400. _util.expect.call(void 0, _types.TokenType.parenR);
  401. }
  402. parseBlock();
  403. if (catchBindingStartTokenIndex != null) {
  404. // We need a special scope for the catch binding which includes the binding itself and the
  405. // catch block.
  406. const endTokenIndex = _base.state.tokens.length;
  407. _base.state.scopes.push(new (0, _state.Scope)(catchBindingStartTokenIndex, endTokenIndex, false));
  408. _base.state.scopeDepth--;
  409. }
  410. }
  411. if (_tokenizer.eat.call(void 0, _types.TokenType._finally)) {
  412. parseBlock();
  413. }
  414. }
  415. function parseVarStatement(isBlockScope) {
  416. _tokenizer.next.call(void 0, );
  417. parseVar(false, isBlockScope);
  418. _util.semicolon.call(void 0, );
  419. } exports.parseVarStatement = parseVarStatement;
  420. function parseWhileStatement() {
  421. _tokenizer.next.call(void 0, );
  422. _expression.parseParenExpression.call(void 0, );
  423. parseStatement(false);
  424. }
  425. function parseEmptyStatement() {
  426. _tokenizer.next.call(void 0, );
  427. }
  428. function parseLabeledStatement() {
  429. parseStatement(true);
  430. }
  431. /**
  432. * Parse a statement starting with an identifier of the given name. Subclasses match on the name
  433. * to handle statements like "declare".
  434. */
  435. function parseIdentifierStatement(contextualKeyword) {
  436. if (_base.isTypeScriptEnabled) {
  437. _typescript.tsParseIdentifierStatement.call(void 0, contextualKeyword);
  438. } else if (_base.isFlowEnabled) {
  439. _flow.flowParseIdentifierStatement.call(void 0, contextualKeyword);
  440. } else {
  441. _util.semicolon.call(void 0, );
  442. }
  443. }
  444. // Parse a semicolon-enclosed block of statements.
  445. function parseBlock(isFunctionScope = false, contextId = 0) {
  446. const startTokenIndex = _base.state.tokens.length;
  447. _base.state.scopeDepth++;
  448. _util.expect.call(void 0, _types.TokenType.braceL);
  449. if (contextId) {
  450. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  451. }
  452. parseBlockBody(_types.TokenType.braceR);
  453. if (contextId) {
  454. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  455. }
  456. const endTokenIndex = _base.state.tokens.length;
  457. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, isFunctionScope));
  458. _base.state.scopeDepth--;
  459. } exports.parseBlock = parseBlock;
  460. function parseBlockBody(end) {
  461. while (!_tokenizer.eat.call(void 0, end) && !_base.state.error) {
  462. parseStatement(true);
  463. }
  464. } exports.parseBlockBody = parseBlockBody;
  465. // Parse a regular `for` loop. The disambiguation code in
  466. // `parseStatement` will already have parsed the init statement or
  467. // expression.
  468. function parseFor() {
  469. _util.expect.call(void 0, _types.TokenType.semi);
  470. if (!_tokenizer.match.call(void 0, _types.TokenType.semi)) {
  471. _expression.parseExpression.call(void 0, );
  472. }
  473. _util.expect.call(void 0, _types.TokenType.semi);
  474. if (!_tokenizer.match.call(void 0, _types.TokenType.parenR)) {
  475. _expression.parseExpression.call(void 0, );
  476. }
  477. _util.expect.call(void 0, _types.TokenType.parenR);
  478. parseStatement(false);
  479. }
  480. // Parse a `for`/`in` and `for`/`of` loop, which are almost
  481. // same from parser's perspective.
  482. function parseForIn(forAwait) {
  483. if (forAwait) {
  484. _util.eatContextual.call(void 0, _keywords.ContextualKeyword._of);
  485. } else {
  486. _tokenizer.next.call(void 0, );
  487. }
  488. _expression.parseExpression.call(void 0, );
  489. _util.expect.call(void 0, _types.TokenType.parenR);
  490. parseStatement(false);
  491. }
  492. // Parse a list of variable declarations.
  493. function parseVar(isFor, isBlockScope) {
  494. while (true) {
  495. parseVarHead(isBlockScope);
  496. if (_tokenizer.eat.call(void 0, _types.TokenType.eq)) {
  497. const eqIndex = _base.state.tokens.length - 1;
  498. _expression.parseMaybeAssign.call(void 0, isFor);
  499. _base.state.tokens[eqIndex].rhsEndIndex = _base.state.tokens.length;
  500. }
  501. if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
  502. break;
  503. }
  504. }
  505. }
  506. function parseVarHead(isBlockScope) {
  507. _lval.parseBindingAtom.call(void 0, isBlockScope);
  508. if (_base.isTypeScriptEnabled) {
  509. _typescript.tsAfterParseVarHead.call(void 0, );
  510. } else if (_base.isFlowEnabled) {
  511. _flow.flowAfterParseVarHead.call(void 0, );
  512. }
  513. }
  514. // Parse a function declaration or literal (depending on the
  515. // `isStatement` parameter).
  516. function parseFunction(
  517. functionStart,
  518. isStatement,
  519. optionalId = false,
  520. ) {
  521. if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
  522. _tokenizer.next.call(void 0, );
  523. }
  524. if (isStatement && !optionalId && !_tokenizer.match.call(void 0, _types.TokenType.name) && !_tokenizer.match.call(void 0, _types.TokenType._yield)) {
  525. _util.unexpected.call(void 0, );
  526. }
  527. let nameScopeStartTokenIndex = null;
  528. if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
  529. // Expression-style functions should limit their name's scope to the function body, so we make
  530. // a new function scope to enforce that.
  531. if (!isStatement) {
  532. nameScopeStartTokenIndex = _base.state.tokens.length;
  533. _base.state.scopeDepth++;
  534. }
  535. _lval.parseBindingIdentifier.call(void 0, false);
  536. }
  537. const startTokenIndex = _base.state.tokens.length;
  538. _base.state.scopeDepth++;
  539. parseFunctionParams();
  540. _expression.parseFunctionBodyAndFinish.call(void 0, functionStart);
  541. const endTokenIndex = _base.state.tokens.length;
  542. // In addition to the block scope of the function body, we need a separate function-style scope
  543. // that includes the params.
  544. _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
  545. _base.state.scopeDepth--;
  546. if (nameScopeStartTokenIndex !== null) {
  547. _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, true));
  548. _base.state.scopeDepth--;
  549. }
  550. } exports.parseFunction = parseFunction;
  551. function parseFunctionParams(
  552. allowModifiers = false,
  553. funcContextId = 0,
  554. ) {
  555. if (_base.isTypeScriptEnabled) {
  556. _typescript.tsStartParseFunctionParams.call(void 0, );
  557. } else if (_base.isFlowEnabled) {
  558. _flow.flowStartParseFunctionParams.call(void 0, );
  559. }
  560. _util.expect.call(void 0, _types.TokenType.parenL);
  561. if (funcContextId) {
  562. _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
  563. }
  564. _lval.parseBindingList.call(void 0,
  565. _types.TokenType.parenR,
  566. false /* isBlockScope */,
  567. false /* allowEmpty */,
  568. allowModifiers,
  569. funcContextId,
  570. );
  571. if (funcContextId) {
  572. _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
  573. }
  574. } exports.parseFunctionParams = parseFunctionParams;
  575. // Parse a class declaration or literal (depending on the
  576. // `isStatement` parameter).
  577. function parseClass(isStatement, optionalId = false) {
  578. // Put a context ID on the class keyword, the open-brace, and the close-brace, so that later
  579. // code can easily navigate to meaningful points on the class.
  580. const contextId = _base.getNextContextId.call(void 0, );
  581. _tokenizer.next.call(void 0, );
  582. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  583. _base.state.tokens[_base.state.tokens.length - 1].isExpression = !isStatement;
  584. // Like with functions, we declare a special "name scope" from the start of the name to the end
  585. // of the class, but only with expression-style classes, to represent the fact that the name is
  586. // available to the body of the class but not an outer declaration.
  587. let nameScopeStartTokenIndex = null;
  588. if (!isStatement) {
  589. nameScopeStartTokenIndex = _base.state.tokens.length;
  590. _base.state.scopeDepth++;
  591. }
  592. parseClassId(isStatement, optionalId);
  593. parseClassSuper();
  594. const openBraceIndex = _base.state.tokens.length;
  595. parseClassBody(contextId);
  596. if (_base.state.error) {
  597. return;
  598. }
  599. _base.state.tokens[openBraceIndex].contextId = contextId;
  600. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  601. if (nameScopeStartTokenIndex !== null) {
  602. const endTokenIndex = _base.state.tokens.length;
  603. _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, false));
  604. _base.state.scopeDepth--;
  605. }
  606. } exports.parseClass = parseClass;
  607. function isClassProperty() {
  608. return _tokenizer.match.call(void 0, _types.TokenType.eq) || _tokenizer.match.call(void 0, _types.TokenType.semi) || _tokenizer.match.call(void 0, _types.TokenType.braceR) || _tokenizer.match.call(void 0, _types.TokenType.bang) || _tokenizer.match.call(void 0, _types.TokenType.colon);
  609. }
  610. function isClassMethod() {
  611. return _tokenizer.match.call(void 0, _types.TokenType.parenL) || _tokenizer.match.call(void 0, _types.TokenType.lessThan);
  612. }
  613. function parseClassBody(classContextId) {
  614. _util.expect.call(void 0, _types.TokenType.braceL);
  615. while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
  616. if (_tokenizer.eat.call(void 0, _types.TokenType.semi)) {
  617. continue;
  618. }
  619. if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
  620. parseDecorator();
  621. continue;
  622. }
  623. const memberStart = _base.state.start;
  624. parseClassMember(memberStart, classContextId);
  625. }
  626. }
  627. function parseClassMember(memberStart, classContextId) {
  628. if (_base.isTypeScriptEnabled) {
  629. _typescript.tsParseModifiers.call(void 0, [
  630. _keywords.ContextualKeyword._declare,
  631. _keywords.ContextualKeyword._public,
  632. _keywords.ContextualKeyword._protected,
  633. _keywords.ContextualKeyword._private,
  634. _keywords.ContextualKeyword._override,
  635. ]);
  636. }
  637. let isStatic = false;
  638. if (_tokenizer.match.call(void 0, _types.TokenType.name) && _base.state.contextualKeyword === _keywords.ContextualKeyword._static) {
  639. _expression.parseIdentifier.call(void 0, ); // eats 'static'
  640. if (isClassMethod()) {
  641. parseClassMethod(memberStart, /* isConstructor */ false);
  642. return;
  643. } else if (isClassProperty()) {
  644. parseClassProperty();
  645. return;
  646. }
  647. // otherwise something static
  648. _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._static;
  649. isStatic = true;
  650. if (_tokenizer.match.call(void 0, _types.TokenType.braceL)) {
  651. // This is a static block. Mark the word "static" with the class context ID for class element
  652. // detection and parse as a regular block.
  653. _base.state.tokens[_base.state.tokens.length - 1].contextId = classContextId;
  654. parseBlock();
  655. return;
  656. }
  657. }
  658. parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
  659. }
  660. function parseClassMemberWithIsStatic(
  661. memberStart,
  662. isStatic,
  663. classContextId,
  664. ) {
  665. if (_base.isTypeScriptEnabled) {
  666. if (_typescript.tsTryParseClassMemberWithIsStatic.call(void 0, isStatic)) {
  667. return;
  668. }
  669. }
  670. if (_tokenizer.eat.call(void 0, _types.TokenType.star)) {
  671. // a generator
  672. parseClassPropertyName(classContextId);
  673. parseClassMethod(memberStart, /* isConstructor */ false);
  674. return;
  675. }
  676. // Get the identifier name so we can tell if it's actually a keyword like "async", "get", or
  677. // "set".
  678. parseClassPropertyName(classContextId);
  679. let isConstructor = false;
  680. const token = _base.state.tokens[_base.state.tokens.length - 1];
  681. // We allow "constructor" as either an identifier or a string.
  682. if (token.contextualKeyword === _keywords.ContextualKeyword._constructor) {
  683. isConstructor = true;
  684. }
  685. parsePostMemberNameModifiers();
  686. if (isClassMethod()) {
  687. parseClassMethod(memberStart, isConstructor);
  688. } else if (isClassProperty()) {
  689. parseClassProperty();
  690. } else if (token.contextualKeyword === _keywords.ContextualKeyword._async && !_util.isLineTerminator.call(void 0, )) {
  691. _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._async;
  692. // an async method
  693. const isGenerator = _tokenizer.match.call(void 0, _types.TokenType.star);
  694. if (isGenerator) {
  695. _tokenizer.next.call(void 0, );
  696. }
  697. // The so-called parsed name would have been "async": get the real name.
  698. parseClassPropertyName(classContextId);
  699. parsePostMemberNameModifiers();
  700. parseClassMethod(memberStart, false /* isConstructor */);
  701. } else if (
  702. (token.contextualKeyword === _keywords.ContextualKeyword._get ||
  703. token.contextualKeyword === _keywords.ContextualKeyword._set) &&
  704. !(_util.isLineTerminator.call(void 0, ) && _tokenizer.match.call(void 0, _types.TokenType.star))
  705. ) {
  706. if (token.contextualKeyword === _keywords.ContextualKeyword._get) {
  707. _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._get;
  708. } else {
  709. _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._set;
  710. }
  711. // `get\n*` is an uninitialized property named 'get' followed by a generator.
  712. // a getter or setter
  713. // The so-called parsed name would have been "get/set": get the real name.
  714. parseClassPropertyName(classContextId);
  715. parseClassMethod(memberStart, /* isConstructor */ false);
  716. } else if (token.contextualKeyword === _keywords.ContextualKeyword._accessor && !_util.isLineTerminator.call(void 0, )) {
  717. parseClassPropertyName(classContextId);
  718. parseClassProperty();
  719. } else if (_util.isLineTerminator.call(void 0, )) {
  720. // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
  721. parseClassProperty();
  722. } else {
  723. _util.unexpected.call(void 0, );
  724. }
  725. }
  726. function parseClassMethod(functionStart, isConstructor) {
  727. if (_base.isTypeScriptEnabled) {
  728. _typescript.tsTryParseTypeParameters.call(void 0, );
  729. } else if (_base.isFlowEnabled) {
  730. if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
  731. _flow.flowParseTypeParameterDeclaration.call(void 0, );
  732. }
  733. }
  734. _expression.parseMethod.call(void 0, functionStart, isConstructor);
  735. }
  736. // Return the name of the class property, if it is a simple identifier.
  737. function parseClassPropertyName(classContextId) {
  738. _expression.parsePropertyName.call(void 0, classContextId);
  739. } exports.parseClassPropertyName = parseClassPropertyName;
  740. function parsePostMemberNameModifiers() {
  741. if (_base.isTypeScriptEnabled) {
  742. const oldIsType = _tokenizer.pushTypeContext.call(void 0, 0);
  743. _tokenizer.eat.call(void 0, _types.TokenType.question);
  744. _tokenizer.popTypeContext.call(void 0, oldIsType);
  745. }
  746. } exports.parsePostMemberNameModifiers = parsePostMemberNameModifiers;
  747. function parseClassProperty() {
  748. if (_base.isTypeScriptEnabled) {
  749. _tokenizer.eatTypeToken.call(void 0, _types.TokenType.bang);
  750. _typescript.tsTryParseTypeAnnotation.call(void 0, );
  751. } else if (_base.isFlowEnabled) {
  752. if (_tokenizer.match.call(void 0, _types.TokenType.colon)) {
  753. _flow.flowParseTypeAnnotation.call(void 0, );
  754. }
  755. }
  756. if (_tokenizer.match.call(void 0, _types.TokenType.eq)) {
  757. const equalsTokenIndex = _base.state.tokens.length;
  758. _tokenizer.next.call(void 0, );
  759. _expression.parseMaybeAssign.call(void 0, );
  760. _base.state.tokens[equalsTokenIndex].rhsEndIndex = _base.state.tokens.length;
  761. }
  762. _util.semicolon.call(void 0, );
  763. } exports.parseClassProperty = parseClassProperty;
  764. function parseClassId(isStatement, optionalId = false) {
  765. if (
  766. _base.isTypeScriptEnabled &&
  767. (!isStatement || optionalId) &&
  768. _util.isContextual.call(void 0, _keywords.ContextualKeyword._implements)
  769. ) {
  770. return;
  771. }
  772. if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
  773. _lval.parseBindingIdentifier.call(void 0, true);
  774. }
  775. if (_base.isTypeScriptEnabled) {
  776. _typescript.tsTryParseTypeParameters.call(void 0, );
  777. } else if (_base.isFlowEnabled) {
  778. if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
  779. _flow.flowParseTypeParameterDeclaration.call(void 0, );
  780. }
  781. }
  782. }
  783. // Returns true if there was a superclass.
  784. function parseClassSuper() {
  785. let hasSuper = false;
  786. if (_tokenizer.eat.call(void 0, _types.TokenType._extends)) {
  787. _expression.parseExprSubscripts.call(void 0, );
  788. hasSuper = true;
  789. } else {
  790. hasSuper = false;
  791. }
  792. if (_base.isTypeScriptEnabled) {
  793. _typescript.tsAfterParseClassSuper.call(void 0, hasSuper);
  794. } else if (_base.isFlowEnabled) {
  795. _flow.flowAfterParseClassSuper.call(void 0, hasSuper);
  796. }
  797. }
  798. // Parses module export declaration.
  799. function parseExport() {
  800. const exportIndex = _base.state.tokens.length - 1;
  801. if (_base.isTypeScriptEnabled) {
  802. if (_typescript.tsTryParseExport.call(void 0, )) {
  803. return;
  804. }
  805. }
  806. // export * from '...'
  807. if (shouldParseExportStar()) {
  808. parseExportStar();
  809. } else if (isExportDefaultSpecifier()) {
  810. // export default from
  811. _expression.parseIdentifier.call(void 0, );
  812. if (_tokenizer.match.call(void 0, _types.TokenType.comma) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.star) {
  813. _util.expect.call(void 0, _types.TokenType.comma);
  814. _util.expect.call(void 0, _types.TokenType.star);
  815. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
  816. _expression.parseIdentifier.call(void 0, );
  817. } else {
  818. parseExportSpecifiersMaybe();
  819. }
  820. parseExportFrom();
  821. } else if (_tokenizer.eat.call(void 0, _types.TokenType._default)) {
  822. // export default ...
  823. parseExportDefaultExpression();
  824. } else if (shouldParseExportDeclaration()) {
  825. parseExportDeclaration();
  826. } else {
  827. // export { x, y as z } [from '...']
  828. parseExportSpecifiers();
  829. parseExportFrom();
  830. }
  831. _base.state.tokens[exportIndex].rhsEndIndex = _base.state.tokens.length;
  832. } exports.parseExport = parseExport;
  833. function parseExportDefaultExpression() {
  834. if (_base.isTypeScriptEnabled) {
  835. if (_typescript.tsTryParseExportDefaultExpression.call(void 0, )) {
  836. return;
  837. }
  838. }
  839. if (_base.isFlowEnabled) {
  840. if (_flow.flowTryParseExportDefaultExpression.call(void 0, )) {
  841. return;
  842. }
  843. }
  844. const functionStart = _base.state.start;
  845. if (_tokenizer.eat.call(void 0, _types.TokenType._function)) {
  846. parseFunction(functionStart, true, true);
  847. } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._async) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType._function) {
  848. // async function declaration
  849. _util.eatContextual.call(void 0, _keywords.ContextualKeyword._async);
  850. _tokenizer.eat.call(void 0, _types.TokenType._function);
  851. parseFunction(functionStart, true, true);
  852. } else if (_tokenizer.match.call(void 0, _types.TokenType._class)) {
  853. parseClass(true, true);
  854. } else if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
  855. parseDecorators();
  856. parseClass(true, true);
  857. } else {
  858. _expression.parseMaybeAssign.call(void 0, );
  859. _util.semicolon.call(void 0, );
  860. }
  861. }
  862. function parseExportDeclaration() {
  863. if (_base.isTypeScriptEnabled) {
  864. _typescript.tsParseExportDeclaration.call(void 0, );
  865. } else if (_base.isFlowEnabled) {
  866. _flow.flowParseExportDeclaration.call(void 0, );
  867. } else {
  868. parseStatement(true);
  869. }
  870. }
  871. function isExportDefaultSpecifier() {
  872. if (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) {
  873. return false;
  874. } else if (_base.isFlowEnabled && _flow.flowShouldDisallowExportDefaultSpecifier.call(void 0, )) {
  875. return false;
  876. }
  877. if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
  878. return _base.state.contextualKeyword !== _keywords.ContextualKeyword._async;
  879. }
  880. if (!_tokenizer.match.call(void 0, _types.TokenType._default)) {
  881. return false;
  882. }
  883. const _next = _tokenizer.nextTokenStart.call(void 0, );
  884. const lookahead = _tokenizer.lookaheadTypeAndKeyword.call(void 0, );
  885. const hasFrom =
  886. lookahead.type === _types.TokenType.name && lookahead.contextualKeyword === _keywords.ContextualKeyword._from;
  887. if (lookahead.type === _types.TokenType.comma) {
  888. return true;
  889. }
  890. // lookahead again when `export default from` is seen
  891. if (hasFrom) {
  892. const nextAfterFrom = _base.input.charCodeAt(_tokenizer.nextTokenStartSince.call(void 0, _next + 4));
  893. return nextAfterFrom === _charcodes.charCodes.quotationMark || nextAfterFrom === _charcodes.charCodes.apostrophe;
  894. }
  895. return false;
  896. }
  897. function parseExportSpecifiersMaybe() {
  898. if (_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
  899. parseExportSpecifiers();
  900. }
  901. }
  902. function parseExportFrom() {
  903. if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._from)) {
  904. _expression.parseExprAtom.call(void 0, );
  905. maybeParseImportAttributes();
  906. }
  907. _util.semicolon.call(void 0, );
  908. } exports.parseExportFrom = parseExportFrom;
  909. function shouldParseExportStar() {
  910. if (_base.isFlowEnabled) {
  911. return _flow.flowShouldParseExportStar.call(void 0, );
  912. } else {
  913. return _tokenizer.match.call(void 0, _types.TokenType.star);
  914. }
  915. }
  916. function parseExportStar() {
  917. if (_base.isFlowEnabled) {
  918. _flow.flowParseExportStar.call(void 0, );
  919. } else {
  920. baseParseExportStar();
  921. }
  922. }
  923. function baseParseExportStar() {
  924. _util.expect.call(void 0, _types.TokenType.star);
  925. if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as)) {
  926. parseExportNamespace();
  927. } else {
  928. parseExportFrom();
  929. }
  930. } exports.baseParseExportStar = baseParseExportStar;
  931. function parseExportNamespace() {
  932. _tokenizer.next.call(void 0, );
  933. _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._as;
  934. _expression.parseIdentifier.call(void 0, );
  935. parseExportSpecifiersMaybe();
  936. parseExportFrom();
  937. }
  938. function shouldParseExportDeclaration() {
  939. return (
  940. (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) ||
  941. (_base.isFlowEnabled && _flow.flowShouldParseExportDeclaration.call(void 0, )) ||
  942. _base.state.type === _types.TokenType._var ||
  943. _base.state.type === _types.TokenType._const ||
  944. _base.state.type === _types.TokenType._let ||
  945. _base.state.type === _types.TokenType._function ||
  946. _base.state.type === _types.TokenType._class ||
  947. _util.isContextual.call(void 0, _keywords.ContextualKeyword._async) ||
  948. _tokenizer.match.call(void 0, _types.TokenType.at)
  949. );
  950. }
  951. // Parses a comma-separated list of module exports.
  952. function parseExportSpecifiers() {
  953. let first = true;
  954. // export { x, y as z } [from '...']
  955. _util.expect.call(void 0, _types.TokenType.braceL);
  956. while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
  957. if (first) {
  958. first = false;
  959. } else {
  960. _util.expect.call(void 0, _types.TokenType.comma);
  961. if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
  962. break;
  963. }
  964. }
  965. parseExportSpecifier();
  966. }
  967. } exports.parseExportSpecifiers = parseExportSpecifiers;
  968. function parseExportSpecifier() {
  969. if (_base.isTypeScriptEnabled) {
  970. _typescript.tsParseExportSpecifier.call(void 0, );
  971. return;
  972. }
  973. _expression.parseIdentifier.call(void 0, );
  974. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _tokenizer.IdentifierRole.ExportAccess;
  975. if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
  976. _expression.parseIdentifier.call(void 0, );
  977. }
  978. }
  979. /**
  980. * Starting at the `module` token in an import, determine if it was truly an
  981. * import reflection token or just looks like one.
  982. *
  983. * Returns true for:
  984. * import module foo from "foo";
  985. * import module from from "foo";
  986. *
  987. * Returns false for:
  988. * import module from "foo";
  989. * import module, {bar} from "foo";
  990. */
  991. function isImportReflection() {
  992. const snapshot = _base.state.snapshot();
  993. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._module);
  994. if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._from)) {
  995. if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._from)) {
  996. _base.state.restoreFromSnapshot(snapshot);
  997. return true;
  998. } else {
  999. _base.state.restoreFromSnapshot(snapshot);
  1000. return false;
  1001. }
  1002. } else if (_tokenizer.match.call(void 0, _types.TokenType.comma)) {
  1003. _base.state.restoreFromSnapshot(snapshot);
  1004. return false;
  1005. } else {
  1006. _base.state.restoreFromSnapshot(snapshot);
  1007. return true;
  1008. }
  1009. }
  1010. /**
  1011. * Eat the "module" token from the import reflection proposal.
  1012. * https://github.com/tc39/proposal-import-reflection
  1013. */
  1014. function parseMaybeImportReflection() {
  1015. // isImportReflection does snapshot/restore, so only run it if we see the word
  1016. // "module".
  1017. if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._module) && isImportReflection()) {
  1018. _tokenizer.next.call(void 0, );
  1019. }
  1020. }
  1021. // Parses import declaration.
  1022. function parseImport() {
  1023. if (_base.isTypeScriptEnabled && _tokenizer.match.call(void 0, _types.TokenType.name) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.eq) {
  1024. _typescript.tsParseImportEqualsDeclaration.call(void 0, );
  1025. return;
  1026. }
  1027. if (_base.isTypeScriptEnabled && _util.isContextual.call(void 0, _keywords.ContextualKeyword._type)) {
  1028. const lookahead = _tokenizer.lookaheadTypeAndKeyword.call(void 0, );
  1029. if (lookahead.type === _types.TokenType.name && lookahead.contextualKeyword !== _keywords.ContextualKeyword._from) {
  1030. // One of these `import type` cases:
  1031. // import type T = require('T');
  1032. // import type A from 'A';
  1033. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._type);
  1034. if (_tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.eq) {
  1035. _typescript.tsParseImportEqualsDeclaration.call(void 0, );
  1036. return;
  1037. }
  1038. // If this is an `import type...from` statement, then we already ate the
  1039. // type token, so proceed to the regular import parser.
  1040. } else if (lookahead.type === _types.TokenType.star || lookahead.type === _types.TokenType.braceL) {
  1041. // One of these `import type` cases, in which case we can eat the type token
  1042. // and proceed as normal:
  1043. // import type * as A from 'A';
  1044. // import type {a} from 'A';
  1045. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._type);
  1046. }
  1047. // Otherwise, we are importing the name "type".
  1048. }
  1049. // import '...'
  1050. if (_tokenizer.match.call(void 0, _types.TokenType.string)) {
  1051. _expression.parseExprAtom.call(void 0, );
  1052. } else {
  1053. parseMaybeImportReflection();
  1054. parseImportSpecifiers();
  1055. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._from);
  1056. _expression.parseExprAtom.call(void 0, );
  1057. }
  1058. maybeParseImportAttributes();
  1059. _util.semicolon.call(void 0, );
  1060. } exports.parseImport = parseImport;
  1061. // eslint-disable-next-line no-unused-vars
  1062. function shouldParseDefaultImport() {
  1063. return _tokenizer.match.call(void 0, _types.TokenType.name);
  1064. }
  1065. function parseImportSpecifierLocal() {
  1066. _lval.parseImportedIdentifier.call(void 0, );
  1067. }
  1068. // Parses a comma-separated list of module imports.
  1069. function parseImportSpecifiers() {
  1070. if (_base.isFlowEnabled) {
  1071. _flow.flowStartParseImportSpecifiers.call(void 0, );
  1072. }
  1073. let first = true;
  1074. if (shouldParseDefaultImport()) {
  1075. // import defaultObj, { x, y as z } from '...'
  1076. parseImportSpecifierLocal();
  1077. if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) return;
  1078. }
  1079. if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
  1080. _tokenizer.next.call(void 0, );
  1081. _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
  1082. parseImportSpecifierLocal();
  1083. return;
  1084. }
  1085. _util.expect.call(void 0, _types.TokenType.braceL);
  1086. while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
  1087. if (first) {
  1088. first = false;
  1089. } else {
  1090. // Detect an attempt to deep destructure
  1091. if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
  1092. _util.unexpected.call(void 0,
  1093. "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  1094. );
  1095. }
  1096. _util.expect.call(void 0, _types.TokenType.comma);
  1097. if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
  1098. break;
  1099. }
  1100. }
  1101. parseImportSpecifier();
  1102. }
  1103. }
  1104. function parseImportSpecifier() {
  1105. if (_base.isTypeScriptEnabled) {
  1106. _typescript.tsParseImportSpecifier.call(void 0, );
  1107. return;
  1108. }
  1109. if (_base.isFlowEnabled) {
  1110. _flow.flowParseImportSpecifier.call(void 0, );
  1111. return;
  1112. }
  1113. _lval.parseImportedIdentifier.call(void 0, );
  1114. if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as)) {
  1115. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _tokenizer.IdentifierRole.ImportAccess;
  1116. _tokenizer.next.call(void 0, );
  1117. _lval.parseImportedIdentifier.call(void 0, );
  1118. }
  1119. }
  1120. /**
  1121. * Parse import attributes like `with {type: "json"}`, or the legacy form
  1122. * `assert {type: "json"}`.
  1123. *
  1124. * Import attributes technically have their own syntax, but are always parseable
  1125. * as a plain JS object, so just do that for simplicity.
  1126. */
  1127. function maybeParseImportAttributes() {
  1128. if (_tokenizer.match.call(void 0, _types.TokenType._with) || (_util.isContextual.call(void 0, _keywords.ContextualKeyword._assert) && !_util.hasPrecedingLineBreak.call(void 0, ))) {
  1129. _tokenizer.next.call(void 0, );
  1130. _expression.parseObj.call(void 0, false, false);
  1131. }
  1132. }