Brak opisu

statement.js 41KB

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