1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = exports.ParseStatementFlag = exports.ParseFunctionFlag = void 0;
|
7 | var _types = require("../tokenizer/types");
|
8 | var _expression = require("./expression");
|
9 | var _parseError = require("../parse-error");
|
10 | var _identifier = require("../util/identifier");
|
11 | var _scopeflags = require("../util/scopeflags");
|
12 | var _util = require("./util");
|
13 | var _productionParameter = require("../util/production-parameter");
|
14 | var _expressionScope = require("../util/expression-scope");
|
15 | var _tokenizer = require("../tokenizer");
|
16 | var _location = require("../util/location");
|
17 | var _node = require("./node");
|
18 | var _lval = require("./lval");
|
19 | const loopLabel = {
|
20 | kind: "loop"
|
21 | },
|
22 | switchLabel = {
|
23 | kind: "switch"
|
24 | };
|
25 | var ParseFunctionFlag = {
|
26 | Expression: 0,
|
27 | Declaration: 1,
|
28 | HangingDeclaration: 2,
|
29 | NullableId: 4,
|
30 | Async: 8
|
31 | };
|
32 | exports.ParseFunctionFlag = ParseFunctionFlag;
|
33 | var ParseStatementFlag = {
|
34 | StatementOnly: 0,
|
35 | AllowImportExport: 1,
|
36 | AllowDeclaration: 2,
|
37 | AllowFunctionDeclaration: 4,
|
38 | AllowLabeledFunction: 8
|
39 | };
|
40 | exports.ParseStatementFlag = ParseStatementFlag;
|
41 | const loneSurrogate = /[\uD800-\uDFFF]/u;
|
42 | const keywordRelationalOperator = /in(?:stanceof)?/y;
|
43 | function babel7CompatTokens(tokens, input) {
|
44 | for (let i = 0; i < tokens.length; i++) {
|
45 | const token = tokens[i];
|
46 | const {
|
47 | type
|
48 | } = token;
|
49 | if (typeof type === "number") {
|
50 | {
|
51 | if (type === 136) {
|
52 | const {
|
53 | loc,
|
54 | start,
|
55 | value,
|
56 | end
|
57 | } = token;
|
58 | const hashEndPos = start + 1;
|
59 | const hashEndLoc = (0, _location.createPositionWithColumnOffset)(loc.start, 1);
|
60 | tokens.splice(i, 1, new _tokenizer.Token({
|
61 | type: (0, _types.getExportedToken)(27),
|
62 | value: "#",
|
63 | start: start,
|
64 | end: hashEndPos,
|
65 | startLoc: loc.start,
|
66 | endLoc: hashEndLoc
|
67 | }), new _tokenizer.Token({
|
68 | type: (0, _types.getExportedToken)(130),
|
69 | value: value,
|
70 | start: hashEndPos,
|
71 | end: end,
|
72 | startLoc: hashEndLoc,
|
73 | endLoc: loc.end
|
74 | }));
|
75 | i++;
|
76 | continue;
|
77 | }
|
78 | if ((0, _types.tokenIsTemplate)(type)) {
|
79 | const {
|
80 | loc,
|
81 | start,
|
82 | value,
|
83 | end
|
84 | } = token;
|
85 | const backquoteEnd = start + 1;
|
86 | const backquoteEndLoc = (0, _location.createPositionWithColumnOffset)(loc.start, 1);
|
87 | let startToken;
|
88 | if (input.charCodeAt(start) === 96) {
|
89 | startToken = new _tokenizer.Token({
|
90 | type: (0, _types.getExportedToken)(22),
|
91 | value: "`",
|
92 | start: start,
|
93 | end: backquoteEnd,
|
94 | startLoc: loc.start,
|
95 | endLoc: backquoteEndLoc
|
96 | });
|
97 | } else {
|
98 | startToken = new _tokenizer.Token({
|
99 | type: (0, _types.getExportedToken)(8),
|
100 | value: "}",
|
101 | start: start,
|
102 | end: backquoteEnd,
|
103 | startLoc: loc.start,
|
104 | endLoc: backquoteEndLoc
|
105 | });
|
106 | }
|
107 | let templateValue, templateElementEnd, templateElementEndLoc, endToken;
|
108 | if (type === 24) {
|
109 | templateElementEnd = end - 1;
|
110 | templateElementEndLoc = (0, _location.createPositionWithColumnOffset)(loc.end, -1);
|
111 | templateValue = value === null ? null : value.slice(1, -1);
|
112 | endToken = new _tokenizer.Token({
|
113 | type: (0, _types.getExportedToken)(22),
|
114 | value: "`",
|
115 | start: templateElementEnd,
|
116 | end: end,
|
117 | startLoc: templateElementEndLoc,
|
118 | endLoc: loc.end
|
119 | });
|
120 | } else {
|
121 | templateElementEnd = end - 2;
|
122 | templateElementEndLoc = (0, _location.createPositionWithColumnOffset)(loc.end, -2);
|
123 | templateValue = value === null ? null : value.slice(1, -2);
|
124 | endToken = new _tokenizer.Token({
|
125 | type: (0, _types.getExportedToken)(23),
|
126 | value: "${",
|
127 | start: templateElementEnd,
|
128 | end: end,
|
129 | startLoc: templateElementEndLoc,
|
130 | endLoc: loc.end
|
131 | });
|
132 | }
|
133 | tokens.splice(i, 1, startToken, new _tokenizer.Token({
|
134 | type: (0, _types.getExportedToken)(20),
|
135 | value: templateValue,
|
136 | start: backquoteEnd,
|
137 | end: templateElementEnd,
|
138 | startLoc: backquoteEndLoc,
|
139 | endLoc: templateElementEndLoc
|
140 | }), endToken);
|
141 | i += 2;
|
142 | continue;
|
143 | }
|
144 | }
|
145 | token.type = (0, _types.getExportedToken)(type);
|
146 | }
|
147 | }
|
148 | return tokens;
|
149 | }
|
150 | class StatementParser extends _expression.default {
|
151 | parseTopLevel(file, program) {
|
152 | file.program = this.parseProgram(program);
|
153 | file.comments = this.state.comments;
|
154 | if (this.options.tokens) {
|
155 | file.tokens = babel7CompatTokens(this.tokens, this.input);
|
156 | }
|
157 | return this.finishNode(file, "File");
|
158 | }
|
159 | parseProgram(program, end = 137, sourceType = this.options.sourceType) {
|
160 | program.sourceType = sourceType;
|
161 | program.interpreter = this.parseInterpreterDirective();
|
162 | this.parseBlockBody(program, true, true, end);
|
163 | if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
|
164 | for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
|
165 | this.raise(_parseError.Errors.ModuleExportUndefined, {
|
166 | at,
|
167 | localName
|
168 | });
|
169 | }
|
170 | }
|
171 | let finishedProgram;
|
172 | if (end === 137) {
|
173 | finishedProgram = this.finishNode(program, "Program");
|
174 | } else {
|
175 | finishedProgram = this.finishNodeAt(program, "Program", (0, _location.createPositionWithColumnOffset)(this.state.startLoc, -1));
|
176 | }
|
177 | return finishedProgram;
|
178 | }
|
179 | stmtToDirective(stmt) {
|
180 | const directive = stmt;
|
181 | directive.type = "Directive";
|
182 | directive.value = directive.expression;
|
183 | delete directive.expression;
|
184 | const directiveLiteral = directive.value;
|
185 | const expressionValue = directiveLiteral.value;
|
186 | const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
|
187 | const val = directiveLiteral.value = raw.slice(1, -1);
|
188 | this.addExtra(directiveLiteral, "raw", raw);
|
189 | this.addExtra(directiveLiteral, "rawValue", val);
|
190 | this.addExtra(directiveLiteral, "expressionValue", expressionValue);
|
191 | directiveLiteral.type = "DirectiveLiteral";
|
192 | return directive;
|
193 | }
|
194 | parseInterpreterDirective() {
|
195 | if (!this.match(28)) {
|
196 | return null;
|
197 | }
|
198 | const node = this.startNode();
|
199 | node.value = this.state.value;
|
200 | this.next();
|
201 | return this.finishNode(node, "InterpreterDirective");
|
202 | }
|
203 | isLet() {
|
204 | if (!this.isContextual(99)) {
|
205 | return false;
|
206 | }
|
207 | return this.hasFollowingBindingAtom();
|
208 | }
|
209 | chStartsBindingIdentifier(ch, pos) {
|
210 | if ((0, _identifier.isIdentifierStart)(ch)) {
|
211 | keywordRelationalOperator.lastIndex = pos;
|
212 | if (keywordRelationalOperator.test(this.input)) {
|
213 | const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
|
214 | if (!(0, _identifier.isIdentifierChar)(endCh) && endCh !== 92) {
|
215 | return false;
|
216 | }
|
217 | }
|
218 | return true;
|
219 | } else if (ch === 92) {
|
220 | return true;
|
221 | } else {
|
222 | return false;
|
223 | }
|
224 | }
|
225 | chStartsBindingPattern(ch) {
|
226 | return ch === 91 || ch === 123;
|
227 | }
|
228 | hasFollowingBindingAtom() {
|
229 | const next = this.nextTokenStart();
|
230 | const nextCh = this.codePointAtPos(next);
|
231 | return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
|
232 | }
|
233 | hasInLineFollowingBindingIdentifier() {
|
234 | const next = this.nextTokenInLineStart();
|
235 | const nextCh = this.codePointAtPos(next);
|
236 | return this.chStartsBindingIdentifier(nextCh, next);
|
237 | }
|
238 | startsUsingForOf() {
|
239 | const {
|
240 | type,
|
241 | containsEsc
|
242 | } = this.lookahead();
|
243 | if (type === 101 && !containsEsc) {
|
244 | return false;
|
245 | } else if ((0, _types.tokenIsIdentifier)(type) && !this.hasFollowingLineBreak()) {
|
246 | this.expectPlugin("explicitResourceManagement");
|
247 | return true;
|
248 | }
|
249 | }
|
250 | startsAwaitUsing() {
|
251 | let next = this.nextTokenInLineStart();
|
252 | if (this.isUnparsedContextual(next, "using")) {
|
253 | next = this.nextTokenInLineStartSince(next + 5);
|
254 | const nextCh = this.codePointAtPos(next);
|
255 | if (this.chStartsBindingIdentifier(nextCh, next)) {
|
256 | this.expectPlugin("explicitResourceManagement");
|
257 | return true;
|
258 | }
|
259 | }
|
260 | return false;
|
261 | }
|
262 | parseModuleItem() {
|
263 | return this.parseStatementLike(ParseStatementFlag.AllowImportExport | ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | ParseStatementFlag.AllowLabeledFunction);
|
264 | }
|
265 | parseStatementListItem() {
|
266 | return this.parseStatementLike(ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | (!this.options.annexB || this.state.strict ? 0 : ParseStatementFlag.AllowLabeledFunction));
|
267 | }
|
268 | parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
|
269 | let flags = ParseStatementFlag.StatementOnly;
|
270 | if (this.options.annexB && !this.state.strict) {
|
271 | flags |= ParseStatementFlag.AllowFunctionDeclaration;
|
272 | if (allowLabeledFunction) {
|
273 | flags |= ParseStatementFlag.AllowLabeledFunction;
|
274 | }
|
275 | }
|
276 | return this.parseStatementLike(flags);
|
277 | }
|
278 | parseStatement() {
|
279 | return this.parseStatementLike(ParseStatementFlag.StatementOnly);
|
280 | }
|
281 | parseStatementLike(flags) {
|
282 | let decorators = null;
|
283 | if (this.match(26)) {
|
284 | decorators = this.parseDecorators(true);
|
285 | }
|
286 | return this.parseStatementContent(flags, decorators);
|
287 | }
|
288 | parseStatementContent(flags, decorators) {
|
289 | const starttype = this.state.type;
|
290 | const node = this.startNode();
|
291 | const allowDeclaration = !!(flags & ParseStatementFlag.AllowDeclaration);
|
292 | const allowFunctionDeclaration = !!(flags & ParseStatementFlag.AllowFunctionDeclaration);
|
293 | const topLevel = flags & ParseStatementFlag.AllowImportExport;
|
294 | switch (starttype) {
|
295 | case 60:
|
296 | return this.parseBreakContinueStatement(node, true);
|
297 | case 63:
|
298 | return this.parseBreakContinueStatement(node, false);
|
299 | case 64:
|
300 | return this.parseDebuggerStatement(node);
|
301 | case 90:
|
302 | return this.parseDoWhileStatement(node);
|
303 | case 91:
|
304 | return this.parseForStatement(node);
|
305 | case 68:
|
306 | if (this.lookaheadCharCode() === 46) break;
|
307 | if (!allowFunctionDeclaration) {
|
308 | this.raise(this.state.strict ? _parseError.Errors.StrictFunction : this.options.annexB ? _parseError.Errors.SloppyFunctionAnnexB : _parseError.Errors.SloppyFunction, {
|
309 | at: this.state.startLoc
|
310 | });
|
311 | }
|
312 | return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
|
313 | case 80:
|
314 | if (!allowDeclaration) this.unexpected();
|
315 | return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
|
316 | case 69:
|
317 | return this.parseIfStatement(node);
|
318 | case 70:
|
319 | return this.parseReturnStatement(node);
|
320 | case 71:
|
321 | return this.parseSwitchStatement(node);
|
322 | case 72:
|
323 | return this.parseThrowStatement(node);
|
324 | case 73:
|
325 | return this.parseTryStatement(node);
|
326 | case 96:
|
327 | if (!this.state.containsEsc && this.startsAwaitUsing()) {
|
328 | if (!this.isAwaitAllowed()) {
|
329 | this.raise(_parseError.Errors.AwaitUsingNotInAsyncContext, {
|
330 | at: node
|
331 | });
|
332 | } else if (!allowDeclaration) {
|
333 | this.raise(_parseError.Errors.UnexpectedLexicalDeclaration, {
|
334 | at: node
|
335 | });
|
336 | }
|
337 | this.next();
|
338 | return this.parseVarStatement(node, "await using");
|
339 | }
|
340 | break;
|
341 | case 105:
|
342 | if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifier()) {
|
343 | break;
|
344 | }
|
345 | this.expectPlugin("explicitResourceManagement");
|
346 | if (!this.scope.inModule && this.scope.inTopLevel) {
|
347 | this.raise(_parseError.Errors.UnexpectedUsingDeclaration, {
|
348 | at: this.state.startLoc
|
349 | });
|
350 | } else if (!allowDeclaration) {
|
351 | this.raise(_parseError.Errors.UnexpectedLexicalDeclaration, {
|
352 | at: this.state.startLoc
|
353 | });
|
354 | }
|
355 | return this.parseVarStatement(node, "using");
|
356 | case 99:
|
357 | {
|
358 | if (this.state.containsEsc) {
|
359 | break;
|
360 | }
|
361 | const next = this.nextTokenStart();
|
362 | const nextCh = this.codePointAtPos(next);
|
363 | if (nextCh !== 91) {
|
364 | if (!allowDeclaration && this.hasFollowingLineBreak()) break;
|
365 | if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
|
366 | break;
|
367 | }
|
368 | }
|
369 | }
|
370 | case 75:
|
371 | {
|
372 | if (!allowDeclaration) {
|
373 | this.raise(_parseError.Errors.UnexpectedLexicalDeclaration, {
|
374 | at: this.state.startLoc
|
375 | });
|
376 | }
|
377 | }
|
378 | case 74:
|
379 | {
|
380 | const kind = this.state.value;
|
381 | return this.parseVarStatement(node, kind);
|
382 | }
|
383 | case 92:
|
384 | return this.parseWhileStatement(node);
|
385 | case 76:
|
386 | return this.parseWithStatement(node);
|
387 | case 5:
|
388 | return this.parseBlock();
|
389 | case 13:
|
390 | return this.parseEmptyStatement(node);
|
391 | case 83:
|
392 | {
|
393 | const nextTokenCharCode = this.lookaheadCharCode();
|
394 | if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
|
395 | break;
|
396 | }
|
397 | }
|
398 | case 82:
|
399 | {
|
400 | if (!this.options.allowImportExportEverywhere && !topLevel) {
|
401 | this.raise(_parseError.Errors.UnexpectedImportExport, {
|
402 | at: this.state.startLoc
|
403 | });
|
404 | }
|
405 | this.next();
|
406 | let result;
|
407 | if (starttype === 83) {
|
408 | result = this.parseImport(node);
|
409 | if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
|
410 | this.sawUnambiguousESM = true;
|
411 | }
|
412 | } else {
|
413 | result = this.parseExport(node, decorators);
|
414 | if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
|
415 | this.sawUnambiguousESM = true;
|
416 | }
|
417 | }
|
418 | this.assertModuleNodeAllowed(result);
|
419 | return result;
|
420 | }
|
421 | default:
|
422 | {
|
423 | if (this.isAsyncFunction()) {
|
424 | if (!allowDeclaration) {
|
425 | this.raise(_parseError.Errors.AsyncFunctionInSingleStatementContext, {
|
426 | at: this.state.startLoc
|
427 | });
|
428 | }
|
429 | this.next();
|
430 | return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
|
431 | }
|
432 | }
|
433 | }
|
434 | const maybeName = this.state.value;
|
435 | const expr = this.parseExpression();
|
436 | if ((0, _types.tokenIsIdentifier)(starttype) && expr.type === "Identifier" && this.eat(14)) {
|
437 | return this.parseLabeledStatement(node, maybeName, expr, flags);
|
438 | } else {
|
439 | return this.parseExpressionStatement(node, expr, decorators);
|
440 | }
|
441 | }
|
442 | assertModuleNodeAllowed(node) {
|
443 | if (!this.options.allowImportExportEverywhere && !this.inModule) {
|
444 | this.raise(_parseError.Errors.ImportOutsideModule, {
|
445 | at: node
|
446 | });
|
447 | }
|
448 | }
|
449 | decoratorsEnabledBeforeExport() {
|
450 | if (this.hasPlugin("decorators-legacy")) return true;
|
451 | return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
|
452 | }
|
453 | maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
|
454 | if (maybeDecorators) {
|
455 | if (classNode.decorators && classNode.decorators.length > 0) {
|
456 | if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
|
457 | this.raise(_parseError.Errors.DecoratorsBeforeAfterExport, {
|
458 | at: classNode.decorators[0]
|
459 | });
|
460 | }
|
461 | classNode.decorators.unshift(...maybeDecorators);
|
462 | } else {
|
463 | classNode.decorators = maybeDecorators;
|
464 | }
|
465 | this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
|
466 | if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
|
467 | }
|
468 | return classNode;
|
469 | }
|
470 | canHaveLeadingDecorator() {
|
471 | return this.match(80);
|
472 | }
|
473 | parseDecorators(allowExport) {
|
474 | const decorators = [];
|
475 | do {
|
476 | decorators.push(this.parseDecorator());
|
477 | } while (this.match(26));
|
478 | if (this.match(82)) {
|
479 | if (!allowExport) {
|
480 | this.unexpected();
|
481 | }
|
482 | if (!this.decoratorsEnabledBeforeExport()) {
|
483 | this.raise(_parseError.Errors.DecoratorExportClass, {
|
484 | at: this.state.startLoc
|
485 | });
|
486 | }
|
487 | } else if (!this.canHaveLeadingDecorator()) {
|
488 | throw this.raise(_parseError.Errors.UnexpectedLeadingDecorator, {
|
489 | at: this.state.startLoc
|
490 | });
|
491 | }
|
492 | return decorators;
|
493 | }
|
494 | parseDecorator() {
|
495 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
496 | const node = this.startNode();
|
497 | this.next();
|
498 | if (this.hasPlugin("decorators")) {
|
499 | const startLoc = this.state.startLoc;
|
500 | let expr;
|
501 | if (this.match(10)) {
|
502 | const startLoc = this.state.startLoc;
|
503 | this.next();
|
504 | expr = this.parseExpression();
|
505 | this.expect(11);
|
506 | expr = this.wrapParenthesis(startLoc, expr);
|
507 | const paramsStartLoc = this.state.startLoc;
|
508 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
509 | if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
|
510 | this.raise(_parseError.Errors.DecoratorArgumentsOutsideParentheses, {
|
511 | at: paramsStartLoc
|
512 | });
|
513 | }
|
514 | } else {
|
515 | expr = this.parseIdentifier(false);
|
516 | while (this.eat(16)) {
|
517 | const node = this.startNodeAt(startLoc);
|
518 | node.object = expr;
|
519 | if (this.match(136)) {
|
520 | this.classScope.usePrivateName(this.state.value, this.state.startLoc);
|
521 | node.property = this.parsePrivateName();
|
522 | } else {
|
523 | node.property = this.parseIdentifier(true);
|
524 | }
|
525 | node.computed = false;
|
526 | expr = this.finishNode(node, "MemberExpression");
|
527 | }
|
528 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
529 | }
|
530 | } else {
|
531 | node.expression = this.parseExprSubscripts();
|
532 | }
|
533 | return this.finishNode(node, "Decorator");
|
534 | }
|
535 | parseMaybeDecoratorArguments(expr) {
|
536 | if (this.eat(10)) {
|
537 | const node = this.startNodeAtNode(expr);
|
538 | node.callee = expr;
|
539 | node.arguments = this.parseCallExpressionArguments(11, false);
|
540 | this.toReferencedList(node.arguments);
|
541 | return this.finishNode(node, "CallExpression");
|
542 | }
|
543 | return expr;
|
544 | }
|
545 | parseBreakContinueStatement(node, isBreak) {
|
546 | this.next();
|
547 | if (this.isLineTerminator()) {
|
548 | node.label = null;
|
549 | } else {
|
550 | node.label = this.parseIdentifier();
|
551 | this.semicolon();
|
552 | }
|
553 | this.verifyBreakContinue(node, isBreak);
|
554 | return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
555 | }
|
556 | verifyBreakContinue(node, isBreak) {
|
557 | let i;
|
558 | for (i = 0; i < this.state.labels.length; ++i) {
|
559 | const lab = this.state.labels[i];
|
560 | if (node.label == null || lab.name === node.label.name) {
|
561 | if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
|
562 | if (node.label && isBreak) break;
|
563 | }
|
564 | }
|
565 | if (i === this.state.labels.length) {
|
566 | const type = isBreak ? "BreakStatement" : "ContinueStatement";
|
567 | this.raise(_parseError.Errors.IllegalBreakContinue, {
|
568 | at: node,
|
569 | type
|
570 | });
|
571 | }
|
572 | }
|
573 | parseDebuggerStatement(node) {
|
574 | this.next();
|
575 | this.semicolon();
|
576 | return this.finishNode(node, "DebuggerStatement");
|
577 | }
|
578 | parseHeaderExpression() {
|
579 | this.expect(10);
|
580 | const val = this.parseExpression();
|
581 | this.expect(11);
|
582 | return val;
|
583 | }
|
584 | parseDoWhileStatement(node) {
|
585 | this.next();
|
586 | this.state.labels.push(loopLabel);
|
587 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
588 | this.state.labels.pop();
|
589 | this.expect(92);
|
590 | node.test = this.parseHeaderExpression();
|
591 | this.eat(13);
|
592 | return this.finishNode(node, "DoWhileStatement");
|
593 | }
|
594 | parseForStatement(node) {
|
595 | this.next();
|
596 | this.state.labels.push(loopLabel);
|
597 | let awaitAt = null;
|
598 | if (this.isAwaitAllowed() && this.eatContextual(96)) {
|
599 | awaitAt = this.state.lastTokStartLoc;
|
600 | }
|
601 | this.scope.enter(_scopeflags.SCOPE_OTHER);
|
602 | this.expect(10);
|
603 | if (this.match(13)) {
|
604 | if (awaitAt !== null) {
|
605 | this.unexpected(awaitAt);
|
606 | }
|
607 | return this.parseFor(node, null);
|
608 | }
|
609 | const startsWithLet = this.isContextual(99);
|
610 | {
|
611 | const startsWithAwaitUsing = this.isContextual(96) && this.startsAwaitUsing();
|
612 | const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(105) && this.startsUsingForOf();
|
613 | const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
|
614 | if (this.match(74) || this.match(75) || isLetOrUsing) {
|
615 | const initNode = this.startNode();
|
616 | let kind;
|
617 | if (startsWithAwaitUsing) {
|
618 | kind = "await using";
|
619 | if (!this.isAwaitAllowed()) {
|
620 | this.raise(_parseError.Errors.AwaitUsingNotInAsyncContext, {
|
621 | at: this.state.startLoc
|
622 | });
|
623 | }
|
624 | this.next();
|
625 | } else {
|
626 | kind = this.state.value;
|
627 | }
|
628 | this.next();
|
629 | this.parseVar(initNode, true, kind);
|
630 | const init = this.finishNode(initNode, "VariableDeclaration");
|
631 | const isForIn = this.match(58);
|
632 | if (isForIn && starsWithUsingDeclaration) {
|
633 | this.raise(_parseError.Errors.ForInUsing, {
|
634 | at: init
|
635 | });
|
636 | }
|
637 | if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
|
638 | return this.parseForIn(node, init, awaitAt);
|
639 | }
|
640 | if (awaitAt !== null) {
|
641 | this.unexpected(awaitAt);
|
642 | }
|
643 | return this.parseFor(node, init);
|
644 | }
|
645 | }
|
646 | const startsWithAsync = this.isContextual(95);
|
647 | const refExpressionErrors = new _util.ExpressionErrors();
|
648 | const init = this.parseExpression(true, refExpressionErrors);
|
649 | const isForOf = this.isContextual(101);
|
650 | if (isForOf) {
|
651 | if (startsWithLet) {
|
652 | this.raise(_parseError.Errors.ForOfLet, {
|
653 | at: init
|
654 | });
|
655 | }
|
656 | if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
|
657 | this.raise(_parseError.Errors.ForOfAsync, {
|
658 | at: init
|
659 | });
|
660 | }
|
661 | }
|
662 | if (isForOf || this.match(58)) {
|
663 | this.checkDestructuringPrivate(refExpressionErrors);
|
664 | this.toAssignable(init, true);
|
665 | const type = isForOf ? "ForOfStatement" : "ForInStatement";
|
666 | this.checkLVal(init, {
|
667 | in: {
|
668 | type
|
669 | }
|
670 | });
|
671 | return this.parseForIn(node, init, awaitAt);
|
672 | } else {
|
673 | this.checkExpressionErrors(refExpressionErrors, true);
|
674 | }
|
675 | if (awaitAt !== null) {
|
676 | this.unexpected(awaitAt);
|
677 | }
|
678 | return this.parseFor(node, init);
|
679 | }
|
680 | parseFunctionStatement(node, isAsync, isHangingDeclaration) {
|
681 | this.next();
|
682 | return this.parseFunction(node, ParseFunctionFlag.Declaration | (isHangingDeclaration ? ParseFunctionFlag.HangingDeclaration : 0) | (isAsync ? ParseFunctionFlag.Async : 0));
|
683 | }
|
684 | parseIfStatement(node) {
|
685 | this.next();
|
686 | node.test = this.parseHeaderExpression();
|
687 | node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
|
688 | node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
|
689 | return this.finishNode(node, "IfStatement");
|
690 | }
|
691 | parseReturnStatement(node) {
|
692 | if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
|
693 | this.raise(_parseError.Errors.IllegalReturn, {
|
694 | at: this.state.startLoc
|
695 | });
|
696 | }
|
697 | this.next();
|
698 | if (this.isLineTerminator()) {
|
699 | node.argument = null;
|
700 | } else {
|
701 | node.argument = this.parseExpression();
|
702 | this.semicolon();
|
703 | }
|
704 | return this.finishNode(node, "ReturnStatement");
|
705 | }
|
706 | parseSwitchStatement(node) {
|
707 | this.next();
|
708 | node.discriminant = this.parseHeaderExpression();
|
709 | const cases = node.cases = [];
|
710 | this.expect(5);
|
711 | this.state.labels.push(switchLabel);
|
712 | this.scope.enter(_scopeflags.SCOPE_OTHER);
|
713 | let cur;
|
714 | for (let sawDefault; !this.match(8);) {
|
715 | if (this.match(61) || this.match(65)) {
|
716 | const isCase = this.match(61);
|
717 | if (cur) this.finishNode(cur, "SwitchCase");
|
718 | cases.push(cur = this.startNode());
|
719 | cur.consequent = [];
|
720 | this.next();
|
721 | if (isCase) {
|
722 | cur.test = this.parseExpression();
|
723 | } else {
|
724 | if (sawDefault) {
|
725 | this.raise(_parseError.Errors.MultipleDefaultsInSwitch, {
|
726 | at: this.state.lastTokStartLoc
|
727 | });
|
728 | }
|
729 | sawDefault = true;
|
730 | cur.test = null;
|
731 | }
|
732 | this.expect(14);
|
733 | } else {
|
734 | if (cur) {
|
735 | cur.consequent.push(this.parseStatementListItem());
|
736 | } else {
|
737 | this.unexpected();
|
738 | }
|
739 | }
|
740 | }
|
741 | this.scope.exit();
|
742 | if (cur) this.finishNode(cur, "SwitchCase");
|
743 | this.next();
|
744 | this.state.labels.pop();
|
745 | return this.finishNode(node, "SwitchStatement");
|
746 | }
|
747 | parseThrowStatement(node) {
|
748 | this.next();
|
749 | if (this.hasPrecedingLineBreak()) {
|
750 | this.raise(_parseError.Errors.NewlineAfterThrow, {
|
751 | at: this.state.lastTokEndLoc
|
752 | });
|
753 | }
|
754 | node.argument = this.parseExpression();
|
755 | this.semicolon();
|
756 | return this.finishNode(node, "ThrowStatement");
|
757 | }
|
758 | parseCatchClauseParam() {
|
759 | const param = this.parseBindingAtom();
|
760 | this.scope.enter(this.options.annexB && param.type === "Identifier" ? _scopeflags.SCOPE_SIMPLE_CATCH : 0);
|
761 | this.checkLVal(param, {
|
762 | in: {
|
763 | type: "CatchClause"
|
764 | },
|
765 | binding: _scopeflags.BIND_CATCH_PARAM
|
766 | });
|
767 | return param;
|
768 | }
|
769 | parseTryStatement(node) {
|
770 | this.next();
|
771 | node.block = this.parseBlock();
|
772 | node.handler = null;
|
773 | if (this.match(62)) {
|
774 | const clause = this.startNode();
|
775 | this.next();
|
776 | if (this.match(10)) {
|
777 | this.expect(10);
|
778 | clause.param = this.parseCatchClauseParam();
|
779 | this.expect(11);
|
780 | } else {
|
781 | clause.param = null;
|
782 | this.scope.enter(_scopeflags.SCOPE_OTHER);
|
783 | }
|
784 | clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
|
785 | this.scope.exit();
|
786 | node.handler = this.finishNode(clause, "CatchClause");
|
787 | }
|
788 | node.finalizer = this.eat(67) ? this.parseBlock() : null;
|
789 | if (!node.handler && !node.finalizer) {
|
790 | this.raise(_parseError.Errors.NoCatchOrFinally, {
|
791 | at: node
|
792 | });
|
793 | }
|
794 | return this.finishNode(node, "TryStatement");
|
795 | }
|
796 | parseVarStatement(node, kind, allowMissingInitializer = false) {
|
797 | this.next();
|
798 | this.parseVar(node, false, kind, allowMissingInitializer);
|
799 | this.semicolon();
|
800 | return this.finishNode(node, "VariableDeclaration");
|
801 | }
|
802 | parseWhileStatement(node) {
|
803 | this.next();
|
804 | node.test = this.parseHeaderExpression();
|
805 | this.state.labels.push(loopLabel);
|
806 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
807 | this.state.labels.pop();
|
808 | return this.finishNode(node, "WhileStatement");
|
809 | }
|
810 | parseWithStatement(node) {
|
811 | if (this.state.strict) {
|
812 | this.raise(_parseError.Errors.StrictWith, {
|
813 | at: this.state.startLoc
|
814 | });
|
815 | }
|
816 | this.next();
|
817 | node.object = this.parseHeaderExpression();
|
818 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
819 | return this.finishNode(node, "WithStatement");
|
820 | }
|
821 | parseEmptyStatement(node) {
|
822 | this.next();
|
823 | return this.finishNode(node, "EmptyStatement");
|
824 | }
|
825 | parseLabeledStatement(node, maybeName, expr, flags) {
|
826 | for (const label of this.state.labels) {
|
827 | if (label.name === maybeName) {
|
828 | this.raise(_parseError.Errors.LabelRedeclaration, {
|
829 | at: expr,
|
830 | labelName: maybeName
|
831 | });
|
832 | }
|
833 | }
|
834 | const kind = (0, _types.tokenIsLoop)(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
|
835 | for (let i = this.state.labels.length - 1; i >= 0; i--) {
|
836 | const label = this.state.labels[i];
|
837 | if (label.statementStart === node.start) {
|
838 | label.statementStart = this.state.start;
|
839 | label.kind = kind;
|
840 | } else {
|
841 | break;
|
842 | }
|
843 | }
|
844 | this.state.labels.push({
|
845 | name: maybeName,
|
846 | kind: kind,
|
847 | statementStart: this.state.start
|
848 | });
|
849 | node.body = flags & ParseStatementFlag.AllowLabeledFunction ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
|
850 | this.state.labels.pop();
|
851 | node.label = expr;
|
852 | return this.finishNode(node, "LabeledStatement");
|
853 | }
|
854 | parseExpressionStatement(node, expr, decorators) {
|
855 | node.expression = expr;
|
856 | this.semicolon();
|
857 | return this.finishNode(node, "ExpressionStatement");
|
858 | }
|
859 | parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
|
860 | const node = this.startNode();
|
861 | if (allowDirectives) {
|
862 | this.state.strictErrors.clear();
|
863 | }
|
864 | this.expect(5);
|
865 | if (createNewLexicalScope) {
|
866 | this.scope.enter(_scopeflags.SCOPE_OTHER);
|
867 | }
|
868 | this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
|
869 | if (createNewLexicalScope) {
|
870 | this.scope.exit();
|
871 | }
|
872 | return this.finishNode(node, "BlockStatement");
|
873 | }
|
874 | isValidDirective(stmt) {
|
875 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
|
876 | }
|
877 | parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
|
878 | const body = node.body = [];
|
879 | const directives = node.directives = [];
|
880 | this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
|
881 | }
|
882 | parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
|
883 | const oldStrict = this.state.strict;
|
884 | let hasStrictModeDirective = false;
|
885 | let parsedNonDirective = false;
|
886 | while (!this.match(end)) {
|
887 | const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
|
888 | if (directives && !parsedNonDirective) {
|
889 | if (this.isValidDirective(stmt)) {
|
890 | const directive = this.stmtToDirective(stmt);
|
891 | directives.push(directive);
|
892 | if (!hasStrictModeDirective && directive.value.value === "use strict") {
|
893 | hasStrictModeDirective = true;
|
894 | this.setStrict(true);
|
895 | }
|
896 | continue;
|
897 | }
|
898 | parsedNonDirective = true;
|
899 | this.state.strictErrors.clear();
|
900 | }
|
901 | body.push(stmt);
|
902 | }
|
903 | if (afterBlockParse) {
|
904 | afterBlockParse.call(this, hasStrictModeDirective);
|
905 | }
|
906 | if (!oldStrict) {
|
907 | this.setStrict(false);
|
908 | }
|
909 | this.next();
|
910 | }
|
911 | parseFor(node, init) {
|
912 | node.init = init;
|
913 | this.semicolon(false);
|
914 | node.test = this.match(13) ? null : this.parseExpression();
|
915 | this.semicolon(false);
|
916 | node.update = this.match(11) ? null : this.parseExpression();
|
917 | this.expect(11);
|
918 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
919 | this.scope.exit();
|
920 | this.state.labels.pop();
|
921 | return this.finishNode(node, "ForStatement");
|
922 | }
|
923 | parseForIn(node, init, awaitAt) {
|
924 | const isForIn = this.match(58);
|
925 | this.next();
|
926 | if (isForIn) {
|
927 | if (awaitAt !== null) this.unexpected(awaitAt);
|
928 | } else {
|
929 | node.await = awaitAt !== null;
|
930 | }
|
931 | if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
|
932 | this.raise(_parseError.Errors.ForInOfLoopInitializer, {
|
933 | at: init,
|
934 | type: isForIn ? "ForInStatement" : "ForOfStatement"
|
935 | });
|
936 | }
|
937 | if (init.type === "AssignmentPattern") {
|
938 | this.raise(_parseError.Errors.InvalidLhs, {
|
939 | at: init,
|
940 | ancestor: {
|
941 | type: "ForStatement"
|
942 | }
|
943 | });
|
944 | }
|
945 | node.left = init;
|
946 | node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
|
947 | this.expect(11);
|
948 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
949 | this.scope.exit();
|
950 | this.state.labels.pop();
|
951 | return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
|
952 | }
|
953 | parseVar(node, isFor, kind, allowMissingInitializer = false) {
|
954 | const declarations = node.declarations = [];
|
955 | node.kind = kind;
|
956 | for (;;) {
|
957 | const decl = this.startNode();
|
958 | this.parseVarId(decl, kind);
|
959 | decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
|
960 | if (decl.init === null && !allowMissingInitializer) {
|
961 | if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
|
962 | this.raise(_parseError.Errors.DeclarationMissingInitializer, {
|
963 | at: this.state.lastTokEndLoc,
|
964 | kind: "destructuring"
|
965 | });
|
966 | } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
|
967 | this.raise(_parseError.Errors.DeclarationMissingInitializer, {
|
968 | at: this.state.lastTokEndLoc,
|
969 | kind: "const"
|
970 | });
|
971 | }
|
972 | }
|
973 | declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
974 | if (!this.eat(12)) break;
|
975 | }
|
976 | return node;
|
977 | }
|
978 | parseVarId(decl, kind) {
|
979 | const id = this.parseBindingAtom();
|
980 | this.checkLVal(id, {
|
981 | in: {
|
982 | type: "VariableDeclarator"
|
983 | },
|
984 | binding: kind === "var" ? _scopeflags.BIND_VAR : _scopeflags.BIND_LEXICAL
|
985 | });
|
986 | decl.id = id;
|
987 | }
|
988 | parseAsyncFunctionExpression(node) {
|
989 | return this.parseFunction(node, ParseFunctionFlag.Async);
|
990 | }
|
991 | parseFunction(node, flags = ParseFunctionFlag.Expression) {
|
992 | const hangingDeclaration = flags & ParseFunctionFlag.HangingDeclaration;
|
993 | const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
|
994 | const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
|
995 | const isAsync = !!(flags & ParseFunctionFlag.Async);
|
996 | this.initFunction(node, isAsync);
|
997 | if (this.match(55)) {
|
998 | if (hangingDeclaration) {
|
999 | this.raise(_parseError.Errors.GeneratorInSingleStatementContext, {
|
1000 | at: this.state.startLoc
|
1001 | });
|
1002 | }
|
1003 | this.next();
|
1004 | node.generator = true;
|
1005 | }
|
1006 | if (isDeclaration) {
|
1007 | node.id = this.parseFunctionId(requireId);
|
1008 | }
|
1009 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
1010 | this.state.maybeInArrowParameters = false;
|
1011 | this.scope.enter(_scopeflags.SCOPE_FUNCTION);
|
1012 | this.prodParam.enter((0, _productionParameter.functionFlags)(isAsync, node.generator));
|
1013 | if (!isDeclaration) {
|
1014 | node.id = this.parseFunctionId();
|
1015 | }
|
1016 | this.parseFunctionParams(node, false);
|
1017 | this.withSmartMixTopicForbiddingContext(() => {
|
1018 | this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
|
1019 | });
|
1020 | this.prodParam.exit();
|
1021 | this.scope.exit();
|
1022 | if (isDeclaration && !hangingDeclaration) {
|
1023 | this.registerFunctionStatementId(node);
|
1024 | }
|
1025 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
1026 | return node;
|
1027 | }
|
1028 | parseFunctionId(requireId) {
|
1029 | return requireId || (0, _types.tokenIsIdentifier)(this.state.type) ? this.parseIdentifier() : null;
|
1030 | }
|
1031 | parseFunctionParams(node, isConstructor) {
|
1032 | this.expect(10);
|
1033 | this.expressionScope.enter((0, _expressionScope.newParameterDeclarationScope)());
|
1034 | node.params = this.parseBindingList(11, 41, _lval.ParseBindingListFlags.IS_FUNCTION_PARAMS | (isConstructor ? _lval.ParseBindingListFlags.IS_CONSTRUCTOR_PARAMS : 0));
|
1035 | this.expressionScope.exit();
|
1036 | }
|
1037 | registerFunctionStatementId(node) {
|
1038 | if (!node.id) return;
|
1039 | this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? _scopeflags.BIND_VAR : _scopeflags.BIND_LEXICAL : _scopeflags.BIND_FUNCTION, node.id.loc.start);
|
1040 | }
|
1041 | parseClass(node, isStatement, optionalId) {
|
1042 | this.next();
|
1043 | const oldStrict = this.state.strict;
|
1044 | this.state.strict = true;
|
1045 | this.parseClassId(node, isStatement, optionalId);
|
1046 | this.parseClassSuper(node);
|
1047 | node.body = this.parseClassBody(!!node.superClass, oldStrict);
|
1048 | return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
|
1049 | }
|
1050 | isClassProperty() {
|
1051 | return this.match(29) || this.match(13) || this.match(8);
|
1052 | }
|
1053 | isClassMethod() {
|
1054 | return this.match(10);
|
1055 | }
|
1056 | isNonstaticConstructor(method) {
|
1057 | return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
|
1058 | }
|
1059 | parseClassBody(hadSuperClass, oldStrict) {
|
1060 | this.classScope.enter();
|
1061 | const state = {
|
1062 | hadConstructor: false,
|
1063 | hadSuperClass
|
1064 | };
|
1065 | let decorators = [];
|
1066 | const classBody = this.startNode();
|
1067 | classBody.body = [];
|
1068 | this.expect(5);
|
1069 | this.withSmartMixTopicForbiddingContext(() => {
|
1070 | while (!this.match(8)) {
|
1071 | if (this.eat(13)) {
|
1072 | if (decorators.length > 0) {
|
1073 | throw this.raise(_parseError.Errors.DecoratorSemicolon, {
|
1074 | at: this.state.lastTokEndLoc
|
1075 | });
|
1076 | }
|
1077 | continue;
|
1078 | }
|
1079 | if (this.match(26)) {
|
1080 | decorators.push(this.parseDecorator());
|
1081 | continue;
|
1082 | }
|
1083 | const member = this.startNode();
|
1084 | if (decorators.length) {
|
1085 | member.decorators = decorators;
|
1086 | this.resetStartLocationFromNode(member, decorators[0]);
|
1087 | decorators = [];
|
1088 | }
|
1089 | this.parseClassMember(classBody, member, state);
|
1090 | if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
|
1091 | this.raise(_parseError.Errors.DecoratorConstructor, {
|
1092 | at: member
|
1093 | });
|
1094 | }
|
1095 | }
|
1096 | });
|
1097 | this.state.strict = oldStrict;
|
1098 | this.next();
|
1099 | if (decorators.length) {
|
1100 | throw this.raise(_parseError.Errors.TrailingDecorator, {
|
1101 | at: this.state.startLoc
|
1102 | });
|
1103 | }
|
1104 | this.classScope.exit();
|
1105 | return this.finishNode(classBody, "ClassBody");
|
1106 | }
|
1107 | parseClassMemberFromModifier(classBody, member) {
|
1108 | const key = this.parseIdentifier(true);
|
1109 | if (this.isClassMethod()) {
|
1110 | const method = member;
|
1111 | method.kind = "method";
|
1112 | method.computed = false;
|
1113 | method.key = key;
|
1114 | method.static = false;
|
1115 | this.pushClassMethod(classBody, method, false, false, false, false);
|
1116 | return true;
|
1117 | } else if (this.isClassProperty()) {
|
1118 | const prop = member;
|
1119 | prop.computed = false;
|
1120 | prop.key = key;
|
1121 | prop.static = false;
|
1122 | classBody.body.push(this.parseClassProperty(prop));
|
1123 | return true;
|
1124 | }
|
1125 | this.resetPreviousNodeTrailingComments(key);
|
1126 | return false;
|
1127 | }
|
1128 | parseClassMember(classBody, member, state) {
|
1129 | const isStatic = this.isContextual(104);
|
1130 | if (isStatic) {
|
1131 | if (this.parseClassMemberFromModifier(classBody, member)) {
|
1132 | return;
|
1133 | }
|
1134 | if (this.eat(5)) {
|
1135 | this.parseClassStaticBlock(classBody, member);
|
1136 | return;
|
1137 | }
|
1138 | }
|
1139 | this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
1140 | }
|
1141 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
1142 | const publicMethod = member;
|
1143 | const privateMethod = member;
|
1144 | const publicProp = member;
|
1145 | const privateProp = member;
|
1146 | const accessorProp = member;
|
1147 | const method = publicMethod;
|
1148 | const publicMember = publicMethod;
|
1149 | member.static = isStatic;
|
1150 | this.parsePropertyNamePrefixOperator(member);
|
1151 | if (this.eat(55)) {
|
1152 | method.kind = "method";
|
1153 | const isPrivateName = this.match(136);
|
1154 | this.parseClassElementName(method);
|
1155 | if (isPrivateName) {
|
1156 | this.pushClassPrivateMethod(classBody, privateMethod, true, false);
|
1157 | return;
|
1158 | }
|
1159 | if (this.isNonstaticConstructor(publicMethod)) {
|
1160 | this.raise(_parseError.Errors.ConstructorIsGenerator, {
|
1161 | at: publicMethod.key
|
1162 | });
|
1163 | }
|
1164 | this.pushClassMethod(classBody, publicMethod, true, false, false, false);
|
1165 | return;
|
1166 | }
|
1167 | const isContextual = (0, _types.tokenIsIdentifier)(this.state.type) && !this.state.containsEsc;
|
1168 | const isPrivate = this.match(136);
|
1169 | const key = this.parseClassElementName(member);
|
1170 | const maybeQuestionTokenStartLoc = this.state.startLoc;
|
1171 | this.parsePostMemberNameModifiers(publicMember);
|
1172 | if (this.isClassMethod()) {
|
1173 | method.kind = "method";
|
1174 | if (isPrivate) {
|
1175 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
1176 | return;
|
1177 | }
|
1178 | const isConstructor = this.isNonstaticConstructor(publicMethod);
|
1179 | let allowsDirectSuper = false;
|
1180 | if (isConstructor) {
|
1181 | publicMethod.kind = "constructor";
|
1182 | if (state.hadConstructor && !this.hasPlugin("typescript")) {
|
1183 | this.raise(_parseError.Errors.DuplicateConstructor, {
|
1184 | at: key
|
1185 | });
|
1186 | }
|
1187 | if (isConstructor && this.hasPlugin("typescript") && member.override) {
|
1188 | this.raise(_parseError.Errors.OverrideOnConstructor, {
|
1189 | at: key
|
1190 | });
|
1191 | }
|
1192 | state.hadConstructor = true;
|
1193 | allowsDirectSuper = state.hadSuperClass;
|
1194 | }
|
1195 | this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
|
1196 | } else if (this.isClassProperty()) {
|
1197 | if (isPrivate) {
|
1198 | this.pushClassPrivateProperty(classBody, privateProp);
|
1199 | } else {
|
1200 | this.pushClassProperty(classBody, publicProp);
|
1201 | }
|
1202 | } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
|
1203 | this.resetPreviousNodeTrailingComments(key);
|
1204 | const isGenerator = this.eat(55);
|
1205 | if (publicMember.optional) {
|
1206 | this.unexpected(maybeQuestionTokenStartLoc);
|
1207 | }
|
1208 | method.kind = "method";
|
1209 | const isPrivate = this.match(136);
|
1210 | this.parseClassElementName(method);
|
1211 | this.parsePostMemberNameModifiers(publicMember);
|
1212 | if (isPrivate) {
|
1213 | this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
|
1214 | } else {
|
1215 | if (this.isNonstaticConstructor(publicMethod)) {
|
1216 | this.raise(_parseError.Errors.ConstructorIsAsync, {
|
1217 | at: publicMethod.key
|
1218 | });
|
1219 | }
|
1220 | this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
|
1221 | }
|
1222 | } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
|
1223 | this.resetPreviousNodeTrailingComments(key);
|
1224 | method.kind = key.name;
|
1225 | const isPrivate = this.match(136);
|
1226 | this.parseClassElementName(publicMethod);
|
1227 | if (isPrivate) {
|
1228 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
1229 | } else {
|
1230 | if (this.isNonstaticConstructor(publicMethod)) {
|
1231 | this.raise(_parseError.Errors.ConstructorIsAccessor, {
|
1232 | at: publicMethod.key
|
1233 | });
|
1234 | }
|
1235 | this.pushClassMethod(classBody, publicMethod, false, false, false, false);
|
1236 | }
|
1237 | this.checkGetterSetterParams(publicMethod);
|
1238 | } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
|
1239 | this.expectPlugin("decoratorAutoAccessors");
|
1240 | this.resetPreviousNodeTrailingComments(key);
|
1241 | const isPrivate = this.match(136);
|
1242 | this.parseClassElementName(publicProp);
|
1243 | this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
|
1244 | } else if (this.isLineTerminator()) {
|
1245 | if (isPrivate) {
|
1246 | this.pushClassPrivateProperty(classBody, privateProp);
|
1247 | } else {
|
1248 | this.pushClassProperty(classBody, publicProp);
|
1249 | }
|
1250 | } else {
|
1251 | this.unexpected();
|
1252 | }
|
1253 | }
|
1254 | parseClassElementName(member) {
|
1255 | const {
|
1256 | type,
|
1257 | value
|
1258 | } = this.state;
|
1259 | if ((type === 130 || type === 131) && member.static && value === "prototype") {
|
1260 | this.raise(_parseError.Errors.StaticPrototype, {
|
1261 | at: this.state.startLoc
|
1262 | });
|
1263 | }
|
1264 | if (type === 136) {
|
1265 | if (value === "constructor") {
|
1266 | this.raise(_parseError.Errors.ConstructorClassPrivateField, {
|
1267 | at: this.state.startLoc
|
1268 | });
|
1269 | }
|
1270 | const key = this.parsePrivateName();
|
1271 | member.key = key;
|
1272 | return key;
|
1273 | }
|
1274 | return this.parsePropertyName(member);
|
1275 | }
|
1276 | parseClassStaticBlock(classBody, member) {
|
1277 | var _member$decorators;
|
1278 | this.scope.enter(_scopeflags.SCOPE_CLASS | _scopeflags.SCOPE_STATIC_BLOCK | _scopeflags.SCOPE_SUPER);
|
1279 | const oldLabels = this.state.labels;
|
1280 | this.state.labels = [];
|
1281 | this.prodParam.enter(_productionParameter.PARAM);
|
1282 | const body = member.body = [];
|
1283 | this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
|
1284 | this.prodParam.exit();
|
1285 | this.scope.exit();
|
1286 | this.state.labels = oldLabels;
|
1287 | classBody.body.push(this.finishNode(member, "StaticBlock"));
|
1288 | if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
|
1289 | this.raise(_parseError.Errors.DecoratorStaticBlock, {
|
1290 | at: member
|
1291 | });
|
1292 | }
|
1293 | }
|
1294 | pushClassProperty(classBody, prop) {
|
1295 | if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
|
1296 | this.raise(_parseError.Errors.ConstructorClassField, {
|
1297 | at: prop.key
|
1298 | });
|
1299 | }
|
1300 | classBody.body.push(this.parseClassProperty(prop));
|
1301 | }
|
1302 | pushClassPrivateProperty(classBody, prop) {
|
1303 | const node = this.parseClassPrivateProperty(prop);
|
1304 | classBody.body.push(node);
|
1305 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), _scopeflags.CLASS_ELEMENT_OTHER, node.key.loc.start);
|
1306 | }
|
1307 | pushClassAccessorProperty(classBody, prop, isPrivate) {
|
1308 | if (!isPrivate && !prop.computed) {
|
1309 | const key = prop.key;
|
1310 | if (key.name === "constructor" || key.value === "constructor") {
|
1311 | this.raise(_parseError.Errors.ConstructorClassField, {
|
1312 | at: key
|
1313 | });
|
1314 | }
|
1315 | }
|
1316 | const node = this.parseClassAccessorProperty(prop);
|
1317 | classBody.body.push(node);
|
1318 | if (isPrivate) {
|
1319 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), _scopeflags.CLASS_ELEMENT_OTHER, node.key.loc.start);
|
1320 | }
|
1321 | }
|
1322 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
1323 | classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
|
1324 | }
|
1325 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
1326 | const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
|
1327 | classBody.body.push(node);
|
1328 | const kind = node.kind === "get" ? node.static ? _scopeflags.CLASS_ELEMENT_STATIC_GETTER : _scopeflags.CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? _scopeflags.CLASS_ELEMENT_STATIC_SETTER : _scopeflags.CLASS_ELEMENT_INSTANCE_SETTER : _scopeflags.CLASS_ELEMENT_OTHER;
|
1329 | this.declareClassPrivateMethodInScope(node, kind);
|
1330 | }
|
1331 | declareClassPrivateMethodInScope(node, kind) {
|
1332 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
|
1333 | }
|
1334 | parsePostMemberNameModifiers(methodOrProp) {}
|
1335 | parseClassPrivateProperty(node) {
|
1336 | this.parseInitializer(node);
|
1337 | this.semicolon();
|
1338 | return this.finishNode(node, "ClassPrivateProperty");
|
1339 | }
|
1340 | parseClassProperty(node) {
|
1341 | this.parseInitializer(node);
|
1342 | this.semicolon();
|
1343 | return this.finishNode(node, "ClassProperty");
|
1344 | }
|
1345 | parseClassAccessorProperty(node) {
|
1346 | this.parseInitializer(node);
|
1347 | this.semicolon();
|
1348 | return this.finishNode(node, "ClassAccessorProperty");
|
1349 | }
|
1350 | parseInitializer(node) {
|
1351 | this.scope.enter(_scopeflags.SCOPE_CLASS | _scopeflags.SCOPE_SUPER);
|
1352 | this.expressionScope.enter((0, _expressionScope.newExpressionScope)());
|
1353 | this.prodParam.enter(_productionParameter.PARAM);
|
1354 | node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
|
1355 | this.expressionScope.exit();
|
1356 | this.prodParam.exit();
|
1357 | this.scope.exit();
|
1358 | }
|
1359 | parseClassId(node, isStatement, optionalId, bindingType = _scopeflags.BIND_CLASS) {
|
1360 | if ((0, _types.tokenIsIdentifier)(this.state.type)) {
|
1361 | node.id = this.parseIdentifier();
|
1362 | if (isStatement) {
|
1363 | this.declareNameFromIdentifier(node.id, bindingType);
|
1364 | }
|
1365 | } else {
|
1366 | if (optionalId || !isStatement) {
|
1367 | node.id = null;
|
1368 | } else {
|
1369 | throw this.raise(_parseError.Errors.MissingClassName, {
|
1370 | at: this.state.startLoc
|
1371 | });
|
1372 | }
|
1373 | }
|
1374 | }
|
1375 | parseClassSuper(node) {
|
1376 | node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
|
1377 | }
|
1378 | parseExport(node, decorators) {
|
1379 | const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
|
1380 | const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
|
1381 | const parseAfterDefault = !hasDefault || this.eat(12);
|
1382 | const hasStar = parseAfterDefault && this.eatExportStar(node);
|
1383 | const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
|
1384 | const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
|
1385 | const isFromRequired = hasDefault || hasStar;
|
1386 | if (hasStar && !hasNamespace) {
|
1387 | if (hasDefault) this.unexpected();
|
1388 | if (decorators) {
|
1389 | throw this.raise(_parseError.Errors.UnsupportedDecoratorExport, {
|
1390 | at: node
|
1391 | });
|
1392 | }
|
1393 | this.parseExportFrom(node, true);
|
1394 | return this.finishNode(node, "ExportAllDeclaration");
|
1395 | }
|
1396 | const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
|
1397 | if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
|
1398 | this.unexpected(null, 5);
|
1399 | }
|
1400 | if (hasNamespace && parseAfterNamespace) {
|
1401 | this.unexpected(null, 97);
|
1402 | }
|
1403 | let hasDeclaration;
|
1404 | if (isFromRequired || hasSpecifiers) {
|
1405 | hasDeclaration = false;
|
1406 | if (decorators) {
|
1407 | throw this.raise(_parseError.Errors.UnsupportedDecoratorExport, {
|
1408 | at: node
|
1409 | });
|
1410 | }
|
1411 | this.parseExportFrom(node, isFromRequired);
|
1412 | } else {
|
1413 | hasDeclaration = this.maybeParseExportDeclaration(node);
|
1414 | }
|
1415 | if (isFromRequired || hasSpecifiers || hasDeclaration) {
|
1416 | var _node2$declaration;
|
1417 | const node2 = node;
|
1418 | this.checkExport(node2, true, false, !!node2.source);
|
1419 | if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
|
1420 | this.maybeTakeDecorators(decorators, node2.declaration, node2);
|
1421 | } else if (decorators) {
|
1422 | throw this.raise(_parseError.Errors.UnsupportedDecoratorExport, {
|
1423 | at: node
|
1424 | });
|
1425 | }
|
1426 | return this.finishNode(node2, "ExportNamedDeclaration");
|
1427 | }
|
1428 | if (this.eat(65)) {
|
1429 | const node2 = node;
|
1430 | const decl = this.parseExportDefaultExpression();
|
1431 | node2.declaration = decl;
|
1432 | if (decl.type === "ClassDeclaration") {
|
1433 | this.maybeTakeDecorators(decorators, decl, node2);
|
1434 | } else if (decorators) {
|
1435 | throw this.raise(_parseError.Errors.UnsupportedDecoratorExport, {
|
1436 | at: node
|
1437 | });
|
1438 | }
|
1439 | this.checkExport(node2, true, true);
|
1440 | return this.finishNode(node2, "ExportDefaultDeclaration");
|
1441 | }
|
1442 | this.unexpected(null, 5);
|
1443 | }
|
1444 | eatExportStar(node) {
|
1445 | return this.eat(55);
|
1446 | }
|
1447 | maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
|
1448 | if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
|
1449 | this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
|
1450 | const id = maybeDefaultIdentifier || this.parseIdentifier(true);
|
1451 | const specifier = this.startNodeAtNode(id);
|
1452 | specifier.exported = id;
|
1453 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
1454 | return true;
|
1455 | }
|
1456 | return false;
|
1457 | }
|
1458 | maybeParseExportNamespaceSpecifier(node) {
|
1459 | if (this.isContextual(93)) {
|
1460 | if (!node.specifiers) node.specifiers = [];
|
1461 | const specifier = this.startNodeAt(this.state.lastTokStartLoc);
|
1462 | this.next();
|
1463 | specifier.exported = this.parseModuleExportName();
|
1464 | node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
|
1465 | return true;
|
1466 | }
|
1467 | return false;
|
1468 | }
|
1469 | maybeParseExportNamedSpecifiers(node) {
|
1470 | if (this.match(5)) {
|
1471 | if (!node.specifiers) node.specifiers = [];
|
1472 | const isTypeExport = node.exportKind === "type";
|
1473 | node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
|
1474 | node.source = null;
|
1475 | node.declaration = null;
|
1476 | if (this.hasPlugin("importAssertions")) {
|
1477 | node.assertions = [];
|
1478 | }
|
1479 | return true;
|
1480 | }
|
1481 | return false;
|
1482 | }
|
1483 | maybeParseExportDeclaration(node) {
|
1484 | if (this.shouldParseExportDeclaration()) {
|
1485 | node.specifiers = [];
|
1486 | node.source = null;
|
1487 | if (this.hasPlugin("importAssertions")) {
|
1488 | node.assertions = [];
|
1489 | }
|
1490 | node.declaration = this.parseExportDeclaration(node);
|
1491 | return true;
|
1492 | }
|
1493 | return false;
|
1494 | }
|
1495 | isAsyncFunction() {
|
1496 | if (!this.isContextual(95)) return false;
|
1497 | const next = this.nextTokenInLineStart();
|
1498 | return this.isUnparsedContextual(next, "function");
|
1499 | }
|
1500 | parseExportDefaultExpression() {
|
1501 | const expr = this.startNode();
|
1502 | if (this.match(68)) {
|
1503 | this.next();
|
1504 | return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId);
|
1505 | } else if (this.isAsyncFunction()) {
|
1506 | this.next();
|
1507 | this.next();
|
1508 | return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId | ParseFunctionFlag.Async);
|
1509 | }
|
1510 | if (this.match(80)) {
|
1511 | return this.parseClass(expr, true, true);
|
1512 | }
|
1513 | if (this.match(26)) {
|
1514 | if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
|
1515 | this.raise(_parseError.Errors.DecoratorBeforeExport, {
|
1516 | at: this.state.startLoc
|
1517 | });
|
1518 | }
|
1519 | return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
|
1520 | }
|
1521 | if (this.match(75) || this.match(74) || this.isLet()) {
|
1522 | throw this.raise(_parseError.Errors.UnsupportedDefaultExport, {
|
1523 | at: this.state.startLoc
|
1524 | });
|
1525 | }
|
1526 | const res = this.parseMaybeAssignAllowIn();
|
1527 | this.semicolon();
|
1528 | return res;
|
1529 | }
|
1530 | parseExportDeclaration(node) {
|
1531 | if (this.match(80)) {
|
1532 | const node = this.parseClass(this.startNode(), true, false);
|
1533 | return node;
|
1534 | }
|
1535 | return this.parseStatementListItem();
|
1536 | }
|
1537 | isExportDefaultSpecifier() {
|
1538 | const {
|
1539 | type
|
1540 | } = this.state;
|
1541 | if ((0, _types.tokenIsIdentifier)(type)) {
|
1542 | if (type === 95 && !this.state.containsEsc || type === 99) {
|
1543 | return false;
|
1544 | }
|
1545 | if ((type === 128 || type === 127) && !this.state.containsEsc) {
|
1546 | const {
|
1547 | type: nextType
|
1548 | } = this.lookahead();
|
1549 | if ((0, _types.tokenIsIdentifier)(nextType) && nextType !== 97 || nextType === 5) {
|
1550 | this.expectOnePlugin(["flow", "typescript"]);
|
1551 | return false;
|
1552 | }
|
1553 | }
|
1554 | } else if (!this.match(65)) {
|
1555 | return false;
|
1556 | }
|
1557 | const next = this.nextTokenStart();
|
1558 | const hasFrom = this.isUnparsedContextual(next, "from");
|
1559 | if (this.input.charCodeAt(next) === 44 || (0, _types.tokenIsIdentifier)(this.state.type) && hasFrom) {
|
1560 | return true;
|
1561 | }
|
1562 | if (this.match(65) && hasFrom) {
|
1563 | const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
|
1564 | return nextAfterFrom === 34 || nextAfterFrom === 39;
|
1565 | }
|
1566 | return false;
|
1567 | }
|
1568 | parseExportFrom(node, expect) {
|
1569 | if (this.eatContextual(97)) {
|
1570 | node.source = this.parseImportSource();
|
1571 | this.checkExport(node);
|
1572 | this.maybeParseImportAttributes(node);
|
1573 | this.checkJSONModuleImport(node);
|
1574 | } else if (expect) {
|
1575 | this.unexpected();
|
1576 | }
|
1577 | this.semicolon();
|
1578 | }
|
1579 | shouldParseExportDeclaration() {
|
1580 | const {
|
1581 | type
|
1582 | } = this.state;
|
1583 | if (type === 26) {
|
1584 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
1585 | if (this.hasPlugin("decorators")) {
|
1586 | if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
|
1587 | this.raise(_parseError.Errors.DecoratorBeforeExport, {
|
1588 | at: this.state.startLoc
|
1589 | });
|
1590 | }
|
1591 | return true;
|
1592 | }
|
1593 | }
|
1594 | return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
|
1595 | }
|
1596 | checkExport(node, checkNames, isDefault, isFrom) {
|
1597 | if (checkNames) {
|
1598 | if (isDefault) {
|
1599 | this.checkDuplicateExports(node, "default");
|
1600 | if (this.hasPlugin("exportDefaultFrom")) {
|
1601 | var _declaration$extra;
|
1602 | const declaration = node.declaration;
|
1603 | if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
|
1604 | this.raise(_parseError.Errors.ExportDefaultFromAsIdentifier, {
|
1605 | at: declaration
|
1606 | });
|
1607 | }
|
1608 | }
|
1609 | } else if (node.specifiers && node.specifiers.length) {
|
1610 | for (const specifier of node.specifiers) {
|
1611 | const {
|
1612 | exported
|
1613 | } = specifier;
|
1614 | const exportName = exported.type === "Identifier" ? exported.name : exported.value;
|
1615 | this.checkDuplicateExports(specifier, exportName);
|
1616 | if (!isFrom && specifier.local) {
|
1617 | const {
|
1618 | local
|
1619 | } = specifier;
|
1620 | if (local.type !== "Identifier") {
|
1621 | this.raise(_parseError.Errors.ExportBindingIsString, {
|
1622 | at: specifier,
|
1623 | localName: local.value,
|
1624 | exportName
|
1625 | });
|
1626 | } else {
|
1627 | this.checkReservedWord(local.name, local.loc.start, true, false);
|
1628 | this.scope.checkLocalExport(local);
|
1629 | }
|
1630 | }
|
1631 | }
|
1632 | } else if (node.declaration) {
|
1633 | if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
|
1634 | const id = node.declaration.id;
|
1635 | if (!id) throw new Error("Assertion failure");
|
1636 | this.checkDuplicateExports(node, id.name);
|
1637 | } else if (node.declaration.type === "VariableDeclaration") {
|
1638 | for (const declaration of node.declaration.declarations) {
|
1639 | this.checkDeclaration(declaration.id);
|
1640 | }
|
1641 | }
|
1642 | }
|
1643 | }
|
1644 | }
|
1645 | checkDeclaration(node) {
|
1646 | if (node.type === "Identifier") {
|
1647 | this.checkDuplicateExports(node, node.name);
|
1648 | } else if (node.type === "ObjectPattern") {
|
1649 | for (const prop of node.properties) {
|
1650 | this.checkDeclaration(prop);
|
1651 | }
|
1652 | } else if (node.type === "ArrayPattern") {
|
1653 | for (const elem of node.elements) {
|
1654 | if (elem) {
|
1655 | this.checkDeclaration(elem);
|
1656 | }
|
1657 | }
|
1658 | } else if (node.type === "ObjectProperty") {
|
1659 | this.checkDeclaration(node.value);
|
1660 | } else if (node.type === "RestElement") {
|
1661 | this.checkDeclaration(node.argument);
|
1662 | } else if (node.type === "AssignmentPattern") {
|
1663 | this.checkDeclaration(node.left);
|
1664 | }
|
1665 | }
|
1666 | checkDuplicateExports(node, exportName) {
|
1667 | if (this.exportedIdentifiers.has(exportName)) {
|
1668 | if (exportName === "default") {
|
1669 | this.raise(_parseError.Errors.DuplicateDefaultExport, {
|
1670 | at: node
|
1671 | });
|
1672 | } else {
|
1673 | this.raise(_parseError.Errors.DuplicateExport, {
|
1674 | at: node,
|
1675 | exportName
|
1676 | });
|
1677 | }
|
1678 | }
|
1679 | this.exportedIdentifiers.add(exportName);
|
1680 | }
|
1681 | parseExportSpecifiers(isInTypeExport) {
|
1682 | const nodes = [];
|
1683 | let first = true;
|
1684 | this.expect(5);
|
1685 | while (!this.eat(8)) {
|
1686 | if (first) {
|
1687 | first = false;
|
1688 | } else {
|
1689 | this.expect(12);
|
1690 | if (this.eat(8)) break;
|
1691 | }
|
1692 | const isMaybeTypeOnly = this.isContextual(128);
|
1693 | const isString = this.match(131);
|
1694 | const node = this.startNode();
|
1695 | node.local = this.parseModuleExportName();
|
1696 | nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
|
1697 | }
|
1698 | return nodes;
|
1699 | }
|
1700 | parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
|
1701 | if (this.eatContextual(93)) {
|
1702 | node.exported = this.parseModuleExportName();
|
1703 | } else if (isString) {
|
1704 | node.exported = (0, _node.cloneStringLiteral)(node.local);
|
1705 | } else if (!node.exported) {
|
1706 | node.exported = (0, _node.cloneIdentifier)(node.local);
|
1707 | }
|
1708 | return this.finishNode(node, "ExportSpecifier");
|
1709 | }
|
1710 | parseModuleExportName() {
|
1711 | if (this.match(131)) {
|
1712 | const result = this.parseStringLiteral(this.state.value);
|
1713 | const surrogate = result.value.match(loneSurrogate);
|
1714 | if (surrogate) {
|
1715 | this.raise(_parseError.Errors.ModuleExportNameHasLoneSurrogate, {
|
1716 | at: result,
|
1717 | surrogateCharCode: surrogate[0].charCodeAt(0)
|
1718 | });
|
1719 | }
|
1720 | return result;
|
1721 | }
|
1722 | return this.parseIdentifier(true);
|
1723 | }
|
1724 | isJSONModuleImport(node) {
|
1725 | if (node.assertions != null) {
|
1726 | return node.assertions.some(({
|
1727 | key,
|
1728 | value
|
1729 | }) => {
|
1730 | return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
|
1731 | });
|
1732 | }
|
1733 | return false;
|
1734 | }
|
1735 | checkImportReflection(node) {
|
1736 | if (node.module) {
|
1737 | var _node$assertions;
|
1738 | if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
|
1739 | this.raise(_parseError.Errors.ImportReflectionNotBinding, {
|
1740 | at: node.specifiers[0].loc.start
|
1741 | });
|
1742 | }
|
1743 | if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
|
1744 | this.raise(_parseError.Errors.ImportReflectionHasAssertion, {
|
1745 | at: node.specifiers[0].loc.start
|
1746 | });
|
1747 | }
|
1748 | }
|
1749 | }
|
1750 | checkJSONModuleImport(node) {
|
1751 | if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
|
1752 | const {
|
1753 | specifiers
|
1754 | } = node;
|
1755 | if (specifiers != null) {
|
1756 | const nonDefaultNamedSpecifier = specifiers.find(specifier => {
|
1757 | let imported;
|
1758 | if (specifier.type === "ExportSpecifier") {
|
1759 | imported = specifier.local;
|
1760 | } else if (specifier.type === "ImportSpecifier") {
|
1761 | imported = specifier.imported;
|
1762 | }
|
1763 | if (imported !== undefined) {
|
1764 | return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
|
1765 | }
|
1766 | });
|
1767 | if (nonDefaultNamedSpecifier !== undefined) {
|
1768 | this.raise(_parseError.Errors.ImportJSONBindingNotDefault, {
|
1769 | at: nonDefaultNamedSpecifier.loc.start
|
1770 | });
|
1771 | }
|
1772 | }
|
1773 | }
|
1774 | }
|
1775 | isPotentialImportPhase(isExport) {
|
1776 | return !isExport && this.isContextual(125);
|
1777 | }
|
1778 | applyImportPhase(node, isExport, phase, loc) {
|
1779 | if (isExport) {
|
1780 | ;
|
1781 | return;
|
1782 | }
|
1783 | if (phase === "module") {
|
1784 | this.expectPlugin("importReflection", loc);
|
1785 | node.module = true;
|
1786 | } else if (this.hasPlugin("importReflection")) {
|
1787 | node.module = false;
|
1788 | }
|
1789 | }
|
1790 | parseMaybeImportPhase(node, isExport) {
|
1791 | if (!this.isPotentialImportPhase(isExport)) {
|
1792 | this.applyImportPhase(node, isExport, null);
|
1793 | return null;
|
1794 | }
|
1795 | const phaseIdentifier = this.parseIdentifier(true);
|
1796 | const {
|
1797 | type
|
1798 | } = this.state;
|
1799 | const isImportPhase = (0, _types.tokenIsKeywordOrIdentifier)(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
|
1800 | if (isImportPhase) {
|
1801 | this.resetPreviousIdentifierLeadingComments(phaseIdentifier);
|
1802 | this.applyImportPhase(node, isExport, phaseIdentifier.name, phaseIdentifier.loc.start);
|
1803 | return null;
|
1804 | } else {
|
1805 | this.applyImportPhase(node, isExport, null);
|
1806 | return phaseIdentifier;
|
1807 | }
|
1808 | }
|
1809 | isPrecedingIdImportPhase(phase) {
|
1810 | const {
|
1811 | type
|
1812 | } = this.state;
|
1813 | return (0, _types.tokenIsIdentifier)(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
|
1814 | }
|
1815 | parseImport(node) {
|
1816 | if (this.match(131)) {
|
1817 | return this.parseImportSourceAndAttributes(node);
|
1818 | }
|
1819 | return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
|
1820 | }
|
1821 | parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
|
1822 | node.specifiers = [];
|
1823 | const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
|
1824 | const parseNext = !hasDefault || this.eat(12);
|
1825 | const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
|
1826 | if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
|
1827 | this.expectContextual(97);
|
1828 | return this.parseImportSourceAndAttributes(node);
|
1829 | }
|
1830 | parseImportSourceAndAttributes(node) {
|
1831 | var _node$specifiers;
|
1832 | (_node$specifiers = node.specifiers) != null ? _node$specifiers : node.specifiers = [];
|
1833 | node.source = this.parseImportSource();
|
1834 | this.maybeParseImportAttributes(node);
|
1835 | this.checkImportReflection(node);
|
1836 | this.checkJSONModuleImport(node);
|
1837 | this.semicolon();
|
1838 | return this.finishNode(node, "ImportDeclaration");
|
1839 | }
|
1840 | parseImportSource() {
|
1841 | if (!this.match(131)) this.unexpected();
|
1842 | return this.parseExprAtom();
|
1843 | }
|
1844 | parseImportSpecifierLocal(node, specifier, type) {
|
1845 | specifier.local = this.parseIdentifier();
|
1846 | node.specifiers.push(this.finishImportSpecifier(specifier, type));
|
1847 | }
|
1848 | finishImportSpecifier(specifier, type, bindingType = _scopeflags.BIND_LEXICAL) {
|
1849 | this.checkLVal(specifier.local, {
|
1850 | in: {
|
1851 | type
|
1852 | },
|
1853 | binding: bindingType
|
1854 | });
|
1855 | return this.finishNode(specifier, type);
|
1856 | }
|
1857 | parseImportAttributes() {
|
1858 | this.expect(5);
|
1859 | const attrs = [];
|
1860 | const attrNames = new Set();
|
1861 | do {
|
1862 | if (this.match(8)) {
|
1863 | break;
|
1864 | }
|
1865 | const node = this.startNode();
|
1866 | const keyName = this.state.value;
|
1867 | if (attrNames.has(keyName)) {
|
1868 | this.raise(_parseError.Errors.ModuleAttributesWithDuplicateKeys, {
|
1869 | at: this.state.startLoc,
|
1870 | key: keyName
|
1871 | });
|
1872 | }
|
1873 | attrNames.add(keyName);
|
1874 | if (this.match(131)) {
|
1875 | node.key = this.parseStringLiteral(keyName);
|
1876 | } else {
|
1877 | node.key = this.parseIdentifier(true);
|
1878 | }
|
1879 | this.expect(14);
|
1880 | if (!this.match(131)) {
|
1881 | throw this.raise(_parseError.Errors.ModuleAttributeInvalidValue, {
|
1882 | at: this.state.startLoc
|
1883 | });
|
1884 | }
|
1885 | node.value = this.parseStringLiteral(this.state.value);
|
1886 | attrs.push(this.finishNode(node, "ImportAttribute"));
|
1887 | } while (this.eat(12));
|
1888 | this.expect(8);
|
1889 | return attrs;
|
1890 | }
|
1891 | parseModuleAttributes() {
|
1892 | const attrs = [];
|
1893 | const attributes = new Set();
|
1894 | do {
|
1895 | const node = this.startNode();
|
1896 | node.key = this.parseIdentifier(true);
|
1897 | if (node.key.name !== "type") {
|
1898 | this.raise(_parseError.Errors.ModuleAttributeDifferentFromType, {
|
1899 | at: node.key
|
1900 | });
|
1901 | }
|
1902 | if (attributes.has(node.key.name)) {
|
1903 | this.raise(_parseError.Errors.ModuleAttributesWithDuplicateKeys, {
|
1904 | at: node.key,
|
1905 | key: node.key.name
|
1906 | });
|
1907 | }
|
1908 | attributes.add(node.key.name);
|
1909 | this.expect(14);
|
1910 | if (!this.match(131)) {
|
1911 | throw this.raise(_parseError.Errors.ModuleAttributeInvalidValue, {
|
1912 | at: this.state.startLoc
|
1913 | });
|
1914 | }
|
1915 | node.value = this.parseStringLiteral(this.state.value);
|
1916 | attrs.push(this.finishNode(node, "ImportAttribute"));
|
1917 | } while (this.eat(12));
|
1918 | return attrs;
|
1919 | }
|
1920 | maybeParseImportAttributes(node) {
|
1921 | let attributes;
|
1922 | let useWith = false;
|
1923 | if (this.match(76)) {
|
1924 | if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
|
1925 | return;
|
1926 | }
|
1927 | this.next();
|
1928 | {
|
1929 | if (this.hasPlugin("moduleAttributes")) {
|
1930 | attributes = this.parseModuleAttributes();
|
1931 | } else {
|
1932 | this.expectImportAttributesPlugin();
|
1933 | attributes = this.parseImportAttributes();
|
1934 | }
|
1935 | }
|
1936 | useWith = true;
|
1937 | } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
|
1938 | if (this.hasPlugin("importAttributes")) {
|
1939 | if (this.getPluginOption("importAttributes", "deprecatedAssertSyntax") !== true) {
|
1940 | this.raise(_parseError.Errors.ImportAttributesUseAssert, {
|
1941 | at: this.state.startLoc
|
1942 | });
|
1943 | }
|
1944 | this.addExtra(node, "deprecatedAssertSyntax", true);
|
1945 | } else {
|
1946 | this.expectOnePlugin(["importAttributes", "importAssertions"]);
|
1947 | }
|
1948 | this.next();
|
1949 | attributes = this.parseImportAttributes();
|
1950 | } else if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
|
1951 | attributes = [];
|
1952 | } else {
|
1953 | if (this.hasPlugin("moduleAttributes")) {
|
1954 | attributes = [];
|
1955 | } else return;
|
1956 | }
|
1957 | if (!useWith && this.hasPlugin("importAssertions")) {
|
1958 | node.assertions = attributes;
|
1959 | } else {
|
1960 | node.attributes = attributes;
|
1961 | }
|
1962 | }
|
1963 | maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
|
1964 | if (maybeDefaultIdentifier) {
|
1965 | const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
|
1966 | specifier.local = maybeDefaultIdentifier;
|
1967 | node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
|
1968 | return true;
|
1969 | } else if ((0, _types.tokenIsKeywordOrIdentifier)(this.state.type)) {
|
1970 | this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
|
1971 | return true;
|
1972 | }
|
1973 | return false;
|
1974 | }
|
1975 | maybeParseStarImportSpecifier(node) {
|
1976 | if (this.match(55)) {
|
1977 | const specifier = this.startNode();
|
1978 | this.next();
|
1979 | this.expectContextual(93);
|
1980 | this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
|
1981 | return true;
|
1982 | }
|
1983 | return false;
|
1984 | }
|
1985 | parseNamedImportSpecifiers(node) {
|
1986 | let first = true;
|
1987 | this.expect(5);
|
1988 | while (!this.eat(8)) {
|
1989 | if (first) {
|
1990 | first = false;
|
1991 | } else {
|
1992 | if (this.eat(14)) {
|
1993 | throw this.raise(_parseError.Errors.DestructureNamedImport, {
|
1994 | at: this.state.startLoc
|
1995 | });
|
1996 | }
|
1997 | this.expect(12);
|
1998 | if (this.eat(8)) break;
|
1999 | }
|
2000 | const specifier = this.startNode();
|
2001 | const importedIsString = this.match(131);
|
2002 | const isMaybeTypeOnly = this.isContextual(128);
|
2003 | specifier.imported = this.parseModuleExportName();
|
2004 | const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
|
2005 | node.specifiers.push(importSpecifier);
|
2006 | }
|
2007 | }
|
2008 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
|
2009 | if (this.eatContextual(93)) {
|
2010 | specifier.local = this.parseIdentifier();
|
2011 | } else {
|
2012 | const {
|
2013 | imported
|
2014 | } = specifier;
|
2015 | if (importedIsString) {
|
2016 | throw this.raise(_parseError.Errors.ImportBindingIsString, {
|
2017 | at: specifier,
|
2018 | importName: imported.value
|
2019 | });
|
2020 | }
|
2021 | this.checkReservedWord(imported.name, specifier.loc.start, true, true);
|
2022 | if (!specifier.local) {
|
2023 | specifier.local = (0, _node.cloneIdentifier)(imported);
|
2024 | }
|
2025 | }
|
2026 | return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
|
2027 | }
|
2028 | isThisParam(param) {
|
2029 | return param.type === "Identifier" && param.name === "this";
|
2030 | }
|
2031 | }
|
2032 | exports.default = StatementParser;
|
2033 |
|
2034 |
|