1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _objectWithoutPropertiesLoose(source, excluded) {
|
6 | if (source == null) return {};
|
7 | var target = {};
|
8 | var sourceKeys = Object.keys(source);
|
9 | var key, i;
|
10 |
|
11 | for (i = 0; i < sourceKeys.length; i++) {
|
12 | key = sourceKeys[i];
|
13 | if (excluded.indexOf(key) >= 0) continue;
|
14 | target[key] = source[key];
|
15 | }
|
16 |
|
17 | return target;
|
18 | }
|
19 |
|
20 | class Position {
|
21 | constructor(line, col, index) {
|
22 | this.line = void 0;
|
23 | this.column = void 0;
|
24 | this.index = void 0;
|
25 | this.line = line;
|
26 | this.column = col;
|
27 | this.index = index;
|
28 | }
|
29 |
|
30 | }
|
31 | class SourceLocation {
|
32 | constructor(start, end) {
|
33 | this.start = void 0;
|
34 | this.end = void 0;
|
35 | this.filename = void 0;
|
36 | this.identifierName = void 0;
|
37 | this.start = start;
|
38 | this.end = end;
|
39 | }
|
40 |
|
41 | }
|
42 | function createPositionWithColumnOffset(position, columnOffset) {
|
43 | const {
|
44 | line,
|
45 | column,
|
46 | index
|
47 | } = position;
|
48 | return new Position(line, column + columnOffset, index + columnOffset);
|
49 | }
|
50 |
|
51 | const ParseErrorCodes = Object.freeze({
|
52 | SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
|
53 | SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
|
54 | });
|
55 |
|
56 | const reflect = (keys, last = keys.length - 1) => ({
|
57 | get() {
|
58 | return keys.reduce((object, key) => object[key], this);
|
59 | },
|
60 |
|
61 | set(value) {
|
62 | keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
|
63 | }
|
64 |
|
65 | });
|
66 |
|
67 | const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
|
68 | value: descriptor,
|
69 | enumerable: false
|
70 | } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
|
71 | configurable: true
|
72 | }, descriptor)), Object.assign(new constructor(), properties));
|
73 |
|
74 | var ModuleErrors = (_ => ({
|
75 | ImportMetaOutsideModule: _(`import.meta may appear only with 'sourceType: "module"'`, {
|
76 | code: ParseErrorCodes.SourceTypeModuleError
|
77 | }),
|
78 | ImportOutsideModule: _(`'import' and 'export' may appear only with 'sourceType: "module"'`, {
|
79 | code: ParseErrorCodes.SourceTypeModuleError
|
80 | })
|
81 | }));
|
82 |
|
83 | const NodeDescriptions = {
|
84 | ArrayPattern: "array destructuring pattern",
|
85 | AssignmentExpression: "assignment expression",
|
86 | AssignmentPattern: "assignment expression",
|
87 | ArrowFunctionExpression: "arrow function expression",
|
88 | ConditionalExpression: "conditional expression",
|
89 | ForOfStatement: "for-of statement",
|
90 | ForInStatement: "for-in statement",
|
91 | ForStatement: "for-loop",
|
92 | FormalParameters: "function parameter list",
|
93 | Identifier: "identifier",
|
94 | ObjectPattern: "object destructuring pattern",
|
95 | ParenthesizedExpression: "parenthesized expression",
|
96 | RestElement: "rest element",
|
97 | UpdateExpression: {
|
98 | true: "prefix operation",
|
99 | false: "postfix operation"
|
100 | },
|
101 | VariableDeclarator: "variable declaration",
|
102 | YieldExpression: "yield expression"
|
103 | };
|
104 |
|
105 | const toNodeDescription = ({
|
106 | type,
|
107 | prefix
|
108 | }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
|
109 |
|
110 | var StandardErrors = (_ => ({
|
111 | AccessorIsGenerator: _(({
|
112 | kind
|
113 | }) => `A ${kind}ter cannot be a generator.`),
|
114 | ArgumentsInClass: _("'arguments' is only allowed in functions and class methods."),
|
115 | AsyncFunctionInSingleStatementContext: _("Async functions can only be declared at the top level or inside a block."),
|
116 | AwaitBindingIdentifier: _("Can not use 'await' as identifier inside an async function."),
|
117 | AwaitBindingIdentifierInStaticBlock: _("Can not use 'await' as identifier inside a static block."),
|
118 | AwaitExpressionFormalParameter: _("'await' is not allowed in async function parameters."),
|
119 | AwaitNotInAsyncContext: _("'await' is only allowed within async functions and at the top levels of modules."),
|
120 | AwaitNotInAsyncFunction: _("'await' is only allowed within async functions."),
|
121 | BadGetterArity: _("A 'get' accesor must not have any formal parameters."),
|
122 | BadSetterArity: _("A 'set' accesor must have exactly one formal parameter."),
|
123 | BadSetterRestParameter: _("A 'set' accesor function argument must not be a rest parameter."),
|
124 | ConstructorClassField: _("Classes may not have a field named 'constructor'."),
|
125 | ConstructorClassPrivateField: _("Classes may not have a private field named '#constructor'."),
|
126 | ConstructorIsAccessor: _("Class constructor may not be an accessor."),
|
127 | ConstructorIsAsync: _("Constructor can't be an async function."),
|
128 | ConstructorIsGenerator: _("Constructor can't be a generator."),
|
129 | DeclarationMissingInitializer: _(({
|
130 | kind
|
131 | }) => `Missing initializer in ${kind} declaration.`),
|
132 | DecoratorBeforeExport: _("Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax."),
|
133 | DecoratorConstructor: _("Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?"),
|
134 | DecoratorExportClass: _("Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead."),
|
135 | DecoratorSemicolon: _("Decorators must not be followed by a semicolon."),
|
136 | DecoratorStaticBlock: _("Decorators can't be used with a static block."),
|
137 | DeletePrivateField: _("Deleting a private field is not allowed."),
|
138 | DestructureNamedImport: _("ES2015 named imports do not destructure. Use another statement for destructuring after the import."),
|
139 | DuplicateConstructor: _("Duplicate constructor in the same class."),
|
140 | DuplicateDefaultExport: _("Only one default export allowed per module."),
|
141 | DuplicateExport: _(({
|
142 | exportName
|
143 | }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`),
|
144 | DuplicateProto: _("Redefinition of __proto__ property."),
|
145 | DuplicateRegExpFlags: _("Duplicate regular expression flag."),
|
146 | ElementAfterRest: _("Rest element must be last element."),
|
147 | EscapedCharNotAnIdentifier: _("Invalid Unicode escape."),
|
148 | ExportBindingIsString: _(({
|
149 | localName,
|
150 | exportName
|
151 | }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`),
|
152 | ExportDefaultFromAsIdentifier: _("'from' is not allowed as an identifier after 'export default'."),
|
153 | ForInOfLoopInitializer: _(({
|
154 | type
|
155 | }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`),
|
156 | ForOfAsync: _("The left-hand side of a for-of loop may not be 'async'."),
|
157 | ForOfLet: _("The left-hand side of a for-of loop may not start with 'let'."),
|
158 | GeneratorInSingleStatementContext: _("Generators can only be declared at the top level or inside a block."),
|
159 | IllegalBreakContinue: _(({
|
160 | type
|
161 | }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`),
|
162 | IllegalLanguageModeDirective: _("Illegal 'use strict' directive in function with non-simple parameter list."),
|
163 | IllegalReturn: _("'return' outside of function."),
|
164 | ImportBindingIsString: _(({
|
165 | importName
|
166 | }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`),
|
167 | ImportCallArgumentTrailingComma: _("Trailing comma is disallowed inside import(...) arguments."),
|
168 | ImportCallArity: _(({
|
169 | maxArgumentCount
|
170 | }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`),
|
171 | ImportCallNotNewExpression: _("Cannot use new with import(...)."),
|
172 | ImportCallSpreadArgument: _("`...` is not allowed in `import()`."),
|
173 | ImportJSONBindingNotDefault: _("A JSON module can only be imported with `default`."),
|
174 | IncompatibleRegExpUVFlags: _("The 'u' and 'v' regular expression flags cannot be enabled at the same time."),
|
175 | InvalidBigIntLiteral: _("Invalid BigIntLiteral."),
|
176 | InvalidCodePoint: _("Code point out of bounds."),
|
177 | InvalidCoverInitializedName: _("Invalid shorthand property initializer."),
|
178 | InvalidDecimal: _("Invalid decimal."),
|
179 | InvalidDigit: _(({
|
180 | radix
|
181 | }) => `Expected number in radix ${radix}.`),
|
182 | InvalidEscapeSequence: _("Bad character escape sequence."),
|
183 | InvalidEscapeSequenceTemplate: _("Invalid escape sequence in template."),
|
184 | InvalidEscapedReservedWord: _(({
|
185 | reservedWord
|
186 | }) => `Escape sequence in keyword ${reservedWord}.`),
|
187 | InvalidIdentifier: _(({
|
188 | identifierName
|
189 | }) => `Invalid identifier ${identifierName}.`),
|
190 | InvalidLhs: _(({
|
191 | ancestor
|
192 | }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`),
|
193 | InvalidLhsBinding: _(({
|
194 | ancestor
|
195 | }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`),
|
196 | InvalidNumber: _("Invalid number."),
|
197 | InvalidOrMissingExponent: _("Floating-point numbers require a valid exponent after the 'e'."),
|
198 | InvalidOrUnexpectedToken: _(({
|
199 | unexpected
|
200 | }) => `Unexpected character '${unexpected}'.`),
|
201 | InvalidParenthesizedAssignment: _("Invalid parenthesized assignment pattern."),
|
202 | InvalidPrivateFieldResolution: _(({
|
203 | identifierName
|
204 | }) => `Private name #${identifierName} is not defined.`),
|
205 | InvalidPropertyBindingPattern: _("Binding member expression."),
|
206 | InvalidRecordProperty: _("Only properties and spread elements are allowed in record definitions."),
|
207 | InvalidRestAssignmentPattern: _("Invalid rest operator's argument."),
|
208 | LabelRedeclaration: _(({
|
209 | labelName
|
210 | }) => `Label '${labelName}' is already declared.`),
|
211 | LetInLexicalBinding: _("'let' is not allowed to be used as a name in 'let' or 'const' declarations."),
|
212 | LineTerminatorBeforeArrow: _("No line break is allowed before '=>'."),
|
213 | MalformedRegExpFlags: _("Invalid regular expression flag."),
|
214 | MissingClassName: _("A class name is required."),
|
215 | MissingEqInAssignment: _("Only '=' operator can be used for specifying default value."),
|
216 | MissingSemicolon: _("Missing semicolon."),
|
217 | MissingPlugin: _(({
|
218 | missingPlugin
|
219 | }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`),
|
220 | MissingOneOfPlugins: _(({
|
221 | missingPlugin
|
222 | }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`),
|
223 | MissingUnicodeEscape: _("Expecting Unicode escape sequence \\uXXXX."),
|
224 | MixingCoalesceWithLogical: _("Nullish coalescing operator(??) requires parens when mixing with logical operators."),
|
225 | ModuleAttributeDifferentFromType: _("The only accepted module attribute is `type`."),
|
226 | ModuleAttributeInvalidValue: _("Only string literals are allowed as module attribute values."),
|
227 | ModuleAttributesWithDuplicateKeys: _(({
|
228 | key
|
229 | }) => `Duplicate key "${key}" is not allowed in module attributes.`),
|
230 | ModuleExportNameHasLoneSurrogate: _(({
|
231 | surrogateCharCode
|
232 | }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`),
|
233 | ModuleExportUndefined: _(({
|
234 | localName
|
235 | }) => `Export '${localName}' is not defined.`),
|
236 | MultipleDefaultsInSwitch: _("Multiple default clauses."),
|
237 | NewlineAfterThrow: _("Illegal newline after throw."),
|
238 | NoCatchOrFinally: _("Missing catch or finally clause."),
|
239 | NumberIdentifier: _("Identifier directly after number."),
|
240 | NumericSeparatorInEscapeSequence: _("Numeric separators are not allowed inside unicode escape sequences or hex escape sequences."),
|
241 | ObsoleteAwaitStar: _("'await*' has been removed from the async functions proposal. Use Promise.all() instead."),
|
242 | OptionalChainingNoNew: _("Constructors in/after an Optional Chain are not allowed."),
|
243 | OptionalChainingNoTemplate: _("Tagged Template Literals are not allowed in optionalChain."),
|
244 | OverrideOnConstructor: _("'override' modifier cannot appear on a constructor declaration."),
|
245 | ParamDupe: _("Argument name clash."),
|
246 | PatternHasAccessor: _("Object pattern can't contain getter or setter."),
|
247 | PatternHasMethod: _("Object pattern can't contain methods."),
|
248 | PrivateInExpectedIn: _(({
|
249 | identifierName
|
250 | }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`),
|
251 | PrivateNameRedeclaration: _(({
|
252 | identifierName
|
253 | }) => `Duplicate private name #${identifierName}.`),
|
254 | RecordExpressionBarIncorrectEndSyntaxType: _("Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
|
255 | RecordExpressionBarIncorrectStartSyntaxType: _("Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
|
256 | RecordExpressionHashIncorrectStartSyntaxType: _("Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'."),
|
257 | RecordNoProto: _("'__proto__' is not allowed in Record expressions."),
|
258 | RestTrailingComma: _("Unexpected trailing comma after rest element."),
|
259 | SloppyFunction: _("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement."),
|
260 | StaticPrototype: _("Classes may not have static property named prototype."),
|
261 | SuperNotAllowed: _("`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?"),
|
262 | SuperPrivateField: _("Private fields can't be accessed on super."),
|
263 | TrailingDecorator: _("Decorators must be attached to a class element."),
|
264 | TupleExpressionBarIncorrectEndSyntaxType: _("Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
|
265 | TupleExpressionBarIncorrectStartSyntaxType: _("Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
|
266 | TupleExpressionHashIncorrectStartSyntaxType: _("Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'."),
|
267 | UnexpectedArgumentPlaceholder: _("Unexpected argument placeholder."),
|
268 | UnexpectedAwaitAfterPipelineBody: _('Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.'),
|
269 | UnexpectedDigitAfterHash: _("Unexpected digit after hash token."),
|
270 | UnexpectedImportExport: _("'import' and 'export' may only appear at the top level."),
|
271 | UnexpectedKeyword: _(({
|
272 | keyword
|
273 | }) => `Unexpected keyword '${keyword}'.`),
|
274 | UnexpectedLeadingDecorator: _("Leading decorators must be attached to a class declaration."),
|
275 | UnexpectedLexicalDeclaration: _("Lexical declaration cannot appear in a single-statement context."),
|
276 | UnexpectedNewTarget: _("`new.target` can only be used in functions or class properties."),
|
277 | UnexpectedNumericSeparator: _("A numeric separator is only allowed between two digits."),
|
278 | UnexpectedPrivateField: _("Unexpected private name."),
|
279 | UnexpectedReservedWord: _(({
|
280 | reservedWord
|
281 | }) => `Unexpected reserved word '${reservedWord}'.`),
|
282 | UnexpectedSuper: _("'super' is only allowed in object methods and classes."),
|
283 | UnexpectedToken: _(({
|
284 | expected,
|
285 | unexpected
|
286 | }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`),
|
287 | UnexpectedTokenUnaryExponentiation: _("Illegal expression. Wrap left hand side or entire exponentiation in parentheses."),
|
288 | UnsupportedBind: _("Binding should be performed on object property."),
|
289 | UnsupportedDecoratorExport: _("A decorated export must export a class declaration."),
|
290 | UnsupportedDefaultExport: _("Only expressions, functions or classes are allowed as the `default` export."),
|
291 | UnsupportedImport: _("`import` can only be used in `import()` or `import.meta`."),
|
292 | UnsupportedMetaProperty: _(({
|
293 | target,
|
294 | onlyValidPropertyName
|
295 | }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`),
|
296 | UnsupportedParameterDecorator: _("Decorators cannot be used to decorate parameters."),
|
297 | UnsupportedPropertyDecorator: _("Decorators cannot be used to decorate object literal properties."),
|
298 | UnsupportedSuper: _("'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop])."),
|
299 | UnterminatedComment: _("Unterminated comment."),
|
300 | UnterminatedRegExp: _("Unterminated regular expression."),
|
301 | UnterminatedString: _("Unterminated string constant."),
|
302 | UnterminatedTemplate: _("Unterminated template."),
|
303 | VarRedeclaration: _(({
|
304 | identifierName
|
305 | }) => `Identifier '${identifierName}' has already been declared.`),
|
306 | YieldBindingIdentifier: _("Can not use 'yield' as identifier inside a generator."),
|
307 | YieldInParameter: _("Yield expression is not allowed in formal parameters."),
|
308 | ZeroDigitNumericSeparator: _("Numeric separator can not be used after leading 0.")
|
309 | }));
|
310 |
|
311 | var StrictModeErrors = (_ => ({
|
312 | StrictDelete: _("Deleting local variable in strict mode."),
|
313 | StrictEvalArguments: _(({
|
314 | referenceName
|
315 | }) => `Assigning to '${referenceName}' in strict mode.`),
|
316 | StrictEvalArgumentsBinding: _(({
|
317 | bindingName
|
318 | }) => `Binding '${bindingName}' in strict mode.`),
|
319 | StrictFunction: _("In strict mode code, functions can only be declared at top level or inside a block."),
|
320 | StrictNumericEscape: _("The only valid numeric escape in strict mode is '\\0'."),
|
321 | StrictOctalLiteral: _("Legacy octal literals are not allowed in strict mode."),
|
322 | StrictWith: _("'with' in strict mode.")
|
323 | }));
|
324 |
|
325 | const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
|
326 | var PipelineOperatorErrors = (_ => ({
|
327 | PipeBodyIsTighter: _("Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence."),
|
328 | PipeTopicRequiresHackPipes: _('Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'),
|
329 | PipeTopicUnbound: _("Topic reference is unbound; it must be inside a pipe body."),
|
330 | PipeTopicUnconfiguredToken: _(({
|
331 | token
|
332 | }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`),
|
333 | PipeTopicUnused: _("Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once."),
|
334 | PipeUnparenthesizedBody: _(({
|
335 | type
|
336 | }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
|
337 | type
|
338 | })}; please wrap it in parentheses.`),
|
339 | PipelineBodyNoArrow: _('Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.'),
|
340 | PipelineBodySequenceExpression: _("Pipeline body may not be a comma-separated sequence expression."),
|
341 | PipelineHeadSequenceExpression: _("Pipeline head should not be a comma-separated sequence expression."),
|
342 | PipelineTopicUnused: _("Pipeline is in topic style but does not use topic reference."),
|
343 | PrimaryTopicNotAllowed: _("Topic reference was used in a lexical context without topic binding."),
|
344 | PrimaryTopicRequiresSmartPipeline: _('Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.')
|
345 | }));
|
346 |
|
347 | const _excluded$1 = ["toMessage"];
|
348 |
|
349 | function toParseErrorConstructor(_ref) {
|
350 | let {
|
351 | toMessage
|
352 | } = _ref,
|
353 | properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
354 |
|
355 | return function constructor({
|
356 | loc,
|
357 | details
|
358 | }) {
|
359 | return instantiate(SyntaxError, Object.assign({}, properties, {
|
360 | loc
|
361 | }), {
|
362 | clone(overrides = {}) {
|
363 | const loc = overrides.loc || {};
|
364 | return constructor({
|
365 | loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
|
366 | details: Object.assign({}, this.details, overrides.details)
|
367 | });
|
368 | },
|
369 |
|
370 | details: {
|
371 | value: details,
|
372 | enumerable: false
|
373 | },
|
374 | message: {
|
375 | get() {
|
376 | return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
|
377 | },
|
378 |
|
379 | set(value) {
|
380 | Object.defineProperty(this, "message", {
|
381 | value
|
382 | });
|
383 | }
|
384 |
|
385 | },
|
386 | pos: {
|
387 | reflect: "loc.index",
|
388 | enumerable: true
|
389 | },
|
390 | missingPlugin: "missingPlugin" in details && {
|
391 | reflect: "details.missingPlugin",
|
392 | enumerable: true
|
393 | }
|
394 | });
|
395 | };
|
396 | }
|
397 |
|
398 | function toParseErrorCredentials(toMessageOrMessage, credentials) {
|
399 | return Object.assign({
|
400 | toMessage: typeof toMessageOrMessage === "string" ? () => toMessageOrMessage : toMessageOrMessage
|
401 | }, credentials);
|
402 | }
|
403 | function ParseErrorEnum(argument, syntaxPlugin) {
|
404 | if (Array.isArray(argument)) {
|
405 | return toParseErrorCredentialsMap => ParseErrorEnum(toParseErrorCredentialsMap, argument[0]);
|
406 | }
|
407 |
|
408 | const partialCredentials = argument(toParseErrorCredentials);
|
409 | const ParseErrorConstructors = {};
|
410 |
|
411 | for (const reasonCode of Object.keys(partialCredentials)) {
|
412 | ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
|
413 | code: ParseErrorCodes.SyntaxError,
|
414 | reasonCode
|
415 | }, syntaxPlugin ? {
|
416 | syntaxPlugin
|
417 | } : {}, partialCredentials[reasonCode]));
|
418 | }
|
419 |
|
420 | return ParseErrorConstructors;
|
421 | }
|
422 | const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
|
423 |
|
424 | const {
|
425 | defineProperty
|
426 | } = Object;
|
427 |
|
428 | const toUnenumerable = (object, key) => defineProperty(object, key, {
|
429 | enumerable: false,
|
430 | value: object[key]
|
431 | });
|
432 |
|
433 | function toESTreeLocation(node) {
|
434 | node.loc.start && toUnenumerable(node.loc.start, "index");
|
435 | node.loc.end && toUnenumerable(node.loc.end, "index");
|
436 | return node;
|
437 | }
|
438 |
|
439 | var estree = (superClass => class extends superClass {
|
440 | parse() {
|
441 | const file = toESTreeLocation(super.parse());
|
442 |
|
443 | if (this.options.tokens) {
|
444 | file.tokens = file.tokens.map(toESTreeLocation);
|
445 | }
|
446 |
|
447 | return file;
|
448 | }
|
449 |
|
450 | parseRegExpLiteral({
|
451 | pattern,
|
452 | flags
|
453 | }) {
|
454 | let regex = null;
|
455 |
|
456 | try {
|
457 | regex = new RegExp(pattern, flags);
|
458 | } catch (e) {}
|
459 |
|
460 | const node = this.estreeParseLiteral(regex);
|
461 | node.regex = {
|
462 | pattern,
|
463 | flags
|
464 | };
|
465 | return node;
|
466 | }
|
467 |
|
468 | parseBigIntLiteral(value) {
|
469 | let bigInt;
|
470 |
|
471 | try {
|
472 | bigInt = BigInt(value);
|
473 | } catch (_unused) {
|
474 | bigInt = null;
|
475 | }
|
476 |
|
477 | const node = this.estreeParseLiteral(bigInt);
|
478 | node.bigint = String(node.value || value);
|
479 | return node;
|
480 | }
|
481 |
|
482 | parseDecimalLiteral(value) {
|
483 | const decimal = null;
|
484 | const node = this.estreeParseLiteral(decimal);
|
485 | node.decimal = String(node.value || value);
|
486 | return node;
|
487 | }
|
488 |
|
489 | estreeParseLiteral(value) {
|
490 | return this.parseLiteral(value, "Literal");
|
491 | }
|
492 |
|
493 | parseStringLiteral(value) {
|
494 | return this.estreeParseLiteral(value);
|
495 | }
|
496 |
|
497 | parseNumericLiteral(value) {
|
498 | return this.estreeParseLiteral(value);
|
499 | }
|
500 |
|
501 | parseNullLiteral() {
|
502 | return this.estreeParseLiteral(null);
|
503 | }
|
504 |
|
505 | parseBooleanLiteral(value) {
|
506 | return this.estreeParseLiteral(value);
|
507 | }
|
508 |
|
509 | directiveToStmt(directive) {
|
510 | const directiveLiteral = directive.value;
|
511 | const stmt = this.startNodeAt(directive.start, directive.loc.start);
|
512 | const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
|
513 | expression.value = directiveLiteral.extra.expressionValue;
|
514 | expression.raw = directiveLiteral.extra.raw;
|
515 | stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.loc.end);
|
516 | stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
|
517 | return this.finishNodeAt(stmt, "ExpressionStatement", directive.loc.end);
|
518 | }
|
519 |
|
520 | initFunction(node, isAsync) {
|
521 | super.initFunction(node, isAsync);
|
522 | node.expression = false;
|
523 | }
|
524 |
|
525 | checkDeclaration(node) {
|
526 | if (node != null && this.isObjectProperty(node)) {
|
527 | this.checkDeclaration(node.value);
|
528 | } else {
|
529 | super.checkDeclaration(node);
|
530 | }
|
531 | }
|
532 |
|
533 | getObjectOrClassMethodParams(method) {
|
534 | return method.value.params;
|
535 | }
|
536 |
|
537 | isValidDirective(stmt) {
|
538 | var _stmt$expression$extr;
|
539 |
|
540 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
|
541 | }
|
542 |
|
543 | parseBlockBody(node, ...args) {
|
544 | super.parseBlockBody(node, ...args);
|
545 | const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
|
546 | node.body = directiveStatements.concat(node.body);
|
547 | delete node.directives;
|
548 | }
|
549 |
|
550 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
551 | this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
|
552 |
|
553 | if (method.typeParameters) {
|
554 | method.value.typeParameters = method.typeParameters;
|
555 | delete method.typeParameters;
|
556 | }
|
557 |
|
558 | classBody.body.push(method);
|
559 | }
|
560 |
|
561 | parsePrivateName() {
|
562 | const node = super.parsePrivateName();
|
563 | {
|
564 | if (!this.getPluginOption("estree", "classFeatures")) {
|
565 | return node;
|
566 | }
|
567 | }
|
568 | return this.convertPrivateNameToPrivateIdentifier(node);
|
569 | }
|
570 |
|
571 | convertPrivateNameToPrivateIdentifier(node) {
|
572 | const name = super.getPrivateNameSV(node);
|
573 | node = node;
|
574 | delete node.id;
|
575 | node.name = name;
|
576 | node.type = "PrivateIdentifier";
|
577 | return node;
|
578 | }
|
579 |
|
580 | isPrivateName(node) {
|
581 | {
|
582 | if (!this.getPluginOption("estree", "classFeatures")) {
|
583 | return super.isPrivateName(node);
|
584 | }
|
585 | }
|
586 | return node.type === "PrivateIdentifier";
|
587 | }
|
588 |
|
589 | getPrivateNameSV(node) {
|
590 | {
|
591 | if (!this.getPluginOption("estree", "classFeatures")) {
|
592 | return super.getPrivateNameSV(node);
|
593 | }
|
594 | }
|
595 | return node.name;
|
596 | }
|
597 |
|
598 | parseLiteral(value, type) {
|
599 | const node = super.parseLiteral(value, type);
|
600 | node.raw = node.extra.raw;
|
601 | delete node.extra;
|
602 | return node;
|
603 | }
|
604 |
|
605 | parseFunctionBody(node, allowExpression, isMethod = false) {
|
606 | super.parseFunctionBody(node, allowExpression, isMethod);
|
607 | node.expression = node.body.type !== "BlockStatement";
|
608 | }
|
609 |
|
610 | parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
|
611 | let funcNode = this.startNode();
|
612 | funcNode.kind = node.kind;
|
613 | funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
|
614 | funcNode.type = "FunctionExpression";
|
615 | delete funcNode.kind;
|
616 | node.value = funcNode;
|
617 |
|
618 | if (type === "ClassPrivateMethod") {
|
619 | node.computed = false;
|
620 | }
|
621 |
|
622 | type = "MethodDefinition";
|
623 | return this.finishNode(node, type);
|
624 | }
|
625 |
|
626 | parseClassProperty(...args) {
|
627 | const propertyNode = super.parseClassProperty(...args);
|
628 | {
|
629 | if (!this.getPluginOption("estree", "classFeatures")) {
|
630 | return propertyNode;
|
631 | }
|
632 | }
|
633 | propertyNode.type = "PropertyDefinition";
|
634 | return propertyNode;
|
635 | }
|
636 |
|
637 | parseClassPrivateProperty(...args) {
|
638 | const propertyNode = super.parseClassPrivateProperty(...args);
|
639 | {
|
640 | if (!this.getPluginOption("estree", "classFeatures")) {
|
641 | return propertyNode;
|
642 | }
|
643 | }
|
644 | propertyNode.type = "PropertyDefinition";
|
645 | propertyNode.computed = false;
|
646 | return propertyNode;
|
647 | }
|
648 |
|
649 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
|
650 | const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
|
651 |
|
652 | if (node) {
|
653 | node.type = "Property";
|
654 | if (node.kind === "method") node.kind = "init";
|
655 | node.shorthand = false;
|
656 | }
|
657 |
|
658 | return node;
|
659 | }
|
660 |
|
661 | parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
|
662 | const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
|
663 |
|
664 | if (node) {
|
665 | node.kind = "init";
|
666 | node.type = "Property";
|
667 | }
|
668 |
|
669 | return node;
|
670 | }
|
671 |
|
672 | isValidLVal(type, ...rest) {
|
673 | return type === "Property" ? "value" : super.isValidLVal(type, ...rest);
|
674 | }
|
675 |
|
676 | isAssignable(node, isBinding) {
|
677 | if (node != null && this.isObjectProperty(node)) {
|
678 | return this.isAssignable(node.value, isBinding);
|
679 | }
|
680 |
|
681 | return super.isAssignable(node, isBinding);
|
682 | }
|
683 |
|
684 | toAssignable(node, isLHS = false) {
|
685 | if (node != null && this.isObjectProperty(node)) {
|
686 | const {
|
687 | key,
|
688 | value
|
689 | } = node;
|
690 |
|
691 | if (this.isPrivateName(key)) {
|
692 | this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
|
693 | }
|
694 |
|
695 | this.toAssignable(value, isLHS);
|
696 | } else {
|
697 | super.toAssignable(node, isLHS);
|
698 | }
|
699 | }
|
700 |
|
701 | toAssignableObjectExpressionProp(prop) {
|
702 | if (prop.kind === "get" || prop.kind === "set") {
|
703 | this.raise(Errors.PatternHasAccessor, {
|
704 | at: prop.key
|
705 | });
|
706 | } else if (prop.method) {
|
707 | this.raise(Errors.PatternHasMethod, {
|
708 | at: prop.key
|
709 | });
|
710 | } else {
|
711 | super.toAssignableObjectExpressionProp(...arguments);
|
712 | }
|
713 | }
|
714 |
|
715 | finishCallExpression(node, optional) {
|
716 | super.finishCallExpression(node, optional);
|
717 |
|
718 | if (node.callee.type === "Import") {
|
719 | node.type = "ImportExpression";
|
720 | node.source = node.arguments[0];
|
721 |
|
722 | if (this.hasPlugin("importAssertions")) {
|
723 | var _node$arguments$;
|
724 |
|
725 | node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
|
726 | }
|
727 |
|
728 | delete node.arguments;
|
729 | delete node.callee;
|
730 | }
|
731 |
|
732 | return node;
|
733 | }
|
734 |
|
735 | toReferencedArguments(node) {
|
736 | if (node.type === "ImportExpression") {
|
737 | return;
|
738 | }
|
739 |
|
740 | super.toReferencedArguments(node);
|
741 | }
|
742 |
|
743 | parseExport(node) {
|
744 | super.parseExport(node);
|
745 |
|
746 | switch (node.type) {
|
747 | case "ExportAllDeclaration":
|
748 | node.exported = null;
|
749 | break;
|
750 |
|
751 | case "ExportNamedDeclaration":
|
752 | if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
|
753 | node.type = "ExportAllDeclaration";
|
754 | node.exported = node.specifiers[0].exported;
|
755 | delete node.specifiers;
|
756 | }
|
757 |
|
758 | break;
|
759 | }
|
760 |
|
761 | return node;
|
762 | }
|
763 |
|
764 | parseSubscript(base, startPos, startLoc, noCalls, state) {
|
765 | const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
|
766 |
|
767 | if (state.optionalChainMember) {
|
768 | if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
|
769 | node.type = node.type.substring(8);
|
770 | }
|
771 |
|
772 | if (state.stop) {
|
773 | const chain = this.startNodeAtNode(node);
|
774 | chain.expression = node;
|
775 | return this.finishNode(chain, "ChainExpression");
|
776 | }
|
777 | } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
|
778 | node.optional = false;
|
779 | }
|
780 |
|
781 | return node;
|
782 | }
|
783 |
|
784 | hasPropertyAsPrivateName(node) {
|
785 | if (node.type === "ChainExpression") {
|
786 | node = node.expression;
|
787 | }
|
788 |
|
789 | return super.hasPropertyAsPrivateName(node);
|
790 | }
|
791 |
|
792 | isOptionalChain(node) {
|
793 | return node.type === "ChainExpression";
|
794 | }
|
795 |
|
796 | isObjectProperty(node) {
|
797 | return node.type === "Property" && node.kind === "init" && !node.method;
|
798 | }
|
799 |
|
800 | isObjectMethod(node) {
|
801 | return node.method || node.kind === "get" || node.kind === "set";
|
802 | }
|
803 |
|
804 | finishNodeAt(node, type, endLoc) {
|
805 | return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
|
806 | }
|
807 |
|
808 | resetStartLocation(node, start, startLoc) {
|
809 | super.resetStartLocation(node, start, startLoc);
|
810 | toESTreeLocation(node);
|
811 | }
|
812 |
|
813 | resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
|
814 | super.resetEndLocation(node, endLoc);
|
815 | toESTreeLocation(node);
|
816 | }
|
817 |
|
818 | });
|
819 |
|
820 | class TokContext {
|
821 | constructor(token, preserveSpace) {
|
822 | this.token = void 0;
|
823 | this.preserveSpace = void 0;
|
824 | this.token = token;
|
825 | this.preserveSpace = !!preserveSpace;
|
826 | }
|
827 |
|
828 | }
|
829 | const types = {
|
830 | brace: new TokContext("{"),
|
831 | j_oTag: new TokContext("<tag"),
|
832 | j_cTag: new TokContext("</tag"),
|
833 | j_expr: new TokContext("<tag>...</tag>", true)
|
834 | };
|
835 | {
|
836 | types.template = new TokContext("`", true);
|
837 | }
|
838 |
|
839 | const beforeExpr = true;
|
840 | const startsExpr = true;
|
841 | const isLoop = true;
|
842 | const isAssign = true;
|
843 | const prefix = true;
|
844 | const postfix = true;
|
845 | class ExportedTokenType {
|
846 | constructor(label, conf = {}) {
|
847 | this.label = void 0;
|
848 | this.keyword = void 0;
|
849 | this.beforeExpr = void 0;
|
850 | this.startsExpr = void 0;
|
851 | this.rightAssociative = void 0;
|
852 | this.isLoop = void 0;
|
853 | this.isAssign = void 0;
|
854 | this.prefix = void 0;
|
855 | this.postfix = void 0;
|
856 | this.binop = void 0;
|
857 | this.label = label;
|
858 | this.keyword = conf.keyword;
|
859 | this.beforeExpr = !!conf.beforeExpr;
|
860 | this.startsExpr = !!conf.startsExpr;
|
861 | this.rightAssociative = !!conf.rightAssociative;
|
862 | this.isLoop = !!conf.isLoop;
|
863 | this.isAssign = !!conf.isAssign;
|
864 | this.prefix = !!conf.prefix;
|
865 | this.postfix = !!conf.postfix;
|
866 | this.binop = conf.binop != null ? conf.binop : null;
|
867 | {
|
868 | this.updateContext = null;
|
869 | }
|
870 | }
|
871 |
|
872 | }
|
873 | const keywords$1 = new Map();
|
874 |
|
875 | function createKeyword(name, options = {}) {
|
876 | options.keyword = name;
|
877 | const token = createToken(name, options);
|
878 | keywords$1.set(name, token);
|
879 | return token;
|
880 | }
|
881 |
|
882 | function createBinop(name, binop) {
|
883 | return createToken(name, {
|
884 | beforeExpr,
|
885 | binop
|
886 | });
|
887 | }
|
888 |
|
889 | let tokenTypeCounter = -1;
|
890 | const tokenTypes = [];
|
891 | const tokenLabels = [];
|
892 | const tokenBinops = [];
|
893 | const tokenBeforeExprs = [];
|
894 | const tokenStartsExprs = [];
|
895 | const tokenPrefixes = [];
|
896 |
|
897 | function createToken(name, options = {}) {
|
898 | var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
|
899 |
|
900 | ++tokenTypeCounter;
|
901 | tokenLabels.push(name);
|
902 | tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
|
903 | tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
|
904 | tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
|
905 | tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
|
906 | tokenTypes.push(new ExportedTokenType(name, options));
|
907 | return tokenTypeCounter;
|
908 | }
|
909 |
|
910 | function createKeywordLike(name, options = {}) {
|
911 | var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
|
912 |
|
913 | ++tokenTypeCounter;
|
914 | keywords$1.set(name, tokenTypeCounter);
|
915 | tokenLabels.push(name);
|
916 | tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
|
917 | tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
|
918 | tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
|
919 | tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
|
920 | tokenTypes.push(new ExportedTokenType("name", options));
|
921 | return tokenTypeCounter;
|
922 | }
|
923 |
|
924 | const tt = {
|
925 | bracketL: createToken("[", {
|
926 | beforeExpr,
|
927 | startsExpr
|
928 | }),
|
929 | bracketHashL: createToken("#[", {
|
930 | beforeExpr,
|
931 | startsExpr
|
932 | }),
|
933 | bracketBarL: createToken("[|", {
|
934 | beforeExpr,
|
935 | startsExpr
|
936 | }),
|
937 | bracketR: createToken("]"),
|
938 | bracketBarR: createToken("|]"),
|
939 | braceL: createToken("{", {
|
940 | beforeExpr,
|
941 | startsExpr
|
942 | }),
|
943 | braceBarL: createToken("{|", {
|
944 | beforeExpr,
|
945 | startsExpr
|
946 | }),
|
947 | braceHashL: createToken("#{", {
|
948 | beforeExpr,
|
949 | startsExpr
|
950 | }),
|
951 | braceR: createToken("}"),
|
952 | braceBarR: createToken("|}"),
|
953 | parenL: createToken("(", {
|
954 | beforeExpr,
|
955 | startsExpr
|
956 | }),
|
957 | parenR: createToken(")"),
|
958 | comma: createToken(",", {
|
959 | beforeExpr
|
960 | }),
|
961 | semi: createToken(";", {
|
962 | beforeExpr
|
963 | }),
|
964 | colon: createToken(":", {
|
965 | beforeExpr
|
966 | }),
|
967 | doubleColon: createToken("::", {
|
968 | beforeExpr
|
969 | }),
|
970 | dot: createToken("."),
|
971 | question: createToken("?", {
|
972 | beforeExpr
|
973 | }),
|
974 | questionDot: createToken("?."),
|
975 | arrow: createToken("=>", {
|
976 | beforeExpr
|
977 | }),
|
978 | template: createToken("template"),
|
979 | ellipsis: createToken("...", {
|
980 | beforeExpr
|
981 | }),
|
982 | backQuote: createToken("`", {
|
983 | startsExpr
|
984 | }),
|
985 | dollarBraceL: createToken("${", {
|
986 | beforeExpr,
|
987 | startsExpr
|
988 | }),
|
989 | templateTail: createToken("...`", {
|
990 | startsExpr
|
991 | }),
|
992 | templateNonTail: createToken("...${", {
|
993 | beforeExpr,
|
994 | startsExpr
|
995 | }),
|
996 | at: createToken("@"),
|
997 | hash: createToken("#", {
|
998 | startsExpr
|
999 | }),
|
1000 | interpreterDirective: createToken("#!..."),
|
1001 | eq: createToken("=", {
|
1002 | beforeExpr,
|
1003 | isAssign
|
1004 | }),
|
1005 | assign: createToken("_=", {
|
1006 | beforeExpr,
|
1007 | isAssign
|
1008 | }),
|
1009 | slashAssign: createToken("_=", {
|
1010 | beforeExpr,
|
1011 | isAssign
|
1012 | }),
|
1013 | xorAssign: createToken("_=", {
|
1014 | beforeExpr,
|
1015 | isAssign
|
1016 | }),
|
1017 | moduloAssign: createToken("_=", {
|
1018 | beforeExpr,
|
1019 | isAssign
|
1020 | }),
|
1021 | incDec: createToken("++/--", {
|
1022 | prefix,
|
1023 | postfix,
|
1024 | startsExpr
|
1025 | }),
|
1026 | bang: createToken("!", {
|
1027 | beforeExpr,
|
1028 | prefix,
|
1029 | startsExpr
|
1030 | }),
|
1031 | tilde: createToken("~", {
|
1032 | beforeExpr,
|
1033 | prefix,
|
1034 | startsExpr
|
1035 | }),
|
1036 | doubleCaret: createToken("^^", {
|
1037 | startsExpr
|
1038 | }),
|
1039 | doubleAt: createToken("@@", {
|
1040 | startsExpr
|
1041 | }),
|
1042 | pipeline: createBinop("|>", 0),
|
1043 | nullishCoalescing: createBinop("??", 1),
|
1044 | logicalOR: createBinop("||", 1),
|
1045 | logicalAND: createBinop("&&", 2),
|
1046 | bitwiseOR: createBinop("|", 3),
|
1047 | bitwiseXOR: createBinop("^", 4),
|
1048 | bitwiseAND: createBinop("&", 5),
|
1049 | equality: createBinop("==/!=/===/!==", 6),
|
1050 | lt: createBinop("</>/<=/>=", 7),
|
1051 | gt: createBinop("</>/<=/>=", 7),
|
1052 | relational: createBinop("</>/<=/>=", 7),
|
1053 | bitShift: createBinop("<</>>/>>>", 8),
|
1054 | bitShiftL: createBinop("<</>>/>>>", 8),
|
1055 | bitShiftR: createBinop("<</>>/>>>", 8),
|
1056 | plusMin: createToken("+/-", {
|
1057 | beforeExpr,
|
1058 | binop: 9,
|
1059 | prefix,
|
1060 | startsExpr
|
1061 | }),
|
1062 | modulo: createToken("%", {
|
1063 | binop: 10,
|
1064 | startsExpr
|
1065 | }),
|
1066 | star: createToken("*", {
|
1067 | binop: 10
|
1068 | }),
|
1069 | slash: createBinop("/", 10),
|
1070 | exponent: createToken("**", {
|
1071 | beforeExpr,
|
1072 | binop: 11,
|
1073 | rightAssociative: true
|
1074 | }),
|
1075 | _in: createKeyword("in", {
|
1076 | beforeExpr,
|
1077 | binop: 7
|
1078 | }),
|
1079 | _instanceof: createKeyword("instanceof", {
|
1080 | beforeExpr,
|
1081 | binop: 7
|
1082 | }),
|
1083 | _break: createKeyword("break"),
|
1084 | _case: createKeyword("case", {
|
1085 | beforeExpr
|
1086 | }),
|
1087 | _catch: createKeyword("catch"),
|
1088 | _continue: createKeyword("continue"),
|
1089 | _debugger: createKeyword("debugger"),
|
1090 | _default: createKeyword("default", {
|
1091 | beforeExpr
|
1092 | }),
|
1093 | _else: createKeyword("else", {
|
1094 | beforeExpr
|
1095 | }),
|
1096 | _finally: createKeyword("finally"),
|
1097 | _function: createKeyword("function", {
|
1098 | startsExpr
|
1099 | }),
|
1100 | _if: createKeyword("if"),
|
1101 | _return: createKeyword("return", {
|
1102 | beforeExpr
|
1103 | }),
|
1104 | _switch: createKeyword("switch"),
|
1105 | _throw: createKeyword("throw", {
|
1106 | beforeExpr,
|
1107 | prefix,
|
1108 | startsExpr
|
1109 | }),
|
1110 | _try: createKeyword("try"),
|
1111 | _var: createKeyword("var"),
|
1112 | _const: createKeyword("const"),
|
1113 | _with: createKeyword("with"),
|
1114 | _new: createKeyword("new", {
|
1115 | beforeExpr,
|
1116 | startsExpr
|
1117 | }),
|
1118 | _this: createKeyword("this", {
|
1119 | startsExpr
|
1120 | }),
|
1121 | _super: createKeyword("super", {
|
1122 | startsExpr
|
1123 | }),
|
1124 | _class: createKeyword("class", {
|
1125 | startsExpr
|
1126 | }),
|
1127 | _extends: createKeyword("extends", {
|
1128 | beforeExpr
|
1129 | }),
|
1130 | _export: createKeyword("export"),
|
1131 | _import: createKeyword("import", {
|
1132 | startsExpr
|
1133 | }),
|
1134 | _null: createKeyword("null", {
|
1135 | startsExpr
|
1136 | }),
|
1137 | _true: createKeyword("true", {
|
1138 | startsExpr
|
1139 | }),
|
1140 | _false: createKeyword("false", {
|
1141 | startsExpr
|
1142 | }),
|
1143 | _typeof: createKeyword("typeof", {
|
1144 | beforeExpr,
|
1145 | prefix,
|
1146 | startsExpr
|
1147 | }),
|
1148 | _void: createKeyword("void", {
|
1149 | beforeExpr,
|
1150 | prefix,
|
1151 | startsExpr
|
1152 | }),
|
1153 | _delete: createKeyword("delete", {
|
1154 | beforeExpr,
|
1155 | prefix,
|
1156 | startsExpr
|
1157 | }),
|
1158 | _do: createKeyword("do", {
|
1159 | isLoop,
|
1160 | beforeExpr
|
1161 | }),
|
1162 | _for: createKeyword("for", {
|
1163 | isLoop
|
1164 | }),
|
1165 | _while: createKeyword("while", {
|
1166 | isLoop
|
1167 | }),
|
1168 | _as: createKeywordLike("as", {
|
1169 | startsExpr
|
1170 | }),
|
1171 | _assert: createKeywordLike("assert", {
|
1172 | startsExpr
|
1173 | }),
|
1174 | _async: createKeywordLike("async", {
|
1175 | startsExpr
|
1176 | }),
|
1177 | _await: createKeywordLike("await", {
|
1178 | startsExpr
|
1179 | }),
|
1180 | _from: createKeywordLike("from", {
|
1181 | startsExpr
|
1182 | }),
|
1183 | _get: createKeywordLike("get", {
|
1184 | startsExpr
|
1185 | }),
|
1186 | _let: createKeywordLike("let", {
|
1187 | startsExpr
|
1188 | }),
|
1189 | _meta: createKeywordLike("meta", {
|
1190 | startsExpr
|
1191 | }),
|
1192 | _of: createKeywordLike("of", {
|
1193 | startsExpr
|
1194 | }),
|
1195 | _sent: createKeywordLike("sent", {
|
1196 | startsExpr
|
1197 | }),
|
1198 | _set: createKeywordLike("set", {
|
1199 | startsExpr
|
1200 | }),
|
1201 | _static: createKeywordLike("static", {
|
1202 | startsExpr
|
1203 | }),
|
1204 | _yield: createKeywordLike("yield", {
|
1205 | startsExpr
|
1206 | }),
|
1207 | _asserts: createKeywordLike("asserts", {
|
1208 | startsExpr
|
1209 | }),
|
1210 | _checks: createKeywordLike("checks", {
|
1211 | startsExpr
|
1212 | }),
|
1213 | _exports: createKeywordLike("exports", {
|
1214 | startsExpr
|
1215 | }),
|
1216 | _global: createKeywordLike("global", {
|
1217 | startsExpr
|
1218 | }),
|
1219 | _implements: createKeywordLike("implements", {
|
1220 | startsExpr
|
1221 | }),
|
1222 | _intrinsic: createKeywordLike("intrinsic", {
|
1223 | startsExpr
|
1224 | }),
|
1225 | _infer: createKeywordLike("infer", {
|
1226 | startsExpr
|
1227 | }),
|
1228 | _is: createKeywordLike("is", {
|
1229 | startsExpr
|
1230 | }),
|
1231 | _mixins: createKeywordLike("mixins", {
|
1232 | startsExpr
|
1233 | }),
|
1234 | _proto: createKeywordLike("proto", {
|
1235 | startsExpr
|
1236 | }),
|
1237 | _require: createKeywordLike("require", {
|
1238 | startsExpr
|
1239 | }),
|
1240 | _keyof: createKeywordLike("keyof", {
|
1241 | startsExpr
|
1242 | }),
|
1243 | _readonly: createKeywordLike("readonly", {
|
1244 | startsExpr
|
1245 | }),
|
1246 | _unique: createKeywordLike("unique", {
|
1247 | startsExpr
|
1248 | }),
|
1249 | _abstract: createKeywordLike("abstract", {
|
1250 | startsExpr
|
1251 | }),
|
1252 | _declare: createKeywordLike("declare", {
|
1253 | startsExpr
|
1254 | }),
|
1255 | _enum: createKeywordLike("enum", {
|
1256 | startsExpr
|
1257 | }),
|
1258 | _module: createKeywordLike("module", {
|
1259 | startsExpr
|
1260 | }),
|
1261 | _namespace: createKeywordLike("namespace", {
|
1262 | startsExpr
|
1263 | }),
|
1264 | _interface: createKeywordLike("interface", {
|
1265 | startsExpr
|
1266 | }),
|
1267 | _type: createKeywordLike("type", {
|
1268 | startsExpr
|
1269 | }),
|
1270 | _opaque: createKeywordLike("opaque", {
|
1271 | startsExpr
|
1272 | }),
|
1273 | name: createToken("name", {
|
1274 | startsExpr
|
1275 | }),
|
1276 | string: createToken("string", {
|
1277 | startsExpr
|
1278 | }),
|
1279 | num: createToken("num", {
|
1280 | startsExpr
|
1281 | }),
|
1282 | bigint: createToken("bigint", {
|
1283 | startsExpr
|
1284 | }),
|
1285 | decimal: createToken("decimal", {
|
1286 | startsExpr
|
1287 | }),
|
1288 | regexp: createToken("regexp", {
|
1289 | startsExpr
|
1290 | }),
|
1291 | privateName: createToken("#name", {
|
1292 | startsExpr
|
1293 | }),
|
1294 | eof: createToken("eof"),
|
1295 | jsxName: createToken("jsxName"),
|
1296 | jsxText: createToken("jsxText", {
|
1297 | beforeExpr: true
|
1298 | }),
|
1299 | jsxTagStart: createToken("jsxTagStart", {
|
1300 | startsExpr: true
|
1301 | }),
|
1302 | jsxTagEnd: createToken("jsxTagEnd"),
|
1303 | placeholder: createToken("%%", {
|
1304 | startsExpr: true
|
1305 | })
|
1306 | };
|
1307 | function tokenIsIdentifier(token) {
|
1308 | return token >= 93 && token <= 128;
|
1309 | }
|
1310 | function tokenKeywordOrIdentifierIsKeyword(token) {
|
1311 | return token <= 92;
|
1312 | }
|
1313 | function tokenIsKeywordOrIdentifier(token) {
|
1314 | return token >= 58 && token <= 128;
|
1315 | }
|
1316 | function tokenIsLiteralPropertyName(token) {
|
1317 | return token >= 58 && token <= 132;
|
1318 | }
|
1319 | function tokenComesBeforeExpression(token) {
|
1320 | return tokenBeforeExprs[token];
|
1321 | }
|
1322 | function tokenCanStartExpression(token) {
|
1323 | return tokenStartsExprs[token];
|
1324 | }
|
1325 | function tokenIsAssignment(token) {
|
1326 | return token >= 29 && token <= 33;
|
1327 | }
|
1328 | function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
|
1329 | return token >= 125 && token <= 127;
|
1330 | }
|
1331 | function tokenIsLoop(token) {
|
1332 | return token >= 90 && token <= 92;
|
1333 | }
|
1334 | function tokenIsKeyword(token) {
|
1335 | return token >= 58 && token <= 92;
|
1336 | }
|
1337 | function tokenIsOperator(token) {
|
1338 | return token >= 39 && token <= 59;
|
1339 | }
|
1340 | function tokenIsPostfix(token) {
|
1341 | return token === 34;
|
1342 | }
|
1343 | function tokenIsPrefix(token) {
|
1344 | return tokenPrefixes[token];
|
1345 | }
|
1346 | function tokenIsTSTypeOperator(token) {
|
1347 | return token >= 117 && token <= 119;
|
1348 | }
|
1349 | function tokenIsTSDeclarationStart(token) {
|
1350 | return token >= 120 && token <= 126;
|
1351 | }
|
1352 | function tokenLabelName(token) {
|
1353 | return tokenLabels[token];
|
1354 | }
|
1355 | function tokenOperatorPrecedence(token) {
|
1356 | return tokenBinops[token];
|
1357 | }
|
1358 | function tokenIsRightAssociative(token) {
|
1359 | return token === 57;
|
1360 | }
|
1361 | function tokenIsTemplate(token) {
|
1362 | return token >= 24 && token <= 25;
|
1363 | }
|
1364 | function getExportedToken(token) {
|
1365 | return tokenTypes[token];
|
1366 | }
|
1367 | {
|
1368 | tokenTypes[8].updateContext = context => {
|
1369 | context.pop();
|
1370 | };
|
1371 |
|
1372 | tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
|
1373 | context.push(types.brace);
|
1374 | };
|
1375 |
|
1376 | tokenTypes[22].updateContext = context => {
|
1377 | if (context[context.length - 1] === types.template) {
|
1378 | context.pop();
|
1379 | } else {
|
1380 | context.push(types.template);
|
1381 | }
|
1382 | };
|
1383 |
|
1384 | tokenTypes[138].updateContext = context => {
|
1385 | context.push(types.j_expr, types.j_oTag);
|
1386 | };
|
1387 | }
|
1388 |
|
1389 | let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
|
1390 | let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
|
1391 | const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
1392 | const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
1393 | nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
|
1394 | const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 190, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1070, 4050, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 46, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 482, 44, 11, 6, 17, 0, 322, 29, 19, 43, 1269, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4152, 8, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
|
1395 | const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 357, 0, 62, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
|
1396 |
|
1397 | function isInAstralSet(code, set) {
|
1398 | let pos = 0x10000;
|
1399 |
|
1400 | for (let i = 0, length = set.length; i < length; i += 2) {
|
1401 | pos += set[i];
|
1402 | if (pos > code) return false;
|
1403 | pos += set[i + 1];
|
1404 | if (pos >= code) return true;
|
1405 | }
|
1406 |
|
1407 | return false;
|
1408 | }
|
1409 |
|
1410 | function isIdentifierStart(code) {
|
1411 | if (code < 65) return code === 36;
|
1412 | if (code <= 90) return true;
|
1413 | if (code < 97) return code === 95;
|
1414 | if (code <= 122) return true;
|
1415 |
|
1416 | if (code <= 0xffff) {
|
1417 | return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
|
1418 | }
|
1419 |
|
1420 | return isInAstralSet(code, astralIdentifierStartCodes);
|
1421 | }
|
1422 | function isIdentifierChar(code) {
|
1423 | if (code < 48) return code === 36;
|
1424 | if (code < 58) return true;
|
1425 | if (code < 65) return false;
|
1426 | if (code <= 90) return true;
|
1427 | if (code < 97) return code === 95;
|
1428 | if (code <= 122) return true;
|
1429 |
|
1430 | if (code <= 0xffff) {
|
1431 | return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
|
1432 | }
|
1433 |
|
1434 | return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
|
1435 | }
|
1436 |
|
1437 | const reservedWords = {
|
1438 | keyword: ["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete"],
|
1439 | strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
|
1440 | strictBind: ["eval", "arguments"]
|
1441 | };
|
1442 | const keywords = new Set(reservedWords.keyword);
|
1443 | const reservedWordsStrictSet = new Set(reservedWords.strict);
|
1444 | const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
|
1445 | function isReservedWord(word, inModule) {
|
1446 | return inModule && word === "await" || word === "enum";
|
1447 | }
|
1448 | function isStrictReservedWord(word, inModule) {
|
1449 | return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
|
1450 | }
|
1451 | function isStrictBindOnlyReservedWord(word) {
|
1452 | return reservedWordsStrictBindSet.has(word);
|
1453 | }
|
1454 | function isStrictBindReservedWord(word, inModule) {
|
1455 | return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
|
1456 | }
|
1457 | function isKeyword(word) {
|
1458 | return keywords.has(word);
|
1459 | }
|
1460 |
|
1461 | function isIteratorStart(current, next, next2) {
|
1462 | return current === 64 && next === 64 && isIdentifierStart(next2);
|
1463 | }
|
1464 | const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
|
1465 | function canBeReservedWord(word) {
|
1466 | return reservedWordLikeSet.has(word);
|
1467 | }
|
1468 |
|
1469 | const SCOPE_OTHER = 0b000000000,
|
1470 | SCOPE_PROGRAM = 0b000000001,
|
1471 | SCOPE_FUNCTION = 0b000000010,
|
1472 | SCOPE_ARROW = 0b000000100,
|
1473 | SCOPE_SIMPLE_CATCH = 0b000001000,
|
1474 | SCOPE_SUPER = 0b000010000,
|
1475 | SCOPE_DIRECT_SUPER = 0b000100000,
|
1476 | SCOPE_CLASS = 0b001000000,
|
1477 | SCOPE_STATIC_BLOCK = 0b010000000,
|
1478 | SCOPE_TS_MODULE = 0b100000000,
|
1479 | SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
|
1480 | const BIND_KIND_VALUE = 0b000000000001,
|
1481 | BIND_KIND_TYPE = 0b000000000010,
|
1482 | BIND_SCOPE_VAR = 0b000000000100,
|
1483 | BIND_SCOPE_LEXICAL = 0b000000001000,
|
1484 | BIND_SCOPE_FUNCTION = 0b000000010000,
|
1485 | BIND_FLAGS_NONE = 0b000001000000,
|
1486 | BIND_FLAGS_CLASS = 0b000010000000,
|
1487 | BIND_FLAGS_TS_ENUM = 0b000100000000,
|
1488 | BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
|
1489 | BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
|
1490 | BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
|
1491 | const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
|
1492 | BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
|
1493 | BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
|
1494 | BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
|
1495 | BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
|
1496 | BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
|
1497 | BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
|
1498 | BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
|
1499 | BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
|
1500 | BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
|
1501 | BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
|
1502 | BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
|
1503 | BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
|
1504 | const CLASS_ELEMENT_FLAG_STATIC = 0b100,
|
1505 | CLASS_ELEMENT_KIND_GETTER = 0b010,
|
1506 | CLASS_ELEMENT_KIND_SETTER = 0b001,
|
1507 | CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
|
1508 | const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
|
1509 | CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
|
1510 | CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
|
1511 | CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
|
1512 | CLASS_ELEMENT_OTHER = 0;
|
1513 |
|
1514 | class BaseParser {
|
1515 | constructor() {
|
1516 | this.sawUnambiguousESM = false;
|
1517 | this.ambiguousScriptDifferentAst = false;
|
1518 | }
|
1519 |
|
1520 | hasPlugin(pluginConfig) {
|
1521 | if (typeof pluginConfig === "string") {
|
1522 | return this.plugins.has(pluginConfig);
|
1523 | } else {
|
1524 | const [pluginName, pluginOptions] = pluginConfig;
|
1525 |
|
1526 | if (!this.hasPlugin(pluginName)) {
|
1527 | return false;
|
1528 | }
|
1529 |
|
1530 | const actualOptions = this.plugins.get(pluginName);
|
1531 |
|
1532 | for (const key of Object.keys(pluginOptions)) {
|
1533 | if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
|
1534 | return false;
|
1535 | }
|
1536 | }
|
1537 |
|
1538 | return true;
|
1539 | }
|
1540 | }
|
1541 |
|
1542 | getPluginOption(plugin, name) {
|
1543 | var _this$plugins$get;
|
1544 |
|
1545 | return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
|
1546 | }
|
1547 |
|
1548 | }
|
1549 |
|
1550 | function setTrailingComments(node, comments) {
|
1551 | if (node.trailingComments === undefined) {
|
1552 | node.trailingComments = comments;
|
1553 | } else {
|
1554 | node.trailingComments.unshift(...comments);
|
1555 | }
|
1556 | }
|
1557 |
|
1558 | function setLeadingComments(node, comments) {
|
1559 | if (node.leadingComments === undefined) {
|
1560 | node.leadingComments = comments;
|
1561 | } else {
|
1562 | node.leadingComments.unshift(...comments);
|
1563 | }
|
1564 | }
|
1565 |
|
1566 | function setInnerComments(node, comments) {
|
1567 | if (node.innerComments === undefined) {
|
1568 | node.innerComments = comments;
|
1569 | } else {
|
1570 | node.innerComments.unshift(...comments);
|
1571 | }
|
1572 | }
|
1573 |
|
1574 | function adjustInnerComments(node, elements, commentWS) {
|
1575 | let lastElement = null;
|
1576 | let i = elements.length;
|
1577 |
|
1578 | while (lastElement === null && i > 0) {
|
1579 | lastElement = elements[--i];
|
1580 | }
|
1581 |
|
1582 | if (lastElement === null || lastElement.start > commentWS.start) {
|
1583 | setInnerComments(node, commentWS.comments);
|
1584 | } else {
|
1585 | setTrailingComments(lastElement, commentWS.comments);
|
1586 | }
|
1587 | }
|
1588 |
|
1589 | class CommentsParser extends BaseParser {
|
1590 | addComment(comment) {
|
1591 | if (this.filename) comment.loc.filename = this.filename;
|
1592 | this.state.comments.push(comment);
|
1593 | }
|
1594 |
|
1595 | processComment(node) {
|
1596 | const {
|
1597 | commentStack
|
1598 | } = this.state;
|
1599 | const commentStackLength = commentStack.length;
|
1600 | if (commentStackLength === 0) return;
|
1601 | let i = commentStackLength - 1;
|
1602 | const lastCommentWS = commentStack[i];
|
1603 |
|
1604 | if (lastCommentWS.start === node.end) {
|
1605 | lastCommentWS.leadingNode = node;
|
1606 | i--;
|
1607 | }
|
1608 |
|
1609 | const {
|
1610 | start: nodeStart
|
1611 | } = node;
|
1612 |
|
1613 | for (; i >= 0; i--) {
|
1614 | const commentWS = commentStack[i];
|
1615 | const commentEnd = commentWS.end;
|
1616 |
|
1617 | if (commentEnd > nodeStart) {
|
1618 | commentWS.containingNode = node;
|
1619 | this.finalizeComment(commentWS);
|
1620 | commentStack.splice(i, 1);
|
1621 | } else {
|
1622 | if (commentEnd === nodeStart) {
|
1623 | commentWS.trailingNode = node;
|
1624 | }
|
1625 |
|
1626 | break;
|
1627 | }
|
1628 | }
|
1629 | }
|
1630 |
|
1631 | finalizeComment(commentWS) {
|
1632 | const {
|
1633 | comments
|
1634 | } = commentWS;
|
1635 |
|
1636 | if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
|
1637 | if (commentWS.leadingNode !== null) {
|
1638 | setTrailingComments(commentWS.leadingNode, comments);
|
1639 | }
|
1640 |
|
1641 | if (commentWS.trailingNode !== null) {
|
1642 | setLeadingComments(commentWS.trailingNode, comments);
|
1643 | }
|
1644 | } else {
|
1645 | const {
|
1646 | containingNode: node,
|
1647 | start: commentStart
|
1648 | } = commentWS;
|
1649 |
|
1650 | if (this.input.charCodeAt(commentStart - 1) === 44) {
|
1651 | switch (node.type) {
|
1652 | case "ObjectExpression":
|
1653 | case "ObjectPattern":
|
1654 | case "RecordExpression":
|
1655 | adjustInnerComments(node, node.properties, commentWS);
|
1656 | break;
|
1657 |
|
1658 | case "CallExpression":
|
1659 | case "OptionalCallExpression":
|
1660 | adjustInnerComments(node, node.arguments, commentWS);
|
1661 | break;
|
1662 |
|
1663 | case "FunctionDeclaration":
|
1664 | case "FunctionExpression":
|
1665 | case "ArrowFunctionExpression":
|
1666 | case "ObjectMethod":
|
1667 | case "ClassMethod":
|
1668 | case "ClassPrivateMethod":
|
1669 | adjustInnerComments(node, node.params, commentWS);
|
1670 | break;
|
1671 |
|
1672 | case "ArrayExpression":
|
1673 | case "ArrayPattern":
|
1674 | case "TupleExpression":
|
1675 | adjustInnerComments(node, node.elements, commentWS);
|
1676 | break;
|
1677 |
|
1678 | case "ExportNamedDeclaration":
|
1679 | case "ImportDeclaration":
|
1680 | adjustInnerComments(node, node.specifiers, commentWS);
|
1681 | break;
|
1682 |
|
1683 | default:
|
1684 | {
|
1685 | setInnerComments(node, comments);
|
1686 | }
|
1687 | }
|
1688 | } else {
|
1689 | setInnerComments(node, comments);
|
1690 | }
|
1691 | }
|
1692 | }
|
1693 |
|
1694 | finalizeRemainingComments() {
|
1695 | const {
|
1696 | commentStack
|
1697 | } = this.state;
|
1698 |
|
1699 | for (let i = commentStack.length - 1; i >= 0; i--) {
|
1700 | this.finalizeComment(commentStack[i]);
|
1701 | }
|
1702 |
|
1703 | this.state.commentStack = [];
|
1704 | }
|
1705 |
|
1706 | resetPreviousNodeTrailingComments(node) {
|
1707 | const {
|
1708 | commentStack
|
1709 | } = this.state;
|
1710 | const {
|
1711 | length
|
1712 | } = commentStack;
|
1713 | if (length === 0) return;
|
1714 | const commentWS = commentStack[length - 1];
|
1715 |
|
1716 | if (commentWS.leadingNode === node) {
|
1717 | commentWS.leadingNode = null;
|
1718 | }
|
1719 | }
|
1720 |
|
1721 | takeSurroundingComments(node, start, end) {
|
1722 | const {
|
1723 | commentStack
|
1724 | } = this.state;
|
1725 | const commentStackLength = commentStack.length;
|
1726 | if (commentStackLength === 0) return;
|
1727 | let i = commentStackLength - 1;
|
1728 |
|
1729 | for (; i >= 0; i--) {
|
1730 | const commentWS = commentStack[i];
|
1731 | const commentEnd = commentWS.end;
|
1732 | const commentStart = commentWS.start;
|
1733 |
|
1734 | if (commentStart === end) {
|
1735 | commentWS.leadingNode = node;
|
1736 | } else if (commentEnd === start) {
|
1737 | commentWS.trailingNode = node;
|
1738 | } else if (commentEnd < start) {
|
1739 | break;
|
1740 | }
|
1741 | }
|
1742 | }
|
1743 |
|
1744 | }
|
1745 |
|
1746 | const lineBreak = /\r\n?|[\n\u2028\u2029]/;
|
1747 | const lineBreakG = new RegExp(lineBreak.source, "g");
|
1748 | function isNewLine(code) {
|
1749 | switch (code) {
|
1750 | case 10:
|
1751 | case 13:
|
1752 | case 8232:
|
1753 | case 8233:
|
1754 | return true;
|
1755 |
|
1756 | default:
|
1757 | return false;
|
1758 | }
|
1759 | }
|
1760 | const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
1761 | const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
|
1762 | const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
|
1763 | function isWhitespace(code) {
|
1764 | switch (code) {
|
1765 | case 0x0009:
|
1766 | case 0x000b:
|
1767 | case 0x000c:
|
1768 | case 32:
|
1769 | case 160:
|
1770 | case 5760:
|
1771 | case 0x2000:
|
1772 | case 0x2001:
|
1773 | case 0x2002:
|
1774 | case 0x2003:
|
1775 | case 0x2004:
|
1776 | case 0x2005:
|
1777 | case 0x2006:
|
1778 | case 0x2007:
|
1779 | case 0x2008:
|
1780 | case 0x2009:
|
1781 | case 0x200a:
|
1782 | case 0x202f:
|
1783 | case 0x205f:
|
1784 | case 0x3000:
|
1785 | case 0xfeff:
|
1786 | return true;
|
1787 |
|
1788 | default:
|
1789 | return false;
|
1790 | }
|
1791 | }
|
1792 |
|
1793 | class State {
|
1794 | constructor() {
|
1795 | this.strict = void 0;
|
1796 | this.curLine = void 0;
|
1797 | this.lineStart = void 0;
|
1798 | this.startLoc = void 0;
|
1799 | this.endLoc = void 0;
|
1800 | this.errors = [];
|
1801 | this.potentialArrowAt = -1;
|
1802 | this.noArrowAt = [];
|
1803 | this.noArrowParamsConversionAt = [];
|
1804 | this.maybeInArrowParameters = false;
|
1805 | this.inType = false;
|
1806 | this.noAnonFunctionType = false;
|
1807 | this.hasFlowComment = false;
|
1808 | this.isAmbientContext = false;
|
1809 | this.inAbstractClass = false;
|
1810 | this.inDisallowConditionalTypesContext = false;
|
1811 | this.topicContext = {
|
1812 | maxNumOfResolvableTopics: 0,
|
1813 | maxTopicIndex: null
|
1814 | };
|
1815 | this.soloAwait = false;
|
1816 | this.inFSharpPipelineDirectBody = false;
|
1817 | this.labels = [];
|
1818 | this.decoratorStack = [[]];
|
1819 | this.comments = [];
|
1820 | this.commentStack = [];
|
1821 | this.pos = 0;
|
1822 | this.type = 135;
|
1823 | this.value = null;
|
1824 | this.start = 0;
|
1825 | this.end = 0;
|
1826 | this.lastTokEndLoc = null;
|
1827 | this.lastTokStartLoc = null;
|
1828 | this.lastTokStart = 0;
|
1829 | this.context = [types.brace];
|
1830 | this.canStartJSXElement = true;
|
1831 | this.containsEsc = false;
|
1832 | this.strictErrors = new Map();
|
1833 | this.tokensLength = 0;
|
1834 | }
|
1835 |
|
1836 | init({
|
1837 | strictMode,
|
1838 | sourceType,
|
1839 | startLine,
|
1840 | startColumn
|
1841 | }) {
|
1842 | this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
|
1843 | this.curLine = startLine;
|
1844 | this.lineStart = -startColumn;
|
1845 | this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
|
1846 | }
|
1847 |
|
1848 | curPosition() {
|
1849 | return new Position(this.curLine, this.pos - this.lineStart, this.pos);
|
1850 | }
|
1851 |
|
1852 | clone(skipArrays) {
|
1853 | const state = new State();
|
1854 | const keys = Object.keys(this);
|
1855 |
|
1856 | for (let i = 0, length = keys.length; i < length; i++) {
|
1857 | const key = keys[i];
|
1858 | let val = this[key];
|
1859 |
|
1860 | if (!skipArrays && Array.isArray(val)) {
|
1861 | val = val.slice();
|
1862 | }
|
1863 |
|
1864 | state[key] = val;
|
1865 | }
|
1866 |
|
1867 | return state;
|
1868 | }
|
1869 |
|
1870 | }
|
1871 |
|
1872 | const _excluded = ["at"],
|
1873 | _excluded2 = ["at"];
|
1874 |
|
1875 | var _isDigit = function isDigit(code) {
|
1876 | return code >= 48 && code <= 57;
|
1877 | };
|
1878 | const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
|
1879 | const forbiddenNumericSeparatorSiblings = {
|
1880 | decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
|
1881 | hex: new Set([46, 88, 95, 120])
|
1882 | };
|
1883 | const isAllowedNumericSeparatorSibling = {
|
1884 | bin: ch => ch === 48 || ch === 49,
|
1885 | oct: ch => ch >= 48 && ch <= 55,
|
1886 | dec: ch => ch >= 48 && ch <= 57,
|
1887 | hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
|
1888 | };
|
1889 | class Token {
|
1890 | constructor(state) {
|
1891 | this.type = state.type;
|
1892 | this.value = state.value;
|
1893 | this.start = state.start;
|
1894 | this.end = state.end;
|
1895 | this.loc = new SourceLocation(state.startLoc, state.endLoc);
|
1896 | }
|
1897 |
|
1898 | }
|
1899 | class Tokenizer extends CommentsParser {
|
1900 | constructor(options, input) {
|
1901 | super();
|
1902 | this.isLookahead = void 0;
|
1903 | this.tokens = [];
|
1904 | this.state = new State();
|
1905 | this.state.init(options);
|
1906 | this.input = input;
|
1907 | this.length = input.length;
|
1908 | this.isLookahead = false;
|
1909 | }
|
1910 |
|
1911 | pushToken(token) {
|
1912 | this.tokens.length = this.state.tokensLength;
|
1913 | this.tokens.push(token);
|
1914 | ++this.state.tokensLength;
|
1915 | }
|
1916 |
|
1917 | next() {
|
1918 | this.checkKeywordEscapes();
|
1919 |
|
1920 | if (this.options.tokens) {
|
1921 | this.pushToken(new Token(this.state));
|
1922 | }
|
1923 |
|
1924 | this.state.lastTokStart = this.state.start;
|
1925 | this.state.lastTokEndLoc = this.state.endLoc;
|
1926 | this.state.lastTokStartLoc = this.state.startLoc;
|
1927 | this.nextToken();
|
1928 | }
|
1929 |
|
1930 | eat(type) {
|
1931 | if (this.match(type)) {
|
1932 | this.next();
|
1933 | return true;
|
1934 | } else {
|
1935 | return false;
|
1936 | }
|
1937 | }
|
1938 |
|
1939 | match(type) {
|
1940 | return this.state.type === type;
|
1941 | }
|
1942 |
|
1943 | createLookaheadState(state) {
|
1944 | return {
|
1945 | pos: state.pos,
|
1946 | value: null,
|
1947 | type: state.type,
|
1948 | start: state.start,
|
1949 | end: state.end,
|
1950 | context: [this.curContext()],
|
1951 | inType: state.inType,
|
1952 | startLoc: state.startLoc,
|
1953 | lastTokEndLoc: state.lastTokEndLoc,
|
1954 | curLine: state.curLine,
|
1955 | lineStart: state.lineStart,
|
1956 | curPosition: state.curPosition
|
1957 | };
|
1958 | }
|
1959 |
|
1960 | lookahead() {
|
1961 | const old = this.state;
|
1962 | this.state = this.createLookaheadState(old);
|
1963 | this.isLookahead = true;
|
1964 | this.nextToken();
|
1965 | this.isLookahead = false;
|
1966 | const curr = this.state;
|
1967 | this.state = old;
|
1968 | return curr;
|
1969 | }
|
1970 |
|
1971 | nextTokenStart() {
|
1972 | return this.nextTokenStartSince(this.state.pos);
|
1973 | }
|
1974 |
|
1975 | nextTokenStartSince(pos) {
|
1976 | skipWhiteSpace.lastIndex = pos;
|
1977 | return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
|
1978 | }
|
1979 |
|
1980 | lookaheadCharCode() {
|
1981 | return this.input.charCodeAt(this.nextTokenStart());
|
1982 | }
|
1983 |
|
1984 | codePointAtPos(pos) {
|
1985 | let cp = this.input.charCodeAt(pos);
|
1986 |
|
1987 | if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
|
1988 | const trail = this.input.charCodeAt(pos);
|
1989 |
|
1990 | if ((trail & 0xfc00) === 0xdc00) {
|
1991 | cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
|
1992 | }
|
1993 | }
|
1994 |
|
1995 | return cp;
|
1996 | }
|
1997 |
|
1998 | setStrict(strict) {
|
1999 | this.state.strict = strict;
|
2000 |
|
2001 | if (strict) {
|
2002 | this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
|
2003 | at
|
2004 | }));
|
2005 | this.state.strictErrors.clear();
|
2006 | }
|
2007 | }
|
2008 |
|
2009 | curContext() {
|
2010 | return this.state.context[this.state.context.length - 1];
|
2011 | }
|
2012 |
|
2013 | nextToken() {
|
2014 | this.skipSpace();
|
2015 | this.state.start = this.state.pos;
|
2016 | if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
|
2017 |
|
2018 | if (this.state.pos >= this.length) {
|
2019 | this.finishToken(135);
|
2020 | return;
|
2021 | }
|
2022 |
|
2023 | this.getTokenFromCode(this.codePointAtPos(this.state.pos));
|
2024 | }
|
2025 |
|
2026 | skipBlockComment() {
|
2027 | let startLoc;
|
2028 | if (!this.isLookahead) startLoc = this.state.curPosition();
|
2029 | const start = this.state.pos;
|
2030 | const end = this.input.indexOf("*/", start + 2);
|
2031 |
|
2032 | if (end === -1) {
|
2033 | throw this.raise(Errors.UnterminatedComment, {
|
2034 | at: this.state.curPosition()
|
2035 | });
|
2036 | }
|
2037 |
|
2038 | this.state.pos = end + 2;
|
2039 | lineBreakG.lastIndex = start + 2;
|
2040 |
|
2041 | while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
|
2042 | ++this.state.curLine;
|
2043 | this.state.lineStart = lineBreakG.lastIndex;
|
2044 | }
|
2045 |
|
2046 | if (this.isLookahead) return;
|
2047 | const comment = {
|
2048 | type: "CommentBlock",
|
2049 | value: this.input.slice(start + 2, end),
|
2050 | start,
|
2051 | end: end + 2,
|
2052 | loc: new SourceLocation(startLoc, this.state.curPosition())
|
2053 | };
|
2054 | if (this.options.tokens) this.pushToken(comment);
|
2055 | return comment;
|
2056 | }
|
2057 |
|
2058 | skipLineComment(startSkip) {
|
2059 | const start = this.state.pos;
|
2060 | let startLoc;
|
2061 | if (!this.isLookahead) startLoc = this.state.curPosition();
|
2062 | let ch = this.input.charCodeAt(this.state.pos += startSkip);
|
2063 |
|
2064 | if (this.state.pos < this.length) {
|
2065 | while (!isNewLine(ch) && ++this.state.pos < this.length) {
|
2066 | ch = this.input.charCodeAt(this.state.pos);
|
2067 | }
|
2068 | }
|
2069 |
|
2070 | if (this.isLookahead) return;
|
2071 | const end = this.state.pos;
|
2072 | const value = this.input.slice(start + startSkip, end);
|
2073 | const comment = {
|
2074 | type: "CommentLine",
|
2075 | value,
|
2076 | start,
|
2077 | end,
|
2078 | loc: new SourceLocation(startLoc, this.state.curPosition())
|
2079 | };
|
2080 | if (this.options.tokens) this.pushToken(comment);
|
2081 | return comment;
|
2082 | }
|
2083 |
|
2084 | skipSpace() {
|
2085 | const spaceStart = this.state.pos;
|
2086 | const comments = [];
|
2087 |
|
2088 | loop: while (this.state.pos < this.length) {
|
2089 | const ch = this.input.charCodeAt(this.state.pos);
|
2090 |
|
2091 | switch (ch) {
|
2092 | case 32:
|
2093 | case 160:
|
2094 | case 9:
|
2095 | ++this.state.pos;
|
2096 | break;
|
2097 |
|
2098 | case 13:
|
2099 | if (this.input.charCodeAt(this.state.pos + 1) === 10) {
|
2100 | ++this.state.pos;
|
2101 | }
|
2102 |
|
2103 | case 10:
|
2104 | case 8232:
|
2105 | case 8233:
|
2106 | ++this.state.pos;
|
2107 | ++this.state.curLine;
|
2108 | this.state.lineStart = this.state.pos;
|
2109 | break;
|
2110 |
|
2111 | case 47:
|
2112 | switch (this.input.charCodeAt(this.state.pos + 1)) {
|
2113 | case 42:
|
2114 | {
|
2115 | const comment = this.skipBlockComment();
|
2116 |
|
2117 | if (comment !== undefined) {
|
2118 | this.addComment(comment);
|
2119 | if (this.options.attachComment) comments.push(comment);
|
2120 | }
|
2121 |
|
2122 | break;
|
2123 | }
|
2124 |
|
2125 | case 47:
|
2126 | {
|
2127 | const comment = this.skipLineComment(2);
|
2128 |
|
2129 | if (comment !== undefined) {
|
2130 | this.addComment(comment);
|
2131 | if (this.options.attachComment) comments.push(comment);
|
2132 | }
|
2133 |
|
2134 | break;
|
2135 | }
|
2136 |
|
2137 | default:
|
2138 | break loop;
|
2139 | }
|
2140 |
|
2141 | break;
|
2142 |
|
2143 | default:
|
2144 | if (isWhitespace(ch)) {
|
2145 | ++this.state.pos;
|
2146 | } else if (ch === 45 && !this.inModule) {
|
2147 | const pos = this.state.pos;
|
2148 |
|
2149 | if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
|
2150 | const comment = this.skipLineComment(3);
|
2151 |
|
2152 | if (comment !== undefined) {
|
2153 | this.addComment(comment);
|
2154 | if (this.options.attachComment) comments.push(comment);
|
2155 | }
|
2156 | } else {
|
2157 | break loop;
|
2158 | }
|
2159 | } else if (ch === 60 && !this.inModule) {
|
2160 | const pos = this.state.pos;
|
2161 |
|
2162 | if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
|
2163 | const comment = this.skipLineComment(4);
|
2164 |
|
2165 | if (comment !== undefined) {
|
2166 | this.addComment(comment);
|
2167 | if (this.options.attachComment) comments.push(comment);
|
2168 | }
|
2169 | } else {
|
2170 | break loop;
|
2171 | }
|
2172 | } else {
|
2173 | break loop;
|
2174 | }
|
2175 |
|
2176 | }
|
2177 | }
|
2178 |
|
2179 | if (comments.length > 0) {
|
2180 | const end = this.state.pos;
|
2181 | const CommentWhitespace = {
|
2182 | start: spaceStart,
|
2183 | end,
|
2184 | comments,
|
2185 | leadingNode: null,
|
2186 | trailingNode: null,
|
2187 | containingNode: null
|
2188 | };
|
2189 | this.state.commentStack.push(CommentWhitespace);
|
2190 | }
|
2191 | }
|
2192 |
|
2193 | finishToken(type, val) {
|
2194 | this.state.end = this.state.pos;
|
2195 | this.state.endLoc = this.state.curPosition();
|
2196 | const prevType = this.state.type;
|
2197 | this.state.type = type;
|
2198 | this.state.value = val;
|
2199 |
|
2200 | if (!this.isLookahead) {
|
2201 | this.updateContext(prevType);
|
2202 | }
|
2203 | }
|
2204 |
|
2205 | replaceToken(type) {
|
2206 | this.state.type = type;
|
2207 | this.updateContext();
|
2208 | }
|
2209 |
|
2210 | readToken_numberSign() {
|
2211 | if (this.state.pos === 0 && this.readToken_interpreter()) {
|
2212 | return;
|
2213 | }
|
2214 |
|
2215 | const nextPos = this.state.pos + 1;
|
2216 | const next = this.codePointAtPos(nextPos);
|
2217 |
|
2218 | if (next >= 48 && next <= 57) {
|
2219 | throw this.raise(Errors.UnexpectedDigitAfterHash, {
|
2220 | at: this.state.curPosition()
|
2221 | });
|
2222 | }
|
2223 |
|
2224 | if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
|
2225 | this.expectPlugin("recordAndTuple");
|
2226 |
|
2227 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
|
2228 | throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
|
2229 | at: this.state.curPosition()
|
2230 | });
|
2231 | }
|
2232 |
|
2233 | this.state.pos += 2;
|
2234 |
|
2235 | if (next === 123) {
|
2236 | this.finishToken(7);
|
2237 | } else {
|
2238 | this.finishToken(1);
|
2239 | }
|
2240 | } else if (isIdentifierStart(next)) {
|
2241 | ++this.state.pos;
|
2242 | this.finishToken(134, this.readWord1(next));
|
2243 | } else if (next === 92) {
|
2244 | ++this.state.pos;
|
2245 | this.finishToken(134, this.readWord1());
|
2246 | } else {
|
2247 | this.finishOp(27, 1);
|
2248 | }
|
2249 | }
|
2250 |
|
2251 | readToken_dot() {
|
2252 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2253 |
|
2254 | if (next >= 48 && next <= 57) {
|
2255 | this.readNumber(true);
|
2256 | return;
|
2257 | }
|
2258 |
|
2259 | if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
|
2260 | this.state.pos += 3;
|
2261 | this.finishToken(21);
|
2262 | } else {
|
2263 | ++this.state.pos;
|
2264 | this.finishToken(16);
|
2265 | }
|
2266 | }
|
2267 |
|
2268 | readToken_slash() {
|
2269 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2270 |
|
2271 | if (next === 61) {
|
2272 | this.finishOp(31, 2);
|
2273 | } else {
|
2274 | this.finishOp(56, 1);
|
2275 | }
|
2276 | }
|
2277 |
|
2278 | readToken_interpreter() {
|
2279 | if (this.state.pos !== 0 || this.length < 2) return false;
|
2280 | let ch = this.input.charCodeAt(this.state.pos + 1);
|
2281 | if (ch !== 33) return false;
|
2282 | const start = this.state.pos;
|
2283 | this.state.pos += 1;
|
2284 |
|
2285 | while (!isNewLine(ch) && ++this.state.pos < this.length) {
|
2286 | ch = this.input.charCodeAt(this.state.pos);
|
2287 | }
|
2288 |
|
2289 | const value = this.input.slice(start + 2, this.state.pos);
|
2290 | this.finishToken(28, value);
|
2291 | return true;
|
2292 | }
|
2293 |
|
2294 | readToken_mult_modulo(code) {
|
2295 | let type = code === 42 ? 55 : 54;
|
2296 | let width = 1;
|
2297 | let next = this.input.charCodeAt(this.state.pos + 1);
|
2298 |
|
2299 | if (code === 42 && next === 42) {
|
2300 | width++;
|
2301 | next = this.input.charCodeAt(this.state.pos + 2);
|
2302 | type = 57;
|
2303 | }
|
2304 |
|
2305 | if (next === 61 && !this.state.inType) {
|
2306 | width++;
|
2307 | type = code === 37 ? 33 : 30;
|
2308 | }
|
2309 |
|
2310 | this.finishOp(type, width);
|
2311 | }
|
2312 |
|
2313 | readToken_pipe_amp(code) {
|
2314 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2315 |
|
2316 | if (next === code) {
|
2317 | if (this.input.charCodeAt(this.state.pos + 2) === 61) {
|
2318 | this.finishOp(30, 3);
|
2319 | } else {
|
2320 | this.finishOp(code === 124 ? 41 : 42, 2);
|
2321 | }
|
2322 |
|
2323 | return;
|
2324 | }
|
2325 |
|
2326 | if (code === 124) {
|
2327 | if (next === 62) {
|
2328 | this.finishOp(39, 2);
|
2329 | return;
|
2330 | }
|
2331 |
|
2332 | if (this.hasPlugin("recordAndTuple") && next === 125) {
|
2333 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2334 | throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
|
2335 | at: this.state.curPosition()
|
2336 | });
|
2337 | }
|
2338 |
|
2339 | this.state.pos += 2;
|
2340 | this.finishToken(9);
|
2341 | return;
|
2342 | }
|
2343 |
|
2344 | if (this.hasPlugin("recordAndTuple") && next === 93) {
|
2345 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2346 | throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
|
2347 | at: this.state.curPosition()
|
2348 | });
|
2349 | }
|
2350 |
|
2351 | this.state.pos += 2;
|
2352 | this.finishToken(4);
|
2353 | return;
|
2354 | }
|
2355 | }
|
2356 |
|
2357 | if (next === 61) {
|
2358 | this.finishOp(30, 2);
|
2359 | return;
|
2360 | }
|
2361 |
|
2362 | this.finishOp(code === 124 ? 43 : 45, 1);
|
2363 | }
|
2364 |
|
2365 | readToken_caret() {
|
2366 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2367 |
|
2368 | if (next === 61 && !this.state.inType) {
|
2369 | this.finishOp(32, 2);
|
2370 | } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
|
2371 | proposal: "hack",
|
2372 | topicToken: "^^"
|
2373 | }])) {
|
2374 | this.finishOp(37, 2);
|
2375 | const lookaheadCh = this.input.codePointAt(this.state.pos);
|
2376 |
|
2377 | if (lookaheadCh === 94) {
|
2378 | throw this.unexpected();
|
2379 | }
|
2380 | } else {
|
2381 | this.finishOp(44, 1);
|
2382 | }
|
2383 | }
|
2384 |
|
2385 | readToken_atSign() {
|
2386 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2387 |
|
2388 | if (next === 64 && this.hasPlugin(["pipelineOperator", {
|
2389 | proposal: "hack",
|
2390 | topicToken: "@@"
|
2391 | }])) {
|
2392 | this.finishOp(38, 2);
|
2393 | } else {
|
2394 | this.finishOp(26, 1);
|
2395 | }
|
2396 | }
|
2397 |
|
2398 | readToken_plus_min(code) {
|
2399 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2400 |
|
2401 | if (next === code) {
|
2402 | this.finishOp(34, 2);
|
2403 | return;
|
2404 | }
|
2405 |
|
2406 | if (next === 61) {
|
2407 | this.finishOp(30, 2);
|
2408 | } else {
|
2409 | this.finishOp(53, 1);
|
2410 | }
|
2411 | }
|
2412 |
|
2413 | readToken_lt() {
|
2414 | const {
|
2415 | pos
|
2416 | } = this.state;
|
2417 | const next = this.input.charCodeAt(pos + 1);
|
2418 |
|
2419 | if (next === 60) {
|
2420 | if (this.input.charCodeAt(pos + 2) === 61) {
|
2421 | this.finishOp(30, 3);
|
2422 | return;
|
2423 | }
|
2424 |
|
2425 | this.finishOp(51, 2);
|
2426 | return;
|
2427 | }
|
2428 |
|
2429 | if (next === 61) {
|
2430 | this.finishOp(49, 2);
|
2431 | return;
|
2432 | }
|
2433 |
|
2434 | this.finishOp(47, 1);
|
2435 | }
|
2436 |
|
2437 | readToken_gt() {
|
2438 | const {
|
2439 | pos
|
2440 | } = this.state;
|
2441 | const next = this.input.charCodeAt(pos + 1);
|
2442 |
|
2443 | if (next === 62) {
|
2444 | const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
|
2445 |
|
2446 | if (this.input.charCodeAt(pos + size) === 61) {
|
2447 | this.finishOp(30, size + 1);
|
2448 | return;
|
2449 | }
|
2450 |
|
2451 | this.finishOp(52, size);
|
2452 | return;
|
2453 | }
|
2454 |
|
2455 | if (next === 61) {
|
2456 | this.finishOp(49, 2);
|
2457 | return;
|
2458 | }
|
2459 |
|
2460 | this.finishOp(48, 1);
|
2461 | }
|
2462 |
|
2463 | readToken_eq_excl(code) {
|
2464 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2465 |
|
2466 | if (next === 61) {
|
2467 | this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
|
2468 | return;
|
2469 | }
|
2470 |
|
2471 | if (code === 61 && next === 62) {
|
2472 | this.state.pos += 2;
|
2473 | this.finishToken(19);
|
2474 | return;
|
2475 | }
|
2476 |
|
2477 | this.finishOp(code === 61 ? 29 : 35, 1);
|
2478 | }
|
2479 |
|
2480 | readToken_question() {
|
2481 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2482 | const next2 = this.input.charCodeAt(this.state.pos + 2);
|
2483 |
|
2484 | if (next === 63) {
|
2485 | if (next2 === 61) {
|
2486 | this.finishOp(30, 3);
|
2487 | } else {
|
2488 | this.finishOp(40, 2);
|
2489 | }
|
2490 | } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
|
2491 | this.state.pos += 2;
|
2492 | this.finishToken(18);
|
2493 | } else {
|
2494 | ++this.state.pos;
|
2495 | this.finishToken(17);
|
2496 | }
|
2497 | }
|
2498 |
|
2499 | getTokenFromCode(code) {
|
2500 | switch (code) {
|
2501 | case 46:
|
2502 | this.readToken_dot();
|
2503 | return;
|
2504 |
|
2505 | case 40:
|
2506 | ++this.state.pos;
|
2507 | this.finishToken(10);
|
2508 | return;
|
2509 |
|
2510 | case 41:
|
2511 | ++this.state.pos;
|
2512 | this.finishToken(11);
|
2513 | return;
|
2514 |
|
2515 | case 59:
|
2516 | ++this.state.pos;
|
2517 | this.finishToken(13);
|
2518 | return;
|
2519 |
|
2520 | case 44:
|
2521 | ++this.state.pos;
|
2522 | this.finishToken(12);
|
2523 | return;
|
2524 |
|
2525 | case 91:
|
2526 | if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
|
2527 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2528 | throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
|
2529 | at: this.state.curPosition()
|
2530 | });
|
2531 | }
|
2532 |
|
2533 | this.state.pos += 2;
|
2534 | this.finishToken(2);
|
2535 | } else {
|
2536 | ++this.state.pos;
|
2537 | this.finishToken(0);
|
2538 | }
|
2539 |
|
2540 | return;
|
2541 |
|
2542 | case 93:
|
2543 | ++this.state.pos;
|
2544 | this.finishToken(3);
|
2545 | return;
|
2546 |
|
2547 | case 123:
|
2548 | if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
|
2549 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2550 | throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
|
2551 | at: this.state.curPosition()
|
2552 | });
|
2553 | }
|
2554 |
|
2555 | this.state.pos += 2;
|
2556 | this.finishToken(6);
|
2557 | } else {
|
2558 | ++this.state.pos;
|
2559 | this.finishToken(5);
|
2560 | }
|
2561 |
|
2562 | return;
|
2563 |
|
2564 | case 125:
|
2565 | ++this.state.pos;
|
2566 | this.finishToken(8);
|
2567 | return;
|
2568 |
|
2569 | case 58:
|
2570 | if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
|
2571 | this.finishOp(15, 2);
|
2572 | } else {
|
2573 | ++this.state.pos;
|
2574 | this.finishToken(14);
|
2575 | }
|
2576 |
|
2577 | return;
|
2578 |
|
2579 | case 63:
|
2580 | this.readToken_question();
|
2581 | return;
|
2582 |
|
2583 | case 96:
|
2584 | this.readTemplateToken();
|
2585 | return;
|
2586 |
|
2587 | case 48:
|
2588 | {
|
2589 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2590 |
|
2591 | if (next === 120 || next === 88) {
|
2592 | this.readRadixNumber(16);
|
2593 | return;
|
2594 | }
|
2595 |
|
2596 | if (next === 111 || next === 79) {
|
2597 | this.readRadixNumber(8);
|
2598 | return;
|
2599 | }
|
2600 |
|
2601 | if (next === 98 || next === 66) {
|
2602 | this.readRadixNumber(2);
|
2603 | return;
|
2604 | }
|
2605 | }
|
2606 |
|
2607 | case 49:
|
2608 | case 50:
|
2609 | case 51:
|
2610 | case 52:
|
2611 | case 53:
|
2612 | case 54:
|
2613 | case 55:
|
2614 | case 56:
|
2615 | case 57:
|
2616 | this.readNumber(false);
|
2617 | return;
|
2618 |
|
2619 | case 34:
|
2620 | case 39:
|
2621 | this.readString(code);
|
2622 | return;
|
2623 |
|
2624 | case 47:
|
2625 | this.readToken_slash();
|
2626 | return;
|
2627 |
|
2628 | case 37:
|
2629 | case 42:
|
2630 | this.readToken_mult_modulo(code);
|
2631 | return;
|
2632 |
|
2633 | case 124:
|
2634 | case 38:
|
2635 | this.readToken_pipe_amp(code);
|
2636 | return;
|
2637 |
|
2638 | case 94:
|
2639 | this.readToken_caret();
|
2640 | return;
|
2641 |
|
2642 | case 43:
|
2643 | case 45:
|
2644 | this.readToken_plus_min(code);
|
2645 | return;
|
2646 |
|
2647 | case 60:
|
2648 | this.readToken_lt();
|
2649 | return;
|
2650 |
|
2651 | case 62:
|
2652 | this.readToken_gt();
|
2653 | return;
|
2654 |
|
2655 | case 61:
|
2656 | case 33:
|
2657 | this.readToken_eq_excl(code);
|
2658 | return;
|
2659 |
|
2660 | case 126:
|
2661 | this.finishOp(36, 1);
|
2662 | return;
|
2663 |
|
2664 | case 64:
|
2665 | this.readToken_atSign();
|
2666 | return;
|
2667 |
|
2668 | case 35:
|
2669 | this.readToken_numberSign();
|
2670 | return;
|
2671 |
|
2672 | case 92:
|
2673 | this.readWord();
|
2674 | return;
|
2675 |
|
2676 | default:
|
2677 | if (isIdentifierStart(code)) {
|
2678 | this.readWord(code);
|
2679 | return;
|
2680 | }
|
2681 |
|
2682 | }
|
2683 |
|
2684 | throw this.raise(Errors.InvalidOrUnexpectedToken, {
|
2685 | at: this.state.curPosition(),
|
2686 | unexpected: String.fromCodePoint(code)
|
2687 | });
|
2688 | }
|
2689 |
|
2690 | finishOp(type, size) {
|
2691 | const str = this.input.slice(this.state.pos, this.state.pos + size);
|
2692 | this.state.pos += size;
|
2693 | this.finishToken(type, str);
|
2694 | }
|
2695 |
|
2696 | readRegexp() {
|
2697 | const startLoc = this.state.startLoc;
|
2698 | const start = this.state.start + 1;
|
2699 | let escaped, inClass;
|
2700 | let {
|
2701 | pos
|
2702 | } = this.state;
|
2703 |
|
2704 | for (;; ++pos) {
|
2705 | if (pos >= this.length) {
|
2706 | throw this.raise(Errors.UnterminatedRegExp, {
|
2707 | at: createPositionWithColumnOffset(startLoc, 1)
|
2708 | });
|
2709 | }
|
2710 |
|
2711 | const ch = this.input.charCodeAt(pos);
|
2712 |
|
2713 | if (isNewLine(ch)) {
|
2714 | throw this.raise(Errors.UnterminatedRegExp, {
|
2715 | at: createPositionWithColumnOffset(startLoc, 1)
|
2716 | });
|
2717 | }
|
2718 |
|
2719 | if (escaped) {
|
2720 | escaped = false;
|
2721 | } else {
|
2722 | if (ch === 91) {
|
2723 | inClass = true;
|
2724 | } else if (ch === 93 && inClass) {
|
2725 | inClass = false;
|
2726 | } else if (ch === 47 && !inClass) {
|
2727 | break;
|
2728 | }
|
2729 |
|
2730 | escaped = ch === 92;
|
2731 | }
|
2732 | }
|
2733 |
|
2734 | const content = this.input.slice(start, pos);
|
2735 | ++pos;
|
2736 | let mods = "";
|
2737 |
|
2738 | const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
|
2739 |
|
2740 | while (pos < this.length) {
|
2741 | const cp = this.codePointAtPos(pos);
|
2742 | const char = String.fromCharCode(cp);
|
2743 |
|
2744 | if (VALID_REGEX_FLAGS.has(cp)) {
|
2745 | if (cp === 118) {
|
2746 | this.expectPlugin("regexpUnicodeSets", nextPos());
|
2747 |
|
2748 | if (mods.includes("u")) {
|
2749 | this.raise(Errors.IncompatibleRegExpUVFlags, {
|
2750 | at: nextPos()
|
2751 | });
|
2752 | }
|
2753 | } else if (cp === 117) {
|
2754 | if (mods.includes("v")) {
|
2755 | this.raise(Errors.IncompatibleRegExpUVFlags, {
|
2756 | at: nextPos()
|
2757 | });
|
2758 | }
|
2759 | }
|
2760 |
|
2761 | if (mods.includes(char)) {
|
2762 | this.raise(Errors.DuplicateRegExpFlags, {
|
2763 | at: nextPos()
|
2764 | });
|
2765 | }
|
2766 | } else if (isIdentifierChar(cp) || cp === 92) {
|
2767 | this.raise(Errors.MalformedRegExpFlags, {
|
2768 | at: nextPos()
|
2769 | });
|
2770 | } else {
|
2771 | break;
|
2772 | }
|
2773 |
|
2774 | ++pos;
|
2775 | mods += char;
|
2776 | }
|
2777 |
|
2778 | this.state.pos = pos;
|
2779 | this.finishToken(133, {
|
2780 | pattern: content,
|
2781 | flags: mods
|
2782 | });
|
2783 | }
|
2784 |
|
2785 | readInt(radix, len, forceLen, allowNumSeparator = true) {
|
2786 | const start = this.state.pos;
|
2787 | const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
|
2788 | const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
|
2789 | let invalid = false;
|
2790 | let total = 0;
|
2791 |
|
2792 | for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
|
2793 | const code = this.input.charCodeAt(this.state.pos);
|
2794 | let val;
|
2795 |
|
2796 | if (code === 95 && allowNumSeparator !== "bail") {
|
2797 | const prev = this.input.charCodeAt(this.state.pos - 1);
|
2798 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2799 |
|
2800 | if (!allowNumSeparator) {
|
2801 | this.raise(Errors.NumericSeparatorInEscapeSequence, {
|
2802 | at: this.state.curPosition()
|
2803 | });
|
2804 | } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
|
2805 | this.raise(Errors.UnexpectedNumericSeparator, {
|
2806 | at: this.state.curPosition()
|
2807 | });
|
2808 | }
|
2809 |
|
2810 | ++this.state.pos;
|
2811 | continue;
|
2812 | }
|
2813 |
|
2814 | if (code >= 97) {
|
2815 | val = code - 97 + 10;
|
2816 | } else if (code >= 65) {
|
2817 | val = code - 65 + 10;
|
2818 | } else if (_isDigit(code)) {
|
2819 | val = code - 48;
|
2820 | } else {
|
2821 | val = Infinity;
|
2822 | }
|
2823 |
|
2824 | if (val >= radix) {
|
2825 | if (this.options.errorRecovery && val <= 9) {
|
2826 | val = 0;
|
2827 | this.raise(Errors.InvalidDigit, {
|
2828 | at: this.state.curPosition(),
|
2829 | radix
|
2830 | });
|
2831 | } else if (forceLen) {
|
2832 | val = 0;
|
2833 | invalid = true;
|
2834 | } else {
|
2835 | break;
|
2836 | }
|
2837 | }
|
2838 |
|
2839 | ++this.state.pos;
|
2840 | total = total * radix + val;
|
2841 | }
|
2842 |
|
2843 | if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
|
2844 | return null;
|
2845 | }
|
2846 |
|
2847 | return total;
|
2848 | }
|
2849 |
|
2850 | readRadixNumber(radix) {
|
2851 | const startLoc = this.state.curPosition();
|
2852 | let isBigInt = false;
|
2853 | this.state.pos += 2;
|
2854 | const val = this.readInt(radix);
|
2855 |
|
2856 | if (val == null) {
|
2857 | this.raise(Errors.InvalidDigit, {
|
2858 | at: createPositionWithColumnOffset(startLoc, 2),
|
2859 | radix
|
2860 | });
|
2861 | }
|
2862 |
|
2863 | const next = this.input.charCodeAt(this.state.pos);
|
2864 |
|
2865 | if (next === 110) {
|
2866 | ++this.state.pos;
|
2867 | isBigInt = true;
|
2868 | } else if (next === 109) {
|
2869 | throw this.raise(Errors.InvalidDecimal, {
|
2870 | at: startLoc
|
2871 | });
|
2872 | }
|
2873 |
|
2874 | if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
|
2875 | throw this.raise(Errors.NumberIdentifier, {
|
2876 | at: this.state.curPosition()
|
2877 | });
|
2878 | }
|
2879 |
|
2880 | if (isBigInt) {
|
2881 | const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
|
2882 | this.finishToken(131, str);
|
2883 | return;
|
2884 | }
|
2885 |
|
2886 | this.finishToken(130, val);
|
2887 | }
|
2888 |
|
2889 | readNumber(startsWithDot) {
|
2890 | const start = this.state.pos;
|
2891 | const startLoc = this.state.curPosition();
|
2892 | let isFloat = false;
|
2893 | let isBigInt = false;
|
2894 | let isDecimal = false;
|
2895 | let hasExponent = false;
|
2896 | let isOctal = false;
|
2897 |
|
2898 | if (!startsWithDot && this.readInt(10) === null) {
|
2899 | this.raise(Errors.InvalidNumber, {
|
2900 | at: this.state.curPosition()
|
2901 | });
|
2902 | }
|
2903 |
|
2904 | const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
|
2905 |
|
2906 | if (hasLeadingZero) {
|
2907 | const integer = this.input.slice(start, this.state.pos);
|
2908 | this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
|
2909 | at: startLoc
|
2910 | });
|
2911 |
|
2912 | if (!this.state.strict) {
|
2913 | const underscorePos = integer.indexOf("_");
|
2914 |
|
2915 | if (underscorePos > 0) {
|
2916 | this.raise(Errors.ZeroDigitNumericSeparator, {
|
2917 | at: createPositionWithColumnOffset(startLoc, underscorePos)
|
2918 | });
|
2919 | }
|
2920 | }
|
2921 |
|
2922 | isOctal = hasLeadingZero && !/[89]/.test(integer);
|
2923 | }
|
2924 |
|
2925 | let next = this.input.charCodeAt(this.state.pos);
|
2926 |
|
2927 | if (next === 46 && !isOctal) {
|
2928 | ++this.state.pos;
|
2929 | this.readInt(10);
|
2930 | isFloat = true;
|
2931 | next = this.input.charCodeAt(this.state.pos);
|
2932 | }
|
2933 |
|
2934 | if ((next === 69 || next === 101) && !isOctal) {
|
2935 | next = this.input.charCodeAt(++this.state.pos);
|
2936 |
|
2937 | if (next === 43 || next === 45) {
|
2938 | ++this.state.pos;
|
2939 | }
|
2940 |
|
2941 | if (this.readInt(10) === null) {
|
2942 | this.raise(Errors.InvalidOrMissingExponent, {
|
2943 | at: startLoc
|
2944 | });
|
2945 | }
|
2946 |
|
2947 | isFloat = true;
|
2948 | hasExponent = true;
|
2949 | next = this.input.charCodeAt(this.state.pos);
|
2950 | }
|
2951 |
|
2952 | if (next === 110) {
|
2953 | if (isFloat || hasLeadingZero) {
|
2954 | this.raise(Errors.InvalidBigIntLiteral, {
|
2955 | at: startLoc
|
2956 | });
|
2957 | }
|
2958 |
|
2959 | ++this.state.pos;
|
2960 | isBigInt = true;
|
2961 | }
|
2962 |
|
2963 | if (next === 109) {
|
2964 | this.expectPlugin("decimal", this.state.curPosition());
|
2965 |
|
2966 | if (hasExponent || hasLeadingZero) {
|
2967 | this.raise(Errors.InvalidDecimal, {
|
2968 | at: startLoc
|
2969 | });
|
2970 | }
|
2971 |
|
2972 | ++this.state.pos;
|
2973 | isDecimal = true;
|
2974 | }
|
2975 |
|
2976 | if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
|
2977 | throw this.raise(Errors.NumberIdentifier, {
|
2978 | at: this.state.curPosition()
|
2979 | });
|
2980 | }
|
2981 |
|
2982 | const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
|
2983 |
|
2984 | if (isBigInt) {
|
2985 | this.finishToken(131, str);
|
2986 | return;
|
2987 | }
|
2988 |
|
2989 | if (isDecimal) {
|
2990 | this.finishToken(132, str);
|
2991 | return;
|
2992 | }
|
2993 |
|
2994 | const val = isOctal ? parseInt(str, 8) : parseFloat(str);
|
2995 | this.finishToken(130, val);
|
2996 | }
|
2997 |
|
2998 | readCodePoint(throwOnInvalid) {
|
2999 | const ch = this.input.charCodeAt(this.state.pos);
|
3000 | let code;
|
3001 |
|
3002 | if (ch === 123) {
|
3003 | ++this.state.pos;
|
3004 | code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
|
3005 | ++this.state.pos;
|
3006 |
|
3007 | if (code !== null && code > 0x10ffff) {
|
3008 | if (throwOnInvalid) {
|
3009 | this.raise(Errors.InvalidCodePoint, {
|
3010 | at: this.state.curPosition()
|
3011 | });
|
3012 | } else {
|
3013 | return null;
|
3014 | }
|
3015 | }
|
3016 | } else {
|
3017 | code = this.readHexChar(4, false, throwOnInvalid);
|
3018 | }
|
3019 |
|
3020 | return code;
|
3021 | }
|
3022 |
|
3023 | readString(quote) {
|
3024 | let out = "",
|
3025 | chunkStart = ++this.state.pos;
|
3026 |
|
3027 | for (;;) {
|
3028 | if (this.state.pos >= this.length) {
|
3029 | throw this.raise(Errors.UnterminatedString, {
|
3030 | at: this.state.startLoc
|
3031 | });
|
3032 | }
|
3033 |
|
3034 | const ch = this.input.charCodeAt(this.state.pos);
|
3035 | if (ch === quote) break;
|
3036 |
|
3037 | if (ch === 92) {
|
3038 | out += this.input.slice(chunkStart, this.state.pos);
|
3039 | out += this.readEscapedChar(false);
|
3040 | chunkStart = this.state.pos;
|
3041 | } else if (ch === 8232 || ch === 8233) {
|
3042 | ++this.state.pos;
|
3043 | ++this.state.curLine;
|
3044 | this.state.lineStart = this.state.pos;
|
3045 | } else if (isNewLine(ch)) {
|
3046 | throw this.raise(Errors.UnterminatedString, {
|
3047 | at: this.state.startLoc
|
3048 | });
|
3049 | } else {
|
3050 | ++this.state.pos;
|
3051 | }
|
3052 | }
|
3053 |
|
3054 | out += this.input.slice(chunkStart, this.state.pos++);
|
3055 | this.finishToken(129, out);
|
3056 | }
|
3057 |
|
3058 | readTemplateContinuation() {
|
3059 | if (!this.match(8)) {
|
3060 | this.unexpected(null, 8);
|
3061 | }
|
3062 |
|
3063 | this.state.pos--;
|
3064 | this.readTemplateToken();
|
3065 | }
|
3066 |
|
3067 | readTemplateToken() {
|
3068 | let out = "",
|
3069 | chunkStart = this.state.pos,
|
3070 | containsInvalid = false;
|
3071 | ++this.state.pos;
|
3072 |
|
3073 | for (;;) {
|
3074 | if (this.state.pos >= this.length) {
|
3075 | throw this.raise(Errors.UnterminatedTemplate, {
|
3076 | at: createPositionWithColumnOffset(this.state.startLoc, 1)
|
3077 | });
|
3078 | }
|
3079 |
|
3080 | const ch = this.input.charCodeAt(this.state.pos);
|
3081 |
|
3082 | if (ch === 96) {
|
3083 | ++this.state.pos;
|
3084 | out += this.input.slice(chunkStart, this.state.pos);
|
3085 | this.finishToken(24, containsInvalid ? null : out);
|
3086 | return;
|
3087 | }
|
3088 |
|
3089 | if (ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
|
3090 | this.state.pos += 2;
|
3091 | out += this.input.slice(chunkStart, this.state.pos);
|
3092 | this.finishToken(25, containsInvalid ? null : out);
|
3093 | return;
|
3094 | }
|
3095 |
|
3096 | if (ch === 92) {
|
3097 | out += this.input.slice(chunkStart, this.state.pos);
|
3098 | const escaped = this.readEscapedChar(true);
|
3099 |
|
3100 | if (escaped === null) {
|
3101 | containsInvalid = true;
|
3102 | } else {
|
3103 | out += escaped;
|
3104 | }
|
3105 |
|
3106 | chunkStart = this.state.pos;
|
3107 | } else if (isNewLine(ch)) {
|
3108 | out += this.input.slice(chunkStart, this.state.pos);
|
3109 | ++this.state.pos;
|
3110 |
|
3111 | switch (ch) {
|
3112 | case 13:
|
3113 | if (this.input.charCodeAt(this.state.pos) === 10) {
|
3114 | ++this.state.pos;
|
3115 | }
|
3116 |
|
3117 | case 10:
|
3118 | out += "\n";
|
3119 | break;
|
3120 |
|
3121 | default:
|
3122 | out += String.fromCharCode(ch);
|
3123 | break;
|
3124 | }
|
3125 |
|
3126 | ++this.state.curLine;
|
3127 | this.state.lineStart = this.state.pos;
|
3128 | chunkStart = this.state.pos;
|
3129 | } else {
|
3130 | ++this.state.pos;
|
3131 | }
|
3132 | }
|
3133 | }
|
3134 |
|
3135 | recordStrictModeErrors(toParseError, {
|
3136 | at
|
3137 | }) {
|
3138 | const index = at.index;
|
3139 |
|
3140 | if (this.state.strict && !this.state.strictErrors.has(index)) {
|
3141 | this.raise(toParseError, {
|
3142 | at
|
3143 | });
|
3144 | } else {
|
3145 | this.state.strictErrors.set(index, [toParseError, at]);
|
3146 | }
|
3147 | }
|
3148 |
|
3149 | readEscapedChar(inTemplate) {
|
3150 | const throwOnInvalid = !inTemplate;
|
3151 | const ch = this.input.charCodeAt(++this.state.pos);
|
3152 | ++this.state.pos;
|
3153 |
|
3154 | switch (ch) {
|
3155 | case 110:
|
3156 | return "\n";
|
3157 |
|
3158 | case 114:
|
3159 | return "\r";
|
3160 |
|
3161 | case 120:
|
3162 | {
|
3163 | const code = this.readHexChar(2, false, throwOnInvalid);
|
3164 | return code === null ? null : String.fromCharCode(code);
|
3165 | }
|
3166 |
|
3167 | case 117:
|
3168 | {
|
3169 | const code = this.readCodePoint(throwOnInvalid);
|
3170 | return code === null ? null : String.fromCodePoint(code);
|
3171 | }
|
3172 |
|
3173 | case 116:
|
3174 | return "\t";
|
3175 |
|
3176 | case 98:
|
3177 | return "\b";
|
3178 |
|
3179 | case 118:
|
3180 | return "\u000b";
|
3181 |
|
3182 | case 102:
|
3183 | return "\f";
|
3184 |
|
3185 | case 13:
|
3186 | if (this.input.charCodeAt(this.state.pos) === 10) {
|
3187 | ++this.state.pos;
|
3188 | }
|
3189 |
|
3190 | case 10:
|
3191 | this.state.lineStart = this.state.pos;
|
3192 | ++this.state.curLine;
|
3193 |
|
3194 | case 8232:
|
3195 | case 8233:
|
3196 | return "";
|
3197 |
|
3198 | case 56:
|
3199 | case 57:
|
3200 | if (inTemplate) {
|
3201 | return null;
|
3202 | } else {
|
3203 | this.recordStrictModeErrors(Errors.StrictNumericEscape, {
|
3204 | at: createPositionWithColumnOffset(this.state.curPosition(), -1)
|
3205 | });
|
3206 | }
|
3207 |
|
3208 | default:
|
3209 | if (ch >= 48 && ch <= 55) {
|
3210 | const codePos = createPositionWithColumnOffset(this.state.curPosition(), -1);
|
3211 | const match = this.input.slice(this.state.pos - 1, this.state.pos + 2).match(/^[0-7]+/);
|
3212 | let octalStr = match[0];
|
3213 | let octal = parseInt(octalStr, 8);
|
3214 |
|
3215 | if (octal > 255) {
|
3216 | octalStr = octalStr.slice(0, -1);
|
3217 | octal = parseInt(octalStr, 8);
|
3218 | }
|
3219 |
|
3220 | this.state.pos += octalStr.length - 1;
|
3221 | const next = this.input.charCodeAt(this.state.pos);
|
3222 |
|
3223 | if (octalStr !== "0" || next === 56 || next === 57) {
|
3224 | if (inTemplate) {
|
3225 | return null;
|
3226 | } else {
|
3227 | this.recordStrictModeErrors(Errors.StrictNumericEscape, {
|
3228 | at: codePos
|
3229 | });
|
3230 | }
|
3231 | }
|
3232 |
|
3233 | return String.fromCharCode(octal);
|
3234 | }
|
3235 |
|
3236 | return String.fromCharCode(ch);
|
3237 | }
|
3238 | }
|
3239 |
|
3240 | readHexChar(len, forceLen, throwOnInvalid) {
|
3241 | const codeLoc = this.state.curPosition();
|
3242 | const n = this.readInt(16, len, forceLen, false);
|
3243 |
|
3244 | if (n === null) {
|
3245 | if (throwOnInvalid) {
|
3246 | this.raise(Errors.InvalidEscapeSequence, {
|
3247 | at: codeLoc
|
3248 | });
|
3249 | } else {
|
3250 | this.state.pos = codeLoc.index - 1;
|
3251 | }
|
3252 | }
|
3253 |
|
3254 | return n;
|
3255 | }
|
3256 |
|
3257 | readWord1(firstCode) {
|
3258 | this.state.containsEsc = false;
|
3259 | let word = "";
|
3260 | const start = this.state.pos;
|
3261 | let chunkStart = this.state.pos;
|
3262 |
|
3263 | if (firstCode !== undefined) {
|
3264 | this.state.pos += firstCode <= 0xffff ? 1 : 2;
|
3265 | }
|
3266 |
|
3267 | while (this.state.pos < this.length) {
|
3268 | const ch = this.codePointAtPos(this.state.pos);
|
3269 |
|
3270 | if (isIdentifierChar(ch)) {
|
3271 | this.state.pos += ch <= 0xffff ? 1 : 2;
|
3272 | } else if (ch === 92) {
|
3273 | this.state.containsEsc = true;
|
3274 | word += this.input.slice(chunkStart, this.state.pos);
|
3275 | const escStart = this.state.curPosition();
|
3276 | const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
|
3277 |
|
3278 | if (this.input.charCodeAt(++this.state.pos) !== 117) {
|
3279 | this.raise(Errors.MissingUnicodeEscape, {
|
3280 | at: this.state.curPosition()
|
3281 | });
|
3282 | chunkStart = this.state.pos - 1;
|
3283 | continue;
|
3284 | }
|
3285 |
|
3286 | ++this.state.pos;
|
3287 | const esc = this.readCodePoint(true);
|
3288 |
|
3289 | if (esc !== null) {
|
3290 | if (!identifierCheck(esc)) {
|
3291 | this.raise(Errors.EscapedCharNotAnIdentifier, {
|
3292 | at: escStart
|
3293 | });
|
3294 | }
|
3295 |
|
3296 | word += String.fromCodePoint(esc);
|
3297 | }
|
3298 |
|
3299 | chunkStart = this.state.pos;
|
3300 | } else {
|
3301 | break;
|
3302 | }
|
3303 | }
|
3304 |
|
3305 | return word + this.input.slice(chunkStart, this.state.pos);
|
3306 | }
|
3307 |
|
3308 | readWord(firstCode) {
|
3309 | const word = this.readWord1(firstCode);
|
3310 | const type = keywords$1.get(word);
|
3311 |
|
3312 | if (type !== undefined) {
|
3313 | this.finishToken(type, tokenLabelName(type));
|
3314 | } else {
|
3315 | this.finishToken(128, word);
|
3316 | }
|
3317 | }
|
3318 |
|
3319 | checkKeywordEscapes() {
|
3320 | const {
|
3321 | type
|
3322 | } = this.state;
|
3323 |
|
3324 | if (tokenIsKeyword(type) && this.state.containsEsc) {
|
3325 | this.raise(Errors.InvalidEscapedReservedWord, {
|
3326 | at: this.state.startLoc,
|
3327 | reservedWord: tokenLabelName(type)
|
3328 | });
|
3329 | }
|
3330 | }
|
3331 |
|
3332 | raise(toParseError, raiseProperties) {
|
3333 | const {
|
3334 | at
|
3335 | } = raiseProperties,
|
3336 | details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
|
3337 |
|
3338 | const loc = at instanceof Position ? at : at.loc.start;
|
3339 | const error = toParseError({
|
3340 | loc,
|
3341 | details
|
3342 | });
|
3343 | if (!this.options.errorRecovery) throw error;
|
3344 | if (!this.isLookahead) this.state.errors.push(error);
|
3345 | return error;
|
3346 | }
|
3347 |
|
3348 | raiseOverwrite(toParseError, raiseProperties) {
|
3349 | const {
|
3350 | at
|
3351 | } = raiseProperties,
|
3352 | details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
|
3353 |
|
3354 | const loc = at instanceof Position ? at : at.loc.start;
|
3355 | const pos = loc.index;
|
3356 | const errors = this.state.errors;
|
3357 |
|
3358 | for (let i = errors.length - 1; i >= 0; i--) {
|
3359 | const error = errors[i];
|
3360 |
|
3361 | if (error.loc.index === pos) {
|
3362 | return errors[i] = toParseError({
|
3363 | loc,
|
3364 | details
|
3365 | });
|
3366 | }
|
3367 |
|
3368 | if (error.loc.index < pos) break;
|
3369 | }
|
3370 |
|
3371 | return this.raise(toParseError, raiseProperties);
|
3372 | }
|
3373 |
|
3374 | updateContext(prevType) {}
|
3375 |
|
3376 | unexpected(loc, type) {
|
3377 | throw this.raise(Errors.UnexpectedToken, {
|
3378 | expected: type ? tokenLabelName(type) : null,
|
3379 | at: loc != null ? loc : this.state.startLoc
|
3380 | });
|
3381 | }
|
3382 |
|
3383 | expectPlugin(pluginName, loc) {
|
3384 | if (this.hasPlugin(pluginName)) {
|
3385 | return true;
|
3386 | }
|
3387 |
|
3388 | throw this.raise(Errors.MissingPlugin, {
|
3389 | at: loc != null ? loc : this.state.startLoc,
|
3390 | missingPlugin: [pluginName]
|
3391 | });
|
3392 | }
|
3393 |
|
3394 | expectOnePlugin(pluginNames) {
|
3395 | if (!pluginNames.some(name => this.hasPlugin(name))) {
|
3396 | throw this.raise(Errors.MissingOneOfPlugins, {
|
3397 | at: this.state.startLoc,
|
3398 | missingPlugin: pluginNames
|
3399 | });
|
3400 | }
|
3401 | }
|
3402 |
|
3403 | }
|
3404 |
|
3405 | class Scope {
|
3406 | constructor(flags) {
|
3407 | this.var = new Set();
|
3408 | this.lexical = new Set();
|
3409 | this.functions = new Set();
|
3410 | this.flags = flags;
|
3411 | }
|
3412 |
|
3413 | }
|
3414 | class ScopeHandler {
|
3415 | constructor(parser, inModule) {
|
3416 | this.parser = void 0;
|
3417 | this.scopeStack = [];
|
3418 | this.inModule = void 0;
|
3419 | this.undefinedExports = new Map();
|
3420 | this.parser = parser;
|
3421 | this.inModule = inModule;
|
3422 | }
|
3423 |
|
3424 | get inFunction() {
|
3425 | return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
|
3426 | }
|
3427 |
|
3428 | get allowSuper() {
|
3429 | return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
|
3430 | }
|
3431 |
|
3432 | get allowDirectSuper() {
|
3433 | return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
|
3434 | }
|
3435 |
|
3436 | get inClass() {
|
3437 | return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
|
3438 | }
|
3439 |
|
3440 | get inClassAndNotInNonArrowFunction() {
|
3441 | const flags = this.currentThisScopeFlags();
|
3442 | return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
|
3443 | }
|
3444 |
|
3445 | get inStaticBlock() {
|
3446 | for (let i = this.scopeStack.length - 1;; i--) {
|
3447 | const {
|
3448 | flags
|
3449 | } = this.scopeStack[i];
|
3450 |
|
3451 | if (flags & SCOPE_STATIC_BLOCK) {
|
3452 | return true;
|
3453 | }
|
3454 |
|
3455 | if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
|
3456 | return false;
|
3457 | }
|
3458 | }
|
3459 | }
|
3460 |
|
3461 | get inNonArrowFunction() {
|
3462 | return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
|
3463 | }
|
3464 |
|
3465 | get treatFunctionsAsVar() {
|
3466 | return this.treatFunctionsAsVarInScope(this.currentScope());
|
3467 | }
|
3468 |
|
3469 | createScope(flags) {
|
3470 | return new Scope(flags);
|
3471 | }
|
3472 |
|
3473 | enter(flags) {
|
3474 | this.scopeStack.push(this.createScope(flags));
|
3475 | }
|
3476 |
|
3477 | exit() {
|
3478 | this.scopeStack.pop();
|
3479 | }
|
3480 |
|
3481 | treatFunctionsAsVarInScope(scope) {
|
3482 | return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
|
3483 | }
|
3484 |
|
3485 | declareName(name, bindingType, loc) {
|
3486 | let scope = this.currentScope();
|
3487 |
|
3488 | if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
|
3489 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
3490 |
|
3491 | if (bindingType & BIND_SCOPE_FUNCTION) {
|
3492 | scope.functions.add(name);
|
3493 | } else {
|
3494 | scope.lexical.add(name);
|
3495 | }
|
3496 |
|
3497 | if (bindingType & BIND_SCOPE_LEXICAL) {
|
3498 | this.maybeExportDefined(scope, name);
|
3499 | }
|
3500 | } else if (bindingType & BIND_SCOPE_VAR) {
|
3501 | for (let i = this.scopeStack.length - 1; i >= 0; --i) {
|
3502 | scope = this.scopeStack[i];
|
3503 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
3504 | scope.var.add(name);
|
3505 | this.maybeExportDefined(scope, name);
|
3506 | if (scope.flags & SCOPE_VAR) break;
|
3507 | }
|
3508 | }
|
3509 |
|
3510 | if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
|
3511 | this.undefinedExports.delete(name);
|
3512 | }
|
3513 | }
|
3514 |
|
3515 | maybeExportDefined(scope, name) {
|
3516 | if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
|
3517 | this.undefinedExports.delete(name);
|
3518 | }
|
3519 | }
|
3520 |
|
3521 | checkRedeclarationInScope(scope, name, bindingType, loc) {
|
3522 | if (this.isRedeclaredInScope(scope, name, bindingType)) {
|
3523 | this.parser.raise(Errors.VarRedeclaration, {
|
3524 | at: loc,
|
3525 | identifierName: name
|
3526 | });
|
3527 | }
|
3528 | }
|
3529 |
|
3530 | isRedeclaredInScope(scope, name, bindingType) {
|
3531 | if (!(bindingType & BIND_KIND_VALUE)) return false;
|
3532 |
|
3533 | if (bindingType & BIND_SCOPE_LEXICAL) {
|
3534 | return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
|
3535 | }
|
3536 |
|
3537 | if (bindingType & BIND_SCOPE_FUNCTION) {
|
3538 | return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
|
3539 | }
|
3540 |
|
3541 | return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
|
3542 | }
|
3543 |
|
3544 | checkLocalExport(id) {
|
3545 | const {
|
3546 | name
|
3547 | } = id;
|
3548 | const topLevelScope = this.scopeStack[0];
|
3549 |
|
3550 | if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
|
3551 | this.undefinedExports.set(name, id.loc.start);
|
3552 | }
|
3553 | }
|
3554 |
|
3555 | currentScope() {
|
3556 | return this.scopeStack[this.scopeStack.length - 1];
|
3557 | }
|
3558 |
|
3559 | currentVarScopeFlags() {
|
3560 | for (let i = this.scopeStack.length - 1;; i--) {
|
3561 | const {
|
3562 | flags
|
3563 | } = this.scopeStack[i];
|
3564 |
|
3565 | if (flags & SCOPE_VAR) {
|
3566 | return flags;
|
3567 | }
|
3568 | }
|
3569 | }
|
3570 |
|
3571 | currentThisScopeFlags() {
|
3572 | for (let i = this.scopeStack.length - 1;; i--) {
|
3573 | const {
|
3574 | flags
|
3575 | } = this.scopeStack[i];
|
3576 |
|
3577 | if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
|
3578 | return flags;
|
3579 | }
|
3580 | }
|
3581 | }
|
3582 |
|
3583 | }
|
3584 |
|
3585 | class FlowScope extends Scope {
|
3586 | constructor(...args) {
|
3587 | super(...args);
|
3588 | this.declareFunctions = new Set();
|
3589 | }
|
3590 |
|
3591 | }
|
3592 |
|
3593 | class FlowScopeHandler extends ScopeHandler {
|
3594 | createScope(flags) {
|
3595 | return new FlowScope(flags);
|
3596 | }
|
3597 |
|
3598 | declareName(name, bindingType, loc) {
|
3599 | const scope = this.currentScope();
|
3600 |
|
3601 | if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
|
3602 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
3603 | this.maybeExportDefined(scope, name);
|
3604 | scope.declareFunctions.add(name);
|
3605 | return;
|
3606 | }
|
3607 |
|
3608 | super.declareName(...arguments);
|
3609 | }
|
3610 |
|
3611 | isRedeclaredInScope(scope, name, bindingType) {
|
3612 | if (super.isRedeclaredInScope(...arguments)) return true;
|
3613 |
|
3614 | if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
|
3615 | return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
|
3616 | }
|
3617 |
|
3618 | return false;
|
3619 | }
|
3620 |
|
3621 | checkLocalExport(id) {
|
3622 | if (!this.scopeStack[0].declareFunctions.has(id.name)) {
|
3623 | super.checkLocalExport(id);
|
3624 | }
|
3625 | }
|
3626 |
|
3627 | }
|
3628 |
|
3629 | class ClassScope {
|
3630 | constructor() {
|
3631 | this.privateNames = new Set();
|
3632 | this.loneAccessors = new Map();
|
3633 | this.undefinedPrivateNames = new Map();
|
3634 | }
|
3635 |
|
3636 | }
|
3637 | class ClassScopeHandler {
|
3638 | constructor(parser) {
|
3639 | this.parser = void 0;
|
3640 | this.stack = [];
|
3641 | this.undefinedPrivateNames = new Map();
|
3642 | this.parser = parser;
|
3643 | }
|
3644 |
|
3645 | current() {
|
3646 | return this.stack[this.stack.length - 1];
|
3647 | }
|
3648 |
|
3649 | enter() {
|
3650 | this.stack.push(new ClassScope());
|
3651 | }
|
3652 |
|
3653 | exit() {
|
3654 | const oldClassScope = this.stack.pop();
|
3655 | const current = this.current();
|
3656 |
|
3657 | for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
|
3658 | if (current) {
|
3659 | if (!current.undefinedPrivateNames.has(name)) {
|
3660 | current.undefinedPrivateNames.set(name, loc);
|
3661 | }
|
3662 | } else {
|
3663 | this.parser.raise(Errors.InvalidPrivateFieldResolution, {
|
3664 | at: loc,
|
3665 | identifierName: name
|
3666 | });
|
3667 | }
|
3668 | }
|
3669 | }
|
3670 |
|
3671 | declarePrivateName(name, elementType, loc) {
|
3672 | const {
|
3673 | privateNames,
|
3674 | loneAccessors,
|
3675 | undefinedPrivateNames
|
3676 | } = this.current();
|
3677 | let redefined = privateNames.has(name);
|
3678 |
|
3679 | if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
|
3680 | const accessor = redefined && loneAccessors.get(name);
|
3681 |
|
3682 | if (accessor) {
|
3683 | const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
|
3684 | const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
|
3685 | const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
|
3686 | const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
|
3687 | redefined = oldKind === newKind || oldStatic !== newStatic;
|
3688 | if (!redefined) loneAccessors.delete(name);
|
3689 | } else if (!redefined) {
|
3690 | loneAccessors.set(name, elementType);
|
3691 | }
|
3692 | }
|
3693 |
|
3694 | if (redefined) {
|
3695 | this.parser.raise(Errors.PrivateNameRedeclaration, {
|
3696 | at: loc,
|
3697 | identifierName: name
|
3698 | });
|
3699 | }
|
3700 |
|
3701 | privateNames.add(name);
|
3702 | undefinedPrivateNames.delete(name);
|
3703 | }
|
3704 |
|
3705 | usePrivateName(name, loc) {
|
3706 | let classScope;
|
3707 |
|
3708 | for (classScope of this.stack) {
|
3709 | if (classScope.privateNames.has(name)) return;
|
3710 | }
|
3711 |
|
3712 | if (classScope) {
|
3713 | classScope.undefinedPrivateNames.set(name, loc);
|
3714 | } else {
|
3715 | this.parser.raise(Errors.InvalidPrivateFieldResolution, {
|
3716 | at: loc,
|
3717 | identifierName: name
|
3718 | });
|
3719 | }
|
3720 | }
|
3721 |
|
3722 | }
|
3723 |
|
3724 | const kExpression = 0,
|
3725 | kMaybeArrowParameterDeclaration = 1,
|
3726 | kMaybeAsyncArrowParameterDeclaration = 2,
|
3727 | kParameterDeclaration = 3;
|
3728 |
|
3729 | class ExpressionScope {
|
3730 | constructor(type = kExpression) {
|
3731 | this.type = void 0;
|
3732 | this.type = type;
|
3733 | }
|
3734 |
|
3735 | canBeArrowParameterDeclaration() {
|
3736 | return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
|
3737 | }
|
3738 |
|
3739 | isCertainlyParameterDeclaration() {
|
3740 | return this.type === kParameterDeclaration;
|
3741 | }
|
3742 |
|
3743 | }
|
3744 |
|
3745 | class ArrowHeadParsingScope extends ExpressionScope {
|
3746 | constructor(type) {
|
3747 | super(type);
|
3748 | this.declarationErrors = new Map();
|
3749 | }
|
3750 |
|
3751 | recordDeclarationError(ParsingErrorClass, {
|
3752 | at
|
3753 | }) {
|
3754 | const index = at.index;
|
3755 | this.declarationErrors.set(index, [ParsingErrorClass, at]);
|
3756 | }
|
3757 |
|
3758 | clearDeclarationError(index) {
|
3759 | this.declarationErrors.delete(index);
|
3760 | }
|
3761 |
|
3762 | iterateErrors(iterator) {
|
3763 | this.declarationErrors.forEach(iterator);
|
3764 | }
|
3765 |
|
3766 | }
|
3767 |
|
3768 | class ExpressionScopeHandler {
|
3769 | constructor(parser) {
|
3770 | this.parser = void 0;
|
3771 | this.stack = [new ExpressionScope()];
|
3772 | this.parser = parser;
|
3773 | }
|
3774 |
|
3775 | enter(scope) {
|
3776 | this.stack.push(scope);
|
3777 | }
|
3778 |
|
3779 | exit() {
|
3780 | this.stack.pop();
|
3781 | }
|
3782 |
|
3783 | recordParameterInitializerError(toParseError, {
|
3784 | at: node
|
3785 | }) {
|
3786 | const origin = {
|
3787 | at: node.loc.start
|
3788 | };
|
3789 | const {
|
3790 | stack
|
3791 | } = this;
|
3792 | let i = stack.length - 1;
|
3793 | let scope = stack[i];
|
3794 |
|
3795 | while (!scope.isCertainlyParameterDeclaration()) {
|
3796 | if (scope.canBeArrowParameterDeclaration()) {
|
3797 | scope.recordDeclarationError(toParseError, origin);
|
3798 | } else {
|
3799 | return;
|
3800 | }
|
3801 |
|
3802 | scope = stack[--i];
|
3803 | }
|
3804 |
|
3805 | this.parser.raise(toParseError, origin);
|
3806 | }
|
3807 |
|
3808 | recordArrowParemeterBindingError(error, {
|
3809 | at: node
|
3810 | }) {
|
3811 | const {
|
3812 | stack
|
3813 | } = this;
|
3814 | const scope = stack[stack.length - 1];
|
3815 | const origin = {
|
3816 | at: node.loc.start
|
3817 | };
|
3818 |
|
3819 | if (scope.isCertainlyParameterDeclaration()) {
|
3820 | this.parser.raise(error, origin);
|
3821 | } else if (scope.canBeArrowParameterDeclaration()) {
|
3822 | scope.recordDeclarationError(error, origin);
|
3823 | } else {
|
3824 | return;
|
3825 | }
|
3826 | }
|
3827 |
|
3828 | recordAsyncArrowParametersError({
|
3829 | at
|
3830 | }) {
|
3831 | const {
|
3832 | stack
|
3833 | } = this;
|
3834 | let i = stack.length - 1;
|
3835 | let scope = stack[i];
|
3836 |
|
3837 | while (scope.canBeArrowParameterDeclaration()) {
|
3838 | if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
|
3839 | scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
|
3840 | at
|
3841 | });
|
3842 | }
|
3843 |
|
3844 | scope = stack[--i];
|
3845 | }
|
3846 | }
|
3847 |
|
3848 | validateAsPattern() {
|
3849 | const {
|
3850 | stack
|
3851 | } = this;
|
3852 | const currentScope = stack[stack.length - 1];
|
3853 | if (!currentScope.canBeArrowParameterDeclaration()) return;
|
3854 | currentScope.iterateErrors(([toParseError, loc]) => {
|
3855 | this.parser.raise(toParseError, {
|
3856 | at: loc
|
3857 | });
|
3858 | let i = stack.length - 2;
|
3859 | let scope = stack[i];
|
3860 |
|
3861 | while (scope.canBeArrowParameterDeclaration()) {
|
3862 | scope.clearDeclarationError(loc.index);
|
3863 | scope = stack[--i];
|
3864 | }
|
3865 | });
|
3866 | }
|
3867 |
|
3868 | }
|
3869 | function newParameterDeclarationScope() {
|
3870 | return new ExpressionScope(kParameterDeclaration);
|
3871 | }
|
3872 | function newArrowHeadScope() {
|
3873 | return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
|
3874 | }
|
3875 | function newAsyncArrowScope() {
|
3876 | return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
|
3877 | }
|
3878 | function newExpressionScope() {
|
3879 | return new ExpressionScope();
|
3880 | }
|
3881 |
|
3882 | const PARAM = 0b0000,
|
3883 | PARAM_YIELD = 0b0001,
|
3884 | PARAM_AWAIT = 0b0010,
|
3885 | PARAM_RETURN = 0b0100,
|
3886 | PARAM_IN = 0b1000;
|
3887 | class ProductionParameterHandler {
|
3888 | constructor() {
|
3889 | this.stacks = [];
|
3890 | }
|
3891 |
|
3892 | enter(flags) {
|
3893 | this.stacks.push(flags);
|
3894 | }
|
3895 |
|
3896 | exit() {
|
3897 | this.stacks.pop();
|
3898 | }
|
3899 |
|
3900 | currentFlags() {
|
3901 | return this.stacks[this.stacks.length - 1];
|
3902 | }
|
3903 |
|
3904 | get hasAwait() {
|
3905 | return (this.currentFlags() & PARAM_AWAIT) > 0;
|
3906 | }
|
3907 |
|
3908 | get hasYield() {
|
3909 | return (this.currentFlags() & PARAM_YIELD) > 0;
|
3910 | }
|
3911 |
|
3912 | get hasReturn() {
|
3913 | return (this.currentFlags() & PARAM_RETURN) > 0;
|
3914 | }
|
3915 |
|
3916 | get hasIn() {
|
3917 | return (this.currentFlags() & PARAM_IN) > 0;
|
3918 | }
|
3919 |
|
3920 | }
|
3921 | function functionFlags(isAsync, isGenerator) {
|
3922 | return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
|
3923 | }
|
3924 |
|
3925 | class UtilParser extends Tokenizer {
|
3926 | addExtra(node, key, value, enumerable = true) {
|
3927 | if (!node) return;
|
3928 | const extra = node.extra = node.extra || {};
|
3929 |
|
3930 | if (enumerable) {
|
3931 | extra[key] = value;
|
3932 | } else {
|
3933 | Object.defineProperty(extra, key, {
|
3934 | enumerable,
|
3935 | value
|
3936 | });
|
3937 | }
|
3938 | }
|
3939 |
|
3940 | isContextual(token) {
|
3941 | return this.state.type === token && !this.state.containsEsc;
|
3942 | }
|
3943 |
|
3944 | isUnparsedContextual(nameStart, name) {
|
3945 | const nameEnd = nameStart + name.length;
|
3946 |
|
3947 | if (this.input.slice(nameStart, nameEnd) === name) {
|
3948 | const nextCh = this.input.charCodeAt(nameEnd);
|
3949 | return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
|
3950 | }
|
3951 |
|
3952 | return false;
|
3953 | }
|
3954 |
|
3955 | isLookaheadContextual(name) {
|
3956 | const next = this.nextTokenStart();
|
3957 | return this.isUnparsedContextual(next, name);
|
3958 | }
|
3959 |
|
3960 | eatContextual(token) {
|
3961 | if (this.isContextual(token)) {
|
3962 | this.next();
|
3963 | return true;
|
3964 | }
|
3965 |
|
3966 | return false;
|
3967 | }
|
3968 |
|
3969 | expectContextual(token, toParseError) {
|
3970 | if (!this.eatContextual(token)) {
|
3971 | if (toParseError != null) {
|
3972 | throw this.raise(toParseError, {
|
3973 | at: this.state.startLoc
|
3974 | });
|
3975 | }
|
3976 |
|
3977 | throw this.unexpected(null, token);
|
3978 | }
|
3979 | }
|
3980 |
|
3981 | canInsertSemicolon() {
|
3982 | return this.match(135) || this.match(8) || this.hasPrecedingLineBreak();
|
3983 | }
|
3984 |
|
3985 | hasPrecedingLineBreak() {
|
3986 | return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
|
3987 | }
|
3988 |
|
3989 | hasFollowingLineBreak() {
|
3990 | skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
|
3991 | return skipWhiteSpaceToLineBreak.test(this.input);
|
3992 | }
|
3993 |
|
3994 | isLineTerminator() {
|
3995 | return this.eat(13) || this.canInsertSemicolon();
|
3996 | }
|
3997 |
|
3998 | semicolon(allowAsi = true) {
|
3999 | if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
|
4000 | this.raise(Errors.MissingSemicolon, {
|
4001 | at: this.state.lastTokEndLoc
|
4002 | });
|
4003 | }
|
4004 |
|
4005 | expect(type, loc) {
|
4006 | this.eat(type) || this.unexpected(loc, type);
|
4007 | }
|
4008 |
|
4009 | tryParse(fn, oldState = this.state.clone()) {
|
4010 | const abortSignal = {
|
4011 | node: null
|
4012 | };
|
4013 |
|
4014 | try {
|
4015 | const node = fn((node = null) => {
|
4016 | abortSignal.node = node;
|
4017 | throw abortSignal;
|
4018 | });
|
4019 |
|
4020 | if (this.state.errors.length > oldState.errors.length) {
|
4021 | const failState = this.state;
|
4022 | this.state = oldState;
|
4023 | this.state.tokensLength = failState.tokensLength;
|
4024 | return {
|
4025 | node,
|
4026 | error: failState.errors[oldState.errors.length],
|
4027 | thrown: false,
|
4028 | aborted: false,
|
4029 | failState
|
4030 | };
|
4031 | }
|
4032 |
|
4033 | return {
|
4034 | node,
|
4035 | error: null,
|
4036 | thrown: false,
|
4037 | aborted: false,
|
4038 | failState: null
|
4039 | };
|
4040 | } catch (error) {
|
4041 | const failState = this.state;
|
4042 | this.state = oldState;
|
4043 |
|
4044 | if (error instanceof SyntaxError) {
|
4045 | return {
|
4046 | node: null,
|
4047 | error,
|
4048 | thrown: true,
|
4049 | aborted: false,
|
4050 | failState
|
4051 | };
|
4052 | }
|
4053 |
|
4054 | if (error === abortSignal) {
|
4055 | return {
|
4056 | node: abortSignal.node,
|
4057 | error: null,
|
4058 | thrown: false,
|
4059 | aborted: true,
|
4060 | failState
|
4061 | };
|
4062 | }
|
4063 |
|
4064 | throw error;
|
4065 | }
|
4066 | }
|
4067 |
|
4068 | checkExpressionErrors(refExpressionErrors, andThrow) {
|
4069 | if (!refExpressionErrors) return false;
|
4070 | const {
|
4071 | shorthandAssignLoc,
|
4072 | doubleProtoLoc,
|
4073 | privateKeyLoc,
|
4074 | optionalParametersLoc
|
4075 | } = refExpressionErrors;
|
4076 | const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
|
4077 |
|
4078 | if (!andThrow) {
|
4079 | return hasErrors;
|
4080 | }
|
4081 |
|
4082 | if (shorthandAssignLoc != null) {
|
4083 | this.raise(Errors.InvalidCoverInitializedName, {
|
4084 | at: shorthandAssignLoc
|
4085 | });
|
4086 | }
|
4087 |
|
4088 | if (doubleProtoLoc != null) {
|
4089 | this.raise(Errors.DuplicateProto, {
|
4090 | at: doubleProtoLoc
|
4091 | });
|
4092 | }
|
4093 |
|
4094 | if (privateKeyLoc != null) {
|
4095 | this.raise(Errors.UnexpectedPrivateField, {
|
4096 | at: privateKeyLoc
|
4097 | });
|
4098 | }
|
4099 |
|
4100 | if (optionalParametersLoc != null) {
|
4101 | this.unexpected(optionalParametersLoc);
|
4102 | }
|
4103 | }
|
4104 |
|
4105 | isLiteralPropertyName() {
|
4106 | return tokenIsLiteralPropertyName(this.state.type);
|
4107 | }
|
4108 |
|
4109 | isPrivateName(node) {
|
4110 | return node.type === "PrivateName";
|
4111 | }
|
4112 |
|
4113 | getPrivateNameSV(node) {
|
4114 | return node.id.name;
|
4115 | }
|
4116 |
|
4117 | hasPropertyAsPrivateName(node) {
|
4118 | return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
|
4119 | }
|
4120 |
|
4121 | isOptionalChain(node) {
|
4122 | return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
|
4123 | }
|
4124 |
|
4125 | isObjectProperty(node) {
|
4126 | return node.type === "ObjectProperty";
|
4127 | }
|
4128 |
|
4129 | isObjectMethod(node) {
|
4130 | return node.type === "ObjectMethod";
|
4131 | }
|
4132 |
|
4133 | initializeScopes(inModule = this.options.sourceType === "module") {
|
4134 | const oldLabels = this.state.labels;
|
4135 | this.state.labels = [];
|
4136 | const oldExportedIdentifiers = this.exportedIdentifiers;
|
4137 | this.exportedIdentifiers = new Set();
|
4138 | const oldInModule = this.inModule;
|
4139 | this.inModule = inModule;
|
4140 | const oldScope = this.scope;
|
4141 | const ScopeHandler = this.getScopeHandler();
|
4142 | this.scope = new ScopeHandler(this, inModule);
|
4143 | const oldProdParam = this.prodParam;
|
4144 | this.prodParam = new ProductionParameterHandler();
|
4145 | const oldClassScope = this.classScope;
|
4146 | this.classScope = new ClassScopeHandler(this);
|
4147 | const oldExpressionScope = this.expressionScope;
|
4148 | this.expressionScope = new ExpressionScopeHandler(this);
|
4149 | return () => {
|
4150 | this.state.labels = oldLabels;
|
4151 | this.exportedIdentifiers = oldExportedIdentifiers;
|
4152 | this.inModule = oldInModule;
|
4153 | this.scope = oldScope;
|
4154 | this.prodParam = oldProdParam;
|
4155 | this.classScope = oldClassScope;
|
4156 | this.expressionScope = oldExpressionScope;
|
4157 | };
|
4158 | }
|
4159 |
|
4160 | enterInitialScopes() {
|
4161 | let paramFlags = PARAM;
|
4162 |
|
4163 | if (this.inModule) {
|
4164 | paramFlags |= PARAM_AWAIT;
|
4165 | }
|
4166 |
|
4167 | this.scope.enter(SCOPE_PROGRAM);
|
4168 | this.prodParam.enter(paramFlags);
|
4169 | }
|
4170 |
|
4171 | checkDestructuringPrivate(refExpressionErrors) {
|
4172 | const {
|
4173 | privateKeyLoc
|
4174 | } = refExpressionErrors;
|
4175 |
|
4176 | if (privateKeyLoc !== null) {
|
4177 | this.expectPlugin("destructuringPrivate", privateKeyLoc);
|
4178 | }
|
4179 | }
|
4180 |
|
4181 | }
|
4182 | class ExpressionErrors {
|
4183 | constructor() {
|
4184 | this.shorthandAssignLoc = null;
|
4185 | this.doubleProtoLoc = null;
|
4186 | this.privateKeyLoc = null;
|
4187 | this.optionalParametersLoc = null;
|
4188 | }
|
4189 |
|
4190 | }
|
4191 |
|
4192 | class Node {
|
4193 | constructor(parser, pos, loc) {
|
4194 | this.type = "";
|
4195 | this.start = pos;
|
4196 | this.end = 0;
|
4197 | this.loc = new SourceLocation(loc);
|
4198 | if (parser != null && parser.options.ranges) this.range = [pos, 0];
|
4199 | if (parser != null && parser.filename) this.loc.filename = parser.filename;
|
4200 | }
|
4201 |
|
4202 | }
|
4203 |
|
4204 | const NodePrototype = Node.prototype;
|
4205 | {
|
4206 | NodePrototype.__clone = function () {
|
4207 | const newNode = new Node();
|
4208 | const keys = Object.keys(this);
|
4209 |
|
4210 | for (let i = 0, length = keys.length; i < length; i++) {
|
4211 | const key = keys[i];
|
4212 |
|
4213 | if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
|
4214 | newNode[key] = this[key];
|
4215 | }
|
4216 | }
|
4217 |
|
4218 | return newNode;
|
4219 | };
|
4220 | }
|
4221 |
|
4222 | function clonePlaceholder(node) {
|
4223 | return cloneIdentifier(node);
|
4224 | }
|
4225 |
|
4226 | function cloneIdentifier(node) {
|
4227 | const {
|
4228 | type,
|
4229 | start,
|
4230 | end,
|
4231 | loc,
|
4232 | range,
|
4233 | extra,
|
4234 | name
|
4235 | } = node;
|
4236 | const cloned = Object.create(NodePrototype);
|
4237 | cloned.type = type;
|
4238 | cloned.start = start;
|
4239 | cloned.end = end;
|
4240 | cloned.loc = loc;
|
4241 | cloned.range = range;
|
4242 | cloned.extra = extra;
|
4243 | cloned.name = name;
|
4244 |
|
4245 | if (type === "Placeholder") {
|
4246 | cloned.expectedNode = node.expectedNode;
|
4247 | }
|
4248 |
|
4249 | return cloned;
|
4250 | }
|
4251 | function cloneStringLiteral(node) {
|
4252 | const {
|
4253 | type,
|
4254 | start,
|
4255 | end,
|
4256 | loc,
|
4257 | range,
|
4258 | extra
|
4259 | } = node;
|
4260 |
|
4261 | if (type === "Placeholder") {
|
4262 | return clonePlaceholder(node);
|
4263 | }
|
4264 |
|
4265 | const cloned = Object.create(NodePrototype);
|
4266 | cloned.type = type;
|
4267 | cloned.start = start;
|
4268 | cloned.end = end;
|
4269 | cloned.loc = loc;
|
4270 | cloned.range = range;
|
4271 |
|
4272 | if (node.raw !== undefined) {
|
4273 | cloned.raw = node.raw;
|
4274 | } else {
|
4275 | cloned.extra = extra;
|
4276 | }
|
4277 |
|
4278 | cloned.value = node.value;
|
4279 | return cloned;
|
4280 | }
|
4281 | class NodeUtils extends UtilParser {
|
4282 | startNode() {
|
4283 | return new Node(this, this.state.start, this.state.startLoc);
|
4284 | }
|
4285 |
|
4286 | startNodeAt(pos, loc) {
|
4287 | return new Node(this, pos, loc);
|
4288 | }
|
4289 |
|
4290 | startNodeAtNode(type) {
|
4291 | return this.startNodeAt(type.start, type.loc.start);
|
4292 | }
|
4293 |
|
4294 | finishNode(node, type) {
|
4295 | return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
|
4296 | }
|
4297 |
|
4298 | finishNodeAt(node, type, endLoc) {
|
4299 |
|
4300 | node.type = type;
|
4301 | node.end = endLoc.index;
|
4302 | node.loc.end = endLoc;
|
4303 | if (this.options.ranges) node.range[1] = endLoc.index;
|
4304 | if (this.options.attachComment) this.processComment(node);
|
4305 | return node;
|
4306 | }
|
4307 |
|
4308 | resetStartLocation(node, start, startLoc) {
|
4309 | node.start = start;
|
4310 | node.loc.start = startLoc;
|
4311 | if (this.options.ranges) node.range[0] = start;
|
4312 | }
|
4313 |
|
4314 | resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
|
4315 | node.end = endLoc.index;
|
4316 | node.loc.end = endLoc;
|
4317 | if (this.options.ranges) node.range[1] = endLoc.index;
|
4318 | }
|
4319 |
|
4320 | resetStartLocationFromNode(node, locationNode) {
|
4321 | this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
|
4322 | }
|
4323 |
|
4324 | }
|
4325 |
|
4326 | const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
|
4327 | const FlowErrors = ParseErrorEnum`flow`(_ => ({
|
4328 | AmbiguousConditionalArrow: _("Ambiguous expression: wrap the arrow functions in parentheses to disambiguate."),
|
4329 | AmbiguousDeclareModuleKind: _("Found both `declare module.exports` and `declare export` in the same module. Modules can only have 1 since they are either an ES module or they are a CommonJS module."),
|
4330 | AssignReservedType: _(({
|
4331 | reservedType
|
4332 | }) => `Cannot overwrite reserved type ${reservedType}.`),
|
4333 | DeclareClassElement: _("The `declare` modifier can only appear on class fields."),
|
4334 | DeclareClassFieldInitializer: _("Initializers are not allowed in fields with the `declare` modifier."),
|
4335 | DuplicateDeclareModuleExports: _("Duplicate `declare module.exports` statement."),
|
4336 | EnumBooleanMemberNotInitialized: _(({
|
4337 | memberName,
|
4338 | enumName
|
4339 | }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`),
|
4340 | EnumDuplicateMemberName: _(({
|
4341 | memberName,
|
4342 | enumName
|
4343 | }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`),
|
4344 | EnumInconsistentMemberValues: _(({
|
4345 | enumName
|
4346 | }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`),
|
4347 | EnumInvalidExplicitType: _(({
|
4348 | invalidEnumType,
|
4349 | enumName
|
4350 | }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`),
|
4351 | EnumInvalidExplicitTypeUnknownSupplied: _(({
|
4352 | enumName
|
4353 | }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`),
|
4354 | EnumInvalidMemberInitializerPrimaryType: _(({
|
4355 | enumName,
|
4356 | memberName,
|
4357 | explicitType
|
4358 | }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`),
|
4359 | EnumInvalidMemberInitializerSymbolType: _(({
|
4360 | enumName,
|
4361 | memberName
|
4362 | }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`),
|
4363 | EnumInvalidMemberInitializerUnknownType: _(({
|
4364 | enumName,
|
4365 | memberName
|
4366 | }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`),
|
4367 | EnumInvalidMemberName: _(({
|
4368 | enumName,
|
4369 | memberName,
|
4370 | suggestion
|
4371 | }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`),
|
4372 | EnumNumberMemberNotInitialized: _(({
|
4373 | enumName,
|
4374 | memberName
|
4375 | }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`),
|
4376 | EnumStringMemberInconsistentlyInitailized: _(({
|
4377 | enumName
|
4378 | }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`),
|
4379 | GetterMayNotHaveThisParam: _("A getter cannot have a `this` parameter."),
|
4380 | ImportTypeShorthandOnlyInPureImport: _("The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements."),
|
4381 | InexactInsideExact: _("Explicit inexact syntax cannot appear inside an explicit exact object type."),
|
4382 | InexactInsideNonObject: _("Explicit inexact syntax cannot appear in class or interface definitions."),
|
4383 | InexactVariance: _("Explicit inexact syntax cannot have variance."),
|
4384 | InvalidNonTypeImportInDeclareModule: _("Imports within a `declare module` body must always be `import type` or `import typeof`."),
|
4385 | MissingTypeParamDefault: _("Type parameter declaration needs a default, since a preceding type parameter declaration has a default."),
|
4386 | NestedDeclareModule: _("`declare module` cannot be used inside another `declare module`."),
|
4387 | NestedFlowComment: _("Cannot have a flow comment inside another flow comment."),
|
4388 | PatternIsOptional: _("A binding pattern parameter cannot be optional in an implementation signature.", {
|
4389 | reasonCode: "OptionalBindingPattern"
|
4390 | }),
|
4391 | SetterMayNotHaveThisParam: _("A setter cannot have a `this` parameter."),
|
4392 | SpreadVariance: _("Spread properties cannot have variance."),
|
4393 | ThisParamAnnotationRequired: _("A type annotation is required for the `this` parameter."),
|
4394 | ThisParamBannedInConstructor: _("Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions."),
|
4395 | ThisParamMayNotBeOptional: _("The `this` parameter cannot be optional."),
|
4396 | ThisParamMustBeFirst: _("The `this` parameter must be the first function parameter."),
|
4397 | ThisParamNoDefault: _("The `this` parameter may not have a default value."),
|
4398 | TypeBeforeInitializer: _("Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`."),
|
4399 | TypeCastInPattern: _("The type cast expression is expected to be wrapped with parenthesis."),
|
4400 | UnexpectedExplicitInexactInObject: _("Explicit inexact syntax must appear at the end of an inexact object."),
|
4401 | UnexpectedReservedType: _(({
|
4402 | reservedType
|
4403 | }) => `Unexpected reserved type ${reservedType}.`),
|
4404 | UnexpectedReservedUnderscore: _("`_` is only allowed as a type argument to call or new."),
|
4405 | UnexpectedSpaceBetweenModuloChecks: _("Spaces between `%` and `checks` are not allowed here."),
|
4406 | UnexpectedSpreadType: _("Spread operator cannot appear in class or interface definitions."),
|
4407 | UnexpectedSubtractionOperand: _('Unexpected token, expected "number" or "bigint".'),
|
4408 | UnexpectedTokenAfterTypeParameter: _("Expected an arrow function after this type parameter declaration."),
|
4409 | UnexpectedTypeParameterBeforeAsyncArrowFunction: _("Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`."),
|
4410 | UnsupportedDeclareExportKind: _(({
|
4411 | unsupportedExportKind,
|
4412 | suggestion
|
4413 | }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`),
|
4414 | UnsupportedStatementInDeclareModule: _("Only declares and type imports are allowed inside declare module."),
|
4415 | UnterminatedFlowComment: _("Unterminated flow-comment.")
|
4416 | }));
|
4417 |
|
4418 | function isEsModuleType(bodyElement) {
|
4419 | return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
|
4420 | }
|
4421 |
|
4422 | function hasTypeImportKind(node) {
|
4423 | return node.importKind === "type" || node.importKind === "typeof";
|
4424 | }
|
4425 |
|
4426 | function isMaybeDefaultImport(type) {
|
4427 | return tokenIsKeywordOrIdentifier(type) && type !== 97;
|
4428 | }
|
4429 |
|
4430 | const exportSuggestions = {
|
4431 | const: "declare export var",
|
4432 | let: "declare export var",
|
4433 | type: "export type",
|
4434 | interface: "export interface"
|
4435 | };
|
4436 |
|
4437 | function partition(list, test) {
|
4438 | const list1 = [];
|
4439 | const list2 = [];
|
4440 |
|
4441 | for (let i = 0; i < list.length; i++) {
|
4442 | (test(list[i], i, list) ? list1 : list2).push(list[i]);
|
4443 | }
|
4444 |
|
4445 | return [list1, list2];
|
4446 | }
|
4447 |
|
4448 | const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
|
4449 | var flow = (superClass => class extends superClass {
|
4450 | constructor(...args) {
|
4451 | super(...args);
|
4452 | this.flowPragma = undefined;
|
4453 | }
|
4454 |
|
4455 | getScopeHandler() {
|
4456 | return FlowScopeHandler;
|
4457 | }
|
4458 |
|
4459 | shouldParseTypes() {
|
4460 | return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
|
4461 | }
|
4462 |
|
4463 | shouldParseEnums() {
|
4464 | return !!this.getPluginOption("flow", "enums");
|
4465 | }
|
4466 |
|
4467 | finishToken(type, val) {
|
4468 | if (type !== 129 && type !== 13 && type !== 28) {
|
4469 | if (this.flowPragma === undefined) {
|
4470 | this.flowPragma = null;
|
4471 | }
|
4472 | }
|
4473 |
|
4474 | return super.finishToken(type, val);
|
4475 | }
|
4476 |
|
4477 | addComment(comment) {
|
4478 | if (this.flowPragma === undefined) {
|
4479 | const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
|
4480 |
|
4481 | if (!matches) ; else if (matches[1] === "flow") {
|
4482 | this.flowPragma = "flow";
|
4483 | } else if (matches[1] === "noflow") {
|
4484 | this.flowPragma = "noflow";
|
4485 | } else {
|
4486 | throw new Error("Unexpected flow pragma");
|
4487 | }
|
4488 | }
|
4489 |
|
4490 | return super.addComment(comment);
|
4491 | }
|
4492 |
|
4493 | flowParseTypeInitialiser(tok) {
|
4494 | const oldInType = this.state.inType;
|
4495 | this.state.inType = true;
|
4496 | this.expect(tok || 14);
|
4497 | const type = this.flowParseType();
|
4498 | this.state.inType = oldInType;
|
4499 | return type;
|
4500 | }
|
4501 |
|
4502 | flowParsePredicate() {
|
4503 | const node = this.startNode();
|
4504 | const moduloLoc = this.state.startLoc;
|
4505 | this.next();
|
4506 | this.expectContextual(107);
|
4507 |
|
4508 | if (this.state.lastTokStart > moduloLoc.index + 1) {
|
4509 | this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
|
4510 | at: moduloLoc
|
4511 | });
|
4512 | }
|
4513 |
|
4514 | if (this.eat(10)) {
|
4515 | node.value = this.parseExpression();
|
4516 | this.expect(11);
|
4517 | return this.finishNode(node, "DeclaredPredicate");
|
4518 | } else {
|
4519 | return this.finishNode(node, "InferredPredicate");
|
4520 | }
|
4521 | }
|
4522 |
|
4523 | flowParseTypeAndPredicateInitialiser() {
|
4524 | const oldInType = this.state.inType;
|
4525 | this.state.inType = true;
|
4526 | this.expect(14);
|
4527 | let type = null;
|
4528 | let predicate = null;
|
4529 |
|
4530 | if (this.match(54)) {
|
4531 | this.state.inType = oldInType;
|
4532 | predicate = this.flowParsePredicate();
|
4533 | } else {
|
4534 | type = this.flowParseType();
|
4535 | this.state.inType = oldInType;
|
4536 |
|
4537 | if (this.match(54)) {
|
4538 | predicate = this.flowParsePredicate();
|
4539 | }
|
4540 | }
|
4541 |
|
4542 | return [type, predicate];
|
4543 | }
|
4544 |
|
4545 | flowParseDeclareClass(node) {
|
4546 | this.next();
|
4547 | this.flowParseInterfaceish(node, true);
|
4548 | return this.finishNode(node, "DeclareClass");
|
4549 | }
|
4550 |
|
4551 | flowParseDeclareFunction(node) {
|
4552 | this.next();
|
4553 | const id = node.id = this.parseIdentifier();
|
4554 | const typeNode = this.startNode();
|
4555 | const typeContainer = this.startNode();
|
4556 |
|
4557 | if (this.match(47)) {
|
4558 | typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
|
4559 | } else {
|
4560 | typeNode.typeParameters = null;
|
4561 | }
|
4562 |
|
4563 | this.expect(10);
|
4564 | const tmp = this.flowParseFunctionTypeParams();
|
4565 | typeNode.params = tmp.params;
|
4566 | typeNode.rest = tmp.rest;
|
4567 | typeNode.this = tmp._this;
|
4568 | this.expect(11);
|
4569 | [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
4570 | typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
|
4571 | id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
|
4572 | this.resetEndLocation(id);
|
4573 | this.semicolon();
|
4574 | this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
|
4575 | return this.finishNode(node, "DeclareFunction");
|
4576 | }
|
4577 |
|
4578 | flowParseDeclare(node, insideModule) {
|
4579 | if (this.match(80)) {
|
4580 | return this.flowParseDeclareClass(node);
|
4581 | } else if (this.match(68)) {
|
4582 | return this.flowParseDeclareFunction(node);
|
4583 | } else if (this.match(74)) {
|
4584 | return this.flowParseDeclareVariable(node);
|
4585 | } else if (this.eatContextual(123)) {
|
4586 | if (this.match(16)) {
|
4587 | return this.flowParseDeclareModuleExports(node);
|
4588 | } else {
|
4589 | if (insideModule) {
|
4590 | this.raise(FlowErrors.NestedDeclareModule, {
|
4591 | at: this.state.lastTokStartLoc
|
4592 | });
|
4593 | }
|
4594 |
|
4595 | return this.flowParseDeclareModule(node);
|
4596 | }
|
4597 | } else if (this.isContextual(126)) {
|
4598 | return this.flowParseDeclareTypeAlias(node);
|
4599 | } else if (this.isContextual(127)) {
|
4600 | return this.flowParseDeclareOpaqueType(node);
|
4601 | } else if (this.isContextual(125)) {
|
4602 | return this.flowParseDeclareInterface(node);
|
4603 | } else if (this.match(82)) {
|
4604 | return this.flowParseDeclareExportDeclaration(node, insideModule);
|
4605 | } else {
|
4606 | throw this.unexpected();
|
4607 | }
|
4608 | }
|
4609 |
|
4610 | flowParseDeclareVariable(node) {
|
4611 | this.next();
|
4612 | node.id = this.flowParseTypeAnnotatableIdentifier(true);
|
4613 | this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
|
4614 | this.semicolon();
|
4615 | return this.finishNode(node, "DeclareVariable");
|
4616 | }
|
4617 |
|
4618 | flowParseDeclareModule(node) {
|
4619 | this.scope.enter(SCOPE_OTHER);
|
4620 |
|
4621 | if (this.match(129)) {
|
4622 | node.id = this.parseExprAtom();
|
4623 | } else {
|
4624 | node.id = this.parseIdentifier();
|
4625 | }
|
4626 |
|
4627 | const bodyNode = node.body = this.startNode();
|
4628 | const body = bodyNode.body = [];
|
4629 | this.expect(5);
|
4630 |
|
4631 | while (!this.match(8)) {
|
4632 | let bodyNode = this.startNode();
|
4633 |
|
4634 | if (this.match(83)) {
|
4635 | this.next();
|
4636 |
|
4637 | if (!this.isContextual(126) && !this.match(87)) {
|
4638 | this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
|
4639 | at: this.state.lastTokStartLoc
|
4640 | });
|
4641 | }
|
4642 |
|
4643 | this.parseImport(bodyNode);
|
4644 | } else {
|
4645 | this.expectContextual(121, FlowErrors.UnsupportedStatementInDeclareModule);
|
4646 | bodyNode = this.flowParseDeclare(bodyNode, true);
|
4647 | }
|
4648 |
|
4649 | body.push(bodyNode);
|
4650 | }
|
4651 |
|
4652 | this.scope.exit();
|
4653 | this.expect(8);
|
4654 | this.finishNode(bodyNode, "BlockStatement");
|
4655 | let kind = null;
|
4656 | let hasModuleExport = false;
|
4657 | body.forEach(bodyElement => {
|
4658 | if (isEsModuleType(bodyElement)) {
|
4659 | if (kind === "CommonJS") {
|
4660 | this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
|
4661 | at: bodyElement
|
4662 | });
|
4663 | }
|
4664 |
|
4665 | kind = "ES";
|
4666 | } else if (bodyElement.type === "DeclareModuleExports") {
|
4667 | if (hasModuleExport) {
|
4668 | this.raise(FlowErrors.DuplicateDeclareModuleExports, {
|
4669 | at: bodyElement
|
4670 | });
|
4671 | }
|
4672 |
|
4673 | if (kind === "ES") {
|
4674 | this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
|
4675 | at: bodyElement
|
4676 | });
|
4677 | }
|
4678 |
|
4679 | kind = "CommonJS";
|
4680 | hasModuleExport = true;
|
4681 | }
|
4682 | });
|
4683 | node.kind = kind || "CommonJS";
|
4684 | return this.finishNode(node, "DeclareModule");
|
4685 | }
|
4686 |
|
4687 | flowParseDeclareExportDeclaration(node, insideModule) {
|
4688 | this.expect(82);
|
4689 |
|
4690 | if (this.eat(65)) {
|
4691 | if (this.match(68) || this.match(80)) {
|
4692 | node.declaration = this.flowParseDeclare(this.startNode());
|
4693 | } else {
|
4694 | node.declaration = this.flowParseType();
|
4695 | this.semicolon();
|
4696 | }
|
4697 |
|
4698 | node.default = true;
|
4699 | return this.finishNode(node, "DeclareExportDeclaration");
|
4700 | } else {
|
4701 | if (this.match(75) || this.isLet() || (this.isContextual(126) || this.isContextual(125)) && !insideModule) {
|
4702 | const label = this.state.value;
|
4703 | throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
|
4704 | at: this.state.startLoc,
|
4705 | unsupportedExportKind: label,
|
4706 | suggestion: exportSuggestions[label]
|
4707 | });
|
4708 | }
|
4709 |
|
4710 | if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(127)) {
|
4711 | node.declaration = this.flowParseDeclare(this.startNode());
|
4712 | node.default = false;
|
4713 | return this.finishNode(node, "DeclareExportDeclaration");
|
4714 | } else if (this.match(55) || this.match(5) || this.isContextual(125) || this.isContextual(126) || this.isContextual(127)) {
|
4715 | node = this.parseExport(node);
|
4716 |
|
4717 | if (node.type === "ExportNamedDeclaration") {
|
4718 | node.type = "ExportDeclaration";
|
4719 | node.default = false;
|
4720 | delete node.exportKind;
|
4721 | }
|
4722 |
|
4723 | node.type = "Declare" + node.type;
|
4724 | return node;
|
4725 | }
|
4726 | }
|
4727 |
|
4728 | throw this.unexpected();
|
4729 | }
|
4730 |
|
4731 | flowParseDeclareModuleExports(node) {
|
4732 | this.next();
|
4733 | this.expectContextual(108);
|
4734 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
4735 | this.semicolon();
|
4736 | return this.finishNode(node, "DeclareModuleExports");
|
4737 | }
|
4738 |
|
4739 | flowParseDeclareTypeAlias(node) {
|
4740 | this.next();
|
4741 | this.flowParseTypeAlias(node);
|
4742 | node.type = "DeclareTypeAlias";
|
4743 | return node;
|
4744 | }
|
4745 |
|
4746 | flowParseDeclareOpaqueType(node) {
|
4747 | this.next();
|
4748 | this.flowParseOpaqueType(node, true);
|
4749 | node.type = "DeclareOpaqueType";
|
4750 | return node;
|
4751 | }
|
4752 |
|
4753 | flowParseDeclareInterface(node) {
|
4754 | this.next();
|
4755 | this.flowParseInterfaceish(node);
|
4756 | return this.finishNode(node, "DeclareInterface");
|
4757 | }
|
4758 |
|
4759 | flowParseInterfaceish(node, isClass = false) {
|
4760 | node.id = this.flowParseRestrictedIdentifier(!isClass, true);
|
4761 | this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
|
4762 |
|
4763 | if (this.match(47)) {
|
4764 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4765 | } else {
|
4766 | node.typeParameters = null;
|
4767 | }
|
4768 |
|
4769 | node.extends = [];
|
4770 | node.implements = [];
|
4771 | node.mixins = [];
|
4772 |
|
4773 | if (this.eat(81)) {
|
4774 | do {
|
4775 | node.extends.push(this.flowParseInterfaceExtends());
|
4776 | } while (!isClass && this.eat(12));
|
4777 | }
|
4778 |
|
4779 | if (this.isContextual(114)) {
|
4780 | this.next();
|
4781 |
|
4782 | do {
|
4783 | node.mixins.push(this.flowParseInterfaceExtends());
|
4784 | } while (this.eat(12));
|
4785 | }
|
4786 |
|
4787 | if (this.isContextual(110)) {
|
4788 | this.next();
|
4789 |
|
4790 | do {
|
4791 | node.implements.push(this.flowParseInterfaceExtends());
|
4792 | } while (this.eat(12));
|
4793 | }
|
4794 |
|
4795 | node.body = this.flowParseObjectType({
|
4796 | allowStatic: isClass,
|
4797 | allowExact: false,
|
4798 | allowSpread: false,
|
4799 | allowProto: isClass,
|
4800 | allowInexact: false
|
4801 | });
|
4802 | }
|
4803 |
|
4804 | flowParseInterfaceExtends() {
|
4805 | const node = this.startNode();
|
4806 | node.id = this.flowParseQualifiedTypeIdentifier();
|
4807 |
|
4808 | if (this.match(47)) {
|
4809 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
4810 | } else {
|
4811 | node.typeParameters = null;
|
4812 | }
|
4813 |
|
4814 | return this.finishNode(node, "InterfaceExtends");
|
4815 | }
|
4816 |
|
4817 | flowParseInterface(node) {
|
4818 | this.flowParseInterfaceish(node);
|
4819 | return this.finishNode(node, "InterfaceDeclaration");
|
4820 | }
|
4821 |
|
4822 | checkNotUnderscore(word) {
|
4823 | if (word === "_") {
|
4824 | this.raise(FlowErrors.UnexpectedReservedUnderscore, {
|
4825 | at: this.state.startLoc
|
4826 | });
|
4827 | }
|
4828 | }
|
4829 |
|
4830 | checkReservedType(word, startLoc, declaration) {
|
4831 | if (!reservedTypes.has(word)) return;
|
4832 | this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
|
4833 | at: startLoc,
|
4834 | reservedType: word
|
4835 | });
|
4836 | }
|
4837 |
|
4838 | flowParseRestrictedIdentifier(liberal, declaration) {
|
4839 | this.checkReservedType(this.state.value, this.state.startLoc, declaration);
|
4840 | return this.parseIdentifier(liberal);
|
4841 | }
|
4842 |
|
4843 | flowParseTypeAlias(node) {
|
4844 | node.id = this.flowParseRestrictedIdentifier(false, true);
|
4845 | this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
|
4846 |
|
4847 | if (this.match(47)) {
|
4848 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4849 | } else {
|
4850 | node.typeParameters = null;
|
4851 | }
|
4852 |
|
4853 | node.right = this.flowParseTypeInitialiser(29);
|
4854 | this.semicolon();
|
4855 | return this.finishNode(node, "TypeAlias");
|
4856 | }
|
4857 |
|
4858 | flowParseOpaqueType(node, declare) {
|
4859 | this.expectContextual(126);
|
4860 | node.id = this.flowParseRestrictedIdentifier(true, true);
|
4861 | this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
|
4862 |
|
4863 | if (this.match(47)) {
|
4864 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4865 | } else {
|
4866 | node.typeParameters = null;
|
4867 | }
|
4868 |
|
4869 | node.supertype = null;
|
4870 |
|
4871 | if (this.match(14)) {
|
4872 | node.supertype = this.flowParseTypeInitialiser(14);
|
4873 | }
|
4874 |
|
4875 | node.impltype = null;
|
4876 |
|
4877 | if (!declare) {
|
4878 | node.impltype = this.flowParseTypeInitialiser(29);
|
4879 | }
|
4880 |
|
4881 | this.semicolon();
|
4882 | return this.finishNode(node, "OpaqueType");
|
4883 | }
|
4884 |
|
4885 | flowParseTypeParameter(requireDefault = false) {
|
4886 | const nodeStartLoc = this.state.startLoc;
|
4887 | const node = this.startNode();
|
4888 | const variance = this.flowParseVariance();
|
4889 | const ident = this.flowParseTypeAnnotatableIdentifier();
|
4890 | node.name = ident.name;
|
4891 | node.variance = variance;
|
4892 | node.bound = ident.typeAnnotation;
|
4893 |
|
4894 | if (this.match(29)) {
|
4895 | this.eat(29);
|
4896 | node.default = this.flowParseType();
|
4897 | } else {
|
4898 | if (requireDefault) {
|
4899 | this.raise(FlowErrors.MissingTypeParamDefault, {
|
4900 | at: nodeStartLoc
|
4901 | });
|
4902 | }
|
4903 | }
|
4904 |
|
4905 | return this.finishNode(node, "TypeParameter");
|
4906 | }
|
4907 |
|
4908 | flowParseTypeParameterDeclaration() {
|
4909 | const oldInType = this.state.inType;
|
4910 | const node = this.startNode();
|
4911 | node.params = [];
|
4912 | this.state.inType = true;
|
4913 |
|
4914 | if (this.match(47) || this.match(138)) {
|
4915 | this.next();
|
4916 | } else {
|
4917 | this.unexpected();
|
4918 | }
|
4919 |
|
4920 | let defaultRequired = false;
|
4921 |
|
4922 | do {
|
4923 | const typeParameter = this.flowParseTypeParameter(defaultRequired);
|
4924 | node.params.push(typeParameter);
|
4925 |
|
4926 | if (typeParameter.default) {
|
4927 | defaultRequired = true;
|
4928 | }
|
4929 |
|
4930 | if (!this.match(48)) {
|
4931 | this.expect(12);
|
4932 | }
|
4933 | } while (!this.match(48));
|
4934 |
|
4935 | this.expect(48);
|
4936 | this.state.inType = oldInType;
|
4937 | return this.finishNode(node, "TypeParameterDeclaration");
|
4938 | }
|
4939 |
|
4940 | flowParseTypeParameterInstantiation() {
|
4941 | const node = this.startNode();
|
4942 | const oldInType = this.state.inType;
|
4943 | node.params = [];
|
4944 | this.state.inType = true;
|
4945 | this.expect(47);
|
4946 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
4947 | this.state.noAnonFunctionType = false;
|
4948 |
|
4949 | while (!this.match(48)) {
|
4950 | node.params.push(this.flowParseType());
|
4951 |
|
4952 | if (!this.match(48)) {
|
4953 | this.expect(12);
|
4954 | }
|
4955 | }
|
4956 |
|
4957 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
4958 | this.expect(48);
|
4959 | this.state.inType = oldInType;
|
4960 | return this.finishNode(node, "TypeParameterInstantiation");
|
4961 | }
|
4962 |
|
4963 | flowParseTypeParameterInstantiationCallOrNew() {
|
4964 | const node = this.startNode();
|
4965 | const oldInType = this.state.inType;
|
4966 | node.params = [];
|
4967 | this.state.inType = true;
|
4968 | this.expect(47);
|
4969 |
|
4970 | while (!this.match(48)) {
|
4971 | node.params.push(this.flowParseTypeOrImplicitInstantiation());
|
4972 |
|
4973 | if (!this.match(48)) {
|
4974 | this.expect(12);
|
4975 | }
|
4976 | }
|
4977 |
|
4978 | this.expect(48);
|
4979 | this.state.inType = oldInType;
|
4980 | return this.finishNode(node, "TypeParameterInstantiation");
|
4981 | }
|
4982 |
|
4983 | flowParseInterfaceType() {
|
4984 | const node = this.startNode();
|
4985 | this.expectContextual(125);
|
4986 | node.extends = [];
|
4987 |
|
4988 | if (this.eat(81)) {
|
4989 | do {
|
4990 | node.extends.push(this.flowParseInterfaceExtends());
|
4991 | } while (this.eat(12));
|
4992 | }
|
4993 |
|
4994 | node.body = this.flowParseObjectType({
|
4995 | allowStatic: false,
|
4996 | allowExact: false,
|
4997 | allowSpread: false,
|
4998 | allowProto: false,
|
4999 | allowInexact: false
|
5000 | });
|
5001 | return this.finishNode(node, "InterfaceTypeAnnotation");
|
5002 | }
|
5003 |
|
5004 | flowParseObjectPropertyKey() {
|
5005 | return this.match(130) || this.match(129) ? this.parseExprAtom() : this.parseIdentifier(true);
|
5006 | }
|
5007 |
|
5008 | flowParseObjectTypeIndexer(node, isStatic, variance) {
|
5009 | node.static = isStatic;
|
5010 |
|
5011 | if (this.lookahead().type === 14) {
|
5012 | node.id = this.flowParseObjectPropertyKey();
|
5013 | node.key = this.flowParseTypeInitialiser();
|
5014 | } else {
|
5015 | node.id = null;
|
5016 | node.key = this.flowParseType();
|
5017 | }
|
5018 |
|
5019 | this.expect(3);
|
5020 | node.value = this.flowParseTypeInitialiser();
|
5021 | node.variance = variance;
|
5022 | return this.finishNode(node, "ObjectTypeIndexer");
|
5023 | }
|
5024 |
|
5025 | flowParseObjectTypeInternalSlot(node, isStatic) {
|
5026 | node.static = isStatic;
|
5027 | node.id = this.flowParseObjectPropertyKey();
|
5028 | this.expect(3);
|
5029 | this.expect(3);
|
5030 |
|
5031 | if (this.match(47) || this.match(10)) {
|
5032 | node.method = true;
|
5033 | node.optional = false;
|
5034 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
|
5035 | } else {
|
5036 | node.method = false;
|
5037 |
|
5038 | if (this.eat(17)) {
|
5039 | node.optional = true;
|
5040 | }
|
5041 |
|
5042 | node.value = this.flowParseTypeInitialiser();
|
5043 | }
|
5044 |
|
5045 | return this.finishNode(node, "ObjectTypeInternalSlot");
|
5046 | }
|
5047 |
|
5048 | flowParseObjectTypeMethodish(node) {
|
5049 | node.params = [];
|
5050 | node.rest = null;
|
5051 | node.typeParameters = null;
|
5052 | node.this = null;
|
5053 |
|
5054 | if (this.match(47)) {
|
5055 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
5056 | }
|
5057 |
|
5058 | this.expect(10);
|
5059 |
|
5060 | if (this.match(78)) {
|
5061 | node.this = this.flowParseFunctionTypeParam(true);
|
5062 | node.this.name = null;
|
5063 |
|
5064 | if (!this.match(11)) {
|
5065 | this.expect(12);
|
5066 | }
|
5067 | }
|
5068 |
|
5069 | while (!this.match(11) && !this.match(21)) {
|
5070 | node.params.push(this.flowParseFunctionTypeParam(false));
|
5071 |
|
5072 | if (!this.match(11)) {
|
5073 | this.expect(12);
|
5074 | }
|
5075 | }
|
5076 |
|
5077 | if (this.eat(21)) {
|
5078 | node.rest = this.flowParseFunctionTypeParam(false);
|
5079 | }
|
5080 |
|
5081 | this.expect(11);
|
5082 | node.returnType = this.flowParseTypeInitialiser();
|
5083 | return this.finishNode(node, "FunctionTypeAnnotation");
|
5084 | }
|
5085 |
|
5086 | flowParseObjectTypeCallProperty(node, isStatic) {
|
5087 | const valueNode = this.startNode();
|
5088 | node.static = isStatic;
|
5089 | node.value = this.flowParseObjectTypeMethodish(valueNode);
|
5090 | return this.finishNode(node, "ObjectTypeCallProperty");
|
5091 | }
|
5092 |
|
5093 | flowParseObjectType({
|
5094 | allowStatic,
|
5095 | allowExact,
|
5096 | allowSpread,
|
5097 | allowProto,
|
5098 | allowInexact
|
5099 | }) {
|
5100 | const oldInType = this.state.inType;
|
5101 | this.state.inType = true;
|
5102 | const nodeStart = this.startNode();
|
5103 | nodeStart.callProperties = [];
|
5104 | nodeStart.properties = [];
|
5105 | nodeStart.indexers = [];
|
5106 | nodeStart.internalSlots = [];
|
5107 | let endDelim;
|
5108 | let exact;
|
5109 | let inexact = false;
|
5110 |
|
5111 | if (allowExact && this.match(6)) {
|
5112 | this.expect(6);
|
5113 | endDelim = 9;
|
5114 | exact = true;
|
5115 | } else {
|
5116 | this.expect(5);
|
5117 | endDelim = 8;
|
5118 | exact = false;
|
5119 | }
|
5120 |
|
5121 | nodeStart.exact = exact;
|
5122 |
|
5123 | while (!this.match(endDelim)) {
|
5124 | let isStatic = false;
|
5125 | let protoStartLoc = null;
|
5126 | let inexactStartLoc = null;
|
5127 | const node = this.startNode();
|
5128 |
|
5129 | if (allowProto && this.isContextual(115)) {
|
5130 | const lookahead = this.lookahead();
|
5131 |
|
5132 | if (lookahead.type !== 14 && lookahead.type !== 17) {
|
5133 | this.next();
|
5134 | protoStartLoc = this.state.startLoc;
|
5135 | allowStatic = false;
|
5136 | }
|
5137 | }
|
5138 |
|
5139 | if (allowStatic && this.isContextual(104)) {
|
5140 | const lookahead = this.lookahead();
|
5141 |
|
5142 | if (lookahead.type !== 14 && lookahead.type !== 17) {
|
5143 | this.next();
|
5144 | isStatic = true;
|
5145 | }
|
5146 | }
|
5147 |
|
5148 | const variance = this.flowParseVariance();
|
5149 |
|
5150 | if (this.eat(0)) {
|
5151 | if (protoStartLoc != null) {
|
5152 | this.unexpected(protoStartLoc);
|
5153 | }
|
5154 |
|
5155 | if (this.eat(0)) {
|
5156 | if (variance) {
|
5157 | this.unexpected(variance.loc.start);
|
5158 | }
|
5159 |
|
5160 | nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
|
5161 | } else {
|
5162 | nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
|
5163 | }
|
5164 | } else if (this.match(10) || this.match(47)) {
|
5165 | if (protoStartLoc != null) {
|
5166 | this.unexpected(protoStartLoc);
|
5167 | }
|
5168 |
|
5169 | if (variance) {
|
5170 | this.unexpected(variance.loc.start);
|
5171 | }
|
5172 |
|
5173 | nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
|
5174 | } else {
|
5175 | let kind = "init";
|
5176 |
|
5177 | if (this.isContextual(98) || this.isContextual(103)) {
|
5178 | const lookahead = this.lookahead();
|
5179 |
|
5180 | if (tokenIsLiteralPropertyName(lookahead.type)) {
|
5181 | kind = this.state.value;
|
5182 | this.next();
|
5183 | }
|
5184 | }
|
5185 |
|
5186 | const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
|
5187 |
|
5188 | if (propOrInexact === null) {
|
5189 | inexact = true;
|
5190 | inexactStartLoc = this.state.lastTokStartLoc;
|
5191 | } else {
|
5192 | nodeStart.properties.push(propOrInexact);
|
5193 | }
|
5194 | }
|
5195 |
|
5196 | this.flowObjectTypeSemicolon();
|
5197 |
|
5198 | if (inexactStartLoc && !this.match(8) && !this.match(9)) {
|
5199 | this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
|
5200 | at: inexactStartLoc
|
5201 | });
|
5202 | }
|
5203 | }
|
5204 |
|
5205 | this.expect(endDelim);
|
5206 |
|
5207 | if (allowSpread) {
|
5208 | nodeStart.inexact = inexact;
|
5209 | }
|
5210 |
|
5211 | const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
|
5212 | this.state.inType = oldInType;
|
5213 | return out;
|
5214 | }
|
5215 |
|
5216 | flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
|
5217 | if (this.eat(21)) {
|
5218 | const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
|
5219 |
|
5220 | if (isInexactToken) {
|
5221 | if (!allowSpread) {
|
5222 | this.raise(FlowErrors.InexactInsideNonObject, {
|
5223 | at: this.state.lastTokStartLoc
|
5224 | });
|
5225 | } else if (!allowInexact) {
|
5226 | this.raise(FlowErrors.InexactInsideExact, {
|
5227 | at: this.state.lastTokStartLoc
|
5228 | });
|
5229 | }
|
5230 |
|
5231 | if (variance) {
|
5232 | this.raise(FlowErrors.InexactVariance, {
|
5233 | at: variance
|
5234 | });
|
5235 | }
|
5236 |
|
5237 | return null;
|
5238 | }
|
5239 |
|
5240 | if (!allowSpread) {
|
5241 | this.raise(FlowErrors.UnexpectedSpreadType, {
|
5242 | at: this.state.lastTokStartLoc
|
5243 | });
|
5244 | }
|
5245 |
|
5246 | if (protoStartLoc != null) {
|
5247 | this.unexpected(protoStartLoc);
|
5248 | }
|
5249 |
|
5250 | if (variance) {
|
5251 | this.raise(FlowErrors.SpreadVariance, {
|
5252 | at: variance
|
5253 | });
|
5254 | }
|
5255 |
|
5256 | node.argument = this.flowParseType();
|
5257 | return this.finishNode(node, "ObjectTypeSpreadProperty");
|
5258 | } else {
|
5259 | node.key = this.flowParseObjectPropertyKey();
|
5260 | node.static = isStatic;
|
5261 | node.proto = protoStartLoc != null;
|
5262 | node.kind = kind;
|
5263 | let optional = false;
|
5264 |
|
5265 | if (this.match(47) || this.match(10)) {
|
5266 | node.method = true;
|
5267 |
|
5268 | if (protoStartLoc != null) {
|
5269 | this.unexpected(protoStartLoc);
|
5270 | }
|
5271 |
|
5272 | if (variance) {
|
5273 | this.unexpected(variance.loc.start);
|
5274 | }
|
5275 |
|
5276 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
|
5277 |
|
5278 | if (kind === "get" || kind === "set") {
|
5279 | this.flowCheckGetterSetterParams(node);
|
5280 | }
|
5281 |
|
5282 | if (!allowSpread && node.key.name === "constructor" && node.value.this) {
|
5283 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
5284 | at: node.value.this
|
5285 | });
|
5286 | }
|
5287 | } else {
|
5288 | if (kind !== "init") this.unexpected();
|
5289 | node.method = false;
|
5290 |
|
5291 | if (this.eat(17)) {
|
5292 | optional = true;
|
5293 | }
|
5294 |
|
5295 | node.value = this.flowParseTypeInitialiser();
|
5296 | node.variance = variance;
|
5297 | }
|
5298 |
|
5299 | node.optional = optional;
|
5300 | return this.finishNode(node, "ObjectTypeProperty");
|
5301 | }
|
5302 | }
|
5303 |
|
5304 | flowCheckGetterSetterParams(property) {
|
5305 | const paramCount = property.kind === "get" ? 0 : 1;
|
5306 | const length = property.value.params.length + (property.value.rest ? 1 : 0);
|
5307 |
|
5308 | if (property.value.this) {
|
5309 | this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
|
5310 | at: property.value.this
|
5311 | });
|
5312 | }
|
5313 |
|
5314 | if (length !== paramCount) {
|
5315 | this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
|
5316 | at: property
|
5317 | });
|
5318 | }
|
5319 |
|
5320 | if (property.kind === "set" && property.value.rest) {
|
5321 | this.raise(Errors.BadSetterRestParameter, {
|
5322 | at: property
|
5323 | });
|
5324 | }
|
5325 | }
|
5326 |
|
5327 | flowObjectTypeSemicolon() {
|
5328 | if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
|
5329 | this.unexpected();
|
5330 | }
|
5331 | }
|
5332 |
|
5333 | flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
|
5334 | startPos = startPos || this.state.start;
|
5335 | startLoc = startLoc || this.state.startLoc;
|
5336 | let node = id || this.flowParseRestrictedIdentifier(true);
|
5337 |
|
5338 | while (this.eat(16)) {
|
5339 | const node2 = this.startNodeAt(startPos, startLoc);
|
5340 | node2.qualification = node;
|
5341 | node2.id = this.flowParseRestrictedIdentifier(true);
|
5342 | node = this.finishNode(node2, "QualifiedTypeIdentifier");
|
5343 | }
|
5344 |
|
5345 | return node;
|
5346 | }
|
5347 |
|
5348 | flowParseGenericType(startPos, startLoc, id) {
|
5349 | const node = this.startNodeAt(startPos, startLoc);
|
5350 | node.typeParameters = null;
|
5351 | node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
|
5352 |
|
5353 | if (this.match(47)) {
|
5354 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
5355 | }
|
5356 |
|
5357 | return this.finishNode(node, "GenericTypeAnnotation");
|
5358 | }
|
5359 |
|
5360 | flowParseTypeofType() {
|
5361 | const node = this.startNode();
|
5362 | this.expect(87);
|
5363 | node.argument = this.flowParsePrimaryType();
|
5364 | return this.finishNode(node, "TypeofTypeAnnotation");
|
5365 | }
|
5366 |
|
5367 | flowParseTupleType() {
|
5368 | const node = this.startNode();
|
5369 | node.types = [];
|
5370 | this.expect(0);
|
5371 |
|
5372 | while (this.state.pos < this.length && !this.match(3)) {
|
5373 | node.types.push(this.flowParseType());
|
5374 | if (this.match(3)) break;
|
5375 | this.expect(12);
|
5376 | }
|
5377 |
|
5378 | this.expect(3);
|
5379 | return this.finishNode(node, "TupleTypeAnnotation");
|
5380 | }
|
5381 |
|
5382 | flowParseFunctionTypeParam(first) {
|
5383 | let name = null;
|
5384 | let optional = false;
|
5385 | let typeAnnotation = null;
|
5386 | const node = this.startNode();
|
5387 | const lh = this.lookahead();
|
5388 | const isThis = this.state.type === 78;
|
5389 |
|
5390 | if (lh.type === 14 || lh.type === 17) {
|
5391 | if (isThis && !first) {
|
5392 | this.raise(FlowErrors.ThisParamMustBeFirst, {
|
5393 | at: node
|
5394 | });
|
5395 | }
|
5396 |
|
5397 | name = this.parseIdentifier(isThis);
|
5398 |
|
5399 | if (this.eat(17)) {
|
5400 | optional = true;
|
5401 |
|
5402 | if (isThis) {
|
5403 | this.raise(FlowErrors.ThisParamMayNotBeOptional, {
|
5404 | at: node
|
5405 | });
|
5406 | }
|
5407 | }
|
5408 |
|
5409 | typeAnnotation = this.flowParseTypeInitialiser();
|
5410 | } else {
|
5411 | typeAnnotation = this.flowParseType();
|
5412 | }
|
5413 |
|
5414 | node.name = name;
|
5415 | node.optional = optional;
|
5416 | node.typeAnnotation = typeAnnotation;
|
5417 | return this.finishNode(node, "FunctionTypeParam");
|
5418 | }
|
5419 |
|
5420 | reinterpretTypeAsFunctionTypeParam(type) {
|
5421 | const node = this.startNodeAt(type.start, type.loc.start);
|
5422 | node.name = null;
|
5423 | node.optional = false;
|
5424 | node.typeAnnotation = type;
|
5425 | return this.finishNode(node, "FunctionTypeParam");
|
5426 | }
|
5427 |
|
5428 | flowParseFunctionTypeParams(params = []) {
|
5429 | let rest = null;
|
5430 | let _this = null;
|
5431 |
|
5432 | if (this.match(78)) {
|
5433 | _this = this.flowParseFunctionTypeParam(true);
|
5434 | _this.name = null;
|
5435 |
|
5436 | if (!this.match(11)) {
|
5437 | this.expect(12);
|
5438 | }
|
5439 | }
|
5440 |
|
5441 | while (!this.match(11) && !this.match(21)) {
|
5442 | params.push(this.flowParseFunctionTypeParam(false));
|
5443 |
|
5444 | if (!this.match(11)) {
|
5445 | this.expect(12);
|
5446 | }
|
5447 | }
|
5448 |
|
5449 | if (this.eat(21)) {
|
5450 | rest = this.flowParseFunctionTypeParam(false);
|
5451 | }
|
5452 |
|
5453 | return {
|
5454 | params,
|
5455 | rest,
|
5456 | _this
|
5457 | };
|
5458 | }
|
5459 |
|
5460 | flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
|
5461 | switch (id.name) {
|
5462 | case "any":
|
5463 | return this.finishNode(node, "AnyTypeAnnotation");
|
5464 |
|
5465 | case "bool":
|
5466 | case "boolean":
|
5467 | return this.finishNode(node, "BooleanTypeAnnotation");
|
5468 |
|
5469 | case "mixed":
|
5470 | return this.finishNode(node, "MixedTypeAnnotation");
|
5471 |
|
5472 | case "empty":
|
5473 | return this.finishNode(node, "EmptyTypeAnnotation");
|
5474 |
|
5475 | case "number":
|
5476 | return this.finishNode(node, "NumberTypeAnnotation");
|
5477 |
|
5478 | case "string":
|
5479 | return this.finishNode(node, "StringTypeAnnotation");
|
5480 |
|
5481 | case "symbol":
|
5482 | return this.finishNode(node, "SymbolTypeAnnotation");
|
5483 |
|
5484 | default:
|
5485 | this.checkNotUnderscore(id.name);
|
5486 | return this.flowParseGenericType(startPos, startLoc, id);
|
5487 | }
|
5488 | }
|
5489 |
|
5490 | flowParsePrimaryType() {
|
5491 | const startPos = this.state.start;
|
5492 | const startLoc = this.state.startLoc;
|
5493 | const node = this.startNode();
|
5494 | let tmp;
|
5495 | let type;
|
5496 | let isGroupedType = false;
|
5497 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
5498 |
|
5499 | switch (this.state.type) {
|
5500 | case 5:
|
5501 | return this.flowParseObjectType({
|
5502 | allowStatic: false,
|
5503 | allowExact: false,
|
5504 | allowSpread: true,
|
5505 | allowProto: false,
|
5506 | allowInexact: true
|
5507 | });
|
5508 |
|
5509 | case 6:
|
5510 | return this.flowParseObjectType({
|
5511 | allowStatic: false,
|
5512 | allowExact: true,
|
5513 | allowSpread: true,
|
5514 | allowProto: false,
|
5515 | allowInexact: false
|
5516 | });
|
5517 |
|
5518 | case 0:
|
5519 | this.state.noAnonFunctionType = false;
|
5520 | type = this.flowParseTupleType();
|
5521 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
5522 | return type;
|
5523 |
|
5524 | case 47:
|
5525 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
5526 | this.expect(10);
|
5527 | tmp = this.flowParseFunctionTypeParams();
|
5528 | node.params = tmp.params;
|
5529 | node.rest = tmp.rest;
|
5530 | node.this = tmp._this;
|
5531 | this.expect(11);
|
5532 | this.expect(19);
|
5533 | node.returnType = this.flowParseType();
|
5534 | return this.finishNode(node, "FunctionTypeAnnotation");
|
5535 |
|
5536 | case 10:
|
5537 | this.next();
|
5538 |
|
5539 | if (!this.match(11) && !this.match(21)) {
|
5540 | if (tokenIsIdentifier(this.state.type) || this.match(78)) {
|
5541 | const token = this.lookahead().type;
|
5542 | isGroupedType = token !== 17 && token !== 14;
|
5543 | } else {
|
5544 | isGroupedType = true;
|
5545 | }
|
5546 | }
|
5547 |
|
5548 | if (isGroupedType) {
|
5549 | this.state.noAnonFunctionType = false;
|
5550 | type = this.flowParseType();
|
5551 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
5552 |
|
5553 | if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
|
5554 | this.expect(11);
|
5555 | return type;
|
5556 | } else {
|
5557 | this.eat(12);
|
5558 | }
|
5559 | }
|
5560 |
|
5561 | if (type) {
|
5562 | tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
|
5563 | } else {
|
5564 | tmp = this.flowParseFunctionTypeParams();
|
5565 | }
|
5566 |
|
5567 | node.params = tmp.params;
|
5568 | node.rest = tmp.rest;
|
5569 | node.this = tmp._this;
|
5570 | this.expect(11);
|
5571 | this.expect(19);
|
5572 | node.returnType = this.flowParseType();
|
5573 | node.typeParameters = null;
|
5574 | return this.finishNode(node, "FunctionTypeAnnotation");
|
5575 |
|
5576 | case 129:
|
5577 | return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
|
5578 |
|
5579 | case 85:
|
5580 | case 86:
|
5581 | node.value = this.match(85);
|
5582 | this.next();
|
5583 | return this.finishNode(node, "BooleanLiteralTypeAnnotation");
|
5584 |
|
5585 | case 53:
|
5586 | if (this.state.value === "-") {
|
5587 | this.next();
|
5588 |
|
5589 | if (this.match(130)) {
|
5590 | return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
|
5591 | }
|
5592 |
|
5593 | if (this.match(131)) {
|
5594 | return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
|
5595 | }
|
5596 |
|
5597 | throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
|
5598 | at: this.state.startLoc
|
5599 | });
|
5600 | }
|
5601 |
|
5602 | throw this.unexpected();
|
5603 |
|
5604 | case 130:
|
5605 | return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
|
5606 |
|
5607 | case 131:
|
5608 | return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
|
5609 |
|
5610 | case 88:
|
5611 | this.next();
|
5612 | return this.finishNode(node, "VoidTypeAnnotation");
|
5613 |
|
5614 | case 84:
|
5615 | this.next();
|
5616 | return this.finishNode(node, "NullLiteralTypeAnnotation");
|
5617 |
|
5618 | case 78:
|
5619 | this.next();
|
5620 | return this.finishNode(node, "ThisTypeAnnotation");
|
5621 |
|
5622 | case 55:
|
5623 | this.next();
|
5624 | return this.finishNode(node, "ExistsTypeAnnotation");
|
5625 |
|
5626 | case 87:
|
5627 | return this.flowParseTypeofType();
|
5628 |
|
5629 | default:
|
5630 | if (tokenIsKeyword(this.state.type)) {
|
5631 | const label = tokenLabelName(this.state.type);
|
5632 | this.next();
|
5633 | return super.createIdentifier(node, label);
|
5634 | } else if (tokenIsIdentifier(this.state.type)) {
|
5635 | if (this.isContextual(125)) {
|
5636 | return this.flowParseInterfaceType();
|
5637 | }
|
5638 |
|
5639 | return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
|
5640 | }
|
5641 |
|
5642 | }
|
5643 |
|
5644 | throw this.unexpected();
|
5645 | }
|
5646 |
|
5647 | flowParsePostfixType() {
|
5648 | const startPos = this.state.start;
|
5649 | const startLoc = this.state.startLoc;
|
5650 | let type = this.flowParsePrimaryType();
|
5651 | let seenOptionalIndexedAccess = false;
|
5652 |
|
5653 | while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
|
5654 | const node = this.startNodeAt(startPos, startLoc);
|
5655 | const optional = this.eat(18);
|
5656 | seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
|
5657 | this.expect(0);
|
5658 |
|
5659 | if (!optional && this.match(3)) {
|
5660 | node.elementType = type;
|
5661 | this.next();
|
5662 | type = this.finishNode(node, "ArrayTypeAnnotation");
|
5663 | } else {
|
5664 | node.objectType = type;
|
5665 | node.indexType = this.flowParseType();
|
5666 | this.expect(3);
|
5667 |
|
5668 | if (seenOptionalIndexedAccess) {
|
5669 | node.optional = optional;
|
5670 | type = this.finishNode(node, "OptionalIndexedAccessType");
|
5671 | } else {
|
5672 | type = this.finishNode(node, "IndexedAccessType");
|
5673 | }
|
5674 | }
|
5675 | }
|
5676 |
|
5677 | return type;
|
5678 | }
|
5679 |
|
5680 | flowParsePrefixType() {
|
5681 | const node = this.startNode();
|
5682 |
|
5683 | if (this.eat(17)) {
|
5684 | node.typeAnnotation = this.flowParsePrefixType();
|
5685 | return this.finishNode(node, "NullableTypeAnnotation");
|
5686 | } else {
|
5687 | return this.flowParsePostfixType();
|
5688 | }
|
5689 | }
|
5690 |
|
5691 | flowParseAnonFunctionWithoutParens() {
|
5692 | const param = this.flowParsePrefixType();
|
5693 |
|
5694 | if (!this.state.noAnonFunctionType && this.eat(19)) {
|
5695 | const node = this.startNodeAt(param.start, param.loc.start);
|
5696 | node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
|
5697 | node.rest = null;
|
5698 | node.this = null;
|
5699 | node.returnType = this.flowParseType();
|
5700 | node.typeParameters = null;
|
5701 | return this.finishNode(node, "FunctionTypeAnnotation");
|
5702 | }
|
5703 |
|
5704 | return param;
|
5705 | }
|
5706 |
|
5707 | flowParseIntersectionType() {
|
5708 | const node = this.startNode();
|
5709 | this.eat(45);
|
5710 | const type = this.flowParseAnonFunctionWithoutParens();
|
5711 | node.types = [type];
|
5712 |
|
5713 | while (this.eat(45)) {
|
5714 | node.types.push(this.flowParseAnonFunctionWithoutParens());
|
5715 | }
|
5716 |
|
5717 | return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
|
5718 | }
|
5719 |
|
5720 | flowParseUnionType() {
|
5721 | const node = this.startNode();
|
5722 | this.eat(43);
|
5723 | const type = this.flowParseIntersectionType();
|
5724 | node.types = [type];
|
5725 |
|
5726 | while (this.eat(43)) {
|
5727 | node.types.push(this.flowParseIntersectionType());
|
5728 | }
|
5729 |
|
5730 | return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
|
5731 | }
|
5732 |
|
5733 | flowParseType() {
|
5734 | const oldInType = this.state.inType;
|
5735 | this.state.inType = true;
|
5736 | const type = this.flowParseUnionType();
|
5737 | this.state.inType = oldInType;
|
5738 | return type;
|
5739 | }
|
5740 |
|
5741 | flowParseTypeOrImplicitInstantiation() {
|
5742 | if (this.state.type === 128 && this.state.value === "_") {
|
5743 | const startPos = this.state.start;
|
5744 | const startLoc = this.state.startLoc;
|
5745 | const node = this.parseIdentifier();
|
5746 | return this.flowParseGenericType(startPos, startLoc, node);
|
5747 | } else {
|
5748 | return this.flowParseType();
|
5749 | }
|
5750 | }
|
5751 |
|
5752 | flowParseTypeAnnotation() {
|
5753 | const node = this.startNode();
|
5754 | node.typeAnnotation = this.flowParseTypeInitialiser();
|
5755 | return this.finishNode(node, "TypeAnnotation");
|
5756 | }
|
5757 |
|
5758 | flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
|
5759 | const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
|
5760 |
|
5761 | if (this.match(14)) {
|
5762 | ident.typeAnnotation = this.flowParseTypeAnnotation();
|
5763 | this.resetEndLocation(ident);
|
5764 | }
|
5765 |
|
5766 | return ident;
|
5767 | }
|
5768 |
|
5769 | typeCastToParameter(node) {
|
5770 | node.expression.typeAnnotation = node.typeAnnotation;
|
5771 | this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
|
5772 | return node.expression;
|
5773 | }
|
5774 |
|
5775 | flowParseVariance() {
|
5776 | let variance = null;
|
5777 |
|
5778 | if (this.match(53)) {
|
5779 | variance = this.startNode();
|
5780 |
|
5781 | if (this.state.value === "+") {
|
5782 | variance.kind = "plus";
|
5783 | } else {
|
5784 | variance.kind = "minus";
|
5785 | }
|
5786 |
|
5787 | this.next();
|
5788 | this.finishNode(variance, "Variance");
|
5789 | }
|
5790 |
|
5791 | return variance;
|
5792 | }
|
5793 |
|
5794 | parseFunctionBody(node, allowExpressionBody, isMethod = false) {
|
5795 | if (allowExpressionBody) {
|
5796 | return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
|
5797 | }
|
5798 |
|
5799 | return super.parseFunctionBody(node, false, isMethod);
|
5800 | }
|
5801 |
|
5802 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
5803 | if (this.match(14)) {
|
5804 | const typeNode = this.startNode();
|
5805 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
5806 | node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
|
5807 | }
|
5808 |
|
5809 | super.parseFunctionBodyAndFinish(node, type, isMethod);
|
5810 | }
|
5811 |
|
5812 | parseStatement(context, topLevel) {
|
5813 | if (this.state.strict && this.isContextual(125)) {
|
5814 | const lookahead = this.lookahead();
|
5815 |
|
5816 | if (tokenIsKeywordOrIdentifier(lookahead.type)) {
|
5817 | const node = this.startNode();
|
5818 | this.next();
|
5819 | return this.flowParseInterface(node);
|
5820 | }
|
5821 | } else if (this.shouldParseEnums() && this.isContextual(122)) {
|
5822 | const node = this.startNode();
|
5823 | this.next();
|
5824 | return this.flowParseEnumDeclaration(node);
|
5825 | }
|
5826 |
|
5827 | const stmt = super.parseStatement(context, topLevel);
|
5828 |
|
5829 | if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
|
5830 | this.flowPragma = null;
|
5831 | }
|
5832 |
|
5833 | return stmt;
|
5834 | }
|
5835 |
|
5836 | parseExpressionStatement(node, expr) {
|
5837 | if (expr.type === "Identifier") {
|
5838 | if (expr.name === "declare") {
|
5839 | if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
|
5840 | return this.flowParseDeclare(node);
|
5841 | }
|
5842 | } else if (tokenIsIdentifier(this.state.type)) {
|
5843 | if (expr.name === "interface") {
|
5844 | return this.flowParseInterface(node);
|
5845 | } else if (expr.name === "type") {
|
5846 | return this.flowParseTypeAlias(node);
|
5847 | } else if (expr.name === "opaque") {
|
5848 | return this.flowParseOpaqueType(node, false);
|
5849 | }
|
5850 | }
|
5851 | }
|
5852 |
|
5853 | return super.parseExpressionStatement(node, expr);
|
5854 | }
|
5855 |
|
5856 | shouldParseExportDeclaration() {
|
5857 | const {
|
5858 | type
|
5859 | } = this.state;
|
5860 |
|
5861 | if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
|
5862 | return !this.state.containsEsc;
|
5863 | }
|
5864 |
|
5865 | return super.shouldParseExportDeclaration();
|
5866 | }
|
5867 |
|
5868 | isExportDefaultSpecifier() {
|
5869 | const {
|
5870 | type
|
5871 | } = this.state;
|
5872 |
|
5873 | if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
|
5874 | return this.state.containsEsc;
|
5875 | }
|
5876 |
|
5877 | return super.isExportDefaultSpecifier();
|
5878 | }
|
5879 |
|
5880 | parseExportDefaultExpression() {
|
5881 | if (this.shouldParseEnums() && this.isContextual(122)) {
|
5882 | const node = this.startNode();
|
5883 | this.next();
|
5884 | return this.flowParseEnumDeclaration(node);
|
5885 | }
|
5886 |
|
5887 | return super.parseExportDefaultExpression();
|
5888 | }
|
5889 |
|
5890 | parseConditional(expr, startPos, startLoc, refExpressionErrors) {
|
5891 | if (!this.match(17)) return expr;
|
5892 |
|
5893 | if (this.state.maybeInArrowParameters) {
|
5894 | const nextCh = this.lookaheadCharCode();
|
5895 |
|
5896 | if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
|
5897 | this.setOptionalParametersError(refExpressionErrors);
|
5898 | return expr;
|
5899 | }
|
5900 | }
|
5901 |
|
5902 | this.expect(17);
|
5903 | const state = this.state.clone();
|
5904 | const originalNoArrowAt = this.state.noArrowAt;
|
5905 | const node = this.startNodeAt(startPos, startLoc);
|
5906 | let {
|
5907 | consequent,
|
5908 | failed
|
5909 | } = this.tryParseConditionalConsequent();
|
5910 | let [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
5911 |
|
5912 | if (failed || invalid.length > 0) {
|
5913 | const noArrowAt = [...originalNoArrowAt];
|
5914 |
|
5915 | if (invalid.length > 0) {
|
5916 | this.state = state;
|
5917 | this.state.noArrowAt = noArrowAt;
|
5918 |
|
5919 | for (let i = 0; i < invalid.length; i++) {
|
5920 | noArrowAt.push(invalid[i].start);
|
5921 | }
|
5922 |
|
5923 | ({
|
5924 | consequent,
|
5925 | failed
|
5926 | } = this.tryParseConditionalConsequent());
|
5927 | [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
5928 | }
|
5929 |
|
5930 | if (failed && valid.length > 1) {
|
5931 | this.raise(FlowErrors.AmbiguousConditionalArrow, {
|
5932 | at: state.startLoc
|
5933 | });
|
5934 | }
|
5935 |
|
5936 | if (failed && valid.length === 1) {
|
5937 | this.state = state;
|
5938 | noArrowAt.push(valid[0].start);
|
5939 | this.state.noArrowAt = noArrowAt;
|
5940 | ({
|
5941 | consequent,
|
5942 | failed
|
5943 | } = this.tryParseConditionalConsequent());
|
5944 | }
|
5945 | }
|
5946 |
|
5947 | this.getArrowLikeExpressions(consequent, true);
|
5948 | this.state.noArrowAt = originalNoArrowAt;
|
5949 | this.expect(14);
|
5950 | node.test = expr;
|
5951 | node.consequent = consequent;
|
5952 | node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
|
5953 | return this.finishNode(node, "ConditionalExpression");
|
5954 | }
|
5955 |
|
5956 | tryParseConditionalConsequent() {
|
5957 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
5958 | const consequent = this.parseMaybeAssignAllowIn();
|
5959 | const failed = !this.match(14);
|
5960 | this.state.noArrowParamsConversionAt.pop();
|
5961 | return {
|
5962 | consequent,
|
5963 | failed
|
5964 | };
|
5965 | }
|
5966 |
|
5967 | getArrowLikeExpressions(node, disallowInvalid) {
|
5968 | const stack = [node];
|
5969 | const arrows = [];
|
5970 |
|
5971 | while (stack.length !== 0) {
|
5972 | const node = stack.pop();
|
5973 |
|
5974 | if (node.type === "ArrowFunctionExpression") {
|
5975 | if (node.typeParameters || !node.returnType) {
|
5976 | this.finishArrowValidation(node);
|
5977 | } else {
|
5978 | arrows.push(node);
|
5979 | }
|
5980 |
|
5981 | stack.push(node.body);
|
5982 | } else if (node.type === "ConditionalExpression") {
|
5983 | stack.push(node.consequent);
|
5984 | stack.push(node.alternate);
|
5985 | }
|
5986 | }
|
5987 |
|
5988 | if (disallowInvalid) {
|
5989 | arrows.forEach(node => this.finishArrowValidation(node));
|
5990 | return [arrows, []];
|
5991 | }
|
5992 |
|
5993 | return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
|
5994 | }
|
5995 |
|
5996 | finishArrowValidation(node) {
|
5997 | var _node$extra;
|
5998 |
|
5999 | this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
|
6000 | this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
|
6001 | super.checkParams(node, false, true);
|
6002 | this.scope.exit();
|
6003 | }
|
6004 |
|
6005 | forwardNoArrowParamsConversionAt(node, parse) {
|
6006 | let result;
|
6007 |
|
6008 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
6009 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
6010 | result = parse();
|
6011 | this.state.noArrowParamsConversionAt.pop();
|
6012 | } else {
|
6013 | result = parse();
|
6014 | }
|
6015 |
|
6016 | return result;
|
6017 | }
|
6018 |
|
6019 | parseParenItem(node, startPos, startLoc) {
|
6020 | node = super.parseParenItem(node, startPos, startLoc);
|
6021 |
|
6022 | if (this.eat(17)) {
|
6023 | node.optional = true;
|
6024 | this.resetEndLocation(node);
|
6025 | }
|
6026 |
|
6027 | if (this.match(14)) {
|
6028 | const typeCastNode = this.startNodeAt(startPos, startLoc);
|
6029 | typeCastNode.expression = node;
|
6030 | typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
|
6031 | return this.finishNode(typeCastNode, "TypeCastExpression");
|
6032 | }
|
6033 |
|
6034 | return node;
|
6035 | }
|
6036 |
|
6037 | assertModuleNodeAllowed(node) {
|
6038 | if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
|
6039 | return;
|
6040 | }
|
6041 |
|
6042 | super.assertModuleNodeAllowed(node);
|
6043 | }
|
6044 |
|
6045 | parseExport(node) {
|
6046 | const decl = super.parseExport(node);
|
6047 |
|
6048 | if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
|
6049 | decl.exportKind = decl.exportKind || "value";
|
6050 | }
|
6051 |
|
6052 | return decl;
|
6053 | }
|
6054 |
|
6055 | parseExportDeclaration(node) {
|
6056 | if (this.isContextual(126)) {
|
6057 | node.exportKind = "type";
|
6058 | const declarationNode = this.startNode();
|
6059 | this.next();
|
6060 |
|
6061 | if (this.match(5)) {
|
6062 | node.specifiers = this.parseExportSpecifiers(true);
|
6063 | this.parseExportFrom(node);
|
6064 | return null;
|
6065 | } else {
|
6066 | return this.flowParseTypeAlias(declarationNode);
|
6067 | }
|
6068 | } else if (this.isContextual(127)) {
|
6069 | node.exportKind = "type";
|
6070 | const declarationNode = this.startNode();
|
6071 | this.next();
|
6072 | return this.flowParseOpaqueType(declarationNode, false);
|
6073 | } else if (this.isContextual(125)) {
|
6074 | node.exportKind = "type";
|
6075 | const declarationNode = this.startNode();
|
6076 | this.next();
|
6077 | return this.flowParseInterface(declarationNode);
|
6078 | } else if (this.shouldParseEnums() && this.isContextual(122)) {
|
6079 | node.exportKind = "value";
|
6080 | const declarationNode = this.startNode();
|
6081 | this.next();
|
6082 | return this.flowParseEnumDeclaration(declarationNode);
|
6083 | } else {
|
6084 | return super.parseExportDeclaration(node);
|
6085 | }
|
6086 | }
|
6087 |
|
6088 | eatExportStar(node) {
|
6089 | if (super.eatExportStar(...arguments)) return true;
|
6090 |
|
6091 | if (this.isContextual(126) && this.lookahead().type === 55) {
|
6092 | node.exportKind = "type";
|
6093 | this.next();
|
6094 | this.next();
|
6095 | return true;
|
6096 | }
|
6097 |
|
6098 | return false;
|
6099 | }
|
6100 |
|
6101 | maybeParseExportNamespaceSpecifier(node) {
|
6102 | const {
|
6103 | startLoc
|
6104 | } = this.state;
|
6105 | const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
|
6106 |
|
6107 | if (hasNamespace && node.exportKind === "type") {
|
6108 | this.unexpected(startLoc);
|
6109 | }
|
6110 |
|
6111 | return hasNamespace;
|
6112 | }
|
6113 |
|
6114 | parseClassId(node, isStatement, optionalId) {
|
6115 | super.parseClassId(node, isStatement, optionalId);
|
6116 |
|
6117 | if (this.match(47)) {
|
6118 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
6119 | }
|
6120 | }
|
6121 |
|
6122 | parseClassMember(classBody, member, state) {
|
6123 | const {
|
6124 | startLoc
|
6125 | } = this.state;
|
6126 |
|
6127 | if (this.isContextual(121)) {
|
6128 | if (this.parseClassMemberFromModifier(classBody, member)) {
|
6129 | return;
|
6130 | }
|
6131 |
|
6132 | member.declare = true;
|
6133 | }
|
6134 |
|
6135 | super.parseClassMember(classBody, member, state);
|
6136 |
|
6137 | if (member.declare) {
|
6138 | if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
|
6139 | this.raise(FlowErrors.DeclareClassElement, {
|
6140 | at: startLoc
|
6141 | });
|
6142 | } else if (member.value) {
|
6143 | this.raise(FlowErrors.DeclareClassFieldInitializer, {
|
6144 | at: member.value
|
6145 | });
|
6146 | }
|
6147 | }
|
6148 | }
|
6149 |
|
6150 | isIterator(word) {
|
6151 | return word === "iterator" || word === "asyncIterator";
|
6152 | }
|
6153 |
|
6154 | readIterator() {
|
6155 | const word = super.readWord1();
|
6156 | const fullWord = "@@" + word;
|
6157 |
|
6158 | if (!this.isIterator(word) || !this.state.inType) {
|
6159 | this.raise(Errors.InvalidIdentifier, {
|
6160 | at: this.state.curPosition(),
|
6161 | identifierName: fullWord
|
6162 | });
|
6163 | }
|
6164 |
|
6165 | this.finishToken(128, fullWord);
|
6166 | }
|
6167 |
|
6168 | getTokenFromCode(code) {
|
6169 | const next = this.input.charCodeAt(this.state.pos + 1);
|
6170 |
|
6171 | if (code === 123 && next === 124) {
|
6172 | return this.finishOp(6, 2);
|
6173 | } else if (this.state.inType && (code === 62 || code === 60)) {
|
6174 | return this.finishOp(code === 62 ? 48 : 47, 1);
|
6175 | } else if (this.state.inType && code === 63) {
|
6176 | if (next === 46) {
|
6177 | return this.finishOp(18, 2);
|
6178 | }
|
6179 |
|
6180 | return this.finishOp(17, 1);
|
6181 | } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
|
6182 | this.state.pos += 2;
|
6183 | return this.readIterator();
|
6184 | } else {
|
6185 | return super.getTokenFromCode(code);
|
6186 | }
|
6187 | }
|
6188 |
|
6189 | isAssignable(node, isBinding) {
|
6190 | if (node.type === "TypeCastExpression") {
|
6191 | return this.isAssignable(node.expression, isBinding);
|
6192 | } else {
|
6193 | return super.isAssignable(node, isBinding);
|
6194 | }
|
6195 | }
|
6196 |
|
6197 | toAssignable(node, isLHS = false) {
|
6198 | if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
|
6199 | node.left = this.typeCastToParameter(node.left);
|
6200 | }
|
6201 |
|
6202 | super.toAssignable(...arguments);
|
6203 | }
|
6204 |
|
6205 | toAssignableList(exprList, trailingCommaLoc, isLHS) {
|
6206 | for (let i = 0; i < exprList.length; i++) {
|
6207 | const expr = exprList[i];
|
6208 |
|
6209 | if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
|
6210 | exprList[i] = this.typeCastToParameter(expr);
|
6211 | }
|
6212 | }
|
6213 |
|
6214 | super.toAssignableList(exprList, trailingCommaLoc, isLHS);
|
6215 | }
|
6216 |
|
6217 | toReferencedList(exprList, isParenthesizedExpr) {
|
6218 | for (let i = 0; i < exprList.length; i++) {
|
6219 | var _expr$extra;
|
6220 |
|
6221 | const expr = exprList[i];
|
6222 |
|
6223 | if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
|
6224 | this.raise(FlowErrors.TypeCastInPattern, {
|
6225 | at: expr.typeAnnotation
|
6226 | });
|
6227 | }
|
6228 | }
|
6229 |
|
6230 | return exprList;
|
6231 | }
|
6232 |
|
6233 | parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
|
6234 | const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
|
6235 |
|
6236 | if (canBePattern && !this.state.maybeInArrowParameters) {
|
6237 | this.toReferencedList(node.elements);
|
6238 | }
|
6239 |
|
6240 | return node;
|
6241 | }
|
6242 |
|
6243 | isValidLVal(type, ...rest) {
|
6244 | return type === "TypeCastExpression" || super.isValidLVal(type, ...rest);
|
6245 | }
|
6246 |
|
6247 | parseClassProperty(node) {
|
6248 | if (this.match(14)) {
|
6249 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
6250 | }
|
6251 |
|
6252 | return super.parseClassProperty(node);
|
6253 | }
|
6254 |
|
6255 | parseClassPrivateProperty(node) {
|
6256 | if (this.match(14)) {
|
6257 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
6258 | }
|
6259 |
|
6260 | return super.parseClassPrivateProperty(node);
|
6261 | }
|
6262 |
|
6263 | isClassMethod() {
|
6264 | return this.match(47) || super.isClassMethod();
|
6265 | }
|
6266 |
|
6267 | isClassProperty() {
|
6268 | return this.match(14) || super.isClassProperty();
|
6269 | }
|
6270 |
|
6271 | isNonstaticConstructor(method) {
|
6272 | return !this.match(14) && super.isNonstaticConstructor(method);
|
6273 | }
|
6274 |
|
6275 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
6276 | if (method.variance) {
|
6277 | this.unexpected(method.variance.loc.start);
|
6278 | }
|
6279 |
|
6280 | delete method.variance;
|
6281 |
|
6282 | if (this.match(47)) {
|
6283 | method.typeParameters = this.flowParseTypeParameterDeclaration();
|
6284 | }
|
6285 |
|
6286 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
|
6287 |
|
6288 | if (method.params && isConstructor) {
|
6289 | const params = method.params;
|
6290 |
|
6291 | if (params.length > 0 && this.isThisParam(params[0])) {
|
6292 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
6293 | at: method
|
6294 | });
|
6295 | }
|
6296 | } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
|
6297 | const params = method.value.params;
|
6298 |
|
6299 | if (params.length > 0 && this.isThisParam(params[0])) {
|
6300 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
6301 | at: method
|
6302 | });
|
6303 | }
|
6304 | }
|
6305 | }
|
6306 |
|
6307 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
6308 | if (method.variance) {
|
6309 | this.unexpected(method.variance.loc.start);
|
6310 | }
|
6311 |
|
6312 | delete method.variance;
|
6313 |
|
6314 | if (this.match(47)) {
|
6315 | method.typeParameters = this.flowParseTypeParameterDeclaration();
|
6316 | }
|
6317 |
|
6318 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
6319 | }
|
6320 |
|
6321 | parseClassSuper(node) {
|
6322 | super.parseClassSuper(node);
|
6323 |
|
6324 | if (node.superClass && this.match(47)) {
|
6325 | node.superTypeParameters = this.flowParseTypeParameterInstantiation();
|
6326 | }
|
6327 |
|
6328 | if (this.isContextual(110)) {
|
6329 | this.next();
|
6330 | const implemented = node.implements = [];
|
6331 |
|
6332 | do {
|
6333 | const node = this.startNode();
|
6334 | node.id = this.flowParseRestrictedIdentifier(true);
|
6335 |
|
6336 | if (this.match(47)) {
|
6337 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
6338 | } else {
|
6339 | node.typeParameters = null;
|
6340 | }
|
6341 |
|
6342 | implemented.push(this.finishNode(node, "ClassImplements"));
|
6343 | } while (this.eat(12));
|
6344 | }
|
6345 | }
|
6346 |
|
6347 | checkGetterSetterParams(method) {
|
6348 | super.checkGetterSetterParams(method);
|
6349 | const params = this.getObjectOrClassMethodParams(method);
|
6350 |
|
6351 | if (params.length > 0) {
|
6352 | const param = params[0];
|
6353 |
|
6354 | if (this.isThisParam(param) && method.kind === "get") {
|
6355 | this.raise(FlowErrors.GetterMayNotHaveThisParam, {
|
6356 | at: param
|
6357 | });
|
6358 | } else if (this.isThisParam(param)) {
|
6359 | this.raise(FlowErrors.SetterMayNotHaveThisParam, {
|
6360 | at: param
|
6361 | });
|
6362 | }
|
6363 | }
|
6364 | }
|
6365 |
|
6366 | parsePropertyNamePrefixOperator(node) {
|
6367 | node.variance = this.flowParseVariance();
|
6368 | }
|
6369 |
|
6370 | parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
|
6371 | if (prop.variance) {
|
6372 | this.unexpected(prop.variance.loc.start);
|
6373 | }
|
6374 |
|
6375 | delete prop.variance;
|
6376 | let typeParameters;
|
6377 |
|
6378 | if (this.match(47) && !isAccessor) {
|
6379 | typeParameters = this.flowParseTypeParameterDeclaration();
|
6380 | if (!this.match(10)) this.unexpected();
|
6381 | }
|
6382 |
|
6383 | super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
|
6384 |
|
6385 | if (typeParameters) {
|
6386 | (prop.value || prop).typeParameters = typeParameters;
|
6387 | }
|
6388 | }
|
6389 |
|
6390 | parseAssignableListItemTypes(param) {
|
6391 | if (this.eat(17)) {
|
6392 | if (param.type !== "Identifier") {
|
6393 | this.raise(FlowErrors.PatternIsOptional, {
|
6394 | at: param
|
6395 | });
|
6396 | }
|
6397 |
|
6398 | if (this.isThisParam(param)) {
|
6399 | this.raise(FlowErrors.ThisParamMayNotBeOptional, {
|
6400 | at: param
|
6401 | });
|
6402 | }
|
6403 |
|
6404 | param.optional = true;
|
6405 | }
|
6406 |
|
6407 | if (this.match(14)) {
|
6408 | param.typeAnnotation = this.flowParseTypeAnnotation();
|
6409 | } else if (this.isThisParam(param)) {
|
6410 | this.raise(FlowErrors.ThisParamAnnotationRequired, {
|
6411 | at: param
|
6412 | });
|
6413 | }
|
6414 |
|
6415 | if (this.match(29) && this.isThisParam(param)) {
|
6416 | this.raise(FlowErrors.ThisParamNoDefault, {
|
6417 | at: param
|
6418 | });
|
6419 | }
|
6420 |
|
6421 | this.resetEndLocation(param);
|
6422 | return param;
|
6423 | }
|
6424 |
|
6425 | parseMaybeDefault(startPos, startLoc, left) {
|
6426 | const node = super.parseMaybeDefault(startPos, startLoc, left);
|
6427 |
|
6428 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
6429 | this.raise(FlowErrors.TypeBeforeInitializer, {
|
6430 | at: node.typeAnnotation
|
6431 | });
|
6432 | }
|
6433 |
|
6434 | return node;
|
6435 | }
|
6436 |
|
6437 | shouldParseDefaultImport(node) {
|
6438 | if (!hasTypeImportKind(node)) {
|
6439 | return super.shouldParseDefaultImport(node);
|
6440 | }
|
6441 |
|
6442 | return isMaybeDefaultImport(this.state.type);
|
6443 | }
|
6444 |
|
6445 | parseImportSpecifierLocal(node, specifier, type) {
|
6446 | specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
|
6447 | node.specifiers.push(this.finishImportSpecifier(specifier, type));
|
6448 | }
|
6449 |
|
6450 | maybeParseDefaultImportSpecifier(node) {
|
6451 | node.importKind = "value";
|
6452 | let kind = null;
|
6453 |
|
6454 | if (this.match(87)) {
|
6455 | kind = "typeof";
|
6456 | } else if (this.isContextual(126)) {
|
6457 | kind = "type";
|
6458 | }
|
6459 |
|
6460 | if (kind) {
|
6461 | const lh = this.lookahead();
|
6462 | const {
|
6463 | type
|
6464 | } = lh;
|
6465 |
|
6466 | if (kind === "type" && type === 55) {
|
6467 | this.unexpected(null, lh.type);
|
6468 | }
|
6469 |
|
6470 | if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
|
6471 | this.next();
|
6472 | node.importKind = kind;
|
6473 | }
|
6474 | }
|
6475 |
|
6476 | return super.maybeParseDefaultImportSpecifier(node);
|
6477 | }
|
6478 |
|
6479 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
|
6480 | const firstIdent = specifier.imported;
|
6481 | let specifierTypeKind = null;
|
6482 |
|
6483 | if (firstIdent.type === "Identifier") {
|
6484 | if (firstIdent.name === "type") {
|
6485 | specifierTypeKind = "type";
|
6486 | } else if (firstIdent.name === "typeof") {
|
6487 | specifierTypeKind = "typeof";
|
6488 | }
|
6489 | }
|
6490 |
|
6491 | let isBinding = false;
|
6492 |
|
6493 | if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
|
6494 | const as_ident = this.parseIdentifier(true);
|
6495 |
|
6496 | if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
|
6497 | specifier.imported = as_ident;
|
6498 | specifier.importKind = specifierTypeKind;
|
6499 | specifier.local = cloneIdentifier(as_ident);
|
6500 | } else {
|
6501 | specifier.imported = firstIdent;
|
6502 | specifier.importKind = null;
|
6503 | specifier.local = this.parseIdentifier();
|
6504 | }
|
6505 | } else {
|
6506 | if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
|
6507 | specifier.imported = this.parseIdentifier(true);
|
6508 | specifier.importKind = specifierTypeKind;
|
6509 | } else {
|
6510 | if (importedIsString) {
|
6511 | throw this.raise(Errors.ImportBindingIsString, {
|
6512 | at: specifier,
|
6513 | importName: firstIdent.value
|
6514 | });
|
6515 | }
|
6516 |
|
6517 | specifier.imported = firstIdent;
|
6518 | specifier.importKind = null;
|
6519 | }
|
6520 |
|
6521 | if (this.eatContextual(93)) {
|
6522 | specifier.local = this.parseIdentifier();
|
6523 | } else {
|
6524 | isBinding = true;
|
6525 | specifier.local = cloneIdentifier(specifier.imported);
|
6526 | }
|
6527 | }
|
6528 |
|
6529 | const specifierIsTypeImport = hasTypeImportKind(specifier);
|
6530 |
|
6531 | if (isInTypeOnlyImport && specifierIsTypeImport) {
|
6532 | this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
|
6533 | at: specifier
|
6534 | });
|
6535 | }
|
6536 |
|
6537 | if (isInTypeOnlyImport || specifierIsTypeImport) {
|
6538 | this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
|
6539 | }
|
6540 |
|
6541 | if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
|
6542 | this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
|
6543 | }
|
6544 |
|
6545 | return this.finishImportSpecifier(specifier, "ImportSpecifier");
|
6546 | }
|
6547 |
|
6548 | parseBindingAtom() {
|
6549 | switch (this.state.type) {
|
6550 | case 78:
|
6551 | return this.parseIdentifier(true);
|
6552 |
|
6553 | default:
|
6554 | return super.parseBindingAtom();
|
6555 | }
|
6556 | }
|
6557 |
|
6558 | parseFunctionParams(node, allowModifiers) {
|
6559 | const kind = node.kind;
|
6560 |
|
6561 | if (kind !== "get" && kind !== "set" && this.match(47)) {
|
6562 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
6563 | }
|
6564 |
|
6565 | super.parseFunctionParams(node, allowModifiers);
|
6566 | }
|
6567 |
|
6568 | parseVarId(decl, kind) {
|
6569 | super.parseVarId(decl, kind);
|
6570 |
|
6571 | if (this.match(14)) {
|
6572 | decl.id.typeAnnotation = this.flowParseTypeAnnotation();
|
6573 | this.resetEndLocation(decl.id);
|
6574 | }
|
6575 | }
|
6576 |
|
6577 | parseAsyncArrowFromCallExpression(node, call) {
|
6578 | if (this.match(14)) {
|
6579 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
6580 | this.state.noAnonFunctionType = true;
|
6581 | node.returnType = this.flowParseTypeAnnotation();
|
6582 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
6583 | }
|
6584 |
|
6585 | return super.parseAsyncArrowFromCallExpression(node, call);
|
6586 | }
|
6587 |
|
6588 | shouldParseAsyncArrow() {
|
6589 | return this.match(14) || super.shouldParseAsyncArrow();
|
6590 | }
|
6591 |
|
6592 | parseMaybeAssign(refExpressionErrors, afterLeftParse) {
|
6593 | var _jsx;
|
6594 |
|
6595 | let state = null;
|
6596 | let jsx;
|
6597 |
|
6598 | if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
|
6599 | state = this.state.clone();
|
6600 | jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
|
6601 | if (!jsx.error) return jsx.node;
|
6602 | const {
|
6603 | context
|
6604 | } = this.state;
|
6605 | const currentContext = context[context.length - 1];
|
6606 |
|
6607 | if (currentContext === types.j_oTag || currentContext === types.j_expr) {
|
6608 | context.pop();
|
6609 | }
|
6610 | }
|
6611 |
|
6612 | if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
|
6613 | var _jsx2, _jsx3;
|
6614 |
|
6615 | state = state || this.state.clone();
|
6616 | let typeParameters;
|
6617 | const arrow = this.tryParse(abort => {
|
6618 | var _arrowExpression$extr;
|
6619 |
|
6620 | typeParameters = this.flowParseTypeParameterDeclaration();
|
6621 | const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
|
6622 | const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
6623 | this.resetStartLocationFromNode(result, typeParameters);
|
6624 | return result;
|
6625 | });
|
6626 | if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
|
6627 | const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
|
6628 | if (expr.type !== "ArrowFunctionExpression") abort();
|
6629 | expr.typeParameters = typeParameters;
|
6630 | this.resetStartLocationFromNode(expr, typeParameters);
|
6631 | return arrowExpression;
|
6632 | }, state);
|
6633 | let arrowExpression = null;
|
6634 |
|
6635 | if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
|
6636 | if (!arrow.error && !arrow.aborted) {
|
6637 | if (arrow.node.async) {
|
6638 | this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
|
6639 | at: typeParameters
|
6640 | });
|
6641 | }
|
6642 |
|
6643 | return arrow.node;
|
6644 | }
|
6645 |
|
6646 | arrowExpression = arrow.node;
|
6647 | }
|
6648 |
|
6649 | if ((_jsx2 = jsx) != null && _jsx2.node) {
|
6650 | this.state = jsx.failState;
|
6651 | return jsx.node;
|
6652 | }
|
6653 |
|
6654 | if (arrowExpression) {
|
6655 | this.state = arrow.failState;
|
6656 | return arrowExpression;
|
6657 | }
|
6658 |
|
6659 | if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
|
6660 | if (arrow.thrown) throw arrow.error;
|
6661 | throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
|
6662 | at: typeParameters
|
6663 | });
|
6664 | }
|
6665 |
|
6666 | return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
6667 | }
|
6668 |
|
6669 | parseArrow(node) {
|
6670 | if (this.match(14)) {
|
6671 | const result = this.tryParse(() => {
|
6672 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
6673 | this.state.noAnonFunctionType = true;
|
6674 | const typeNode = this.startNode();
|
6675 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
6676 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
6677 | if (this.canInsertSemicolon()) this.unexpected();
|
6678 | if (!this.match(19)) this.unexpected();
|
6679 | return typeNode;
|
6680 | });
|
6681 | if (result.thrown) return null;
|
6682 | if (result.error) this.state = result.failState;
|
6683 | node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
|
6684 | }
|
6685 |
|
6686 | return super.parseArrow(node);
|
6687 | }
|
6688 |
|
6689 | shouldParseArrow(params) {
|
6690 | return this.match(14) || super.shouldParseArrow(params);
|
6691 | }
|
6692 |
|
6693 | setArrowFunctionParameters(node, params) {
|
6694 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
6695 | node.params = params;
|
6696 | } else {
|
6697 | super.setArrowFunctionParameters(node, params);
|
6698 | }
|
6699 | }
|
6700 |
|
6701 | checkParams(node, allowDuplicates, isArrowFunction) {
|
6702 | if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
6703 | return;
|
6704 | }
|
6705 |
|
6706 | for (let i = 0; i < node.params.length; i++) {
|
6707 | if (this.isThisParam(node.params[i]) && i > 0) {
|
6708 | this.raise(FlowErrors.ThisParamMustBeFirst, {
|
6709 | at: node.params[i]
|
6710 | });
|
6711 | }
|
6712 | }
|
6713 |
|
6714 | return super.checkParams(...arguments);
|
6715 | }
|
6716 |
|
6717 | parseParenAndDistinguishExpression(canBeArrow) {
|
6718 | return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
|
6719 | }
|
6720 |
|
6721 | parseSubscripts(base, startPos, startLoc, noCalls) {
|
6722 | if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
|
6723 | this.next();
|
6724 | const node = this.startNodeAt(startPos, startLoc);
|
6725 | node.callee = base;
|
6726 | node.arguments = this.parseCallExpressionArguments(11, false);
|
6727 | base = this.finishNode(node, "CallExpression");
|
6728 | } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
|
6729 | const state = this.state.clone();
|
6730 | const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
|
6731 | if (!arrow.error && !arrow.aborted) return arrow.node;
|
6732 | const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
|
6733 | if (result.node && !result.error) return result.node;
|
6734 |
|
6735 | if (arrow.node) {
|
6736 | this.state = arrow.failState;
|
6737 | return arrow.node;
|
6738 | }
|
6739 |
|
6740 | if (result.node) {
|
6741 | this.state = result.failState;
|
6742 | return result.node;
|
6743 | }
|
6744 |
|
6745 | throw arrow.error || result.error;
|
6746 | }
|
6747 |
|
6748 | return super.parseSubscripts(base, startPos, startLoc, noCalls);
|
6749 | }
|
6750 |
|
6751 | parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
|
6752 | if (this.match(18) && this.isLookaheadToken_lt()) {
|
6753 | subscriptState.optionalChainMember = true;
|
6754 |
|
6755 | if (noCalls) {
|
6756 | subscriptState.stop = true;
|
6757 | return base;
|
6758 | }
|
6759 |
|
6760 | this.next();
|
6761 | const node = this.startNodeAt(startPos, startLoc);
|
6762 | node.callee = base;
|
6763 | node.typeArguments = this.flowParseTypeParameterInstantiation();
|
6764 | this.expect(10);
|
6765 | node.arguments = this.parseCallExpressionArguments(11, false);
|
6766 | node.optional = true;
|
6767 | return this.finishCallExpression(node, true);
|
6768 | } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
|
6769 | const node = this.startNodeAt(startPos, startLoc);
|
6770 | node.callee = base;
|
6771 | const result = this.tryParse(() => {
|
6772 | node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
|
6773 | this.expect(10);
|
6774 | node.arguments = this.parseCallExpressionArguments(11, false);
|
6775 | if (subscriptState.optionalChainMember) node.optional = false;
|
6776 | return this.finishCallExpression(node, subscriptState.optionalChainMember);
|
6777 | });
|
6778 |
|
6779 | if (result.node) {
|
6780 | if (result.error) this.state = result.failState;
|
6781 | return result.node;
|
6782 | }
|
6783 | }
|
6784 |
|
6785 | return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
|
6786 | }
|
6787 |
|
6788 | parseNewCallee(node) {
|
6789 | super.parseNewCallee(node);
|
6790 | let targs = null;
|
6791 |
|
6792 | if (this.shouldParseTypes() && this.match(47)) {
|
6793 | targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
|
6794 | }
|
6795 |
|
6796 | node.typeArguments = targs;
|
6797 | }
|
6798 |
|
6799 | parseAsyncArrowWithTypeParameters(startPos, startLoc) {
|
6800 | const node = this.startNodeAt(startPos, startLoc);
|
6801 | this.parseFunctionParams(node);
|
6802 | if (!this.parseArrow(node)) return;
|
6803 | return this.parseArrowExpression(node, undefined, true);
|
6804 | }
|
6805 |
|
6806 | readToken_mult_modulo(code) {
|
6807 | const next = this.input.charCodeAt(this.state.pos + 1);
|
6808 |
|
6809 | if (code === 42 && next === 47 && this.state.hasFlowComment) {
|
6810 | this.state.hasFlowComment = false;
|
6811 | this.state.pos += 2;
|
6812 | this.nextToken();
|
6813 | return;
|
6814 | }
|
6815 |
|
6816 | super.readToken_mult_modulo(code);
|
6817 | }
|
6818 |
|
6819 | readToken_pipe_amp(code) {
|
6820 | const next = this.input.charCodeAt(this.state.pos + 1);
|
6821 |
|
6822 | if (code === 124 && next === 125) {
|
6823 | this.finishOp(9, 2);
|
6824 | return;
|
6825 | }
|
6826 |
|
6827 | super.readToken_pipe_amp(code);
|
6828 | }
|
6829 |
|
6830 | parseTopLevel(file, program) {
|
6831 | const fileNode = super.parseTopLevel(file, program);
|
6832 |
|
6833 | if (this.state.hasFlowComment) {
|
6834 | this.raise(FlowErrors.UnterminatedFlowComment, {
|
6835 | at: this.state.curPosition()
|
6836 | });
|
6837 | }
|
6838 |
|
6839 | return fileNode;
|
6840 | }
|
6841 |
|
6842 | skipBlockComment() {
|
6843 | if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
|
6844 | if (this.state.hasFlowComment) {
|
6845 | throw this.raise(FlowErrors.NestedFlowComment, {
|
6846 | at: this.state.startLoc
|
6847 | });
|
6848 | }
|
6849 |
|
6850 | this.hasFlowCommentCompletion();
|
6851 | this.state.pos += this.skipFlowComment();
|
6852 | this.state.hasFlowComment = true;
|
6853 | return;
|
6854 | }
|
6855 |
|
6856 | if (this.state.hasFlowComment) {
|
6857 | const end = this.input.indexOf("*-/", this.state.pos + 2);
|
6858 |
|
6859 | if (end === -1) {
|
6860 | throw this.raise(Errors.UnterminatedComment, {
|
6861 | at: this.state.curPosition()
|
6862 | });
|
6863 | }
|
6864 |
|
6865 | this.state.pos = end + 2 + 3;
|
6866 | return;
|
6867 | }
|
6868 |
|
6869 | return super.skipBlockComment();
|
6870 | }
|
6871 |
|
6872 | skipFlowComment() {
|
6873 | const {
|
6874 | pos
|
6875 | } = this.state;
|
6876 | let shiftToFirstNonWhiteSpace = 2;
|
6877 |
|
6878 | while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
|
6879 | shiftToFirstNonWhiteSpace++;
|
6880 | }
|
6881 |
|
6882 | const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
|
6883 | const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
|
6884 |
|
6885 | if (ch2 === 58 && ch3 === 58) {
|
6886 | return shiftToFirstNonWhiteSpace + 2;
|
6887 | }
|
6888 |
|
6889 | if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
|
6890 | return shiftToFirstNonWhiteSpace + 12;
|
6891 | }
|
6892 |
|
6893 | if (ch2 === 58 && ch3 !== 58) {
|
6894 | return shiftToFirstNonWhiteSpace;
|
6895 | }
|
6896 |
|
6897 | return false;
|
6898 | }
|
6899 |
|
6900 | hasFlowCommentCompletion() {
|
6901 | const end = this.input.indexOf("*/", this.state.pos);
|
6902 |
|
6903 | if (end === -1) {
|
6904 | throw this.raise(Errors.UnterminatedComment, {
|
6905 | at: this.state.curPosition()
|
6906 | });
|
6907 | }
|
6908 | }
|
6909 |
|
6910 | flowEnumErrorBooleanMemberNotInitialized(loc, {
|
6911 | enumName,
|
6912 | memberName
|
6913 | }) {
|
6914 | this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
|
6915 | at: loc,
|
6916 | memberName,
|
6917 | enumName
|
6918 | });
|
6919 | }
|
6920 |
|
6921 | flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
|
6922 | return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
|
6923 | at: loc
|
6924 | }, enumContext));
|
6925 | }
|
6926 |
|
6927 | flowEnumErrorNumberMemberNotInitialized(loc, {
|
6928 | enumName,
|
6929 | memberName
|
6930 | }) {
|
6931 | this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
|
6932 | at: loc,
|
6933 | enumName,
|
6934 | memberName
|
6935 | });
|
6936 | }
|
6937 |
|
6938 | flowEnumErrorStringMemberInconsistentlyInitailized(node, {
|
6939 | enumName
|
6940 | }) {
|
6941 | this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
|
6942 | at: node,
|
6943 | enumName
|
6944 | });
|
6945 | }
|
6946 |
|
6947 | flowEnumMemberInit() {
|
6948 | const startLoc = this.state.startLoc;
|
6949 |
|
6950 | const endOfInit = () => this.match(12) || this.match(8);
|
6951 |
|
6952 | switch (this.state.type) {
|
6953 | case 130:
|
6954 | {
|
6955 | const literal = this.parseNumericLiteral(this.state.value);
|
6956 |
|
6957 | if (endOfInit()) {
|
6958 | return {
|
6959 | type: "number",
|
6960 | loc: literal.loc.start,
|
6961 | value: literal
|
6962 | };
|
6963 | }
|
6964 |
|
6965 | return {
|
6966 | type: "invalid",
|
6967 | loc: startLoc
|
6968 | };
|
6969 | }
|
6970 |
|
6971 | case 129:
|
6972 | {
|
6973 | const literal = this.parseStringLiteral(this.state.value);
|
6974 |
|
6975 | if (endOfInit()) {
|
6976 | return {
|
6977 | type: "string",
|
6978 | loc: literal.loc.start,
|
6979 | value: literal
|
6980 | };
|
6981 | }
|
6982 |
|
6983 | return {
|
6984 | type: "invalid",
|
6985 | loc: startLoc
|
6986 | };
|
6987 | }
|
6988 |
|
6989 | case 85:
|
6990 | case 86:
|
6991 | {
|
6992 | const literal = this.parseBooleanLiteral(this.match(85));
|
6993 |
|
6994 | if (endOfInit()) {
|
6995 | return {
|
6996 | type: "boolean",
|
6997 | loc: literal.loc.start,
|
6998 | value: literal
|
6999 | };
|
7000 | }
|
7001 |
|
7002 | return {
|
7003 | type: "invalid",
|
7004 | loc: startLoc
|
7005 | };
|
7006 | }
|
7007 |
|
7008 | default:
|
7009 | return {
|
7010 | type: "invalid",
|
7011 | loc: startLoc
|
7012 | };
|
7013 | }
|
7014 | }
|
7015 |
|
7016 | flowEnumMemberRaw() {
|
7017 | const loc = this.state.startLoc;
|
7018 | const id = this.parseIdentifier(true);
|
7019 | const init = this.eat(29) ? this.flowEnumMemberInit() : {
|
7020 | type: "none",
|
7021 | loc
|
7022 | };
|
7023 | return {
|
7024 | id,
|
7025 | init
|
7026 | };
|
7027 | }
|
7028 |
|
7029 | flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
|
7030 | const {
|
7031 | explicitType
|
7032 | } = context;
|
7033 |
|
7034 | if (explicitType === null) {
|
7035 | return;
|
7036 | }
|
7037 |
|
7038 | if (explicitType !== expectedType) {
|
7039 | this.flowEnumErrorInvalidMemberInitializer(loc, context);
|
7040 | }
|
7041 | }
|
7042 |
|
7043 | flowEnumMembers({
|
7044 | enumName,
|
7045 | explicitType
|
7046 | }) {
|
7047 | const seenNames = new Set();
|
7048 | const members = {
|
7049 | booleanMembers: [],
|
7050 | numberMembers: [],
|
7051 | stringMembers: [],
|
7052 | defaultedMembers: []
|
7053 | };
|
7054 | let hasUnknownMembers = false;
|
7055 |
|
7056 | while (!this.match(8)) {
|
7057 | if (this.eat(21)) {
|
7058 | hasUnknownMembers = true;
|
7059 | break;
|
7060 | }
|
7061 |
|
7062 | const memberNode = this.startNode();
|
7063 | const {
|
7064 | id,
|
7065 | init
|
7066 | } = this.flowEnumMemberRaw();
|
7067 | const memberName = id.name;
|
7068 |
|
7069 | if (memberName === "") {
|
7070 | continue;
|
7071 | }
|
7072 |
|
7073 | if (/^[a-z]/.test(memberName)) {
|
7074 | this.raise(FlowErrors.EnumInvalidMemberName, {
|
7075 | at: id,
|
7076 | memberName,
|
7077 | suggestion: memberName[0].toUpperCase() + memberName.slice(1),
|
7078 | enumName
|
7079 | });
|
7080 | }
|
7081 |
|
7082 | if (seenNames.has(memberName)) {
|
7083 | this.raise(FlowErrors.EnumDuplicateMemberName, {
|
7084 | at: id,
|
7085 | memberName,
|
7086 | enumName
|
7087 | });
|
7088 | }
|
7089 |
|
7090 | seenNames.add(memberName);
|
7091 | const context = {
|
7092 | enumName,
|
7093 | explicitType,
|
7094 | memberName
|
7095 | };
|
7096 | memberNode.id = id;
|
7097 |
|
7098 | switch (init.type) {
|
7099 | case "boolean":
|
7100 | {
|
7101 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
|
7102 | memberNode.init = init.value;
|
7103 | members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
|
7104 | break;
|
7105 | }
|
7106 |
|
7107 | case "number":
|
7108 | {
|
7109 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
|
7110 | memberNode.init = init.value;
|
7111 | members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
|
7112 | break;
|
7113 | }
|
7114 |
|
7115 | case "string":
|
7116 | {
|
7117 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
|
7118 | memberNode.init = init.value;
|
7119 | members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
|
7120 | break;
|
7121 | }
|
7122 |
|
7123 | case "invalid":
|
7124 | {
|
7125 | throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
|
7126 | }
|
7127 |
|
7128 | case "none":
|
7129 | {
|
7130 | switch (explicitType) {
|
7131 | case "boolean":
|
7132 | this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
|
7133 | break;
|
7134 |
|
7135 | case "number":
|
7136 | this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
|
7137 | break;
|
7138 |
|
7139 | default:
|
7140 | members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
|
7141 | }
|
7142 | }
|
7143 | }
|
7144 |
|
7145 | if (!this.match(8)) {
|
7146 | this.expect(12);
|
7147 | }
|
7148 | }
|
7149 |
|
7150 | return {
|
7151 | members,
|
7152 | hasUnknownMembers
|
7153 | };
|
7154 | }
|
7155 |
|
7156 | flowEnumStringMembers(initializedMembers, defaultedMembers, {
|
7157 | enumName
|
7158 | }) {
|
7159 | if (initializedMembers.length === 0) {
|
7160 | return defaultedMembers;
|
7161 | } else if (defaultedMembers.length === 0) {
|
7162 | return initializedMembers;
|
7163 | } else if (defaultedMembers.length > initializedMembers.length) {
|
7164 | for (const member of initializedMembers) {
|
7165 | this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
|
7166 | enumName
|
7167 | });
|
7168 | }
|
7169 |
|
7170 | return defaultedMembers;
|
7171 | } else {
|
7172 | for (const member of defaultedMembers) {
|
7173 | this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
|
7174 | enumName
|
7175 | });
|
7176 | }
|
7177 |
|
7178 | return initializedMembers;
|
7179 | }
|
7180 | }
|
7181 |
|
7182 | flowEnumParseExplicitType({
|
7183 | enumName
|
7184 | }) {
|
7185 | if (!this.eatContextual(101)) return null;
|
7186 |
|
7187 | if (!tokenIsIdentifier(this.state.type)) {
|
7188 | throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
|
7189 | at: this.state.startLoc,
|
7190 | enumName
|
7191 | });
|
7192 | }
|
7193 |
|
7194 | const {
|
7195 | value
|
7196 | } = this.state;
|
7197 | this.next();
|
7198 |
|
7199 | if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
|
7200 | this.raise(FlowErrors.EnumInvalidExplicitType, {
|
7201 | at: this.state.startLoc,
|
7202 | enumName,
|
7203 | invalidEnumType: value
|
7204 | });
|
7205 | }
|
7206 |
|
7207 | return value;
|
7208 | }
|
7209 |
|
7210 | flowEnumBody(node, id) {
|
7211 | const enumName = id.name;
|
7212 | const nameLoc = id.loc.start;
|
7213 | const explicitType = this.flowEnumParseExplicitType({
|
7214 | enumName
|
7215 | });
|
7216 | this.expect(5);
|
7217 | const {
|
7218 | members,
|
7219 | hasUnknownMembers
|
7220 | } = this.flowEnumMembers({
|
7221 | enumName,
|
7222 | explicitType
|
7223 | });
|
7224 | node.hasUnknownMembers = hasUnknownMembers;
|
7225 |
|
7226 | switch (explicitType) {
|
7227 | case "boolean":
|
7228 | node.explicitType = true;
|
7229 | node.members = members.booleanMembers;
|
7230 | this.expect(8);
|
7231 | return this.finishNode(node, "EnumBooleanBody");
|
7232 |
|
7233 | case "number":
|
7234 | node.explicitType = true;
|
7235 | node.members = members.numberMembers;
|
7236 | this.expect(8);
|
7237 | return this.finishNode(node, "EnumNumberBody");
|
7238 |
|
7239 | case "string":
|
7240 | node.explicitType = true;
|
7241 | node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
|
7242 | enumName
|
7243 | });
|
7244 | this.expect(8);
|
7245 | return this.finishNode(node, "EnumStringBody");
|
7246 |
|
7247 | case "symbol":
|
7248 | node.members = members.defaultedMembers;
|
7249 | this.expect(8);
|
7250 | return this.finishNode(node, "EnumSymbolBody");
|
7251 |
|
7252 | default:
|
7253 | {
|
7254 | const empty = () => {
|
7255 | node.members = [];
|
7256 | this.expect(8);
|
7257 | return this.finishNode(node, "EnumStringBody");
|
7258 | };
|
7259 |
|
7260 | node.explicitType = false;
|
7261 | const boolsLen = members.booleanMembers.length;
|
7262 | const numsLen = members.numberMembers.length;
|
7263 | const strsLen = members.stringMembers.length;
|
7264 | const defaultedLen = members.defaultedMembers.length;
|
7265 |
|
7266 | if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
|
7267 | return empty();
|
7268 | } else if (!boolsLen && !numsLen) {
|
7269 | node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
|
7270 | enumName
|
7271 | });
|
7272 | this.expect(8);
|
7273 | return this.finishNode(node, "EnumStringBody");
|
7274 | } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
|
7275 | for (const member of members.defaultedMembers) {
|
7276 | this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
|
7277 | enumName,
|
7278 | memberName: member.id.name
|
7279 | });
|
7280 | }
|
7281 |
|
7282 | node.members = members.booleanMembers;
|
7283 | this.expect(8);
|
7284 | return this.finishNode(node, "EnumBooleanBody");
|
7285 | } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
|
7286 | for (const member of members.defaultedMembers) {
|
7287 | this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
|
7288 | enumName,
|
7289 | memberName: member.id.name
|
7290 | });
|
7291 | }
|
7292 |
|
7293 | node.members = members.numberMembers;
|
7294 | this.expect(8);
|
7295 | return this.finishNode(node, "EnumNumberBody");
|
7296 | } else {
|
7297 | this.raise(FlowErrors.EnumInconsistentMemberValues, {
|
7298 | at: nameLoc,
|
7299 | enumName
|
7300 | });
|
7301 | return empty();
|
7302 | }
|
7303 | }
|
7304 | }
|
7305 | }
|
7306 |
|
7307 | flowParseEnumDeclaration(node) {
|
7308 | const id = this.parseIdentifier();
|
7309 | node.id = id;
|
7310 | node.body = this.flowEnumBody(this.startNode(), id);
|
7311 | return this.finishNode(node, "EnumDeclaration");
|
7312 | }
|
7313 |
|
7314 | isLookaheadToken_lt() {
|
7315 | const next = this.nextTokenStart();
|
7316 |
|
7317 | if (this.input.charCodeAt(next) === 60) {
|
7318 | const afterNext = this.input.charCodeAt(next + 1);
|
7319 | return afterNext !== 60 && afterNext !== 61;
|
7320 | }
|
7321 |
|
7322 | return false;
|
7323 | }
|
7324 |
|
7325 | maybeUnwrapTypeCastExpression(node) {
|
7326 | return node.type === "TypeCastExpression" ? node.expression : node;
|
7327 | }
|
7328 |
|
7329 | });
|
7330 |
|
7331 | const entities = {
|
7332 | __proto__: null,
|
7333 | quot: "\u0022",
|
7334 | amp: "&",
|
7335 | apos: "\u0027",
|
7336 | lt: "<",
|
7337 | gt: ">",
|
7338 | nbsp: "\u00A0",
|
7339 | iexcl: "\u00A1",
|
7340 | cent: "\u00A2",
|
7341 | pound: "\u00A3",
|
7342 | curren: "\u00A4",
|
7343 | yen: "\u00A5",
|
7344 | brvbar: "\u00A6",
|
7345 | sect: "\u00A7",
|
7346 | uml: "\u00A8",
|
7347 | copy: "\u00A9",
|
7348 | ordf: "\u00AA",
|
7349 | laquo: "\u00AB",
|
7350 | not: "\u00AC",
|
7351 | shy: "\u00AD",
|
7352 | reg: "\u00AE",
|
7353 | macr: "\u00AF",
|
7354 | deg: "\u00B0",
|
7355 | plusmn: "\u00B1",
|
7356 | sup2: "\u00B2",
|
7357 | sup3: "\u00B3",
|
7358 | acute: "\u00B4",
|
7359 | micro: "\u00B5",
|
7360 | para: "\u00B6",
|
7361 | middot: "\u00B7",
|
7362 | cedil: "\u00B8",
|
7363 | sup1: "\u00B9",
|
7364 | ordm: "\u00BA",
|
7365 | raquo: "\u00BB",
|
7366 | frac14: "\u00BC",
|
7367 | frac12: "\u00BD",
|
7368 | frac34: "\u00BE",
|
7369 | iquest: "\u00BF",
|
7370 | Agrave: "\u00C0",
|
7371 | Aacute: "\u00C1",
|
7372 | Acirc: "\u00C2",
|
7373 | Atilde: "\u00C3",
|
7374 | Auml: "\u00C4",
|
7375 | Aring: "\u00C5",
|
7376 | AElig: "\u00C6",
|
7377 | Ccedil: "\u00C7",
|
7378 | Egrave: "\u00C8",
|
7379 | Eacute: "\u00C9",
|
7380 | Ecirc: "\u00CA",
|
7381 | Euml: "\u00CB",
|
7382 | Igrave: "\u00CC",
|
7383 | Iacute: "\u00CD",
|
7384 | Icirc: "\u00CE",
|
7385 | Iuml: "\u00CF",
|
7386 | ETH: "\u00D0",
|
7387 | Ntilde: "\u00D1",
|
7388 | Ograve: "\u00D2",
|
7389 | Oacute: "\u00D3",
|
7390 | Ocirc: "\u00D4",
|
7391 | Otilde: "\u00D5",
|
7392 | Ouml: "\u00D6",
|
7393 | times: "\u00D7",
|
7394 | Oslash: "\u00D8",
|
7395 | Ugrave: "\u00D9",
|
7396 | Uacute: "\u00DA",
|
7397 | Ucirc: "\u00DB",
|
7398 | Uuml: "\u00DC",
|
7399 | Yacute: "\u00DD",
|
7400 | THORN: "\u00DE",
|
7401 | szlig: "\u00DF",
|
7402 | agrave: "\u00E0",
|
7403 | aacute: "\u00E1",
|
7404 | acirc: "\u00E2",
|
7405 | atilde: "\u00E3",
|
7406 | auml: "\u00E4",
|
7407 | aring: "\u00E5",
|
7408 | aelig: "\u00E6",
|
7409 | ccedil: "\u00E7",
|
7410 | egrave: "\u00E8",
|
7411 | eacute: "\u00E9",
|
7412 | ecirc: "\u00EA",
|
7413 | euml: "\u00EB",
|
7414 | igrave: "\u00EC",
|
7415 | iacute: "\u00ED",
|
7416 | icirc: "\u00EE",
|
7417 | iuml: "\u00EF",
|
7418 | eth: "\u00F0",
|
7419 | ntilde: "\u00F1",
|
7420 | ograve: "\u00F2",
|
7421 | oacute: "\u00F3",
|
7422 | ocirc: "\u00F4",
|
7423 | otilde: "\u00F5",
|
7424 | ouml: "\u00F6",
|
7425 | divide: "\u00F7",
|
7426 | oslash: "\u00F8",
|
7427 | ugrave: "\u00F9",
|
7428 | uacute: "\u00FA",
|
7429 | ucirc: "\u00FB",
|
7430 | uuml: "\u00FC",
|
7431 | yacute: "\u00FD",
|
7432 | thorn: "\u00FE",
|
7433 | yuml: "\u00FF",
|
7434 | OElig: "\u0152",
|
7435 | oelig: "\u0153",
|
7436 | Scaron: "\u0160",
|
7437 | scaron: "\u0161",
|
7438 | Yuml: "\u0178",
|
7439 | fnof: "\u0192",
|
7440 | circ: "\u02C6",
|
7441 | tilde: "\u02DC",
|
7442 | Alpha: "\u0391",
|
7443 | Beta: "\u0392",
|
7444 | Gamma: "\u0393",
|
7445 | Delta: "\u0394",
|
7446 | Epsilon: "\u0395",
|
7447 | Zeta: "\u0396",
|
7448 | Eta: "\u0397",
|
7449 | Theta: "\u0398",
|
7450 | Iota: "\u0399",
|
7451 | Kappa: "\u039A",
|
7452 | Lambda: "\u039B",
|
7453 | Mu: "\u039C",
|
7454 | Nu: "\u039D",
|
7455 | Xi: "\u039E",
|
7456 | Omicron: "\u039F",
|
7457 | Pi: "\u03A0",
|
7458 | Rho: "\u03A1",
|
7459 | Sigma: "\u03A3",
|
7460 | Tau: "\u03A4",
|
7461 | Upsilon: "\u03A5",
|
7462 | Phi: "\u03A6",
|
7463 | Chi: "\u03A7",
|
7464 | Psi: "\u03A8",
|
7465 | Omega: "\u03A9",
|
7466 | alpha: "\u03B1",
|
7467 | beta: "\u03B2",
|
7468 | gamma: "\u03B3",
|
7469 | delta: "\u03B4",
|
7470 | epsilon: "\u03B5",
|
7471 | zeta: "\u03B6",
|
7472 | eta: "\u03B7",
|
7473 | theta: "\u03B8",
|
7474 | iota: "\u03B9",
|
7475 | kappa: "\u03BA",
|
7476 | lambda: "\u03BB",
|
7477 | mu: "\u03BC",
|
7478 | nu: "\u03BD",
|
7479 | xi: "\u03BE",
|
7480 | omicron: "\u03BF",
|
7481 | pi: "\u03C0",
|
7482 | rho: "\u03C1",
|
7483 | sigmaf: "\u03C2",
|
7484 | sigma: "\u03C3",
|
7485 | tau: "\u03C4",
|
7486 | upsilon: "\u03C5",
|
7487 | phi: "\u03C6",
|
7488 | chi: "\u03C7",
|
7489 | psi: "\u03C8",
|
7490 | omega: "\u03C9",
|
7491 | thetasym: "\u03D1",
|
7492 | upsih: "\u03D2",
|
7493 | piv: "\u03D6",
|
7494 | ensp: "\u2002",
|
7495 | emsp: "\u2003",
|
7496 | thinsp: "\u2009",
|
7497 | zwnj: "\u200C",
|
7498 | zwj: "\u200D",
|
7499 | lrm: "\u200E",
|
7500 | rlm: "\u200F",
|
7501 | ndash: "\u2013",
|
7502 | mdash: "\u2014",
|
7503 | lsquo: "\u2018",
|
7504 | rsquo: "\u2019",
|
7505 | sbquo: "\u201A",
|
7506 | ldquo: "\u201C",
|
7507 | rdquo: "\u201D",
|
7508 | bdquo: "\u201E",
|
7509 | dagger: "\u2020",
|
7510 | Dagger: "\u2021",
|
7511 | bull: "\u2022",
|
7512 | hellip: "\u2026",
|
7513 | permil: "\u2030",
|
7514 | prime: "\u2032",
|
7515 | Prime: "\u2033",
|
7516 | lsaquo: "\u2039",
|
7517 | rsaquo: "\u203A",
|
7518 | oline: "\u203E",
|
7519 | frasl: "\u2044",
|
7520 | euro: "\u20AC",
|
7521 | image: "\u2111",
|
7522 | weierp: "\u2118",
|
7523 | real: "\u211C",
|
7524 | trade: "\u2122",
|
7525 | alefsym: "\u2135",
|
7526 | larr: "\u2190",
|
7527 | uarr: "\u2191",
|
7528 | rarr: "\u2192",
|
7529 | darr: "\u2193",
|
7530 | harr: "\u2194",
|
7531 | crarr: "\u21B5",
|
7532 | lArr: "\u21D0",
|
7533 | uArr: "\u21D1",
|
7534 | rArr: "\u21D2",
|
7535 | dArr: "\u21D3",
|
7536 | hArr: "\u21D4",
|
7537 | forall: "\u2200",
|
7538 | part: "\u2202",
|
7539 | exist: "\u2203",
|
7540 | empty: "\u2205",
|
7541 | nabla: "\u2207",
|
7542 | isin: "\u2208",
|
7543 | notin: "\u2209",
|
7544 | ni: "\u220B",
|
7545 | prod: "\u220F",
|
7546 | sum: "\u2211",
|
7547 | minus: "\u2212",
|
7548 | lowast: "\u2217",
|
7549 | radic: "\u221A",
|
7550 | prop: "\u221D",
|
7551 | infin: "\u221E",
|
7552 | ang: "\u2220",
|
7553 | and: "\u2227",
|
7554 | or: "\u2228",
|
7555 | cap: "\u2229",
|
7556 | cup: "\u222A",
|
7557 | int: "\u222B",
|
7558 | there4: "\u2234",
|
7559 | sim: "\u223C",
|
7560 | cong: "\u2245",
|
7561 | asymp: "\u2248",
|
7562 | ne: "\u2260",
|
7563 | equiv: "\u2261",
|
7564 | le: "\u2264",
|
7565 | ge: "\u2265",
|
7566 | sub: "\u2282",
|
7567 | sup: "\u2283",
|
7568 | nsub: "\u2284",
|
7569 | sube: "\u2286",
|
7570 | supe: "\u2287",
|
7571 | oplus: "\u2295",
|
7572 | otimes: "\u2297",
|
7573 | perp: "\u22A5",
|
7574 | sdot: "\u22C5",
|
7575 | lceil: "\u2308",
|
7576 | rceil: "\u2309",
|
7577 | lfloor: "\u230A",
|
7578 | rfloor: "\u230B",
|
7579 | lang: "\u2329",
|
7580 | rang: "\u232A",
|
7581 | loz: "\u25CA",
|
7582 | spades: "\u2660",
|
7583 | clubs: "\u2663",
|
7584 | hearts: "\u2665",
|
7585 | diams: "\u2666"
|
7586 | };
|
7587 |
|
7588 | const JsxErrors = ParseErrorEnum`jsx`(_ => ({
|
7589 | AttributeIsEmpty: _("JSX attributes must only be assigned a non-empty expression."),
|
7590 | MissingClosingTagElement: _(({
|
7591 | openingTagName
|
7592 | }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`),
|
7593 | MissingClosingTagFragment: _("Expected corresponding JSX closing tag for <>."),
|
7594 | UnexpectedSequenceExpression: _("Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?"),
|
7595 | UnexpectedToken: _(({
|
7596 | unexpected,
|
7597 | HTMLEntity
|
7598 | }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`),
|
7599 | UnsupportedJsxValue: _("JSX value should be either an expression or a quoted JSX text."),
|
7600 | UnterminatedJsxContent: _("Unterminated JSX contents."),
|
7601 | UnwrappedAdjacentJSXElements: _("Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?")
|
7602 | }));
|
7603 |
|
7604 | function isFragment(object) {
|
7605 | return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
|
7606 | }
|
7607 |
|
7608 | function getQualifiedJSXName(object) {
|
7609 | if (object.type === "JSXIdentifier") {
|
7610 | return object.name;
|
7611 | }
|
7612 |
|
7613 | if (object.type === "JSXNamespacedName") {
|
7614 | return object.namespace.name + ":" + object.name.name;
|
7615 | }
|
7616 |
|
7617 | if (object.type === "JSXMemberExpression") {
|
7618 | return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
|
7619 | }
|
7620 |
|
7621 | throw new Error("Node had unexpected type: " + object.type);
|
7622 | }
|
7623 |
|
7624 | var jsx = (superClass => class extends superClass {
|
7625 | jsxReadToken() {
|
7626 | let out = "";
|
7627 | let chunkStart = this.state.pos;
|
7628 |
|
7629 | for (;;) {
|
7630 | if (this.state.pos >= this.length) {
|
7631 | throw this.raise(JsxErrors.UnterminatedJsxContent, {
|
7632 | at: this.state.startLoc
|
7633 | });
|
7634 | }
|
7635 |
|
7636 | const ch = this.input.charCodeAt(this.state.pos);
|
7637 |
|
7638 | switch (ch) {
|
7639 | case 60:
|
7640 | case 123:
|
7641 | if (this.state.pos === this.state.start) {
|
7642 | if (ch === 60 && this.state.canStartJSXElement) {
|
7643 | ++this.state.pos;
|
7644 | return this.finishToken(138);
|
7645 | }
|
7646 |
|
7647 | return super.getTokenFromCode(ch);
|
7648 | }
|
7649 |
|
7650 | out += this.input.slice(chunkStart, this.state.pos);
|
7651 | return this.finishToken(137, out);
|
7652 |
|
7653 | case 38:
|
7654 | out += this.input.slice(chunkStart, this.state.pos);
|
7655 | out += this.jsxReadEntity();
|
7656 | chunkStart = this.state.pos;
|
7657 | break;
|
7658 |
|
7659 | case 62:
|
7660 | case 125:
|
7661 |
|
7662 | default:
|
7663 | if (isNewLine(ch)) {
|
7664 | out += this.input.slice(chunkStart, this.state.pos);
|
7665 | out += this.jsxReadNewLine(true);
|
7666 | chunkStart = this.state.pos;
|
7667 | } else {
|
7668 | ++this.state.pos;
|
7669 | }
|
7670 |
|
7671 | }
|
7672 | }
|
7673 | }
|
7674 |
|
7675 | jsxReadNewLine(normalizeCRLF) {
|
7676 | const ch = this.input.charCodeAt(this.state.pos);
|
7677 | let out;
|
7678 | ++this.state.pos;
|
7679 |
|
7680 | if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
|
7681 | ++this.state.pos;
|
7682 | out = normalizeCRLF ? "\n" : "\r\n";
|
7683 | } else {
|
7684 | out = String.fromCharCode(ch);
|
7685 | }
|
7686 |
|
7687 | ++this.state.curLine;
|
7688 | this.state.lineStart = this.state.pos;
|
7689 | return out;
|
7690 | }
|
7691 |
|
7692 | jsxReadString(quote) {
|
7693 | let out = "";
|
7694 | let chunkStart = ++this.state.pos;
|
7695 |
|
7696 | for (;;) {
|
7697 | if (this.state.pos >= this.length) {
|
7698 | throw this.raise(Errors.UnterminatedString, {
|
7699 | at: this.state.startLoc
|
7700 | });
|
7701 | }
|
7702 |
|
7703 | const ch = this.input.charCodeAt(this.state.pos);
|
7704 | if (ch === quote) break;
|
7705 |
|
7706 | if (ch === 38) {
|
7707 | out += this.input.slice(chunkStart, this.state.pos);
|
7708 | out += this.jsxReadEntity();
|
7709 | chunkStart = this.state.pos;
|
7710 | } else if (isNewLine(ch)) {
|
7711 | out += this.input.slice(chunkStart, this.state.pos);
|
7712 | out += this.jsxReadNewLine(false);
|
7713 | chunkStart = this.state.pos;
|
7714 | } else {
|
7715 | ++this.state.pos;
|
7716 | }
|
7717 | }
|
7718 |
|
7719 | out += this.input.slice(chunkStart, this.state.pos++);
|
7720 | return this.finishToken(129, out);
|
7721 | }
|
7722 |
|
7723 | jsxReadEntity() {
|
7724 | const startPos = ++this.state.pos;
|
7725 |
|
7726 | if (this.codePointAtPos(this.state.pos) === 35) {
|
7727 | ++this.state.pos;
|
7728 | let radix = 10;
|
7729 |
|
7730 | if (this.codePointAtPos(this.state.pos) === 120) {
|
7731 | radix = 16;
|
7732 | ++this.state.pos;
|
7733 | }
|
7734 |
|
7735 | const codePoint = this.readInt(radix, undefined, false, "bail");
|
7736 |
|
7737 | if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
|
7738 | ++this.state.pos;
|
7739 | return String.fromCodePoint(codePoint);
|
7740 | }
|
7741 | } else {
|
7742 | let count = 0;
|
7743 | let semi = false;
|
7744 |
|
7745 | while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
|
7746 | ++this.state.pos;
|
7747 | }
|
7748 |
|
7749 | if (semi) {
|
7750 | const desc = this.input.slice(startPos, this.state.pos);
|
7751 | const entity = entities[desc];
|
7752 | ++this.state.pos;
|
7753 |
|
7754 | if (entity) {
|
7755 | return entity;
|
7756 | }
|
7757 | }
|
7758 | }
|
7759 |
|
7760 | this.state.pos = startPos;
|
7761 | return "&";
|
7762 | }
|
7763 |
|
7764 | jsxReadWord() {
|
7765 | let ch;
|
7766 | const start = this.state.pos;
|
7767 |
|
7768 | do {
|
7769 | ch = this.input.charCodeAt(++this.state.pos);
|
7770 | } while (isIdentifierChar(ch) || ch === 45);
|
7771 |
|
7772 | return this.finishToken(136, this.input.slice(start, this.state.pos));
|
7773 | }
|
7774 |
|
7775 | jsxParseIdentifier() {
|
7776 | const node = this.startNode();
|
7777 |
|
7778 | if (this.match(136)) {
|
7779 | node.name = this.state.value;
|
7780 | } else if (tokenIsKeyword(this.state.type)) {
|
7781 | node.name = tokenLabelName(this.state.type);
|
7782 | } else {
|
7783 | this.unexpected();
|
7784 | }
|
7785 |
|
7786 | this.next();
|
7787 | return this.finishNode(node, "JSXIdentifier");
|
7788 | }
|
7789 |
|
7790 | jsxParseNamespacedName() {
|
7791 | const startPos = this.state.start;
|
7792 | const startLoc = this.state.startLoc;
|
7793 | const name = this.jsxParseIdentifier();
|
7794 | if (!this.eat(14)) return name;
|
7795 | const node = this.startNodeAt(startPos, startLoc);
|
7796 | node.namespace = name;
|
7797 | node.name = this.jsxParseIdentifier();
|
7798 | return this.finishNode(node, "JSXNamespacedName");
|
7799 | }
|
7800 |
|
7801 | jsxParseElementName() {
|
7802 | const startPos = this.state.start;
|
7803 | const startLoc = this.state.startLoc;
|
7804 | let node = this.jsxParseNamespacedName();
|
7805 |
|
7806 | if (node.type === "JSXNamespacedName") {
|
7807 | return node;
|
7808 | }
|
7809 |
|
7810 | while (this.eat(16)) {
|
7811 | const newNode = this.startNodeAt(startPos, startLoc);
|
7812 | newNode.object = node;
|
7813 | newNode.property = this.jsxParseIdentifier();
|
7814 | node = this.finishNode(newNode, "JSXMemberExpression");
|
7815 | }
|
7816 |
|
7817 | return node;
|
7818 | }
|
7819 |
|
7820 | jsxParseAttributeValue() {
|
7821 | let node;
|
7822 |
|
7823 | switch (this.state.type) {
|
7824 | case 5:
|
7825 | node = this.startNode();
|
7826 | this.setContext(types.brace);
|
7827 | this.next();
|
7828 | node = this.jsxParseExpressionContainer(node, types.j_oTag);
|
7829 |
|
7830 | if (node.expression.type === "JSXEmptyExpression") {
|
7831 | this.raise(JsxErrors.AttributeIsEmpty, {
|
7832 | at: node
|
7833 | });
|
7834 | }
|
7835 |
|
7836 | return node;
|
7837 |
|
7838 | case 138:
|
7839 | case 129:
|
7840 | return this.parseExprAtom();
|
7841 |
|
7842 | default:
|
7843 | throw this.raise(JsxErrors.UnsupportedJsxValue, {
|
7844 | at: this.state.startLoc
|
7845 | });
|
7846 | }
|
7847 | }
|
7848 |
|
7849 | jsxParseEmptyExpression() {
|
7850 | const node = this.startNodeAt(this.state.lastTokEndLoc.index, this.state.lastTokEndLoc);
|
7851 | return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
|
7852 | }
|
7853 |
|
7854 | jsxParseSpreadChild(node) {
|
7855 | this.next();
|
7856 | node.expression = this.parseExpression();
|
7857 | this.setContext(types.j_oTag);
|
7858 | this.state.canStartJSXElement = true;
|
7859 | this.expect(8);
|
7860 | return this.finishNode(node, "JSXSpreadChild");
|
7861 | }
|
7862 |
|
7863 | jsxParseExpressionContainer(node, previousContext) {
|
7864 | if (this.match(8)) {
|
7865 | node.expression = this.jsxParseEmptyExpression();
|
7866 | } else {
|
7867 | const expression = this.parseExpression();
|
7868 | node.expression = expression;
|
7869 | }
|
7870 |
|
7871 | this.setContext(previousContext);
|
7872 | this.state.canStartJSXElement = true;
|
7873 | this.expect(8);
|
7874 | return this.finishNode(node, "JSXExpressionContainer");
|
7875 | }
|
7876 |
|
7877 | jsxParseAttribute() {
|
7878 | const node = this.startNode();
|
7879 |
|
7880 | if (this.match(5)) {
|
7881 | this.setContext(types.brace);
|
7882 | this.next();
|
7883 | this.expect(21);
|
7884 | node.argument = this.parseMaybeAssignAllowIn();
|
7885 | this.setContext(types.j_oTag);
|
7886 | this.state.canStartJSXElement = true;
|
7887 | this.expect(8);
|
7888 | return this.finishNode(node, "JSXSpreadAttribute");
|
7889 | }
|
7890 |
|
7891 | node.name = this.jsxParseNamespacedName();
|
7892 | node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
|
7893 | return this.finishNode(node, "JSXAttribute");
|
7894 | }
|
7895 |
|
7896 | jsxParseOpeningElementAt(startPos, startLoc) {
|
7897 | const node = this.startNodeAt(startPos, startLoc);
|
7898 |
|
7899 | if (this.eat(139)) {
|
7900 | return this.finishNode(node, "JSXOpeningFragment");
|
7901 | }
|
7902 |
|
7903 | node.name = this.jsxParseElementName();
|
7904 | return this.jsxParseOpeningElementAfterName(node);
|
7905 | }
|
7906 |
|
7907 | jsxParseOpeningElementAfterName(node) {
|
7908 | const attributes = [];
|
7909 |
|
7910 | while (!this.match(56) && !this.match(139)) {
|
7911 | attributes.push(this.jsxParseAttribute());
|
7912 | }
|
7913 |
|
7914 | node.attributes = attributes;
|
7915 | node.selfClosing = this.eat(56);
|
7916 | this.expect(139);
|
7917 | return this.finishNode(node, "JSXOpeningElement");
|
7918 | }
|
7919 |
|
7920 | jsxParseClosingElementAt(startPos, startLoc) {
|
7921 | const node = this.startNodeAt(startPos, startLoc);
|
7922 |
|
7923 | if (this.eat(139)) {
|
7924 | return this.finishNode(node, "JSXClosingFragment");
|
7925 | }
|
7926 |
|
7927 | node.name = this.jsxParseElementName();
|
7928 | this.expect(139);
|
7929 | return this.finishNode(node, "JSXClosingElement");
|
7930 | }
|
7931 |
|
7932 | jsxParseElementAt(startPos, startLoc) {
|
7933 | const node = this.startNodeAt(startPos, startLoc);
|
7934 | const children = [];
|
7935 | const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
|
7936 | let closingElement = null;
|
7937 |
|
7938 | if (!openingElement.selfClosing) {
|
7939 | contents: for (;;) {
|
7940 | switch (this.state.type) {
|
7941 | case 138:
|
7942 | startPos = this.state.start;
|
7943 | startLoc = this.state.startLoc;
|
7944 | this.next();
|
7945 |
|
7946 | if (this.eat(56)) {
|
7947 | closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
|
7948 | break contents;
|
7949 | }
|
7950 |
|
7951 | children.push(this.jsxParseElementAt(startPos, startLoc));
|
7952 | break;
|
7953 |
|
7954 | case 137:
|
7955 | children.push(this.parseExprAtom());
|
7956 | break;
|
7957 |
|
7958 | case 5:
|
7959 | {
|
7960 | const node = this.startNode();
|
7961 | this.setContext(types.brace);
|
7962 | this.next();
|
7963 |
|
7964 | if (this.match(21)) {
|
7965 | children.push(this.jsxParseSpreadChild(node));
|
7966 | } else {
|
7967 | children.push(this.jsxParseExpressionContainer(node, types.j_expr));
|
7968 | }
|
7969 |
|
7970 | break;
|
7971 | }
|
7972 |
|
7973 | default:
|
7974 | throw this.unexpected();
|
7975 | }
|
7976 | }
|
7977 |
|
7978 | if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
|
7979 | this.raise(JsxErrors.MissingClosingTagFragment, {
|
7980 | at: closingElement
|
7981 | });
|
7982 | } else if (!isFragment(openingElement) && isFragment(closingElement)) {
|
7983 | this.raise(JsxErrors.MissingClosingTagElement, {
|
7984 | at: closingElement,
|
7985 | openingTagName: getQualifiedJSXName(openingElement.name)
|
7986 | });
|
7987 | } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
|
7988 | if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
|
7989 | this.raise(JsxErrors.MissingClosingTagElement, {
|
7990 | at: closingElement,
|
7991 | openingTagName: getQualifiedJSXName(openingElement.name)
|
7992 | });
|
7993 | }
|
7994 | }
|
7995 | }
|
7996 |
|
7997 | if (isFragment(openingElement)) {
|
7998 | node.openingFragment = openingElement;
|
7999 | node.closingFragment = closingElement;
|
8000 | } else {
|
8001 | node.openingElement = openingElement;
|
8002 | node.closingElement = closingElement;
|
8003 | }
|
8004 |
|
8005 | node.children = children;
|
8006 |
|
8007 | if (this.match(47)) {
|
8008 | throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
|
8009 | at: this.state.startLoc
|
8010 | });
|
8011 | }
|
8012 |
|
8013 | return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
|
8014 | }
|
8015 |
|
8016 | jsxParseElement() {
|
8017 | const startPos = this.state.start;
|
8018 | const startLoc = this.state.startLoc;
|
8019 | this.next();
|
8020 | return this.jsxParseElementAt(startPos, startLoc);
|
8021 | }
|
8022 |
|
8023 | setContext(newContext) {
|
8024 | const {
|
8025 | context
|
8026 | } = this.state;
|
8027 | context[context.length - 1] = newContext;
|
8028 | }
|
8029 |
|
8030 | parseExprAtom(refExpressionErrors) {
|
8031 | if (this.match(137)) {
|
8032 | return this.parseLiteral(this.state.value, "JSXText");
|
8033 | } else if (this.match(138)) {
|
8034 | return this.jsxParseElement();
|
8035 | } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
|
8036 | this.replaceToken(138);
|
8037 | return this.jsxParseElement();
|
8038 | } else {
|
8039 | return super.parseExprAtom(refExpressionErrors);
|
8040 | }
|
8041 | }
|
8042 |
|
8043 | skipSpace() {
|
8044 | const curContext = this.curContext();
|
8045 | if (!curContext.preserveSpace) super.skipSpace();
|
8046 | }
|
8047 |
|
8048 | getTokenFromCode(code) {
|
8049 | const context = this.curContext();
|
8050 |
|
8051 | if (context === types.j_expr) {
|
8052 | return this.jsxReadToken();
|
8053 | }
|
8054 |
|
8055 | if (context === types.j_oTag || context === types.j_cTag) {
|
8056 | if (isIdentifierStart(code)) {
|
8057 | return this.jsxReadWord();
|
8058 | }
|
8059 |
|
8060 | if (code === 62) {
|
8061 | ++this.state.pos;
|
8062 | return this.finishToken(139);
|
8063 | }
|
8064 |
|
8065 | if ((code === 34 || code === 39) && context === types.j_oTag) {
|
8066 | return this.jsxReadString(code);
|
8067 | }
|
8068 | }
|
8069 |
|
8070 | if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
|
8071 | ++this.state.pos;
|
8072 | return this.finishToken(138);
|
8073 | }
|
8074 |
|
8075 | return super.getTokenFromCode(code);
|
8076 | }
|
8077 |
|
8078 | updateContext(prevType) {
|
8079 | const {
|
8080 | context,
|
8081 | type
|
8082 | } = this.state;
|
8083 |
|
8084 | if (type === 56 && prevType === 138) {
|
8085 | context.splice(-2, 2, types.j_cTag);
|
8086 | this.state.canStartJSXElement = false;
|
8087 | } else if (type === 138) {
|
8088 | context.push(types.j_oTag);
|
8089 | } else if (type === 139) {
|
8090 | const out = context[context.length - 1];
|
8091 |
|
8092 | if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
|
8093 | context.pop();
|
8094 | this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
|
8095 | } else {
|
8096 | this.setContext(types.j_expr);
|
8097 | this.state.canStartJSXElement = true;
|
8098 | }
|
8099 | } else {
|
8100 | this.state.canStartJSXElement = tokenComesBeforeExpression(type);
|
8101 | }
|
8102 | }
|
8103 |
|
8104 | });
|
8105 |
|
8106 | class TypeScriptScope extends Scope {
|
8107 | constructor(...args) {
|
8108 | super(...args);
|
8109 | this.types = new Set();
|
8110 | this.enums = new Set();
|
8111 | this.constEnums = new Set();
|
8112 | this.classes = new Set();
|
8113 | this.exportOnlyBindings = new Set();
|
8114 | }
|
8115 |
|
8116 | }
|
8117 |
|
8118 | class TypeScriptScopeHandler extends ScopeHandler {
|
8119 | createScope(flags) {
|
8120 | return new TypeScriptScope(flags);
|
8121 | }
|
8122 |
|
8123 | declareName(name, bindingType, loc) {
|
8124 | const scope = this.currentScope();
|
8125 |
|
8126 | if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
|
8127 | this.maybeExportDefined(scope, name);
|
8128 | scope.exportOnlyBindings.add(name);
|
8129 | return;
|
8130 | }
|
8131 |
|
8132 | super.declareName(...arguments);
|
8133 |
|
8134 | if (bindingType & BIND_KIND_TYPE) {
|
8135 | if (!(bindingType & BIND_KIND_VALUE)) {
|
8136 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
8137 | this.maybeExportDefined(scope, name);
|
8138 | }
|
8139 |
|
8140 | scope.types.add(name);
|
8141 | }
|
8142 |
|
8143 | if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
|
8144 | if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
|
8145 | if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
|
8146 | }
|
8147 |
|
8148 | isRedeclaredInScope(scope, name, bindingType) {
|
8149 | if (scope.enums.has(name)) {
|
8150 | if (bindingType & BIND_FLAGS_TS_ENUM) {
|
8151 | const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
|
8152 | const wasConst = scope.constEnums.has(name);
|
8153 | return isConst !== wasConst;
|
8154 | }
|
8155 |
|
8156 | return true;
|
8157 | }
|
8158 |
|
8159 | if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
|
8160 | if (scope.lexical.has(name)) {
|
8161 | return !!(bindingType & BIND_KIND_VALUE);
|
8162 | } else {
|
8163 | return false;
|
8164 | }
|
8165 | }
|
8166 |
|
8167 | if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
|
8168 | return true;
|
8169 | }
|
8170 |
|
8171 | return super.isRedeclaredInScope(...arguments);
|
8172 | }
|
8173 |
|
8174 | checkLocalExport(id) {
|
8175 | const topLevelScope = this.scopeStack[0];
|
8176 | const {
|
8177 | name
|
8178 | } = id;
|
8179 |
|
8180 | if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
|
8181 | super.checkLocalExport(id);
|
8182 | }
|
8183 | }
|
8184 |
|
8185 | }
|
8186 |
|
8187 | const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
|
8188 |
|
8189 | function nonNull(x) {
|
8190 | if (x == null) {
|
8191 | throw new Error(`Unexpected ${x} value.`);
|
8192 | }
|
8193 |
|
8194 | return x;
|
8195 | }
|
8196 |
|
8197 | function assert(x) {
|
8198 | if (!x) {
|
8199 | throw new Error("Assert fail");
|
8200 | }
|
8201 | }
|
8202 |
|
8203 | const TSErrors = ParseErrorEnum`typescript`(_ => ({
|
8204 | AbstractMethodHasImplementation: _(({
|
8205 | methodName
|
8206 | }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`),
|
8207 | AbstractPropertyHasInitializer: _(({
|
8208 | propertyName
|
8209 | }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`),
|
8210 | AccesorCannotDeclareThisParameter: _("'get' and 'set' accessors cannot declare 'this' parameters."),
|
8211 | AccesorCannotHaveTypeParameters: _("An accessor cannot have type parameters."),
|
8212 | CannotFindName: _(({
|
8213 | name
|
8214 | }) => `Cannot find name '${name}'.`),
|
8215 | ClassMethodHasDeclare: _("Class methods cannot have the 'declare' modifier."),
|
8216 | ClassMethodHasReadonly: _("Class methods cannot have the 'readonly' modifier."),
|
8217 | ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: _("A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference."),
|
8218 | ConstructorHasTypeParameters: _("Type parameters cannot appear on a constructor declaration."),
|
8219 | DeclareAccessor: _(({
|
8220 | kind
|
8221 | }) => `'declare' is not allowed in ${kind}ters.`),
|
8222 | DeclareClassFieldHasInitializer: _("Initializers are not allowed in ambient contexts."),
|
8223 | DeclareFunctionHasImplementation: _("An implementation cannot be declared in ambient contexts."),
|
8224 | DuplicateAccessibilityModifier: _(({
|
8225 | modifier
|
8226 | }) => `Accessibility modifier already seen.`),
|
8227 | DuplicateModifier: _(({
|
8228 | modifier
|
8229 | }) => `Duplicate modifier: '${modifier}'.`),
|
8230 | EmptyHeritageClauseType: _(({
|
8231 | token
|
8232 | }) => `'${token}' list cannot be empty.`),
|
8233 | EmptyTypeArguments: _("Type argument list cannot be empty."),
|
8234 | EmptyTypeParameters: _("Type parameter list cannot be empty."),
|
8235 | ExpectedAmbientAfterExportDeclare: _("'export declare' must be followed by an ambient declaration."),
|
8236 | ImportAliasHasImportType: _("An import alias can not use 'import type'."),
|
8237 | IncompatibleModifiers: _(({
|
8238 | modifiers
|
8239 | }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`),
|
8240 | IndexSignatureHasAbstract: _("Index signatures cannot have the 'abstract' modifier."),
|
8241 | IndexSignatureHasAccessibility: _(({
|
8242 | modifier
|
8243 | }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`),
|
8244 | IndexSignatureHasDeclare: _("Index signatures cannot have the 'declare' modifier."),
|
8245 | IndexSignatureHasOverride: _("'override' modifier cannot appear on an index signature."),
|
8246 | IndexSignatureHasStatic: _("Index signatures cannot have the 'static' modifier."),
|
8247 | InitializerNotAllowedInAmbientContext: _("Initializers are not allowed in ambient contexts."),
|
8248 | InvalidModifierOnTypeMember: _(({
|
8249 | modifier
|
8250 | }) => `'${modifier}' modifier cannot appear on a type member.`),
|
8251 | InvalidModifierOnTypeParameter: _(({
|
8252 | modifier
|
8253 | }) => `'${modifier}' modifier cannot appear on a type parameter.`),
|
8254 | InvalidModifierOnTypeParameterPositions: _(({
|
8255 | modifier
|
8256 | }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`),
|
8257 | InvalidModifiersOrder: _(({
|
8258 | orderedModifiers
|
8259 | }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`),
|
8260 | InvalidPropertyAccessAfterInstantiationExpression: _("Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments."),
|
8261 | InvalidTupleMemberLabel: _("Tuple members must be labeled with a simple identifier."),
|
8262 | MissingInterfaceName: _("'interface' declarations must be followed by an identifier."),
|
8263 | MixedLabeledAndUnlabeledElements: _("Tuple members must all have names or all not have names."),
|
8264 | NonAbstractClassHasAbstractMethod: _("Abstract methods can only appear within an abstract class."),
|
8265 | NonClassMethodPropertyHasAbstractModifer: _("'abstract' modifier can only appear on a class, method, or property declaration."),
|
8266 | OptionalTypeBeforeRequired: _("A required element cannot follow an optional element."),
|
8267 | OverrideNotInSubClass: _("This member cannot have an 'override' modifier because its containing class does not extend another class."),
|
8268 | PatternIsOptional: _("A binding pattern parameter cannot be optional in an implementation signature."),
|
8269 | PrivateElementHasAbstract: _("Private elements cannot have the 'abstract' modifier."),
|
8270 | PrivateElementHasAccessibility: _(({
|
8271 | modifier
|
8272 | }) => `Private elements cannot have an accessibility modifier ('${modifier}').`),
|
8273 | ReadonlyForMethodSignature: _("'readonly' modifier can only appear on a property declaration or index signature."),
|
8274 | ReservedArrowTypeParam: _("This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`."),
|
8275 | ReservedTypeAssertion: _("This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead."),
|
8276 | SetAccesorCannotHaveOptionalParameter: _("A 'set' accessor cannot have an optional parameter."),
|
8277 | SetAccesorCannotHaveRestParameter: _("A 'set' accessor cannot have rest parameter."),
|
8278 | SetAccesorCannotHaveReturnType: _("A 'set' accessor cannot have a return type annotation."),
|
8279 | SingleTypeParameterWithoutTrailingComma: _(({
|
8280 | typeParameterName
|
8281 | }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`),
|
8282 | StaticBlockCannotHaveModifier: _("Static class blocks cannot have any modifier."),
|
8283 | TypeAnnotationAfterAssign: _("Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`."),
|
8284 | TypeImportCannotSpecifyDefaultAndNamed: _("A type-only import can specify a default import or named bindings, but not both."),
|
8285 | TypeModifierIsUsedInTypeExports: _("The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement."),
|
8286 | TypeModifierIsUsedInTypeImports: _("The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement."),
|
8287 | UnexpectedParameterModifier: _("A parameter property is only allowed in a constructor implementation."),
|
8288 | UnexpectedReadonly: _("'readonly' type modifier is only permitted on array and tuple literal types."),
|
8289 | UnexpectedTypeAnnotation: _("Did not expect a type annotation here."),
|
8290 | UnexpectedTypeCastInParameter: _("Unexpected type cast in parameter position."),
|
8291 | UnsupportedImportTypeArgument: _("Argument in a type import must be a string literal."),
|
8292 | UnsupportedParameterPropertyKind: _("A parameter property may not be declared using a binding pattern."),
|
8293 | UnsupportedSignatureParameterKind: _(({
|
8294 | type
|
8295 | }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`)
|
8296 | }));
|
8297 |
|
8298 | function keywordTypeFromName(value) {
|
8299 | switch (value) {
|
8300 | case "any":
|
8301 | return "TSAnyKeyword";
|
8302 |
|
8303 | case "boolean":
|
8304 | return "TSBooleanKeyword";
|
8305 |
|
8306 | case "bigint":
|
8307 | return "TSBigIntKeyword";
|
8308 |
|
8309 | case "never":
|
8310 | return "TSNeverKeyword";
|
8311 |
|
8312 | case "number":
|
8313 | return "TSNumberKeyword";
|
8314 |
|
8315 | case "object":
|
8316 | return "TSObjectKeyword";
|
8317 |
|
8318 | case "string":
|
8319 | return "TSStringKeyword";
|
8320 |
|
8321 | case "symbol":
|
8322 | return "TSSymbolKeyword";
|
8323 |
|
8324 | case "undefined":
|
8325 | return "TSUndefinedKeyword";
|
8326 |
|
8327 | case "unknown":
|
8328 | return "TSUnknownKeyword";
|
8329 |
|
8330 | default:
|
8331 | return undefined;
|
8332 | }
|
8333 | }
|
8334 |
|
8335 | function tsIsAccessModifier(modifier) {
|
8336 | return modifier === "private" || modifier === "public" || modifier === "protected";
|
8337 | }
|
8338 |
|
8339 | function tsIsVarianceAnnotations(modifier) {
|
8340 | return modifier === "in" || modifier === "out";
|
8341 | }
|
8342 |
|
8343 | var typescript = (superClass => class extends superClass {
|
8344 | getScopeHandler() {
|
8345 | return TypeScriptScopeHandler;
|
8346 | }
|
8347 |
|
8348 | tsIsIdentifier() {
|
8349 | return tokenIsIdentifier(this.state.type);
|
8350 | }
|
8351 |
|
8352 | tsTokenCanFollowModifier() {
|
8353 | return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(134) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
|
8354 | }
|
8355 |
|
8356 | tsNextTokenCanFollowModifier() {
|
8357 | this.next();
|
8358 | return this.tsTokenCanFollowModifier();
|
8359 | }
|
8360 |
|
8361 | tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
|
8362 | if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58) {
|
8363 | return undefined;
|
8364 | }
|
8365 |
|
8366 | const modifier = this.state.value;
|
8367 |
|
8368 | if (allowedModifiers.indexOf(modifier) !== -1) {
|
8369 | if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
|
8370 | return undefined;
|
8371 | }
|
8372 |
|
8373 | if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
|
8374 | return modifier;
|
8375 | }
|
8376 | }
|
8377 |
|
8378 | return undefined;
|
8379 | }
|
8380 |
|
8381 | tsParseModifiers({
|
8382 | modified,
|
8383 | allowedModifiers,
|
8384 | disallowedModifiers,
|
8385 | stopOnStartOfClassStaticBlock,
|
8386 | errorTemplate = TSErrors.InvalidModifierOnTypeMember
|
8387 | }) {
|
8388 | const enforceOrder = (loc, modifier, before, after) => {
|
8389 | if (modifier === before && modified[after]) {
|
8390 | this.raise(TSErrors.InvalidModifiersOrder, {
|
8391 | at: loc,
|
8392 | orderedModifiers: [before, after]
|
8393 | });
|
8394 | }
|
8395 | };
|
8396 |
|
8397 | const incompatible = (loc, modifier, mod1, mod2) => {
|
8398 | if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
|
8399 | this.raise(TSErrors.IncompatibleModifiers, {
|
8400 | at: loc,
|
8401 | modifiers: [mod1, mod2]
|
8402 | });
|
8403 | }
|
8404 | };
|
8405 |
|
8406 | for (;;) {
|
8407 | const {
|
8408 | startLoc
|
8409 | } = this.state;
|
8410 | const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
|
8411 | if (!modifier) break;
|
8412 |
|
8413 | if (tsIsAccessModifier(modifier)) {
|
8414 | if (modified.accessibility) {
|
8415 | this.raise(TSErrors.DuplicateAccessibilityModifier, {
|
8416 | at: startLoc,
|
8417 | modifier
|
8418 | });
|
8419 | } else {
|
8420 | enforceOrder(startLoc, modifier, modifier, "override");
|
8421 | enforceOrder(startLoc, modifier, modifier, "static");
|
8422 | enforceOrder(startLoc, modifier, modifier, "readonly");
|
8423 | modified.accessibility = modifier;
|
8424 | }
|
8425 | } else if (tsIsVarianceAnnotations(modifier)) {
|
8426 | if (modified[modifier]) {
|
8427 | this.raise(TSErrors.DuplicateModifier, {
|
8428 | at: startLoc,
|
8429 | modifier
|
8430 | });
|
8431 | }
|
8432 |
|
8433 | modified[modifier] = true;
|
8434 | enforceOrder(startLoc, modifier, "in", "out");
|
8435 | } else {
|
8436 | if (Object.hasOwnProperty.call(modified, modifier)) {
|
8437 | this.raise(TSErrors.DuplicateModifier, {
|
8438 | at: startLoc,
|
8439 | modifier
|
8440 | });
|
8441 | } else {
|
8442 | enforceOrder(startLoc, modifier, "static", "readonly");
|
8443 | enforceOrder(startLoc, modifier, "static", "override");
|
8444 | enforceOrder(startLoc, modifier, "override", "readonly");
|
8445 | enforceOrder(startLoc, modifier, "abstract", "override");
|
8446 | incompatible(startLoc, modifier, "declare", "override");
|
8447 | incompatible(startLoc, modifier, "static", "abstract");
|
8448 | }
|
8449 |
|
8450 | modified[modifier] = true;
|
8451 | }
|
8452 |
|
8453 | if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
|
8454 | this.raise(errorTemplate, {
|
8455 | at: startLoc,
|
8456 | modifier
|
8457 | });
|
8458 | }
|
8459 | }
|
8460 | }
|
8461 |
|
8462 | tsIsListTerminator(kind) {
|
8463 | switch (kind) {
|
8464 | case "EnumMembers":
|
8465 | case "TypeMembers":
|
8466 | return this.match(8);
|
8467 |
|
8468 | case "HeritageClauseElement":
|
8469 | return this.match(5);
|
8470 |
|
8471 | case "TupleElementTypes":
|
8472 | return this.match(3);
|
8473 |
|
8474 | case "TypeParametersOrArguments":
|
8475 | return this.match(48);
|
8476 | }
|
8477 |
|
8478 | throw new Error("Unreachable");
|
8479 | }
|
8480 |
|
8481 | tsParseList(kind, parseElement) {
|
8482 | const result = [];
|
8483 |
|
8484 | while (!this.tsIsListTerminator(kind)) {
|
8485 | result.push(parseElement());
|
8486 | }
|
8487 |
|
8488 | return result;
|
8489 | }
|
8490 |
|
8491 | tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
|
8492 | return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
|
8493 | }
|
8494 |
|
8495 | tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
|
8496 | const result = [];
|
8497 | let trailingCommaPos = -1;
|
8498 |
|
8499 | for (;;) {
|
8500 | if (this.tsIsListTerminator(kind)) {
|
8501 | break;
|
8502 | }
|
8503 |
|
8504 | trailingCommaPos = -1;
|
8505 | const element = parseElement();
|
8506 |
|
8507 | if (element == null) {
|
8508 | return undefined;
|
8509 | }
|
8510 |
|
8511 | result.push(element);
|
8512 |
|
8513 | if (this.eat(12)) {
|
8514 | trailingCommaPos = this.state.lastTokStart;
|
8515 | continue;
|
8516 | }
|
8517 |
|
8518 | if (this.tsIsListTerminator(kind)) {
|
8519 | break;
|
8520 | }
|
8521 |
|
8522 | if (expectSuccess) {
|
8523 | this.expect(12);
|
8524 | }
|
8525 |
|
8526 | return undefined;
|
8527 | }
|
8528 |
|
8529 | if (refTrailingCommaPos) {
|
8530 | refTrailingCommaPos.value = trailingCommaPos;
|
8531 | }
|
8532 |
|
8533 | return result;
|
8534 | }
|
8535 |
|
8536 | tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
|
8537 | if (!skipFirstToken) {
|
8538 | if (bracket) {
|
8539 | this.expect(0);
|
8540 | } else {
|
8541 | this.expect(47);
|
8542 | }
|
8543 | }
|
8544 |
|
8545 | const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
|
8546 |
|
8547 | if (bracket) {
|
8548 | this.expect(3);
|
8549 | } else {
|
8550 | this.expect(48);
|
8551 | }
|
8552 |
|
8553 | return result;
|
8554 | }
|
8555 |
|
8556 | tsParseImportType() {
|
8557 | const node = this.startNode();
|
8558 | this.expect(83);
|
8559 | this.expect(10);
|
8560 |
|
8561 | if (!this.match(129)) {
|
8562 | this.raise(TSErrors.UnsupportedImportTypeArgument, {
|
8563 | at: this.state.startLoc
|
8564 | });
|
8565 | }
|
8566 |
|
8567 | node.argument = this.parseExprAtom();
|
8568 | this.expect(11);
|
8569 |
|
8570 | if (this.eat(16)) {
|
8571 | node.qualifier = this.tsParseEntityName();
|
8572 | }
|
8573 |
|
8574 | if (this.match(47)) {
|
8575 | node.typeParameters = this.tsParseTypeArguments();
|
8576 | }
|
8577 |
|
8578 | return this.finishNode(node, "TSImportType");
|
8579 | }
|
8580 |
|
8581 | tsParseEntityName(allowReservedWords = true) {
|
8582 | let entity = this.parseIdentifier(allowReservedWords);
|
8583 |
|
8584 | while (this.eat(16)) {
|
8585 | const node = this.startNodeAtNode(entity);
|
8586 | node.left = entity;
|
8587 | node.right = this.parseIdentifier(allowReservedWords);
|
8588 | entity = this.finishNode(node, "TSQualifiedName");
|
8589 | }
|
8590 |
|
8591 | return entity;
|
8592 | }
|
8593 |
|
8594 | tsParseTypeReference() {
|
8595 | const node = this.startNode();
|
8596 | node.typeName = this.tsParseEntityName();
|
8597 |
|
8598 | if (!this.hasPrecedingLineBreak() && this.match(47)) {
|
8599 | node.typeParameters = this.tsParseTypeArguments();
|
8600 | }
|
8601 |
|
8602 | return this.finishNode(node, "TSTypeReference");
|
8603 | }
|
8604 |
|
8605 | tsParseThisTypePredicate(lhs) {
|
8606 | this.next();
|
8607 | const node = this.startNodeAtNode(lhs);
|
8608 | node.parameterName = lhs;
|
8609 | node.typeAnnotation = this.tsParseTypeAnnotation(false);
|
8610 | node.asserts = false;
|
8611 | return this.finishNode(node, "TSTypePredicate");
|
8612 | }
|
8613 |
|
8614 | tsParseThisTypeNode() {
|
8615 | const node = this.startNode();
|
8616 | this.next();
|
8617 | return this.finishNode(node, "TSThisType");
|
8618 | }
|
8619 |
|
8620 | tsParseTypeQuery() {
|
8621 | const node = this.startNode();
|
8622 | this.expect(87);
|
8623 |
|
8624 | if (this.match(83)) {
|
8625 | node.exprName = this.tsParseImportType();
|
8626 | } else {
|
8627 | node.exprName = this.tsParseEntityName();
|
8628 | }
|
8629 |
|
8630 | if (!this.hasPrecedingLineBreak() && this.match(47)) {
|
8631 | node.typeParameters = this.tsParseTypeArguments();
|
8632 | }
|
8633 |
|
8634 | return this.finishNode(node, "TSTypeQuery");
|
8635 | }
|
8636 |
|
8637 | tsParseInOutModifiers(node) {
|
8638 | this.tsParseModifiers({
|
8639 | modified: node,
|
8640 | allowedModifiers: ["in", "out"],
|
8641 | disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
|
8642 | errorTemplate: TSErrors.InvalidModifierOnTypeParameter
|
8643 | });
|
8644 | }
|
8645 |
|
8646 | tsParseNoneModifiers(node) {
|
8647 | this.tsParseModifiers({
|
8648 | modified: node,
|
8649 | allowedModifiers: [],
|
8650 | disallowedModifiers: ["in", "out"],
|
8651 | errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
|
8652 | });
|
8653 | }
|
8654 |
|
8655 | tsParseTypeParameter(parseModifiers = this.tsParseNoneModifiers.bind(this)) {
|
8656 | const node = this.startNode();
|
8657 | parseModifiers(node);
|
8658 | node.name = this.tsParseTypeParameterName();
|
8659 | node.constraint = this.tsEatThenParseType(81);
|
8660 | node.default = this.tsEatThenParseType(29);
|
8661 | return this.finishNode(node, "TSTypeParameter");
|
8662 | }
|
8663 |
|
8664 | tsTryParseTypeParameters(parseModifiers) {
|
8665 | if (this.match(47)) {
|
8666 | return this.tsParseTypeParameters(parseModifiers);
|
8667 | }
|
8668 | }
|
8669 |
|
8670 | tsParseTypeParameters(parseModifiers) {
|
8671 | const node = this.startNode();
|
8672 |
|
8673 | if (this.match(47) || this.match(138)) {
|
8674 | this.next();
|
8675 | } else {
|
8676 | this.unexpected();
|
8677 | }
|
8678 |
|
8679 | const refTrailingCommaPos = {
|
8680 | value: -1
|
8681 | };
|
8682 | node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
|
8683 |
|
8684 | if (node.params.length === 0) {
|
8685 | this.raise(TSErrors.EmptyTypeParameters, {
|
8686 | at: node
|
8687 | });
|
8688 | }
|
8689 |
|
8690 | if (refTrailingCommaPos.value !== -1) {
|
8691 | this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
|
8692 | }
|
8693 |
|
8694 | return this.finishNode(node, "TSTypeParameterDeclaration");
|
8695 | }
|
8696 |
|
8697 | tsTryNextParseConstantContext() {
|
8698 | if (this.lookahead().type !== 75) return null;
|
8699 | this.next();
|
8700 | const typeReference = this.tsParseTypeReference();
|
8701 |
|
8702 | if (typeReference.typeParameters) {
|
8703 | this.raise(TSErrors.CannotFindName, {
|
8704 | at: typeReference.typeName,
|
8705 | name: "const"
|
8706 | });
|
8707 | }
|
8708 |
|
8709 | return typeReference;
|
8710 | }
|
8711 |
|
8712 | tsFillSignature(returnToken, signature) {
|
8713 | const returnTokenRequired = returnToken === 19;
|
8714 | const paramsKey = "parameters";
|
8715 | const returnTypeKey = "typeAnnotation";
|
8716 | signature.typeParameters = this.tsTryParseTypeParameters();
|
8717 | this.expect(10);
|
8718 | signature[paramsKey] = this.tsParseBindingListForSignature();
|
8719 |
|
8720 | if (returnTokenRequired) {
|
8721 | signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
8722 | } else if (this.match(returnToken)) {
|
8723 | signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
8724 | }
|
8725 | }
|
8726 |
|
8727 | tsParseBindingListForSignature() {
|
8728 | return this.parseBindingList(11, 41).map(pattern => {
|
8729 | if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
|
8730 | this.raise(TSErrors.UnsupportedSignatureParameterKind, {
|
8731 | at: pattern,
|
8732 | type: pattern.type
|
8733 | });
|
8734 | }
|
8735 |
|
8736 | return pattern;
|
8737 | });
|
8738 | }
|
8739 |
|
8740 | tsParseTypeMemberSemicolon() {
|
8741 | if (!this.eat(12) && !this.isLineTerminator()) {
|
8742 | this.expect(13);
|
8743 | }
|
8744 | }
|
8745 |
|
8746 | tsParseSignatureMember(kind, node) {
|
8747 | this.tsFillSignature(14, node);
|
8748 | this.tsParseTypeMemberSemicolon();
|
8749 | return this.finishNode(node, kind);
|
8750 | }
|
8751 |
|
8752 | tsIsUnambiguouslyIndexSignature() {
|
8753 | this.next();
|
8754 |
|
8755 | if (tokenIsIdentifier(this.state.type)) {
|
8756 | this.next();
|
8757 | return this.match(14);
|
8758 | }
|
8759 |
|
8760 | return false;
|
8761 | }
|
8762 |
|
8763 | tsTryParseIndexSignature(node) {
|
8764 | if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
|
8765 | return undefined;
|
8766 | }
|
8767 |
|
8768 | this.expect(0);
|
8769 | const id = this.parseIdentifier();
|
8770 | id.typeAnnotation = this.tsParseTypeAnnotation();
|
8771 | this.resetEndLocation(id);
|
8772 | this.expect(3);
|
8773 | node.parameters = [id];
|
8774 | const type = this.tsTryParseTypeAnnotation();
|
8775 | if (type) node.typeAnnotation = type;
|
8776 | this.tsParseTypeMemberSemicolon();
|
8777 | return this.finishNode(node, "TSIndexSignature");
|
8778 | }
|
8779 |
|
8780 | tsParsePropertyOrMethodSignature(node, readonly) {
|
8781 | if (this.eat(17)) node.optional = true;
|
8782 | const nodeAny = node;
|
8783 |
|
8784 | if (this.match(10) || this.match(47)) {
|
8785 | if (readonly) {
|
8786 | this.raise(TSErrors.ReadonlyForMethodSignature, {
|
8787 | at: node
|
8788 | });
|
8789 | }
|
8790 |
|
8791 | const method = nodeAny;
|
8792 |
|
8793 | if (method.kind && this.match(47)) {
|
8794 | this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
|
8795 | at: this.state.curPosition()
|
8796 | });
|
8797 | }
|
8798 |
|
8799 | this.tsFillSignature(14, method);
|
8800 | this.tsParseTypeMemberSemicolon();
|
8801 | const paramsKey = "parameters";
|
8802 | const returnTypeKey = "typeAnnotation";
|
8803 |
|
8804 | if (method.kind === "get") {
|
8805 | if (method[paramsKey].length > 0) {
|
8806 | this.raise(Errors.BadGetterArity, {
|
8807 | at: this.state.curPosition()
|
8808 | });
|
8809 |
|
8810 | if (this.isThisParam(method[paramsKey][0])) {
|
8811 | this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
|
8812 | at: this.state.curPosition()
|
8813 | });
|
8814 | }
|
8815 | }
|
8816 | } else if (method.kind === "set") {
|
8817 | if (method[paramsKey].length !== 1) {
|
8818 | this.raise(Errors.BadSetterArity, {
|
8819 | at: this.state.curPosition()
|
8820 | });
|
8821 | } else {
|
8822 | const firstParameter = method[paramsKey][0];
|
8823 |
|
8824 | if (this.isThisParam(firstParameter)) {
|
8825 | this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
|
8826 | at: this.state.curPosition()
|
8827 | });
|
8828 | }
|
8829 |
|
8830 | if (firstParameter.type === "Identifier" && firstParameter.optional) {
|
8831 | this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
|
8832 | at: this.state.curPosition()
|
8833 | });
|
8834 | }
|
8835 |
|
8836 | if (firstParameter.type === "RestElement") {
|
8837 | this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
|
8838 | at: this.state.curPosition()
|
8839 | });
|
8840 | }
|
8841 | }
|
8842 |
|
8843 | if (method[returnTypeKey]) {
|
8844 | this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
|
8845 | at: method[returnTypeKey]
|
8846 | });
|
8847 | }
|
8848 | } else {
|
8849 | method.kind = "method";
|
8850 | }
|
8851 |
|
8852 | return this.finishNode(method, "TSMethodSignature");
|
8853 | } else {
|
8854 | const property = nodeAny;
|
8855 | if (readonly) property.readonly = true;
|
8856 | const type = this.tsTryParseTypeAnnotation();
|
8857 | if (type) property.typeAnnotation = type;
|
8858 | this.tsParseTypeMemberSemicolon();
|
8859 | return this.finishNode(property, "TSPropertySignature");
|
8860 | }
|
8861 | }
|
8862 |
|
8863 | tsParseTypeMember() {
|
8864 | const node = this.startNode();
|
8865 |
|
8866 | if (this.match(10) || this.match(47)) {
|
8867 | return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
|
8868 | }
|
8869 |
|
8870 | if (this.match(77)) {
|
8871 | const id = this.startNode();
|
8872 | this.next();
|
8873 |
|
8874 | if (this.match(10) || this.match(47)) {
|
8875 | return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
|
8876 | } else {
|
8877 | node.key = this.createIdentifier(id, "new");
|
8878 | return this.tsParsePropertyOrMethodSignature(node, false);
|
8879 | }
|
8880 | }
|
8881 |
|
8882 | this.tsParseModifiers({
|
8883 | modified: node,
|
8884 | allowedModifiers: ["readonly"],
|
8885 | disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
|
8886 | });
|
8887 | const idx = this.tsTryParseIndexSignature(node);
|
8888 |
|
8889 | if (idx) {
|
8890 | return idx;
|
8891 | }
|
8892 |
|
8893 | this.parsePropertyName(node);
|
8894 |
|
8895 | if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
|
8896 | node.kind = node.key.name;
|
8897 | this.parsePropertyName(node);
|
8898 | }
|
8899 |
|
8900 | return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
|
8901 | }
|
8902 |
|
8903 | tsParseTypeLiteral() {
|
8904 | const node = this.startNode();
|
8905 | node.members = this.tsParseObjectTypeMembers();
|
8906 | return this.finishNode(node, "TSTypeLiteral");
|
8907 | }
|
8908 |
|
8909 | tsParseObjectTypeMembers() {
|
8910 | this.expect(5);
|
8911 | const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
|
8912 | this.expect(8);
|
8913 | return members;
|
8914 | }
|
8915 |
|
8916 | tsIsStartOfMappedType() {
|
8917 | this.next();
|
8918 |
|
8919 | if (this.eat(53)) {
|
8920 | return this.isContextual(118);
|
8921 | }
|
8922 |
|
8923 | if (this.isContextual(118)) {
|
8924 | this.next();
|
8925 | }
|
8926 |
|
8927 | if (!this.match(0)) {
|
8928 | return false;
|
8929 | }
|
8930 |
|
8931 | this.next();
|
8932 |
|
8933 | if (!this.tsIsIdentifier()) {
|
8934 | return false;
|
8935 | }
|
8936 |
|
8937 | this.next();
|
8938 | return this.match(58);
|
8939 | }
|
8940 |
|
8941 | tsParseMappedTypeParameter() {
|
8942 | const node = this.startNode();
|
8943 | node.name = this.tsParseTypeParameterName();
|
8944 | node.constraint = this.tsExpectThenParseType(58);
|
8945 | return this.finishNode(node, "TSTypeParameter");
|
8946 | }
|
8947 |
|
8948 | tsParseMappedType() {
|
8949 | const node = this.startNode();
|
8950 | this.expect(5);
|
8951 |
|
8952 | if (this.match(53)) {
|
8953 | node.readonly = this.state.value;
|
8954 | this.next();
|
8955 | this.expectContextual(118);
|
8956 | } else if (this.eatContextual(118)) {
|
8957 | node.readonly = true;
|
8958 | }
|
8959 |
|
8960 | this.expect(0);
|
8961 | node.typeParameter = this.tsParseMappedTypeParameter();
|
8962 | node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
|
8963 | this.expect(3);
|
8964 |
|
8965 | if (this.match(53)) {
|
8966 | node.optional = this.state.value;
|
8967 | this.next();
|
8968 | this.expect(17);
|
8969 | } else if (this.eat(17)) {
|
8970 | node.optional = true;
|
8971 | }
|
8972 |
|
8973 | node.typeAnnotation = this.tsTryParseType();
|
8974 | this.semicolon();
|
8975 | this.expect(8);
|
8976 | return this.finishNode(node, "TSMappedType");
|
8977 | }
|
8978 |
|
8979 | tsParseTupleType() {
|
8980 | const node = this.startNode();
|
8981 | node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
|
8982 | let seenOptionalElement = false;
|
8983 | let labeledElements = null;
|
8984 | node.elementTypes.forEach(elementNode => {
|
8985 | var _labeledElements;
|
8986 |
|
8987 | let {
|
8988 | type
|
8989 | } = elementNode;
|
8990 |
|
8991 | if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
|
8992 | this.raise(TSErrors.OptionalTypeBeforeRequired, {
|
8993 | at: elementNode
|
8994 | });
|
8995 | }
|
8996 |
|
8997 | seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
|
8998 |
|
8999 | if (type === "TSRestType") {
|
9000 | elementNode = elementNode.typeAnnotation;
|
9001 | type = elementNode.type;
|
9002 | }
|
9003 |
|
9004 | const isLabeled = type === "TSNamedTupleMember";
|
9005 | labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
|
9006 |
|
9007 | if (labeledElements !== isLabeled) {
|
9008 | this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
|
9009 | at: elementNode
|
9010 | });
|
9011 | }
|
9012 | });
|
9013 | return this.finishNode(node, "TSTupleType");
|
9014 | }
|
9015 |
|
9016 | tsParseTupleElementType() {
|
9017 | const {
|
9018 | start: startPos,
|
9019 | startLoc
|
9020 | } = this.state;
|
9021 | const rest = this.eat(21);
|
9022 | let type = this.tsParseType();
|
9023 | const optional = this.eat(17);
|
9024 | const labeled = this.eat(14);
|
9025 |
|
9026 | if (labeled) {
|
9027 | const labeledNode = this.startNodeAtNode(type);
|
9028 | labeledNode.optional = optional;
|
9029 |
|
9030 | if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
|
9031 | labeledNode.label = type.typeName;
|
9032 | } else {
|
9033 | this.raise(TSErrors.InvalidTupleMemberLabel, {
|
9034 | at: type
|
9035 | });
|
9036 | labeledNode.label = type;
|
9037 | }
|
9038 |
|
9039 | labeledNode.elementType = this.tsParseType();
|
9040 | type = this.finishNode(labeledNode, "TSNamedTupleMember");
|
9041 | } else if (optional) {
|
9042 | const optionalTypeNode = this.startNodeAtNode(type);
|
9043 | optionalTypeNode.typeAnnotation = type;
|
9044 | type = this.finishNode(optionalTypeNode, "TSOptionalType");
|
9045 | }
|
9046 |
|
9047 | if (rest) {
|
9048 | const restNode = this.startNodeAt(startPos, startLoc);
|
9049 | restNode.typeAnnotation = type;
|
9050 | type = this.finishNode(restNode, "TSRestType");
|
9051 | }
|
9052 |
|
9053 | return type;
|
9054 | }
|
9055 |
|
9056 | tsParseParenthesizedType() {
|
9057 | const node = this.startNode();
|
9058 | this.expect(10);
|
9059 | node.typeAnnotation = this.tsParseType();
|
9060 | this.expect(11);
|
9061 | return this.finishNode(node, "TSParenthesizedType");
|
9062 | }
|
9063 |
|
9064 | tsParseFunctionOrConstructorType(type, abstract) {
|
9065 | const node = this.startNode();
|
9066 |
|
9067 | if (type === "TSConstructorType") {
|
9068 | node.abstract = !!abstract;
|
9069 | if (abstract) this.next();
|
9070 | this.next();
|
9071 | }
|
9072 |
|
9073 | this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
|
9074 | return this.finishNode(node, type);
|
9075 | }
|
9076 |
|
9077 | tsParseLiteralTypeNode() {
|
9078 | const node = this.startNode();
|
9079 |
|
9080 | node.literal = (() => {
|
9081 | switch (this.state.type) {
|
9082 | case 130:
|
9083 | case 131:
|
9084 | case 129:
|
9085 | case 85:
|
9086 | case 86:
|
9087 | return this.parseExprAtom();
|
9088 |
|
9089 | default:
|
9090 | throw this.unexpected();
|
9091 | }
|
9092 | })();
|
9093 |
|
9094 | return this.finishNode(node, "TSLiteralType");
|
9095 | }
|
9096 |
|
9097 | tsParseTemplateLiteralType() {
|
9098 | const node = this.startNode();
|
9099 | node.literal = this.parseTemplate(false);
|
9100 | return this.finishNode(node, "TSLiteralType");
|
9101 | }
|
9102 |
|
9103 | parseTemplateSubstitution() {
|
9104 | if (this.state.inType) return this.tsParseType();
|
9105 | return super.parseTemplateSubstitution();
|
9106 | }
|
9107 |
|
9108 | tsParseThisTypeOrThisTypePredicate() {
|
9109 | const thisKeyword = this.tsParseThisTypeNode();
|
9110 |
|
9111 | if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
|
9112 | return this.tsParseThisTypePredicate(thisKeyword);
|
9113 | } else {
|
9114 | return thisKeyword;
|
9115 | }
|
9116 | }
|
9117 |
|
9118 | tsParseNonArrayType() {
|
9119 | switch (this.state.type) {
|
9120 | case 129:
|
9121 | case 130:
|
9122 | case 131:
|
9123 | case 85:
|
9124 | case 86:
|
9125 | return this.tsParseLiteralTypeNode();
|
9126 |
|
9127 | case 53:
|
9128 | if (this.state.value === "-") {
|
9129 | const node = this.startNode();
|
9130 | const nextToken = this.lookahead();
|
9131 |
|
9132 | if (nextToken.type !== 130 && nextToken.type !== 131) {
|
9133 | throw this.unexpected();
|
9134 | }
|
9135 |
|
9136 | node.literal = this.parseMaybeUnary();
|
9137 | return this.finishNode(node, "TSLiteralType");
|
9138 | }
|
9139 |
|
9140 | break;
|
9141 |
|
9142 | case 78:
|
9143 | return this.tsParseThisTypeOrThisTypePredicate();
|
9144 |
|
9145 | case 87:
|
9146 | return this.tsParseTypeQuery();
|
9147 |
|
9148 | case 83:
|
9149 | return this.tsParseImportType();
|
9150 |
|
9151 | case 5:
|
9152 | return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
|
9153 |
|
9154 | case 0:
|
9155 | return this.tsParseTupleType();
|
9156 |
|
9157 | case 10:
|
9158 | return this.tsParseParenthesizedType();
|
9159 |
|
9160 | case 25:
|
9161 | case 24:
|
9162 | return this.tsParseTemplateLiteralType();
|
9163 |
|
9164 | default:
|
9165 | {
|
9166 | const {
|
9167 | type
|
9168 | } = this.state;
|
9169 |
|
9170 | if (tokenIsIdentifier(type) || type === 88 || type === 84) {
|
9171 | const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
|
9172 |
|
9173 | if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
|
9174 | const node = this.startNode();
|
9175 | this.next();
|
9176 | return this.finishNode(node, nodeType);
|
9177 | }
|
9178 |
|
9179 | return this.tsParseTypeReference();
|
9180 | }
|
9181 | }
|
9182 | }
|
9183 |
|
9184 | throw this.unexpected();
|
9185 | }
|
9186 |
|
9187 | tsParseArrayTypeOrHigher() {
|
9188 | let type = this.tsParseNonArrayType();
|
9189 |
|
9190 | while (!this.hasPrecedingLineBreak() && this.eat(0)) {
|
9191 | if (this.match(3)) {
|
9192 | const node = this.startNodeAtNode(type);
|
9193 | node.elementType = type;
|
9194 | this.expect(3);
|
9195 | type = this.finishNode(node, "TSArrayType");
|
9196 | } else {
|
9197 | const node = this.startNodeAtNode(type);
|
9198 | node.objectType = type;
|
9199 | node.indexType = this.tsParseType();
|
9200 | this.expect(3);
|
9201 | type = this.finishNode(node, "TSIndexedAccessType");
|
9202 | }
|
9203 | }
|
9204 |
|
9205 | return type;
|
9206 | }
|
9207 |
|
9208 | tsParseTypeOperator() {
|
9209 | const node = this.startNode();
|
9210 | const operator = this.state.value;
|
9211 | this.next();
|
9212 | node.operator = operator;
|
9213 | node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
|
9214 |
|
9215 | if (operator === "readonly") {
|
9216 | this.tsCheckTypeAnnotationForReadOnly(node);
|
9217 | }
|
9218 |
|
9219 | return this.finishNode(node, "TSTypeOperator");
|
9220 | }
|
9221 |
|
9222 | tsCheckTypeAnnotationForReadOnly(node) {
|
9223 | switch (node.typeAnnotation.type) {
|
9224 | case "TSTupleType":
|
9225 | case "TSArrayType":
|
9226 | return;
|
9227 |
|
9228 | default:
|
9229 | this.raise(TSErrors.UnexpectedReadonly, {
|
9230 | at: node
|
9231 | });
|
9232 | }
|
9233 | }
|
9234 |
|
9235 | tsParseInferType() {
|
9236 | const node = this.startNode();
|
9237 | this.expectContextual(112);
|
9238 | const typeParameter = this.startNode();
|
9239 | typeParameter.name = this.tsParseTypeParameterName();
|
9240 | typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
|
9241 | node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
|
9242 | return this.finishNode(node, "TSInferType");
|
9243 | }
|
9244 |
|
9245 | tsParseConstraintForInferType() {
|
9246 | if (this.eat(81)) {
|
9247 | const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
|
9248 |
|
9249 | if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
|
9250 | return constraint;
|
9251 | }
|
9252 | }
|
9253 | }
|
9254 |
|
9255 | tsParseTypeOperatorOrHigher() {
|
9256 | const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
|
9257 | return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(112) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
|
9258 | }
|
9259 |
|
9260 | tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
|
9261 | const node = this.startNode();
|
9262 | const hasLeadingOperator = this.eat(operator);
|
9263 | const types = [];
|
9264 |
|
9265 | do {
|
9266 | types.push(parseConstituentType());
|
9267 | } while (this.eat(operator));
|
9268 |
|
9269 | if (types.length === 1 && !hasLeadingOperator) {
|
9270 | return types[0];
|
9271 | }
|
9272 |
|
9273 | node.types = types;
|
9274 | return this.finishNode(node, kind);
|
9275 | }
|
9276 |
|
9277 | tsParseIntersectionTypeOrHigher() {
|
9278 | return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
|
9279 | }
|
9280 |
|
9281 | tsParseUnionTypeOrHigher() {
|
9282 | return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
|
9283 | }
|
9284 |
|
9285 | tsIsStartOfFunctionType() {
|
9286 | if (this.match(47)) {
|
9287 | return true;
|
9288 | }
|
9289 |
|
9290 | return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
|
9291 | }
|
9292 |
|
9293 | tsSkipParameterStart() {
|
9294 | if (tokenIsIdentifier(this.state.type) || this.match(78)) {
|
9295 | this.next();
|
9296 | return true;
|
9297 | }
|
9298 |
|
9299 | if (this.match(5)) {
|
9300 | const {
|
9301 | errors
|
9302 | } = this.state;
|
9303 | const previousErrorCount = errors.length;
|
9304 |
|
9305 | try {
|
9306 | this.parseObjectLike(8, true);
|
9307 | return errors.length === previousErrorCount;
|
9308 | } catch (_unused) {
|
9309 | return false;
|
9310 | }
|
9311 | }
|
9312 |
|
9313 | if (this.match(0)) {
|
9314 | this.next();
|
9315 | const {
|
9316 | errors
|
9317 | } = this.state;
|
9318 | const previousErrorCount = errors.length;
|
9319 |
|
9320 | try {
|
9321 | this.parseBindingList(3, 93, true);
|
9322 | return errors.length === previousErrorCount;
|
9323 | } catch (_unused2) {
|
9324 | return false;
|
9325 | }
|
9326 | }
|
9327 |
|
9328 | return false;
|
9329 | }
|
9330 |
|
9331 | tsIsUnambiguouslyStartOfFunctionType() {
|
9332 | this.next();
|
9333 |
|
9334 | if (this.match(11) || this.match(21)) {
|
9335 | return true;
|
9336 | }
|
9337 |
|
9338 | if (this.tsSkipParameterStart()) {
|
9339 | if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
|
9340 | return true;
|
9341 | }
|
9342 |
|
9343 | if (this.match(11)) {
|
9344 | this.next();
|
9345 |
|
9346 | if (this.match(19)) {
|
9347 | return true;
|
9348 | }
|
9349 | }
|
9350 | }
|
9351 |
|
9352 | return false;
|
9353 | }
|
9354 |
|
9355 | tsParseTypeOrTypePredicateAnnotation(returnToken) {
|
9356 | return this.tsInType(() => {
|
9357 | const t = this.startNode();
|
9358 | this.expect(returnToken);
|
9359 | const node = this.startNode();
|
9360 | const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
|
9361 |
|
9362 | if (asserts && this.match(78)) {
|
9363 | let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
|
9364 |
|
9365 | if (thisTypePredicate.type === "TSThisType") {
|
9366 | node.parameterName = thisTypePredicate;
|
9367 | node.asserts = true;
|
9368 | node.typeAnnotation = null;
|
9369 | thisTypePredicate = this.finishNode(node, "TSTypePredicate");
|
9370 | } else {
|
9371 | this.resetStartLocationFromNode(thisTypePredicate, node);
|
9372 | thisTypePredicate.asserts = true;
|
9373 | }
|
9374 |
|
9375 | t.typeAnnotation = thisTypePredicate;
|
9376 | return this.finishNode(t, "TSTypeAnnotation");
|
9377 | }
|
9378 |
|
9379 | const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
|
9380 |
|
9381 | if (!typePredicateVariable) {
|
9382 | if (!asserts) {
|
9383 | return this.tsParseTypeAnnotation(false, t);
|
9384 | }
|
9385 |
|
9386 | node.parameterName = this.parseIdentifier();
|
9387 | node.asserts = asserts;
|
9388 | node.typeAnnotation = null;
|
9389 | t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
|
9390 | return this.finishNode(t, "TSTypeAnnotation");
|
9391 | }
|
9392 |
|
9393 | const type = this.tsParseTypeAnnotation(false);
|
9394 | node.parameterName = typePredicateVariable;
|
9395 | node.typeAnnotation = type;
|
9396 | node.asserts = asserts;
|
9397 | t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
|
9398 | return this.finishNode(t, "TSTypeAnnotation");
|
9399 | });
|
9400 | }
|
9401 |
|
9402 | tsTryParseTypeOrTypePredicateAnnotation() {
|
9403 | return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
|
9404 | }
|
9405 |
|
9406 | tsTryParseTypeAnnotation() {
|
9407 | return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
|
9408 | }
|
9409 |
|
9410 | tsTryParseType() {
|
9411 | return this.tsEatThenParseType(14);
|
9412 | }
|
9413 |
|
9414 | tsParseTypePredicatePrefix() {
|
9415 | const id = this.parseIdentifier();
|
9416 |
|
9417 | if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
|
9418 | this.next();
|
9419 | return id;
|
9420 | }
|
9421 | }
|
9422 |
|
9423 | tsParseTypePredicateAsserts() {
|
9424 | if (this.state.type !== 106) {
|
9425 | return false;
|
9426 | }
|
9427 |
|
9428 | const containsEsc = this.state.containsEsc;
|
9429 | this.next();
|
9430 |
|
9431 | if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
|
9432 | return false;
|
9433 | }
|
9434 |
|
9435 | if (containsEsc) {
|
9436 | this.raise(Errors.InvalidEscapedReservedWord, {
|
9437 | at: this.state.lastTokStartLoc,
|
9438 | reservedWord: "asserts"
|
9439 | });
|
9440 | }
|
9441 |
|
9442 | return true;
|
9443 | }
|
9444 |
|
9445 | tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
|
9446 | this.tsInType(() => {
|
9447 | if (eatColon) this.expect(14);
|
9448 | t.typeAnnotation = this.tsParseType();
|
9449 | });
|
9450 | return this.finishNode(t, "TSTypeAnnotation");
|
9451 | }
|
9452 |
|
9453 | tsParseType() {
|
9454 | assert(this.state.inType);
|
9455 | const type = this.tsParseNonConditionalType();
|
9456 |
|
9457 | if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
|
9458 | return type;
|
9459 | }
|
9460 |
|
9461 | const node = this.startNodeAtNode(type);
|
9462 | node.checkType = type;
|
9463 | node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
|
9464 | this.expect(17);
|
9465 | node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
|
9466 | this.expect(14);
|
9467 | node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
|
9468 | return this.finishNode(node, "TSConditionalType");
|
9469 | }
|
9470 |
|
9471 | isAbstractConstructorSignature() {
|
9472 | return this.isContextual(120) && this.lookahead().type === 77;
|
9473 | }
|
9474 |
|
9475 | tsParseNonConditionalType() {
|
9476 | if (this.tsIsStartOfFunctionType()) {
|
9477 | return this.tsParseFunctionOrConstructorType("TSFunctionType");
|
9478 | }
|
9479 |
|
9480 | if (this.match(77)) {
|
9481 | return this.tsParseFunctionOrConstructorType("TSConstructorType");
|
9482 | } else if (this.isAbstractConstructorSignature()) {
|
9483 | return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
|
9484 | }
|
9485 |
|
9486 | return this.tsParseUnionTypeOrHigher();
|
9487 | }
|
9488 |
|
9489 | tsParseTypeAssertion() {
|
9490 | if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
|
9491 | this.raise(TSErrors.ReservedTypeAssertion, {
|
9492 | at: this.state.startLoc
|
9493 | });
|
9494 | }
|
9495 |
|
9496 | const node = this.startNode();
|
9497 |
|
9498 | const _const = this.tsTryNextParseConstantContext();
|
9499 |
|
9500 | node.typeAnnotation = _const || this.tsNextThenParseType();
|
9501 | this.expect(48);
|
9502 | node.expression = this.parseMaybeUnary();
|
9503 | return this.finishNode(node, "TSTypeAssertion");
|
9504 | }
|
9505 |
|
9506 | tsParseHeritageClause(token) {
|
9507 | const originalStartLoc = this.state.startLoc;
|
9508 | const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
|
9509 | const node = this.startNode();
|
9510 | node.expression = this.tsParseEntityName();
|
9511 |
|
9512 | if (this.match(47)) {
|
9513 | node.typeParameters = this.tsParseTypeArguments();
|
9514 | }
|
9515 |
|
9516 | return this.finishNode(node, "TSExpressionWithTypeArguments");
|
9517 | });
|
9518 |
|
9519 | if (!delimitedList.length) {
|
9520 | this.raise(TSErrors.EmptyHeritageClauseType, {
|
9521 | at: originalStartLoc,
|
9522 | token
|
9523 | });
|
9524 | }
|
9525 |
|
9526 | return delimitedList;
|
9527 | }
|
9528 |
|
9529 | tsParseInterfaceDeclaration(node, properties = {}) {
|
9530 | if (this.hasFollowingLineBreak()) return null;
|
9531 | this.expectContextual(125);
|
9532 | if (properties.declare) node.declare = true;
|
9533 |
|
9534 | if (tokenIsIdentifier(this.state.type)) {
|
9535 | node.id = this.parseIdentifier();
|
9536 | this.checkIdentifier(node.id, BIND_TS_INTERFACE);
|
9537 | } else {
|
9538 | node.id = null;
|
9539 | this.raise(TSErrors.MissingInterfaceName, {
|
9540 | at: this.state.startLoc
|
9541 | });
|
9542 | }
|
9543 |
|
9544 | node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
|
9545 |
|
9546 | if (this.eat(81)) {
|
9547 | node.extends = this.tsParseHeritageClause("extends");
|
9548 | }
|
9549 |
|
9550 | const body = this.startNode();
|
9551 | body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
|
9552 | node.body = this.finishNode(body, "TSInterfaceBody");
|
9553 | return this.finishNode(node, "TSInterfaceDeclaration");
|
9554 | }
|
9555 |
|
9556 | tsParseTypeAliasDeclaration(node) {
|
9557 | node.id = this.parseIdentifier();
|
9558 | this.checkIdentifier(node.id, BIND_TS_TYPE);
|
9559 | node.typeAnnotation = this.tsInType(() => {
|
9560 | node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
|
9561 | this.expect(29);
|
9562 |
|
9563 | if (this.isContextual(111) && this.lookahead().type !== 16) {
|
9564 | const node = this.startNode();
|
9565 | this.next();
|
9566 | return this.finishNode(node, "TSIntrinsicKeyword");
|
9567 | }
|
9568 |
|
9569 | return this.tsParseType();
|
9570 | });
|
9571 | this.semicolon();
|
9572 | return this.finishNode(node, "TSTypeAliasDeclaration");
|
9573 | }
|
9574 |
|
9575 | tsInNoContext(cb) {
|
9576 | const oldContext = this.state.context;
|
9577 | this.state.context = [oldContext[0]];
|
9578 |
|
9579 | try {
|
9580 | return cb();
|
9581 | } finally {
|
9582 | this.state.context = oldContext;
|
9583 | }
|
9584 | }
|
9585 |
|
9586 | tsInType(cb) {
|
9587 | const oldInType = this.state.inType;
|
9588 | this.state.inType = true;
|
9589 |
|
9590 | try {
|
9591 | return cb();
|
9592 | } finally {
|
9593 | this.state.inType = oldInType;
|
9594 | }
|
9595 | }
|
9596 |
|
9597 | tsInDisallowConditionalTypesContext(cb) {
|
9598 | const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
|
9599 | this.state.inDisallowConditionalTypesContext = true;
|
9600 |
|
9601 | try {
|
9602 | return cb();
|
9603 | } finally {
|
9604 | this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
|
9605 | }
|
9606 | }
|
9607 |
|
9608 | tsInAllowConditionalTypesContext(cb) {
|
9609 | const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
|
9610 | this.state.inDisallowConditionalTypesContext = false;
|
9611 |
|
9612 | try {
|
9613 | return cb();
|
9614 | } finally {
|
9615 | this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
|
9616 | }
|
9617 | }
|
9618 |
|
9619 | tsEatThenParseType(token) {
|
9620 | return !this.match(token) ? undefined : this.tsNextThenParseType();
|
9621 | }
|
9622 |
|
9623 | tsExpectThenParseType(token) {
|
9624 | return this.tsDoThenParseType(() => this.expect(token));
|
9625 | }
|
9626 |
|
9627 | tsNextThenParseType() {
|
9628 | return this.tsDoThenParseType(() => this.next());
|
9629 | }
|
9630 |
|
9631 | tsDoThenParseType(cb) {
|
9632 | return this.tsInType(() => {
|
9633 | cb();
|
9634 | return this.tsParseType();
|
9635 | });
|
9636 | }
|
9637 |
|
9638 | tsParseEnumMember() {
|
9639 | const node = this.startNode();
|
9640 | node.id = this.match(129) ? this.parseExprAtom() : this.parseIdentifier(true);
|
9641 |
|
9642 | if (this.eat(29)) {
|
9643 | node.initializer = this.parseMaybeAssignAllowIn();
|
9644 | }
|
9645 |
|
9646 | return this.finishNode(node, "TSEnumMember");
|
9647 | }
|
9648 |
|
9649 | tsParseEnumDeclaration(node, properties = {}) {
|
9650 | if (properties.const) node.const = true;
|
9651 | if (properties.declare) node.declare = true;
|
9652 | this.expectContextual(122);
|
9653 | node.id = this.parseIdentifier();
|
9654 | this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
|
9655 | this.expect(5);
|
9656 | node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
|
9657 | this.expect(8);
|
9658 | return this.finishNode(node, "TSEnumDeclaration");
|
9659 | }
|
9660 |
|
9661 | tsParseModuleBlock() {
|
9662 | const node = this.startNode();
|
9663 | this.scope.enter(SCOPE_OTHER);
|
9664 | this.expect(5);
|
9665 | this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
|
9666 | this.scope.exit();
|
9667 | return this.finishNode(node, "TSModuleBlock");
|
9668 | }
|
9669 |
|
9670 | tsParseModuleOrNamespaceDeclaration(node, nested = false) {
|
9671 | node.id = this.parseIdentifier();
|
9672 |
|
9673 | if (!nested) {
|
9674 | this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
|
9675 | }
|
9676 |
|
9677 | if (this.eat(16)) {
|
9678 | const inner = this.startNode();
|
9679 | this.tsParseModuleOrNamespaceDeclaration(inner, true);
|
9680 | node.body = inner;
|
9681 | } else {
|
9682 | this.scope.enter(SCOPE_TS_MODULE);
|
9683 | this.prodParam.enter(PARAM);
|
9684 | node.body = this.tsParseModuleBlock();
|
9685 | this.prodParam.exit();
|
9686 | this.scope.exit();
|
9687 | }
|
9688 |
|
9689 | return this.finishNode(node, "TSModuleDeclaration");
|
9690 | }
|
9691 |
|
9692 | tsParseAmbientExternalModuleDeclaration(node) {
|
9693 | if (this.isContextual(109)) {
|
9694 | node.global = true;
|
9695 | node.id = this.parseIdentifier();
|
9696 | } else if (this.match(129)) {
|
9697 | node.id = this.parseExprAtom();
|
9698 | } else {
|
9699 | this.unexpected();
|
9700 | }
|
9701 |
|
9702 | if (this.match(5)) {
|
9703 | this.scope.enter(SCOPE_TS_MODULE);
|
9704 | this.prodParam.enter(PARAM);
|
9705 | node.body = this.tsParseModuleBlock();
|
9706 | this.prodParam.exit();
|
9707 | this.scope.exit();
|
9708 | } else {
|
9709 | this.semicolon();
|
9710 | }
|
9711 |
|
9712 | return this.finishNode(node, "TSModuleDeclaration");
|
9713 | }
|
9714 |
|
9715 | tsParseImportEqualsDeclaration(node, isExport) {
|
9716 | node.isExport = isExport || false;
|
9717 | node.id = this.parseIdentifier();
|
9718 | this.checkIdentifier(node.id, BIND_LEXICAL);
|
9719 | this.expect(29);
|
9720 | const moduleReference = this.tsParseModuleReference();
|
9721 |
|
9722 | if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
|
9723 | this.raise(TSErrors.ImportAliasHasImportType, {
|
9724 | at: moduleReference
|
9725 | });
|
9726 | }
|
9727 |
|
9728 | node.moduleReference = moduleReference;
|
9729 | this.semicolon();
|
9730 | return this.finishNode(node, "TSImportEqualsDeclaration");
|
9731 | }
|
9732 |
|
9733 | tsIsExternalModuleReference() {
|
9734 | return this.isContextual(116) && this.lookaheadCharCode() === 40;
|
9735 | }
|
9736 |
|
9737 | tsParseModuleReference() {
|
9738 | return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
|
9739 | }
|
9740 |
|
9741 | tsParseExternalModuleReference() {
|
9742 | const node = this.startNode();
|
9743 | this.expectContextual(116);
|
9744 | this.expect(10);
|
9745 |
|
9746 | if (!this.match(129)) {
|
9747 | throw this.unexpected();
|
9748 | }
|
9749 |
|
9750 | node.expression = this.parseExprAtom();
|
9751 | this.expect(11);
|
9752 | return this.finishNode(node, "TSExternalModuleReference");
|
9753 | }
|
9754 |
|
9755 | tsLookAhead(f) {
|
9756 | const state = this.state.clone();
|
9757 | const res = f();
|
9758 | this.state = state;
|
9759 | return res;
|
9760 | }
|
9761 |
|
9762 | tsTryParseAndCatch(f) {
|
9763 | const result = this.tryParse(abort => f() || abort());
|
9764 | if (result.aborted || !result.node) return undefined;
|
9765 | if (result.error) this.state = result.failState;
|
9766 | return result.node;
|
9767 | }
|
9768 |
|
9769 | tsTryParse(f) {
|
9770 | const state = this.state.clone();
|
9771 | const result = f();
|
9772 |
|
9773 | if (result !== undefined && result !== false) {
|
9774 | return result;
|
9775 | } else {
|
9776 | this.state = state;
|
9777 | return undefined;
|
9778 | }
|
9779 | }
|
9780 |
|
9781 | tsTryParseDeclare(nany) {
|
9782 | if (this.isLineTerminator()) {
|
9783 | return;
|
9784 | }
|
9785 |
|
9786 | let starttype = this.state.type;
|
9787 | let kind;
|
9788 |
|
9789 | if (this.isContextual(99)) {
|
9790 | starttype = 74;
|
9791 | kind = "let";
|
9792 | }
|
9793 |
|
9794 | return this.tsInAmbientContext(() => {
|
9795 | if (starttype === 68) {
|
9796 | nany.declare = true;
|
9797 | return this.parseFunctionStatement(nany, false, true);
|
9798 | }
|
9799 |
|
9800 | if (starttype === 80) {
|
9801 | nany.declare = true;
|
9802 | return this.parseClass(nany, true, false);
|
9803 | }
|
9804 |
|
9805 | if (starttype === 122) {
|
9806 | return this.tsParseEnumDeclaration(nany, {
|
9807 | declare: true
|
9808 | });
|
9809 | }
|
9810 |
|
9811 | if (starttype === 109) {
|
9812 | return this.tsParseAmbientExternalModuleDeclaration(nany);
|
9813 | }
|
9814 |
|
9815 | if (starttype === 75 || starttype === 74) {
|
9816 | if (!this.match(75) || !this.isLookaheadContextual("enum")) {
|
9817 | nany.declare = true;
|
9818 | return this.parseVarStatement(nany, kind || this.state.value, true);
|
9819 | }
|
9820 |
|
9821 | this.expect(75);
|
9822 | return this.tsParseEnumDeclaration(nany, {
|
9823 | const: true,
|
9824 | declare: true
|
9825 | });
|
9826 | }
|
9827 |
|
9828 | if (starttype === 125) {
|
9829 | const result = this.tsParseInterfaceDeclaration(nany, {
|
9830 | declare: true
|
9831 | });
|
9832 | if (result) return result;
|
9833 | }
|
9834 |
|
9835 | if (tokenIsIdentifier(starttype)) {
|
9836 | return this.tsParseDeclaration(nany, this.state.value, true);
|
9837 | }
|
9838 | });
|
9839 | }
|
9840 |
|
9841 | tsTryParseExportDeclaration() {
|
9842 | return this.tsParseDeclaration(this.startNode(), this.state.value, true);
|
9843 | }
|
9844 |
|
9845 | tsParseExpressionStatement(node, expr) {
|
9846 | switch (expr.name) {
|
9847 | case "declare":
|
9848 | {
|
9849 | const declaration = this.tsTryParseDeclare(node);
|
9850 |
|
9851 | if (declaration) {
|
9852 | declaration.declare = true;
|
9853 | return declaration;
|
9854 | }
|
9855 |
|
9856 | break;
|
9857 | }
|
9858 |
|
9859 | case "global":
|
9860 | if (this.match(5)) {
|
9861 | this.scope.enter(SCOPE_TS_MODULE);
|
9862 | this.prodParam.enter(PARAM);
|
9863 | const mod = node;
|
9864 | mod.global = true;
|
9865 | mod.id = expr;
|
9866 | mod.body = this.tsParseModuleBlock();
|
9867 | this.scope.exit();
|
9868 | this.prodParam.exit();
|
9869 | return this.finishNode(mod, "TSModuleDeclaration");
|
9870 | }
|
9871 |
|
9872 | break;
|
9873 |
|
9874 | default:
|
9875 | return this.tsParseDeclaration(node, expr.name, false);
|
9876 | }
|
9877 | }
|
9878 |
|
9879 | tsParseDeclaration(node, value, next) {
|
9880 | switch (value) {
|
9881 | case "abstract":
|
9882 | if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
|
9883 | return this.tsParseAbstractDeclaration(node);
|
9884 | }
|
9885 |
|
9886 | break;
|
9887 |
|
9888 | case "module":
|
9889 | if (this.tsCheckLineTerminator(next)) {
|
9890 | if (this.match(129)) {
|
9891 | return this.tsParseAmbientExternalModuleDeclaration(node);
|
9892 | } else if (tokenIsIdentifier(this.state.type)) {
|
9893 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
9894 | }
|
9895 | }
|
9896 |
|
9897 | break;
|
9898 |
|
9899 | case "namespace":
|
9900 | if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
|
9901 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
9902 | }
|
9903 |
|
9904 | break;
|
9905 |
|
9906 | case "type":
|
9907 | if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
|
9908 | return this.tsParseTypeAliasDeclaration(node);
|
9909 | }
|
9910 |
|
9911 | break;
|
9912 | }
|
9913 | }
|
9914 |
|
9915 | tsCheckLineTerminator(next) {
|
9916 | if (next) {
|
9917 | if (this.hasFollowingLineBreak()) return false;
|
9918 | this.next();
|
9919 | return true;
|
9920 | }
|
9921 |
|
9922 | return !this.isLineTerminator();
|
9923 | }
|
9924 |
|
9925 | tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
|
9926 | if (!this.match(47)) {
|
9927 | return undefined;
|
9928 | }
|
9929 |
|
9930 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
9931 | this.state.maybeInArrowParameters = true;
|
9932 | const res = this.tsTryParseAndCatch(() => {
|
9933 | const node = this.startNodeAt(startPos, startLoc);
|
9934 | node.typeParameters = this.tsParseTypeParameters();
|
9935 | super.parseFunctionParams(node);
|
9936 | node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
|
9937 | this.expect(19);
|
9938 | return node;
|
9939 | });
|
9940 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
9941 |
|
9942 | if (!res) {
|
9943 | return undefined;
|
9944 | }
|
9945 |
|
9946 | return this.parseArrowExpression(res, null, true);
|
9947 | }
|
9948 |
|
9949 | tsParseTypeArgumentsInExpression() {
|
9950 | if (this.reScan_lt() !== 47) {
|
9951 | return undefined;
|
9952 | }
|
9953 |
|
9954 | return this.tsParseTypeArguments();
|
9955 | }
|
9956 |
|
9957 | tsParseTypeArguments() {
|
9958 | const node = this.startNode();
|
9959 | node.params = this.tsInType(() => this.tsInNoContext(() => {
|
9960 | this.expect(47);
|
9961 | return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
|
9962 | }));
|
9963 |
|
9964 | if (node.params.length === 0) {
|
9965 | this.raise(TSErrors.EmptyTypeArguments, {
|
9966 | at: node
|
9967 | });
|
9968 | }
|
9969 |
|
9970 | this.expect(48);
|
9971 | return this.finishNode(node, "TSTypeParameterInstantiation");
|
9972 | }
|
9973 |
|
9974 | tsIsDeclarationStart() {
|
9975 | return tokenIsTSDeclarationStart(this.state.type);
|
9976 | }
|
9977 |
|
9978 | isExportDefaultSpecifier() {
|
9979 | if (this.tsIsDeclarationStart()) return false;
|
9980 | return super.isExportDefaultSpecifier();
|
9981 | }
|
9982 |
|
9983 | parseAssignableListItem(allowModifiers, decorators) {
|
9984 | const startPos = this.state.start;
|
9985 | const startLoc = this.state.startLoc;
|
9986 | let accessibility;
|
9987 | let readonly = false;
|
9988 | let override = false;
|
9989 |
|
9990 | if (allowModifiers !== undefined) {
|
9991 | const modified = {};
|
9992 | this.tsParseModifiers({
|
9993 | modified,
|
9994 | allowedModifiers: ["public", "private", "protected", "override", "readonly"]
|
9995 | });
|
9996 | accessibility = modified.accessibility;
|
9997 | override = modified.override;
|
9998 | readonly = modified.readonly;
|
9999 |
|
10000 | if (allowModifiers === false && (accessibility || readonly || override)) {
|
10001 | this.raise(TSErrors.UnexpectedParameterModifier, {
|
10002 | at: startLoc
|
10003 | });
|
10004 | }
|
10005 | }
|
10006 |
|
10007 | const left = this.parseMaybeDefault();
|
10008 | this.parseAssignableListItemTypes(left);
|
10009 | const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
|
10010 |
|
10011 | if (accessibility || readonly || override) {
|
10012 | const pp = this.startNodeAt(startPos, startLoc);
|
10013 |
|
10014 | if (decorators.length) {
|
10015 | pp.decorators = decorators;
|
10016 | }
|
10017 |
|
10018 | if (accessibility) pp.accessibility = accessibility;
|
10019 | if (readonly) pp.readonly = readonly;
|
10020 | if (override) pp.override = override;
|
10021 |
|
10022 | if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
|
10023 | this.raise(TSErrors.UnsupportedParameterPropertyKind, {
|
10024 | at: pp
|
10025 | });
|
10026 | }
|
10027 |
|
10028 | pp.parameter = elt;
|
10029 | return this.finishNode(pp, "TSParameterProperty");
|
10030 | }
|
10031 |
|
10032 | if (decorators.length) {
|
10033 | left.decorators = decorators;
|
10034 | }
|
10035 |
|
10036 | return elt;
|
10037 | }
|
10038 |
|
10039 | isSimpleParameter(node) {
|
10040 | return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
|
10041 | }
|
10042 |
|
10043 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
10044 | if (this.match(14)) {
|
10045 | node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
|
10046 | }
|
10047 |
|
10048 | const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
|
10049 |
|
10050 | if (bodilessType && !this.match(5) && this.isLineTerminator()) {
|
10051 | this.finishNode(node, bodilessType);
|
10052 | return;
|
10053 | }
|
10054 |
|
10055 | if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
|
10056 | this.raise(TSErrors.DeclareFunctionHasImplementation, {
|
10057 | at: node
|
10058 | });
|
10059 |
|
10060 | if (node.declare) {
|
10061 | super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
|
10062 | return;
|
10063 | }
|
10064 | }
|
10065 |
|
10066 | super.parseFunctionBodyAndFinish(node, type, isMethod);
|
10067 | }
|
10068 |
|
10069 | registerFunctionStatementId(node) {
|
10070 | if (!node.body && node.id) {
|
10071 | this.checkIdentifier(node.id, BIND_TS_AMBIENT);
|
10072 | } else {
|
10073 | super.registerFunctionStatementId(...arguments);
|
10074 | }
|
10075 | }
|
10076 |
|
10077 | tsCheckForInvalidTypeCasts(items) {
|
10078 | items.forEach(node => {
|
10079 | if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
|
10080 | this.raise(TSErrors.UnexpectedTypeAnnotation, {
|
10081 | at: node.typeAnnotation
|
10082 | });
|
10083 | }
|
10084 | });
|
10085 | }
|
10086 |
|
10087 | toReferencedList(exprList, isInParens) {
|
10088 | this.tsCheckForInvalidTypeCasts(exprList);
|
10089 | return exprList;
|
10090 | }
|
10091 |
|
10092 | parseArrayLike(...args) {
|
10093 | const node = super.parseArrayLike(...args);
|
10094 |
|
10095 | if (node.type === "ArrayExpression") {
|
10096 | this.tsCheckForInvalidTypeCasts(node.elements);
|
10097 | }
|
10098 |
|
10099 | return node;
|
10100 | }
|
10101 |
|
10102 | parseSubscript(base, startPos, startLoc, noCalls, state) {
|
10103 | if (!this.hasPrecedingLineBreak() && this.match(35)) {
|
10104 | this.state.canStartJSXElement = false;
|
10105 | this.next();
|
10106 | const nonNullExpression = this.startNodeAt(startPos, startLoc);
|
10107 | nonNullExpression.expression = base;
|
10108 | return this.finishNode(nonNullExpression, "TSNonNullExpression");
|
10109 | }
|
10110 |
|
10111 | let isOptionalCall = false;
|
10112 |
|
10113 | if (this.match(18) && this.lookaheadCharCode() === 60) {
|
10114 | if (noCalls) {
|
10115 | state.stop = true;
|
10116 | return base;
|
10117 | }
|
10118 |
|
10119 | state.optionalChainMember = isOptionalCall = true;
|
10120 | this.next();
|
10121 | }
|
10122 |
|
10123 | if (this.match(47) || this.match(51)) {
|
10124 | let missingParenErrorLoc;
|
10125 | const result = this.tsTryParseAndCatch(() => {
|
10126 | if (!noCalls && this.atPossibleAsyncArrow(base)) {
|
10127 | const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
|
10128 |
|
10129 | if (asyncArrowFn) {
|
10130 | return asyncArrowFn;
|
10131 | }
|
10132 | }
|
10133 |
|
10134 | const typeArguments = this.tsParseTypeArgumentsInExpression();
|
10135 | if (!typeArguments) return;
|
10136 |
|
10137 | if (isOptionalCall && !this.match(10)) {
|
10138 | missingParenErrorLoc = this.state.curPosition();
|
10139 | return;
|
10140 | }
|
10141 |
|
10142 | if (tokenIsTemplate(this.state.type)) {
|
10143 | const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
|
10144 | result.typeParameters = typeArguments;
|
10145 | return result;
|
10146 | }
|
10147 |
|
10148 | if (!noCalls && this.eat(10)) {
|
10149 | const node = this.startNodeAt(startPos, startLoc);
|
10150 | node.callee = base;
|
10151 | node.arguments = this.parseCallExpressionArguments(11, false);
|
10152 | this.tsCheckForInvalidTypeCasts(node.arguments);
|
10153 | node.typeParameters = typeArguments;
|
10154 |
|
10155 | if (state.optionalChainMember) {
|
10156 | node.optional = isOptionalCall;
|
10157 | }
|
10158 |
|
10159 | return this.finishCallExpression(node, state.optionalChainMember);
|
10160 | }
|
10161 |
|
10162 | const tokenType = this.state.type;
|
10163 |
|
10164 | if (tokenType === 48 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
|
10165 | return;
|
10166 | }
|
10167 |
|
10168 | const node = this.startNodeAt(startPos, startLoc);
|
10169 | node.expression = base;
|
10170 | node.typeParameters = typeArguments;
|
10171 | return this.finishNode(node, "TSInstantiationExpression");
|
10172 | });
|
10173 |
|
10174 | if (missingParenErrorLoc) {
|
10175 | this.unexpected(missingParenErrorLoc, 10);
|
10176 | }
|
10177 |
|
10178 | if (result) {
|
10179 | if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
|
10180 | this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
|
10181 | at: this.state.startLoc
|
10182 | });
|
10183 | }
|
10184 |
|
10185 | return result;
|
10186 | }
|
10187 | }
|
10188 |
|
10189 | return super.parseSubscript(base, startPos, startLoc, noCalls, state);
|
10190 | }
|
10191 |
|
10192 | parseNewCallee(node) {
|
10193 | var _callee$extra;
|
10194 |
|
10195 | super.parseNewCallee(node);
|
10196 | const {
|
10197 | callee
|
10198 | } = node;
|
10199 |
|
10200 | if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
|
10201 | node.typeParameters = callee.typeParameters;
|
10202 | node.callee = callee.expression;
|
10203 | }
|
10204 | }
|
10205 |
|
10206 | parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
|
10207 | if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(93)) {
|
10208 | const node = this.startNodeAt(leftStartPos, leftStartLoc);
|
10209 | node.expression = left;
|
10210 |
|
10211 | const _const = this.tsTryNextParseConstantContext();
|
10212 |
|
10213 | if (_const) {
|
10214 | node.typeAnnotation = _const;
|
10215 | } else {
|
10216 | node.typeAnnotation = this.tsNextThenParseType();
|
10217 | }
|
10218 |
|
10219 | this.finishNode(node, "TSAsExpression");
|
10220 | this.reScan_lt_gt();
|
10221 | return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
|
10222 | }
|
10223 |
|
10224 | return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
|
10225 | }
|
10226 |
|
10227 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
10228 | if (!this.state.isAmbientContext) {
|
10229 | super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
|
10230 | }
|
10231 | }
|
10232 |
|
10233 | checkDuplicateExports() {}
|
10234 |
|
10235 | parseImport(node) {
|
10236 | node.importKind = "value";
|
10237 |
|
10238 | if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
|
10239 | let ahead = this.lookahead();
|
10240 |
|
10241 | if (this.isContextual(126) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
|
10242 | node.importKind = "type";
|
10243 | this.next();
|
10244 | ahead = this.lookahead();
|
10245 | }
|
10246 |
|
10247 | if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
|
10248 | return this.tsParseImportEqualsDeclaration(node);
|
10249 | }
|
10250 | }
|
10251 |
|
10252 | const importNode = super.parseImport(node);
|
10253 |
|
10254 | if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
|
10255 | this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
|
10256 | at: importNode
|
10257 | });
|
10258 | }
|
10259 |
|
10260 | return importNode;
|
10261 | }
|
10262 |
|
10263 | parseExport(node) {
|
10264 | if (this.match(83)) {
|
10265 | this.next();
|
10266 |
|
10267 | if (this.isContextual(126) && this.lookaheadCharCode() !== 61) {
|
10268 | node.importKind = "type";
|
10269 | this.next();
|
10270 | } else {
|
10271 | node.importKind = "value";
|
10272 | }
|
10273 |
|
10274 | return this.tsParseImportEqualsDeclaration(node, true);
|
10275 | } else if (this.eat(29)) {
|
10276 | const assign = node;
|
10277 | assign.expression = this.parseExpression();
|
10278 | this.semicolon();
|
10279 | return this.finishNode(assign, "TSExportAssignment");
|
10280 | } else if (this.eatContextual(93)) {
|
10281 | const decl = node;
|
10282 | this.expectContextual(124);
|
10283 | decl.id = this.parseIdentifier();
|
10284 | this.semicolon();
|
10285 | return this.finishNode(decl, "TSNamespaceExportDeclaration");
|
10286 | } else {
|
10287 | if (this.isContextual(126) && this.lookahead().type === 5) {
|
10288 | this.next();
|
10289 | node.exportKind = "type";
|
10290 | } else {
|
10291 | node.exportKind = "value";
|
10292 | }
|
10293 |
|
10294 | return super.parseExport(node);
|
10295 | }
|
10296 | }
|
10297 |
|
10298 | isAbstractClass() {
|
10299 | return this.isContextual(120) && this.lookahead().type === 80;
|
10300 | }
|
10301 |
|
10302 | parseExportDefaultExpression() {
|
10303 | if (this.isAbstractClass()) {
|
10304 | const cls = this.startNode();
|
10305 | this.next();
|
10306 | cls.abstract = true;
|
10307 | this.parseClass(cls, true, true);
|
10308 | return cls;
|
10309 | }
|
10310 |
|
10311 | if (this.match(125)) {
|
10312 | const result = this.tsParseInterfaceDeclaration(this.startNode());
|
10313 | if (result) return result;
|
10314 | }
|
10315 |
|
10316 | return super.parseExportDefaultExpression();
|
10317 | }
|
10318 |
|
10319 | parseVarStatement(node, kind, allowMissingInitializer = false) {
|
10320 | const {
|
10321 | isAmbientContext
|
10322 | } = this.state;
|
10323 | const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
|
10324 | if (!isAmbientContext) return declaration;
|
10325 |
|
10326 | for (const {
|
10327 | id,
|
10328 | init
|
10329 | } of declaration.declarations) {
|
10330 | if (!init) continue;
|
10331 |
|
10332 | if (kind !== "const" || !!id.typeAnnotation) {
|
10333 | this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
|
10334 | at: init
|
10335 | });
|
10336 | } else if (init.type !== "StringLiteral" && init.type !== "BooleanLiteral" && init.type !== "NumericLiteral" && init.type !== "BigIntLiteral" && (init.type !== "TemplateLiteral" || init.expressions.length > 0) && !isPossiblyLiteralEnum(init)) {
|
10337 | this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
|
10338 | at: init
|
10339 | });
|
10340 | }
|
10341 | }
|
10342 |
|
10343 | return declaration;
|
10344 | }
|
10345 |
|
10346 | parseStatementContent(context, topLevel) {
|
10347 | if (this.match(75) && this.isLookaheadContextual("enum")) {
|
10348 | const node = this.startNode();
|
10349 | this.expect(75);
|
10350 | return this.tsParseEnumDeclaration(node, {
|
10351 | const: true
|
10352 | });
|
10353 | }
|
10354 |
|
10355 | if (this.isContextual(122)) {
|
10356 | return this.tsParseEnumDeclaration(this.startNode());
|
10357 | }
|
10358 |
|
10359 | if (this.isContextual(125)) {
|
10360 | const result = this.tsParseInterfaceDeclaration(this.startNode());
|
10361 | if (result) return result;
|
10362 | }
|
10363 |
|
10364 | return super.parseStatementContent(context, topLevel);
|
10365 | }
|
10366 |
|
10367 | parseAccessModifier() {
|
10368 | return this.tsParseModifier(["public", "protected", "private"]);
|
10369 | }
|
10370 |
|
10371 | tsHasSomeModifiers(member, modifiers) {
|
10372 | return modifiers.some(modifier => {
|
10373 | if (tsIsAccessModifier(modifier)) {
|
10374 | return member.accessibility === modifier;
|
10375 | }
|
10376 |
|
10377 | return !!member[modifier];
|
10378 | });
|
10379 | }
|
10380 |
|
10381 | tsIsStartOfStaticBlocks() {
|
10382 | return this.isContextual(104) && this.lookaheadCharCode() === 123;
|
10383 | }
|
10384 |
|
10385 | parseClassMember(classBody, member, state) {
|
10386 | const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
|
10387 | this.tsParseModifiers({
|
10388 | modified: member,
|
10389 | allowedModifiers: modifiers,
|
10390 | disallowedModifiers: ["in", "out"],
|
10391 | stopOnStartOfClassStaticBlock: true,
|
10392 | errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
|
10393 | });
|
10394 |
|
10395 | const callParseClassMemberWithIsStatic = () => {
|
10396 | if (this.tsIsStartOfStaticBlocks()) {
|
10397 | this.next();
|
10398 | this.next();
|
10399 |
|
10400 | if (this.tsHasSomeModifiers(member, modifiers)) {
|
10401 | this.raise(TSErrors.StaticBlockCannotHaveModifier, {
|
10402 | at: this.state.curPosition()
|
10403 | });
|
10404 | }
|
10405 |
|
10406 | this.parseClassStaticBlock(classBody, member);
|
10407 | } else {
|
10408 | this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
|
10409 | }
|
10410 | };
|
10411 |
|
10412 | if (member.declare) {
|
10413 | this.tsInAmbientContext(callParseClassMemberWithIsStatic);
|
10414 | } else {
|
10415 | callParseClassMemberWithIsStatic();
|
10416 | }
|
10417 | }
|
10418 |
|
10419 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
10420 | const idx = this.tsTryParseIndexSignature(member);
|
10421 |
|
10422 | if (idx) {
|
10423 | classBody.body.push(idx);
|
10424 |
|
10425 | if (member.abstract) {
|
10426 | this.raise(TSErrors.IndexSignatureHasAbstract, {
|
10427 | at: member
|
10428 | });
|
10429 | }
|
10430 |
|
10431 | if (member.accessibility) {
|
10432 | this.raise(TSErrors.IndexSignatureHasAccessibility, {
|
10433 | at: member,
|
10434 | modifier: member.accessibility
|
10435 | });
|
10436 | }
|
10437 |
|
10438 | if (member.declare) {
|
10439 | this.raise(TSErrors.IndexSignatureHasDeclare, {
|
10440 | at: member
|
10441 | });
|
10442 | }
|
10443 |
|
10444 | if (member.override) {
|
10445 | this.raise(TSErrors.IndexSignatureHasOverride, {
|
10446 | at: member
|
10447 | });
|
10448 | }
|
10449 |
|
10450 | return;
|
10451 | }
|
10452 |
|
10453 | if (!this.state.inAbstractClass && member.abstract) {
|
10454 | this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
|
10455 | at: member
|
10456 | });
|
10457 | }
|
10458 |
|
10459 | if (member.override) {
|
10460 | if (!state.hadSuperClass) {
|
10461 | this.raise(TSErrors.OverrideNotInSubClass, {
|
10462 | at: member
|
10463 | });
|
10464 | }
|
10465 | }
|
10466 |
|
10467 | super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
10468 | }
|
10469 |
|
10470 | parsePostMemberNameModifiers(methodOrProp) {
|
10471 | const optional = this.eat(17);
|
10472 | if (optional) methodOrProp.optional = true;
|
10473 |
|
10474 | if (methodOrProp.readonly && this.match(10)) {
|
10475 | this.raise(TSErrors.ClassMethodHasReadonly, {
|
10476 | at: methodOrProp
|
10477 | });
|
10478 | }
|
10479 |
|
10480 | if (methodOrProp.declare && this.match(10)) {
|
10481 | this.raise(TSErrors.ClassMethodHasDeclare, {
|
10482 | at: methodOrProp
|
10483 | });
|
10484 | }
|
10485 | }
|
10486 |
|
10487 | parseExpressionStatement(node, expr) {
|
10488 | const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
|
10489 | return decl || super.parseExpressionStatement(node, expr);
|
10490 | }
|
10491 |
|
10492 | shouldParseExportDeclaration() {
|
10493 | if (this.tsIsDeclarationStart()) return true;
|
10494 | return super.shouldParseExportDeclaration();
|
10495 | }
|
10496 |
|
10497 | parseConditional(expr, startPos, startLoc, refExpressionErrors) {
|
10498 | if (!this.state.maybeInArrowParameters || !this.match(17)) {
|
10499 | return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
|
10500 | }
|
10501 |
|
10502 | const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
|
10503 |
|
10504 | if (!result.node) {
|
10505 | if (result.error) {
|
10506 | super.setOptionalParametersError(refExpressionErrors, result.error);
|
10507 | }
|
10508 |
|
10509 | return expr;
|
10510 | }
|
10511 |
|
10512 | if (result.error) this.state = result.failState;
|
10513 | return result.node;
|
10514 | }
|
10515 |
|
10516 | parseParenItem(node, startPos, startLoc) {
|
10517 | node = super.parseParenItem(node, startPos, startLoc);
|
10518 |
|
10519 | if (this.eat(17)) {
|
10520 | node.optional = true;
|
10521 | this.resetEndLocation(node);
|
10522 | }
|
10523 |
|
10524 | if (this.match(14)) {
|
10525 | const typeCastNode = this.startNodeAt(startPos, startLoc);
|
10526 | typeCastNode.expression = node;
|
10527 | typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
|
10528 | return this.finishNode(typeCastNode, "TSTypeCastExpression");
|
10529 | }
|
10530 |
|
10531 | return node;
|
10532 | }
|
10533 |
|
10534 | parseExportDeclaration(node) {
|
10535 | if (!this.state.isAmbientContext && this.isContextual(121)) {
|
10536 | return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
|
10537 | }
|
10538 |
|
10539 | const startPos = this.state.start;
|
10540 | const startLoc = this.state.startLoc;
|
10541 | const isDeclare = this.eatContextual(121);
|
10542 |
|
10543 | if (isDeclare && (this.isContextual(121) || !this.shouldParseExportDeclaration())) {
|
10544 | throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
|
10545 | at: this.state.startLoc
|
10546 | });
|
10547 | }
|
10548 |
|
10549 | const isIdentifier = tokenIsIdentifier(this.state.type);
|
10550 | const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
|
10551 | if (!declaration) return null;
|
10552 |
|
10553 | if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
|
10554 | node.exportKind = "type";
|
10555 | }
|
10556 |
|
10557 | if (isDeclare) {
|
10558 | this.resetStartLocation(declaration, startPos, startLoc);
|
10559 | declaration.declare = true;
|
10560 | }
|
10561 |
|
10562 | return declaration;
|
10563 | }
|
10564 |
|
10565 | parseClassId(node, isStatement, optionalId) {
|
10566 | if ((!isStatement || optionalId) && this.isContextual(110)) {
|
10567 | return;
|
10568 | }
|
10569 |
|
10570 | super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
|
10571 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
|
10572 | if (typeParameters) node.typeParameters = typeParameters;
|
10573 | }
|
10574 |
|
10575 | parseClassPropertyAnnotation(node) {
|
10576 | if (!node.optional && this.eat(35)) {
|
10577 | node.definite = true;
|
10578 | }
|
10579 |
|
10580 | const type = this.tsTryParseTypeAnnotation();
|
10581 | if (type) node.typeAnnotation = type;
|
10582 | }
|
10583 |
|
10584 | parseClassProperty(node) {
|
10585 | this.parseClassPropertyAnnotation(node);
|
10586 |
|
10587 | if (this.state.isAmbientContext && this.match(29)) {
|
10588 | this.raise(TSErrors.DeclareClassFieldHasInitializer, {
|
10589 | at: this.state.startLoc
|
10590 | });
|
10591 | }
|
10592 |
|
10593 | if (node.abstract && this.match(29)) {
|
10594 | const {
|
10595 | key
|
10596 | } = node;
|
10597 | this.raise(TSErrors.AbstractPropertyHasInitializer, {
|
10598 | at: this.state.startLoc,
|
10599 | propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
|
10600 | });
|
10601 | }
|
10602 |
|
10603 | return super.parseClassProperty(node);
|
10604 | }
|
10605 |
|
10606 | parseClassPrivateProperty(node) {
|
10607 | if (node.abstract) {
|
10608 | this.raise(TSErrors.PrivateElementHasAbstract, {
|
10609 | at: node
|
10610 | });
|
10611 | }
|
10612 |
|
10613 | if (node.accessibility) {
|
10614 | this.raise(TSErrors.PrivateElementHasAccessibility, {
|
10615 | at: node,
|
10616 | modifier: node.accessibility
|
10617 | });
|
10618 | }
|
10619 |
|
10620 | this.parseClassPropertyAnnotation(node);
|
10621 | return super.parseClassPrivateProperty(node);
|
10622 | }
|
10623 |
|
10624 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
10625 | const typeParameters = this.tsTryParseTypeParameters();
|
10626 |
|
10627 | if (typeParameters && isConstructor) {
|
10628 | this.raise(TSErrors.ConstructorHasTypeParameters, {
|
10629 | at: typeParameters
|
10630 | });
|
10631 | }
|
10632 |
|
10633 | const {
|
10634 | declare = false,
|
10635 | kind
|
10636 | } = method;
|
10637 |
|
10638 | if (declare && (kind === "get" || kind === "set")) {
|
10639 | this.raise(TSErrors.DeclareAccessor, {
|
10640 | at: method,
|
10641 | kind
|
10642 | });
|
10643 | }
|
10644 |
|
10645 | if (typeParameters) method.typeParameters = typeParameters;
|
10646 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
|
10647 | }
|
10648 |
|
10649 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
10650 | const typeParameters = this.tsTryParseTypeParameters();
|
10651 | if (typeParameters) method.typeParameters = typeParameters;
|
10652 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
10653 | }
|
10654 |
|
10655 | declareClassPrivateMethodInScope(node, kind) {
|
10656 | if (node.type === "TSDeclareMethod") return;
|
10657 | if (node.type === "MethodDefinition" && !node.value.body) return;
|
10658 | super.declareClassPrivateMethodInScope(node, kind);
|
10659 | }
|
10660 |
|
10661 | parseClassSuper(node) {
|
10662 | super.parseClassSuper(node);
|
10663 |
|
10664 | if (node.superClass && (this.match(47) || this.match(51))) {
|
10665 | node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
|
10666 | }
|
10667 |
|
10668 | if (this.eatContextual(110)) {
|
10669 | node.implements = this.tsParseHeritageClause("implements");
|
10670 | }
|
10671 | }
|
10672 |
|
10673 | parseObjPropValue(prop, ...args) {
|
10674 | const typeParameters = this.tsTryParseTypeParameters();
|
10675 | if (typeParameters) prop.typeParameters = typeParameters;
|
10676 | super.parseObjPropValue(prop, ...args);
|
10677 | }
|
10678 |
|
10679 | parseFunctionParams(node, allowModifiers) {
|
10680 | const typeParameters = this.tsTryParseTypeParameters();
|
10681 | if (typeParameters) node.typeParameters = typeParameters;
|
10682 | super.parseFunctionParams(node, allowModifiers);
|
10683 | }
|
10684 |
|
10685 | parseVarId(decl, kind) {
|
10686 | super.parseVarId(decl, kind);
|
10687 |
|
10688 | if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
|
10689 | decl.definite = true;
|
10690 | }
|
10691 |
|
10692 | const type = this.tsTryParseTypeAnnotation();
|
10693 |
|
10694 | if (type) {
|
10695 | decl.id.typeAnnotation = type;
|
10696 | this.resetEndLocation(decl.id);
|
10697 | }
|
10698 | }
|
10699 |
|
10700 | parseAsyncArrowFromCallExpression(node, call) {
|
10701 | if (this.match(14)) {
|
10702 | node.returnType = this.tsParseTypeAnnotation();
|
10703 | }
|
10704 |
|
10705 | return super.parseAsyncArrowFromCallExpression(node, call);
|
10706 | }
|
10707 |
|
10708 | parseMaybeAssign(...args) {
|
10709 | var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
|
10710 |
|
10711 | let state;
|
10712 | let jsx;
|
10713 | let typeCast;
|
10714 |
|
10715 | if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
|
10716 | state = this.state.clone();
|
10717 | jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
|
10718 | if (!jsx.error) return jsx.node;
|
10719 | const {
|
10720 | context
|
10721 | } = this.state;
|
10722 | const currentContext = context[context.length - 1];
|
10723 |
|
10724 | if (currentContext === types.j_oTag || currentContext === types.j_expr) {
|
10725 | context.pop();
|
10726 | }
|
10727 | }
|
10728 |
|
10729 | if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
|
10730 | return super.parseMaybeAssign(...args);
|
10731 | }
|
10732 |
|
10733 | if (!state || state === this.state) state = this.state.clone();
|
10734 | let typeParameters;
|
10735 | const arrow = this.tryParse(abort => {
|
10736 | var _expr$extra, _typeParameters;
|
10737 |
|
10738 | typeParameters = this.tsParseTypeParameters();
|
10739 | const expr = super.parseMaybeAssign(...args);
|
10740 |
|
10741 | if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
|
10742 | abort();
|
10743 | }
|
10744 |
|
10745 | if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
|
10746 | this.resetStartLocationFromNode(expr, typeParameters);
|
10747 | }
|
10748 |
|
10749 | expr.typeParameters = typeParameters;
|
10750 | return expr;
|
10751 | }, state);
|
10752 |
|
10753 | if (!arrow.error && !arrow.aborted) {
|
10754 | if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
|
10755 | return arrow.node;
|
10756 | }
|
10757 |
|
10758 | if (!jsx) {
|
10759 | assert(!this.hasPlugin("jsx"));
|
10760 | typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
|
10761 | if (!typeCast.error) return typeCast.node;
|
10762 | }
|
10763 |
|
10764 | if ((_jsx2 = jsx) != null && _jsx2.node) {
|
10765 | this.state = jsx.failState;
|
10766 | return jsx.node;
|
10767 | }
|
10768 |
|
10769 | if (arrow.node) {
|
10770 | this.state = arrow.failState;
|
10771 | if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
|
10772 | return arrow.node;
|
10773 | }
|
10774 |
|
10775 | if ((_typeCast = typeCast) != null && _typeCast.node) {
|
10776 | this.state = typeCast.failState;
|
10777 | return typeCast.node;
|
10778 | }
|
10779 |
|
10780 | if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
|
10781 | if (arrow.thrown) throw arrow.error;
|
10782 | if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
|
10783 | throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
|
10784 | }
|
10785 |
|
10786 | reportReservedArrowTypeParam(node) {
|
10787 | var _node$extra;
|
10788 |
|
10789 | if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
|
10790 | this.raise(TSErrors.ReservedArrowTypeParam, {
|
10791 | at: node
|
10792 | });
|
10793 | }
|
10794 | }
|
10795 |
|
10796 | parseMaybeUnary(refExpressionErrors) {
|
10797 | if (!this.hasPlugin("jsx") && this.match(47)) {
|
10798 | return this.tsParseTypeAssertion();
|
10799 | } else {
|
10800 | return super.parseMaybeUnary(refExpressionErrors);
|
10801 | }
|
10802 | }
|
10803 |
|
10804 | parseArrow(node) {
|
10805 | if (this.match(14)) {
|
10806 | const result = this.tryParse(abort => {
|
10807 | const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
|
10808 | if (this.canInsertSemicolon() || !this.match(19)) abort();
|
10809 | return returnType;
|
10810 | });
|
10811 | if (result.aborted) return;
|
10812 |
|
10813 | if (!result.thrown) {
|
10814 | if (result.error) this.state = result.failState;
|
10815 | node.returnType = result.node;
|
10816 | }
|
10817 | }
|
10818 |
|
10819 | return super.parseArrow(node);
|
10820 | }
|
10821 |
|
10822 | parseAssignableListItemTypes(param) {
|
10823 | if (this.eat(17)) {
|
10824 | if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
|
10825 | this.raise(TSErrors.PatternIsOptional, {
|
10826 | at: param
|
10827 | });
|
10828 | }
|
10829 |
|
10830 | param.optional = true;
|
10831 | }
|
10832 |
|
10833 | const type = this.tsTryParseTypeAnnotation();
|
10834 | if (type) param.typeAnnotation = type;
|
10835 | this.resetEndLocation(param);
|
10836 | return param;
|
10837 | }
|
10838 |
|
10839 | isAssignable(node, isBinding) {
|
10840 | switch (node.type) {
|
10841 | case "TSTypeCastExpression":
|
10842 | return this.isAssignable(node.expression, isBinding);
|
10843 |
|
10844 | case "TSParameterProperty":
|
10845 | return true;
|
10846 |
|
10847 | default:
|
10848 | return super.isAssignable(node, isBinding);
|
10849 | }
|
10850 | }
|
10851 |
|
10852 | toAssignable(node, isLHS = false) {
|
10853 | switch (node.type) {
|
10854 | case "ParenthesizedExpression":
|
10855 | this.toAssignableParenthesizedExpression(node, isLHS);
|
10856 | break;
|
10857 |
|
10858 | case "TSAsExpression":
|
10859 | case "TSNonNullExpression":
|
10860 | case "TSTypeAssertion":
|
10861 | if (isLHS) {
|
10862 | this.expressionScope.recordArrowParemeterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
|
10863 | at: node
|
10864 | });
|
10865 | } else {
|
10866 | this.raise(TSErrors.UnexpectedTypeCastInParameter, {
|
10867 | at: node
|
10868 | });
|
10869 | }
|
10870 |
|
10871 | this.toAssignable(node.expression, isLHS);
|
10872 | break;
|
10873 |
|
10874 | case "AssignmentExpression":
|
10875 | if (!isLHS && node.left.type === "TSTypeCastExpression") {
|
10876 | node.left = this.typeCastToParameter(node.left);
|
10877 | }
|
10878 |
|
10879 | default:
|
10880 | super.toAssignable(node, isLHS);
|
10881 | }
|
10882 | }
|
10883 |
|
10884 | toAssignableParenthesizedExpression(node, isLHS) {
|
10885 | switch (node.expression.type) {
|
10886 | case "TSAsExpression":
|
10887 | case "TSNonNullExpression":
|
10888 | case "TSTypeAssertion":
|
10889 | case "ParenthesizedExpression":
|
10890 | this.toAssignable(node.expression, isLHS);
|
10891 | break;
|
10892 |
|
10893 | default:
|
10894 | super.toAssignable(node, isLHS);
|
10895 | }
|
10896 | }
|
10897 |
|
10898 | checkToRestConversion(node, allowPattern) {
|
10899 | switch (node.type) {
|
10900 | case "TSAsExpression":
|
10901 | case "TSTypeAssertion":
|
10902 | case "TSNonNullExpression":
|
10903 | this.checkToRestConversion(node.expression, false);
|
10904 | break;
|
10905 |
|
10906 | default:
|
10907 | super.checkToRestConversion(node, allowPattern);
|
10908 | }
|
10909 | }
|
10910 |
|
10911 | isValidLVal(type, isUnparenthesizedInAssign, binding) {
|
10912 | return getOwn$1({
|
10913 | TSTypeCastExpression: true,
|
10914 | TSParameterProperty: "parameter",
|
10915 | TSNonNullExpression: "expression",
|
10916 | TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
|
10917 | TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
|
10918 | }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
|
10919 | }
|
10920 |
|
10921 | parseBindingAtom() {
|
10922 | switch (this.state.type) {
|
10923 | case 78:
|
10924 | return this.parseIdentifier(true);
|
10925 |
|
10926 | default:
|
10927 | return super.parseBindingAtom();
|
10928 | }
|
10929 | }
|
10930 |
|
10931 | parseMaybeDecoratorArguments(expr) {
|
10932 | if (this.match(47) || this.match(51)) {
|
10933 | const typeArguments = this.tsParseTypeArgumentsInExpression();
|
10934 |
|
10935 | if (this.match(10)) {
|
10936 | const call = super.parseMaybeDecoratorArguments(expr);
|
10937 | call.typeParameters = typeArguments;
|
10938 | return call;
|
10939 | }
|
10940 |
|
10941 | this.unexpected(null, 10);
|
10942 | }
|
10943 |
|
10944 | return super.parseMaybeDecoratorArguments(expr);
|
10945 | }
|
10946 |
|
10947 | checkCommaAfterRest(close) {
|
10948 | if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
|
10949 | this.next();
|
10950 | return false;
|
10951 | } else {
|
10952 | return super.checkCommaAfterRest(close);
|
10953 | }
|
10954 | }
|
10955 |
|
10956 | isClassMethod() {
|
10957 | return this.match(47) || super.isClassMethod();
|
10958 | }
|
10959 |
|
10960 | isClassProperty() {
|
10961 | return this.match(35) || this.match(14) || super.isClassProperty();
|
10962 | }
|
10963 |
|
10964 | parseMaybeDefault(...args) {
|
10965 | const node = super.parseMaybeDefault(...args);
|
10966 |
|
10967 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
10968 | this.raise(TSErrors.TypeAnnotationAfterAssign, {
|
10969 | at: node.typeAnnotation
|
10970 | });
|
10971 | }
|
10972 |
|
10973 | return node;
|
10974 | }
|
10975 |
|
10976 | getTokenFromCode(code) {
|
10977 | if (this.state.inType) {
|
10978 | if (code === 62) {
|
10979 | return this.finishOp(48, 1);
|
10980 | }
|
10981 |
|
10982 | if (code === 60) {
|
10983 | return this.finishOp(47, 1);
|
10984 | }
|
10985 | }
|
10986 |
|
10987 | return super.getTokenFromCode(code);
|
10988 | }
|
10989 |
|
10990 | reScan_lt_gt() {
|
10991 | const {
|
10992 | type
|
10993 | } = this.state;
|
10994 |
|
10995 | if (type === 47) {
|
10996 | this.state.pos -= 1;
|
10997 | this.readToken_lt();
|
10998 | } else if (type === 48) {
|
10999 | this.state.pos -= 1;
|
11000 | this.readToken_gt();
|
11001 | }
|
11002 | }
|
11003 |
|
11004 | reScan_lt() {
|
11005 | const {
|
11006 | type
|
11007 | } = this.state;
|
11008 |
|
11009 | if (type === 51) {
|
11010 | this.state.pos -= 2;
|
11011 | this.finishOp(47, 1);
|
11012 | return 47;
|
11013 | }
|
11014 |
|
11015 | return type;
|
11016 | }
|
11017 |
|
11018 | toAssignableList(exprList) {
|
11019 | for (let i = 0; i < exprList.length; i++) {
|
11020 | const expr = exprList[i];
|
11021 |
|
11022 | if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
|
11023 | exprList[i] = this.typeCastToParameter(expr);
|
11024 | }
|
11025 | }
|
11026 |
|
11027 | super.toAssignableList(...arguments);
|
11028 | }
|
11029 |
|
11030 | typeCastToParameter(node) {
|
11031 | node.expression.typeAnnotation = node.typeAnnotation;
|
11032 | this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
|
11033 | return node.expression;
|
11034 | }
|
11035 |
|
11036 | shouldParseArrow(params) {
|
11037 | if (this.match(14)) {
|
11038 | return params.every(expr => this.isAssignable(expr, true));
|
11039 | }
|
11040 |
|
11041 | return super.shouldParseArrow(params);
|
11042 | }
|
11043 |
|
11044 | shouldParseAsyncArrow() {
|
11045 | return this.match(14) || super.shouldParseAsyncArrow();
|
11046 | }
|
11047 |
|
11048 | canHaveLeadingDecorator() {
|
11049 | return super.canHaveLeadingDecorator() || this.isAbstractClass();
|
11050 | }
|
11051 |
|
11052 | jsxParseOpeningElementAfterName(node) {
|
11053 | if (this.match(47) || this.match(51)) {
|
11054 | const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
|
11055 | if (typeArguments) node.typeParameters = typeArguments;
|
11056 | }
|
11057 |
|
11058 | return super.jsxParseOpeningElementAfterName(node);
|
11059 | }
|
11060 |
|
11061 | getGetterSetterExpectedParamCount(method) {
|
11062 | const baseCount = super.getGetterSetterExpectedParamCount(method);
|
11063 | const params = this.getObjectOrClassMethodParams(method);
|
11064 | const firstParam = params[0];
|
11065 | const hasContextParam = firstParam && this.isThisParam(firstParam);
|
11066 | return hasContextParam ? baseCount + 1 : baseCount;
|
11067 | }
|
11068 |
|
11069 | parseCatchClauseParam() {
|
11070 | const param = super.parseCatchClauseParam();
|
11071 | const type = this.tsTryParseTypeAnnotation();
|
11072 |
|
11073 | if (type) {
|
11074 | param.typeAnnotation = type;
|
11075 | this.resetEndLocation(param);
|
11076 | }
|
11077 |
|
11078 | return param;
|
11079 | }
|
11080 |
|
11081 | tsInAmbientContext(cb) {
|
11082 | const oldIsAmbientContext = this.state.isAmbientContext;
|
11083 | this.state.isAmbientContext = true;
|
11084 |
|
11085 | try {
|
11086 | return cb();
|
11087 | } finally {
|
11088 | this.state.isAmbientContext = oldIsAmbientContext;
|
11089 | }
|
11090 | }
|
11091 |
|
11092 | parseClass(node, ...args) {
|
11093 | const oldInAbstractClass = this.state.inAbstractClass;
|
11094 | this.state.inAbstractClass = !!node.abstract;
|
11095 |
|
11096 | try {
|
11097 | return super.parseClass(node, ...args);
|
11098 | } finally {
|
11099 | this.state.inAbstractClass = oldInAbstractClass;
|
11100 | }
|
11101 | }
|
11102 |
|
11103 | tsParseAbstractDeclaration(node) {
|
11104 | if (this.match(80)) {
|
11105 | node.abstract = true;
|
11106 | return this.parseClass(node, true, false);
|
11107 | } else if (this.isContextual(125)) {
|
11108 | if (!this.hasFollowingLineBreak()) {
|
11109 | node.abstract = true;
|
11110 | this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
|
11111 | at: node
|
11112 | });
|
11113 | return this.tsParseInterfaceDeclaration(node);
|
11114 | }
|
11115 | } else {
|
11116 | this.unexpected(null, 80);
|
11117 | }
|
11118 | }
|
11119 |
|
11120 | parseMethod(...args) {
|
11121 | const method = super.parseMethod(...args);
|
11122 |
|
11123 | if (method.abstract) {
|
11124 | const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
|
11125 |
|
11126 | if (hasBody) {
|
11127 | const {
|
11128 | key
|
11129 | } = method;
|
11130 | this.raise(TSErrors.AbstractMethodHasImplementation, {
|
11131 | at: method,
|
11132 | methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
|
11133 | });
|
11134 | }
|
11135 | }
|
11136 |
|
11137 | return method;
|
11138 | }
|
11139 |
|
11140 | tsParseTypeParameterName() {
|
11141 | const typeName = this.parseIdentifier();
|
11142 | return typeName.name;
|
11143 | }
|
11144 |
|
11145 | shouldParseAsAmbientContext() {
|
11146 | return !!this.getPluginOption("typescript", "dts");
|
11147 | }
|
11148 |
|
11149 | parse() {
|
11150 | if (this.shouldParseAsAmbientContext()) {
|
11151 | this.state.isAmbientContext = true;
|
11152 | }
|
11153 |
|
11154 | return super.parse();
|
11155 | }
|
11156 |
|
11157 | getExpression() {
|
11158 | if (this.shouldParseAsAmbientContext()) {
|
11159 | this.state.isAmbientContext = true;
|
11160 | }
|
11161 |
|
11162 | return super.getExpression();
|
11163 | }
|
11164 |
|
11165 | parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
|
11166 | if (!isString && isMaybeTypeOnly) {
|
11167 | this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
|
11168 | return this.finishNode(node, "ExportSpecifier");
|
11169 | }
|
11170 |
|
11171 | node.exportKind = "value";
|
11172 | return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
|
11173 | }
|
11174 |
|
11175 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
|
11176 | if (!importedIsString && isMaybeTypeOnly) {
|
11177 | this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
|
11178 | return this.finishNode(specifier, "ImportSpecifier");
|
11179 | }
|
11180 |
|
11181 | specifier.importKind = "value";
|
11182 | return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
|
11183 | }
|
11184 |
|
11185 | parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
|
11186 | const leftOfAsKey = isImport ? "imported" : "local";
|
11187 | const rightOfAsKey = isImport ? "local" : "exported";
|
11188 | let leftOfAs = node[leftOfAsKey];
|
11189 | let rightOfAs;
|
11190 | let hasTypeSpecifier = false;
|
11191 | let canParseAsKeyword = true;
|
11192 | const loc = leftOfAs.loc.start;
|
11193 |
|
11194 | if (this.isContextual(93)) {
|
11195 | const firstAs = this.parseIdentifier();
|
11196 |
|
11197 | if (this.isContextual(93)) {
|
11198 | const secondAs = this.parseIdentifier();
|
11199 |
|
11200 | if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
11201 | hasTypeSpecifier = true;
|
11202 | leftOfAs = firstAs;
|
11203 | rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
11204 | canParseAsKeyword = false;
|
11205 | } else {
|
11206 | rightOfAs = secondAs;
|
11207 | canParseAsKeyword = false;
|
11208 | }
|
11209 | } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
11210 | canParseAsKeyword = false;
|
11211 | rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
11212 | } else {
|
11213 | hasTypeSpecifier = true;
|
11214 | leftOfAs = firstAs;
|
11215 | }
|
11216 | } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
11217 | hasTypeSpecifier = true;
|
11218 |
|
11219 | if (isImport) {
|
11220 | leftOfAs = this.parseIdentifier(true);
|
11221 |
|
11222 | if (!this.isContextual(93)) {
|
11223 | this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
|
11224 | }
|
11225 | } else {
|
11226 | leftOfAs = this.parseModuleExportName();
|
11227 | }
|
11228 | }
|
11229 |
|
11230 | if (hasTypeSpecifier && isInTypeOnlyImportExport) {
|
11231 | this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
|
11232 | at: loc
|
11233 | });
|
11234 | }
|
11235 |
|
11236 | node[leftOfAsKey] = leftOfAs;
|
11237 | node[rightOfAsKey] = rightOfAs;
|
11238 | const kindKey = isImport ? "importKind" : "exportKind";
|
11239 | node[kindKey] = hasTypeSpecifier ? "type" : "value";
|
11240 |
|
11241 | if (canParseAsKeyword && this.eatContextual(93)) {
|
11242 | node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
11243 | }
|
11244 |
|
11245 | if (!node[rightOfAsKey]) {
|
11246 | node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
|
11247 | }
|
11248 |
|
11249 | if (isImport) {
|
11250 | this.checkIdentifier(node[rightOfAsKey], BIND_LEXICAL);
|
11251 | }
|
11252 | }
|
11253 |
|
11254 | });
|
11255 |
|
11256 | function isPossiblyLiteralEnum(expression) {
|
11257 | if (expression.type !== "MemberExpression") return false;
|
11258 | const {
|
11259 | computed,
|
11260 | property
|
11261 | } = expression;
|
11262 |
|
11263 | if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
|
11264 | return false;
|
11265 | }
|
11266 |
|
11267 | return isUncomputedMemberExpressionChain(expression.object);
|
11268 | }
|
11269 |
|
11270 | function isUncomputedMemberExpressionChain(expression) {
|
11271 | if (expression.type === "Identifier") return true;
|
11272 | if (expression.type !== "MemberExpression") return false;
|
11273 | if (expression.computed) return false;
|
11274 | return isUncomputedMemberExpressionChain(expression.object);
|
11275 | }
|
11276 |
|
11277 | const PlaceholderErrors = ParseErrorEnum`placeholders`(_ => ({
|
11278 | ClassNameIsRequired: _("A class name is required."),
|
11279 | UnexpectedSpace: _("Unexpected space in placeholder.")
|
11280 | }));
|
11281 | var placeholders = (superClass => class extends superClass {
|
11282 | parsePlaceholder(expectedNode) {
|
11283 | if (this.match(140)) {
|
11284 | const node = this.startNode();
|
11285 | this.next();
|
11286 | this.assertNoSpace();
|
11287 | node.name = super.parseIdentifier(true);
|
11288 | this.assertNoSpace();
|
11289 | this.expect(140);
|
11290 | return this.finishPlaceholder(node, expectedNode);
|
11291 | }
|
11292 | }
|
11293 |
|
11294 | finishPlaceholder(node, expectedNode) {
|
11295 | const isFinished = !!(node.expectedNode && node.type === "Placeholder");
|
11296 | node.expectedNode = expectedNode;
|
11297 | return isFinished ? node : this.finishNode(node, "Placeholder");
|
11298 | }
|
11299 |
|
11300 | getTokenFromCode(code) {
|
11301 | if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
|
11302 | return this.finishOp(140, 2);
|
11303 | }
|
11304 |
|
11305 | return super.getTokenFromCode(...arguments);
|
11306 | }
|
11307 |
|
11308 | parseExprAtom() {
|
11309 | return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
|
11310 | }
|
11311 |
|
11312 | parseIdentifier() {
|
11313 | return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
|
11314 | }
|
11315 |
|
11316 | checkReservedWord(word) {
|
11317 | if (word !== undefined) super.checkReservedWord(...arguments);
|
11318 | }
|
11319 |
|
11320 | parseBindingAtom() {
|
11321 | return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
|
11322 | }
|
11323 |
|
11324 | isValidLVal(type, ...rest) {
|
11325 | return type === "Placeholder" || super.isValidLVal(type, ...rest);
|
11326 | }
|
11327 |
|
11328 | toAssignable(node) {
|
11329 | if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
|
11330 | node.expectedNode = "Pattern";
|
11331 | } else {
|
11332 | super.toAssignable(...arguments);
|
11333 | }
|
11334 | }
|
11335 |
|
11336 | isLet(context) {
|
11337 | if (super.isLet(context)) {
|
11338 | return true;
|
11339 | }
|
11340 |
|
11341 | if (!this.isContextual(99)) {
|
11342 | return false;
|
11343 | }
|
11344 |
|
11345 | if (context) return false;
|
11346 | const nextToken = this.lookahead();
|
11347 |
|
11348 | if (nextToken.type === 140) {
|
11349 | return true;
|
11350 | }
|
11351 |
|
11352 | return false;
|
11353 | }
|
11354 |
|
11355 | verifyBreakContinue(node) {
|
11356 | if (node.label && node.label.type === "Placeholder") return;
|
11357 | super.verifyBreakContinue(...arguments);
|
11358 | }
|
11359 |
|
11360 | parseExpressionStatement(node, expr) {
|
11361 | if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
|
11362 | return super.parseExpressionStatement(...arguments);
|
11363 | }
|
11364 |
|
11365 | if (this.match(14)) {
|
11366 | const stmt = node;
|
11367 | stmt.label = this.finishPlaceholder(expr, "Identifier");
|
11368 | this.next();
|
11369 | stmt.body = this.parseStatement("label");
|
11370 | return this.finishNode(stmt, "LabeledStatement");
|
11371 | }
|
11372 |
|
11373 | this.semicolon();
|
11374 | node.name = expr.name;
|
11375 | return this.finishPlaceholder(node, "Statement");
|
11376 | }
|
11377 |
|
11378 | parseBlock() {
|
11379 | return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
|
11380 | }
|
11381 |
|
11382 | parseFunctionId() {
|
11383 | return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
|
11384 | }
|
11385 |
|
11386 | parseClass(node, isStatement, optionalId) {
|
11387 | const type = isStatement ? "ClassDeclaration" : "ClassExpression";
|
11388 | this.next();
|
11389 | this.takeDecorators(node);
|
11390 | const oldStrict = this.state.strict;
|
11391 | const placeholder = this.parsePlaceholder("Identifier");
|
11392 |
|
11393 | if (placeholder) {
|
11394 | if (this.match(81) || this.match(140) || this.match(5)) {
|
11395 | node.id = placeholder;
|
11396 | } else if (optionalId || !isStatement) {
|
11397 | node.id = null;
|
11398 | node.body = this.finishPlaceholder(placeholder, "ClassBody");
|
11399 | return this.finishNode(node, type);
|
11400 | } else {
|
11401 | throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
|
11402 | at: this.state.startLoc
|
11403 | });
|
11404 | }
|
11405 | } else {
|
11406 | this.parseClassId(node, isStatement, optionalId);
|
11407 | }
|
11408 |
|
11409 | this.parseClassSuper(node);
|
11410 | node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
|
11411 | return this.finishNode(node, type);
|
11412 | }
|
11413 |
|
11414 | parseExport(node) {
|
11415 | const placeholder = this.parsePlaceholder("Identifier");
|
11416 | if (!placeholder) return super.parseExport(...arguments);
|
11417 |
|
11418 | if (!this.isContextual(97) && !this.match(12)) {
|
11419 | node.specifiers = [];
|
11420 | node.source = null;
|
11421 | node.declaration = this.finishPlaceholder(placeholder, "Declaration");
|
11422 | return this.finishNode(node, "ExportNamedDeclaration");
|
11423 | }
|
11424 |
|
11425 | this.expectPlugin("exportDefaultFrom");
|
11426 | const specifier = this.startNode();
|
11427 | specifier.exported = placeholder;
|
11428 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
11429 | return super.parseExport(node);
|
11430 | }
|
11431 |
|
11432 | isExportDefaultSpecifier() {
|
11433 | if (this.match(65)) {
|
11434 | const next = this.nextTokenStart();
|
11435 |
|
11436 | if (this.isUnparsedContextual(next, "from")) {
|
11437 | if (this.input.startsWith(tokenLabelName(140), this.nextTokenStartSince(next + 4))) {
|
11438 | return true;
|
11439 | }
|
11440 | }
|
11441 | }
|
11442 |
|
11443 | return super.isExportDefaultSpecifier();
|
11444 | }
|
11445 |
|
11446 | maybeParseExportDefaultSpecifier(node) {
|
11447 | if (node.specifiers && node.specifiers.length > 0) {
|
11448 | return true;
|
11449 | }
|
11450 |
|
11451 | return super.maybeParseExportDefaultSpecifier(...arguments);
|
11452 | }
|
11453 |
|
11454 | checkExport(node) {
|
11455 | const {
|
11456 | specifiers
|
11457 | } = node;
|
11458 |
|
11459 | if (specifiers != null && specifiers.length) {
|
11460 | node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
|
11461 | }
|
11462 |
|
11463 | super.checkExport(node);
|
11464 | node.specifiers = specifiers;
|
11465 | }
|
11466 |
|
11467 | parseImport(node) {
|
11468 | const placeholder = this.parsePlaceholder("Identifier");
|
11469 | if (!placeholder) return super.parseImport(...arguments);
|
11470 | node.specifiers = [];
|
11471 |
|
11472 | if (!this.isContextual(97) && !this.match(12)) {
|
11473 | node.source = this.finishPlaceholder(placeholder, "StringLiteral");
|
11474 | this.semicolon();
|
11475 | return this.finishNode(node, "ImportDeclaration");
|
11476 | }
|
11477 |
|
11478 | const specifier = this.startNodeAtNode(placeholder);
|
11479 | specifier.local = placeholder;
|
11480 | this.finishNode(specifier, "ImportDefaultSpecifier");
|
11481 | node.specifiers.push(specifier);
|
11482 |
|
11483 | if (this.eat(12)) {
|
11484 | const hasStarImport = this.maybeParseStarImportSpecifier(node);
|
11485 | if (!hasStarImport) this.parseNamedImportSpecifiers(node);
|
11486 | }
|
11487 |
|
11488 | this.expectContextual(97);
|
11489 | node.source = this.parseImportSource();
|
11490 | this.semicolon();
|
11491 | return this.finishNode(node, "ImportDeclaration");
|
11492 | }
|
11493 |
|
11494 | parseImportSource() {
|
11495 | return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
|
11496 | }
|
11497 |
|
11498 | assertNoSpace() {
|
11499 | if (this.state.start > this.state.lastTokEndLoc.index) {
|
11500 | this.raise(PlaceholderErrors.UnexpectedSpace, {
|
11501 | at: this.state.lastTokEndLoc
|
11502 | });
|
11503 | }
|
11504 | }
|
11505 |
|
11506 | });
|
11507 |
|
11508 | var v8intrinsic = (superClass => class extends superClass {
|
11509 | parseV8Intrinsic() {
|
11510 | if (this.match(54)) {
|
11511 | const v8IntrinsicStartLoc = this.state.startLoc;
|
11512 | const node = this.startNode();
|
11513 | this.next();
|
11514 |
|
11515 | if (tokenIsIdentifier(this.state.type)) {
|
11516 | const name = this.parseIdentifierName(this.state.start);
|
11517 | const identifier = this.createIdentifier(node, name);
|
11518 | identifier.type = "V8IntrinsicIdentifier";
|
11519 |
|
11520 | if (this.match(10)) {
|
11521 | return identifier;
|
11522 | }
|
11523 | }
|
11524 |
|
11525 | this.unexpected(v8IntrinsicStartLoc);
|
11526 | }
|
11527 | }
|
11528 |
|
11529 | parseExprAtom() {
|
11530 | return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
|
11531 | }
|
11532 |
|
11533 | });
|
11534 |
|
11535 | function hasPlugin(plugins, expectedConfig) {
|
11536 | const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
|
11537 | const expectedKeys = Object.keys(expectedOptions);
|
11538 | const expectedOptionsIsEmpty = expectedKeys.length === 0;
|
11539 | return plugins.some(p => {
|
11540 | if (typeof p === "string") {
|
11541 | return expectedOptionsIsEmpty && p === expectedName;
|
11542 | } else {
|
11543 | const [pluginName, pluginOptions] = p;
|
11544 |
|
11545 | if (pluginName !== expectedName) {
|
11546 | return false;
|
11547 | }
|
11548 |
|
11549 | for (const key of expectedKeys) {
|
11550 | if (pluginOptions[key] !== expectedOptions[key]) {
|
11551 | return false;
|
11552 | }
|
11553 | }
|
11554 |
|
11555 | return true;
|
11556 | }
|
11557 | });
|
11558 | }
|
11559 | function getPluginOption(plugins, name, option) {
|
11560 | const plugin = plugins.find(plugin => {
|
11561 | if (Array.isArray(plugin)) {
|
11562 | return plugin[0] === name;
|
11563 | } else {
|
11564 | return plugin === name;
|
11565 | }
|
11566 | });
|
11567 |
|
11568 | if (plugin && Array.isArray(plugin)) {
|
11569 | return plugin[1][option];
|
11570 | }
|
11571 |
|
11572 | return null;
|
11573 | }
|
11574 | const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
|
11575 | const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
|
11576 | const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
|
11577 | function validatePlugins(plugins) {
|
11578 | if (hasPlugin(plugins, "decorators")) {
|
11579 | if (hasPlugin(plugins, "decorators-legacy")) {
|
11580 | throw new Error("Cannot use the decorators and decorators-legacy plugin together");
|
11581 | }
|
11582 |
|
11583 | const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
|
11584 |
|
11585 | if (decoratorsBeforeExport == null) {
|
11586 | throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
|
11587 | } else if (typeof decoratorsBeforeExport !== "boolean") {
|
11588 | throw new Error("'decoratorsBeforeExport' must be a boolean.");
|
11589 | }
|
11590 | }
|
11591 |
|
11592 | if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
|
11593 | throw new Error("Cannot combine flow and typescript plugins.");
|
11594 | }
|
11595 |
|
11596 | if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
|
11597 | throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
|
11598 | }
|
11599 |
|
11600 | if (hasPlugin(plugins, "pipelineOperator")) {
|
11601 | const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
|
11602 |
|
11603 | if (!PIPELINE_PROPOSALS.includes(proposal)) {
|
11604 | const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
|
11605 | throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
|
11606 | }
|
11607 |
|
11608 | const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
|
11609 | syntaxType: "hash"
|
11610 | }]);
|
11611 |
|
11612 | if (proposal === "hack") {
|
11613 | if (hasPlugin(plugins, "placeholders")) {
|
11614 | throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
|
11615 | }
|
11616 |
|
11617 | if (hasPlugin(plugins, "v8intrinsic")) {
|
11618 | throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
|
11619 | }
|
11620 |
|
11621 | const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
|
11622 |
|
11623 | if (!TOPIC_TOKENS.includes(topicToken)) {
|
11624 | const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
|
11625 | throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
|
11626 | }
|
11627 |
|
11628 | if (topicToken === "#" && tupleSyntaxIsHash) {
|
11629 | throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
|
11630 | }
|
11631 | } else if (proposal === "smart" && tupleSyntaxIsHash) {
|
11632 | throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
|
11633 | }
|
11634 | }
|
11635 |
|
11636 | if (hasPlugin(plugins, "moduleAttributes")) {
|
11637 | {
|
11638 | if (hasPlugin(plugins, "importAssertions")) {
|
11639 | throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
|
11640 | }
|
11641 |
|
11642 | const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
|
11643 |
|
11644 | if (moduleAttributesVersionPluginOption !== "may-2020") {
|
11645 | throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
|
11646 | }
|
11647 | }
|
11648 | }
|
11649 |
|
11650 | if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
|
11651 | throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
|
11652 | }
|
11653 |
|
11654 | if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
|
11655 | const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
|
11656 | error.missingPlugins = "doExpressions";
|
11657 | throw error;
|
11658 | }
|
11659 | }
|
11660 | const mixinPlugins = {
|
11661 | estree,
|
11662 | jsx,
|
11663 | flow,
|
11664 | typescript,
|
11665 | v8intrinsic,
|
11666 | placeholders
|
11667 | };
|
11668 | const mixinPluginNames = Object.keys(mixinPlugins);
|
11669 |
|
11670 | const defaultOptions = {
|
11671 | sourceType: "script",
|
11672 | sourceFilename: undefined,
|
11673 | startColumn: 0,
|
11674 | startLine: 1,
|
11675 | allowAwaitOutsideFunction: false,
|
11676 | allowReturnOutsideFunction: false,
|
11677 | allowImportExportEverywhere: false,
|
11678 | allowSuperOutsideMethod: false,
|
11679 | allowUndeclaredExports: false,
|
11680 | plugins: [],
|
11681 | strictMode: null,
|
11682 | ranges: false,
|
11683 | tokens: false,
|
11684 | createParenthesizedExpressions: false,
|
11685 | errorRecovery: false,
|
11686 | attachComment: true
|
11687 | };
|
11688 | function getOptions(opts) {
|
11689 | const options = {};
|
11690 |
|
11691 | for (const key of Object.keys(defaultOptions)) {
|
11692 | options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
|
11693 | }
|
11694 |
|
11695 | return options;
|
11696 | }
|
11697 |
|
11698 | const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
|
11699 |
|
11700 | const unwrapParenthesizedExpression = node => {
|
11701 | return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
|
11702 | };
|
11703 |
|
11704 | class LValParser extends NodeUtils {
|
11705 | toAssignable(node, isLHS = false) {
|
11706 | var _node$extra, _node$extra3;
|
11707 |
|
11708 | let parenthesized = undefined;
|
11709 |
|
11710 | if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
|
11711 | parenthesized = unwrapParenthesizedExpression(node);
|
11712 |
|
11713 | if (isLHS) {
|
11714 | if (parenthesized.type === "Identifier") {
|
11715 | this.expressionScope.recordArrowParemeterBindingError(Errors.InvalidParenthesizedAssignment, {
|
11716 | at: node
|
11717 | });
|
11718 | } else if (parenthesized.type !== "MemberExpression") {
|
11719 | this.raise(Errors.InvalidParenthesizedAssignment, {
|
11720 | at: node
|
11721 | });
|
11722 | }
|
11723 | } else {
|
11724 | this.raise(Errors.InvalidParenthesizedAssignment, {
|
11725 | at: node
|
11726 | });
|
11727 | }
|
11728 | }
|
11729 |
|
11730 | switch (node.type) {
|
11731 | case "Identifier":
|
11732 | case "ObjectPattern":
|
11733 | case "ArrayPattern":
|
11734 | case "AssignmentPattern":
|
11735 | case "RestElement":
|
11736 | break;
|
11737 |
|
11738 | case "ObjectExpression":
|
11739 | node.type = "ObjectPattern";
|
11740 |
|
11741 | for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
|
11742 | var _node$extra2;
|
11743 |
|
11744 | const prop = node.properties[i];
|
11745 | const isLast = i === last;
|
11746 | this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
|
11747 |
|
11748 | if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
|
11749 | this.raise(Errors.RestTrailingComma, {
|
11750 | at: node.extra.trailingCommaLoc
|
11751 | });
|
11752 | }
|
11753 | }
|
11754 |
|
11755 | break;
|
11756 |
|
11757 | case "ObjectProperty":
|
11758 | {
|
11759 | const {
|
11760 | key,
|
11761 | value
|
11762 | } = node;
|
11763 |
|
11764 | if (this.isPrivateName(key)) {
|
11765 | this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
|
11766 | }
|
11767 |
|
11768 | this.toAssignable(value, isLHS);
|
11769 | break;
|
11770 | }
|
11771 |
|
11772 | case "SpreadElement":
|
11773 | {
|
11774 | throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
|
11775 | }
|
11776 |
|
11777 | case "ArrayExpression":
|
11778 | node.type = "ArrayPattern";
|
11779 | this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
|
11780 | break;
|
11781 |
|
11782 | case "AssignmentExpression":
|
11783 | if (node.operator !== "=") {
|
11784 | this.raise(Errors.MissingEqInAssignment, {
|
11785 | at: node.left.loc.end
|
11786 | });
|
11787 | }
|
11788 |
|
11789 | node.type = "AssignmentPattern";
|
11790 | delete node.operator;
|
11791 | this.toAssignable(node.left, isLHS);
|
11792 | break;
|
11793 |
|
11794 | case "ParenthesizedExpression":
|
11795 | this.toAssignable(parenthesized, isLHS);
|
11796 | break;
|
11797 | }
|
11798 | }
|
11799 |
|
11800 | toAssignableObjectExpressionProp(prop, isLast, isLHS) {
|
11801 | if (prop.type === "ObjectMethod") {
|
11802 | this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
|
11803 | at: prop.key
|
11804 | });
|
11805 | } else if (prop.type === "SpreadElement") {
|
11806 | prop.type = "RestElement";
|
11807 | const arg = prop.argument;
|
11808 | this.checkToRestConversion(arg, false);
|
11809 | this.toAssignable(arg, isLHS);
|
11810 |
|
11811 | if (!isLast) {
|
11812 | this.raise(Errors.RestTrailingComma, {
|
11813 | at: prop
|
11814 | });
|
11815 | }
|
11816 | } else {
|
11817 | this.toAssignable(prop, isLHS);
|
11818 | }
|
11819 | }
|
11820 |
|
11821 | toAssignableList(exprList, trailingCommaLoc, isLHS) {
|
11822 | const end = exprList.length - 1;
|
11823 |
|
11824 | for (let i = 0; i <= end; i++) {
|
11825 | const elt = exprList[i];
|
11826 | if (!elt) continue;
|
11827 |
|
11828 | if (elt.type === "SpreadElement") {
|
11829 | elt.type = "RestElement";
|
11830 | const arg = elt.argument;
|
11831 | this.checkToRestConversion(arg, true);
|
11832 | this.toAssignable(arg, isLHS);
|
11833 | } else {
|
11834 | this.toAssignable(elt, isLHS);
|
11835 | }
|
11836 |
|
11837 | if (elt.type === "RestElement") {
|
11838 | if (i < end) {
|
11839 | this.raise(Errors.RestTrailingComma, {
|
11840 | at: elt
|
11841 | });
|
11842 | } else if (trailingCommaLoc) {
|
11843 | this.raise(Errors.RestTrailingComma, {
|
11844 | at: trailingCommaLoc
|
11845 | });
|
11846 | }
|
11847 | }
|
11848 | }
|
11849 | }
|
11850 |
|
11851 | isAssignable(node, isBinding) {
|
11852 | switch (node.type) {
|
11853 | case "Identifier":
|
11854 | case "ObjectPattern":
|
11855 | case "ArrayPattern":
|
11856 | case "AssignmentPattern":
|
11857 | case "RestElement":
|
11858 | return true;
|
11859 |
|
11860 | case "ObjectExpression":
|
11861 | {
|
11862 | const last = node.properties.length - 1;
|
11863 | return node.properties.every((prop, i) => {
|
11864 | return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
|
11865 | });
|
11866 | }
|
11867 |
|
11868 | case "ObjectProperty":
|
11869 | return this.isAssignable(node.value);
|
11870 |
|
11871 | case "SpreadElement":
|
11872 | return this.isAssignable(node.argument);
|
11873 |
|
11874 | case "ArrayExpression":
|
11875 | return node.elements.every(element => element === null || this.isAssignable(element));
|
11876 |
|
11877 | case "AssignmentExpression":
|
11878 | return node.operator === "=";
|
11879 |
|
11880 | case "ParenthesizedExpression":
|
11881 | return this.isAssignable(node.expression);
|
11882 |
|
11883 | case "MemberExpression":
|
11884 | case "OptionalMemberExpression":
|
11885 | return !isBinding;
|
11886 |
|
11887 | default:
|
11888 | return false;
|
11889 | }
|
11890 | }
|
11891 |
|
11892 | toReferencedList(exprList, isParenthesizedExpr) {
|
11893 | return exprList;
|
11894 | }
|
11895 |
|
11896 | toReferencedListDeep(exprList, isParenthesizedExpr) {
|
11897 | this.toReferencedList(exprList, isParenthesizedExpr);
|
11898 |
|
11899 | for (const expr of exprList) {
|
11900 | if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
|
11901 | this.toReferencedListDeep(expr.elements);
|
11902 | }
|
11903 | }
|
11904 | }
|
11905 |
|
11906 | parseSpread(refExpressionErrors, refNeedsArrowPos) {
|
11907 | const node = this.startNode();
|
11908 | this.next();
|
11909 | node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
|
11910 | return this.finishNode(node, "SpreadElement");
|
11911 | }
|
11912 |
|
11913 | parseRestBinding() {
|
11914 | const node = this.startNode();
|
11915 | this.next();
|
11916 | node.argument = this.parseBindingAtom();
|
11917 | return this.finishNode(node, "RestElement");
|
11918 | }
|
11919 |
|
11920 | parseBindingAtom() {
|
11921 | switch (this.state.type) {
|
11922 | case 0:
|
11923 | {
|
11924 | const node = this.startNode();
|
11925 | this.next();
|
11926 | node.elements = this.parseBindingList(3, 93, true);
|
11927 | return this.finishNode(node, "ArrayPattern");
|
11928 | }
|
11929 |
|
11930 | case 5:
|
11931 | return this.parseObjectLike(8, true);
|
11932 | }
|
11933 |
|
11934 | return this.parseIdentifier();
|
11935 | }
|
11936 |
|
11937 | parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
|
11938 | const elts = [];
|
11939 | let first = true;
|
11940 |
|
11941 | while (!this.eat(close)) {
|
11942 | if (first) {
|
11943 | first = false;
|
11944 | } else {
|
11945 | this.expect(12);
|
11946 | }
|
11947 |
|
11948 | if (allowEmpty && this.match(12)) {
|
11949 | elts.push(null);
|
11950 | } else if (this.eat(close)) {
|
11951 | break;
|
11952 | } else if (this.match(21)) {
|
11953 | elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
|
11954 |
|
11955 | if (!this.checkCommaAfterRest(closeCharCode)) {
|
11956 | this.expect(close);
|
11957 | break;
|
11958 | }
|
11959 | } else {
|
11960 | const decorators = [];
|
11961 |
|
11962 | if (this.match(26) && this.hasPlugin("decorators")) {
|
11963 | this.raise(Errors.UnsupportedParameterDecorator, {
|
11964 | at: this.state.startLoc
|
11965 | });
|
11966 | }
|
11967 |
|
11968 | while (this.match(26)) {
|
11969 | decorators.push(this.parseDecorator());
|
11970 | }
|
11971 |
|
11972 | elts.push(this.parseAssignableListItem(allowModifiers, decorators));
|
11973 | }
|
11974 | }
|
11975 |
|
11976 | return elts;
|
11977 | }
|
11978 |
|
11979 | parseBindingRestProperty(prop) {
|
11980 | this.next();
|
11981 | prop.argument = this.parseIdentifier();
|
11982 | this.checkCommaAfterRest(125);
|
11983 | return this.finishNode(prop, "RestElement");
|
11984 | }
|
11985 |
|
11986 | parseBindingProperty() {
|
11987 | const prop = this.startNode();
|
11988 | const {
|
11989 | type,
|
11990 | start: startPos,
|
11991 | startLoc
|
11992 | } = this.state;
|
11993 |
|
11994 | if (type === 21) {
|
11995 | return this.parseBindingRestProperty(prop);
|
11996 | } else if (type === 134) {
|
11997 | this.expectPlugin("destructuringPrivate", startLoc);
|
11998 | this.classScope.usePrivateName(this.state.value, startLoc);
|
11999 | prop.key = this.parsePrivateName();
|
12000 | } else {
|
12001 | this.parsePropertyName(prop);
|
12002 | }
|
12003 |
|
12004 | prop.method = false;
|
12005 | this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
|
12006 | return prop;
|
12007 | }
|
12008 |
|
12009 | parseAssignableListItem(allowModifiers, decorators) {
|
12010 | const left = this.parseMaybeDefault();
|
12011 | this.parseAssignableListItemTypes(left);
|
12012 | const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
|
12013 |
|
12014 | if (decorators.length) {
|
12015 | left.decorators = decorators;
|
12016 | }
|
12017 |
|
12018 | return elt;
|
12019 | }
|
12020 |
|
12021 | parseAssignableListItemTypes(param) {
|
12022 | return param;
|
12023 | }
|
12024 |
|
12025 | parseMaybeDefault(startPos, startLoc, left) {
|
12026 | var _startLoc, _startPos, _left;
|
12027 |
|
12028 | startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
|
12029 | startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
|
12030 | left = (_left = left) != null ? _left : this.parseBindingAtom();
|
12031 | if (!this.eat(29)) return left;
|
12032 | const node = this.startNodeAt(startPos, startLoc);
|
12033 | node.left = left;
|
12034 | node.right = this.parseMaybeAssignAllowIn();
|
12035 | return this.finishNode(node, "AssignmentPattern");
|
12036 | }
|
12037 |
|
12038 | isValidLVal(type, isUnparenthesizedInAssign, binding) {
|
12039 | return getOwn({
|
12040 | AssignmentPattern: "left",
|
12041 | RestElement: "argument",
|
12042 | ObjectProperty: "value",
|
12043 | ParenthesizedExpression: "expression",
|
12044 | ArrayPattern: "elements",
|
12045 | ObjectPattern: "properties"
|
12046 | }, type);
|
12047 | }
|
12048 |
|
12049 | checkLVal(expression, {
|
12050 | in: ancestor,
|
12051 | binding = BIND_NONE,
|
12052 | checkClashes = false,
|
12053 | strictModeChanged = false,
|
12054 | allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
|
12055 | hasParenthesizedAncestor = false
|
12056 | }) {
|
12057 | var _expression$extra;
|
12058 |
|
12059 | const type = expression.type;
|
12060 | if (this.isObjectMethod(expression)) return;
|
12061 |
|
12062 | if (type === "MemberExpression") {
|
12063 | if (binding !== BIND_NONE) {
|
12064 | this.raise(Errors.InvalidPropertyBindingPattern, {
|
12065 | at: expression
|
12066 | });
|
12067 | }
|
12068 |
|
12069 | return;
|
12070 | }
|
12071 |
|
12072 | if (expression.type === "Identifier") {
|
12073 | this.checkIdentifier(expression, binding, strictModeChanged, allowingSloppyLetBinding);
|
12074 | const {
|
12075 | name
|
12076 | } = expression;
|
12077 |
|
12078 | if (checkClashes) {
|
12079 | if (checkClashes.has(name)) {
|
12080 | this.raise(Errors.ParamDupe, {
|
12081 | at: expression
|
12082 | });
|
12083 | } else {
|
12084 | checkClashes.add(name);
|
12085 | }
|
12086 | }
|
12087 |
|
12088 | return;
|
12089 | }
|
12090 |
|
12091 | const validity = this.isValidLVal(expression.type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
|
12092 | if (validity === true) return;
|
12093 |
|
12094 | if (validity === false) {
|
12095 | const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
|
12096 | this.raise(ParseErrorClass, {
|
12097 | at: expression,
|
12098 | ancestor: ancestor.type === "UpdateExpression" ? {
|
12099 | type: "UpdateExpression",
|
12100 | prefix: ancestor.prefix
|
12101 | } : {
|
12102 | type: ancestor.type
|
12103 | }
|
12104 | });
|
12105 | return;
|
12106 | }
|
12107 |
|
12108 | const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
|
12109 | const nextAncestor = expression.type === "ArrayPattern" || expression.type === "ObjectPattern" || expression.type === "ParenthesizedExpression" ? expression : ancestor;
|
12110 |
|
12111 | for (const child of [].concat(expression[key])) {
|
12112 | if (child) {
|
12113 | this.checkLVal(child, {
|
12114 | in: nextAncestor,
|
12115 | binding,
|
12116 | checkClashes,
|
12117 | allowingSloppyLetBinding,
|
12118 | strictModeChanged,
|
12119 | hasParenthesizedAncestor: isParenthesizedExpression
|
12120 | });
|
12121 | }
|
12122 | }
|
12123 | }
|
12124 |
|
12125 | checkIdentifier(at, bindingType, strictModeChanged = false, allowLetBinding = !(bindingType & BIND_SCOPE_LEXICAL)) {
|
12126 | if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
|
12127 | if (bindingType === BIND_NONE) {
|
12128 | this.raise(Errors.StrictEvalArguments, {
|
12129 | at,
|
12130 | referenceName: at.name
|
12131 | });
|
12132 | } else {
|
12133 | this.raise(Errors.StrictEvalArgumentsBinding, {
|
12134 | at,
|
12135 | bindingName: at.name
|
12136 | });
|
12137 | }
|
12138 | }
|
12139 |
|
12140 | if (!allowLetBinding && at.name === "let") {
|
12141 | this.raise(Errors.LetInLexicalBinding, {
|
12142 | at
|
12143 | });
|
12144 | }
|
12145 |
|
12146 | if (!(bindingType & BIND_NONE)) {
|
12147 | this.declareNameFromIdentifier(at, bindingType);
|
12148 | }
|
12149 | }
|
12150 |
|
12151 | declareNameFromIdentifier(identifier, binding) {
|
12152 | this.scope.declareName(identifier.name, binding, identifier.loc.start);
|
12153 | }
|
12154 |
|
12155 | checkToRestConversion(node, allowPattern) {
|
12156 | switch (node.type) {
|
12157 | case "ParenthesizedExpression":
|
12158 | this.checkToRestConversion(node.expression, allowPattern);
|
12159 | break;
|
12160 |
|
12161 | case "Identifier":
|
12162 | case "MemberExpression":
|
12163 | break;
|
12164 |
|
12165 | case "ArrayExpression":
|
12166 | case "ObjectExpression":
|
12167 | if (allowPattern) break;
|
12168 |
|
12169 | default:
|
12170 | this.raise(Errors.InvalidRestAssignmentPattern, {
|
12171 | at: node
|
12172 | });
|
12173 | }
|
12174 | }
|
12175 |
|
12176 | checkCommaAfterRest(close) {
|
12177 | if (!this.match(12)) {
|
12178 | return false;
|
12179 | }
|
12180 |
|
12181 | this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
|
12182 | at: this.state.startLoc
|
12183 | });
|
12184 | return true;
|
12185 | }
|
12186 |
|
12187 | }
|
12188 |
|
12189 | class ExpressionParser extends LValParser {
|
12190 | checkProto(prop, isRecord, protoRef, refExpressionErrors) {
|
12191 | if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
|
12192 | return;
|
12193 | }
|
12194 |
|
12195 | const key = prop.key;
|
12196 | const name = key.type === "Identifier" ? key.name : key.value;
|
12197 |
|
12198 | if (name === "__proto__") {
|
12199 | if (isRecord) {
|
12200 | this.raise(Errors.RecordNoProto, {
|
12201 | at: key
|
12202 | });
|
12203 | return;
|
12204 | }
|
12205 |
|
12206 | if (protoRef.used) {
|
12207 | if (refExpressionErrors) {
|
12208 | if (refExpressionErrors.doubleProtoLoc === null) {
|
12209 | refExpressionErrors.doubleProtoLoc = key.loc.start;
|
12210 | }
|
12211 | } else {
|
12212 | this.raise(Errors.DuplicateProto, {
|
12213 | at: key
|
12214 | });
|
12215 | }
|
12216 | }
|
12217 |
|
12218 | protoRef.used = true;
|
12219 | }
|
12220 | }
|
12221 |
|
12222 | shouldExitDescending(expr, potentialArrowAt) {
|
12223 | return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
|
12224 | }
|
12225 |
|
12226 | getExpression() {
|
12227 | this.enterInitialScopes();
|
12228 | this.nextToken();
|
12229 | const expr = this.parseExpression();
|
12230 |
|
12231 | if (!this.match(135)) {
|
12232 | this.unexpected();
|
12233 | }
|
12234 |
|
12235 | this.finalizeRemainingComments();
|
12236 | expr.comments = this.state.comments;
|
12237 | expr.errors = this.state.errors;
|
12238 |
|
12239 | if (this.options.tokens) {
|
12240 | expr.tokens = this.tokens;
|
12241 | }
|
12242 |
|
12243 | return expr;
|
12244 | }
|
12245 |
|
12246 | parseExpression(disallowIn, refExpressionErrors) {
|
12247 | if (disallowIn) {
|
12248 | return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
|
12249 | }
|
12250 |
|
12251 | return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
|
12252 | }
|
12253 |
|
12254 | parseExpressionBase(refExpressionErrors) {
|
12255 | const startPos = this.state.start;
|
12256 | const startLoc = this.state.startLoc;
|
12257 | const expr = this.parseMaybeAssign(refExpressionErrors);
|
12258 |
|
12259 | if (this.match(12)) {
|
12260 | const node = this.startNodeAt(startPos, startLoc);
|
12261 | node.expressions = [expr];
|
12262 |
|
12263 | while (this.eat(12)) {
|
12264 | node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
|
12265 | }
|
12266 |
|
12267 | this.toReferencedList(node.expressions);
|
12268 | return this.finishNode(node, "SequenceExpression");
|
12269 | }
|
12270 |
|
12271 | return expr;
|
12272 | }
|
12273 |
|
12274 | parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
|
12275 | return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
|
12276 | }
|
12277 |
|
12278 | parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
|
12279 | return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
|
12280 | }
|
12281 |
|
12282 | setOptionalParametersError(refExpressionErrors, resultError) {
|
12283 | var _resultError$loc;
|
12284 |
|
12285 | refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
|
12286 | }
|
12287 |
|
12288 | parseMaybeAssign(refExpressionErrors, afterLeftParse) {
|
12289 | const startPos = this.state.start;
|
12290 | const startLoc = this.state.startLoc;
|
12291 |
|
12292 | if (this.isContextual(105)) {
|
12293 | if (this.prodParam.hasYield) {
|
12294 | let left = this.parseYield();
|
12295 |
|
12296 | if (afterLeftParse) {
|
12297 | left = afterLeftParse.call(this, left, startPos, startLoc);
|
12298 | }
|
12299 |
|
12300 | return left;
|
12301 | }
|
12302 | }
|
12303 |
|
12304 | let ownExpressionErrors;
|
12305 |
|
12306 | if (refExpressionErrors) {
|
12307 | ownExpressionErrors = false;
|
12308 | } else {
|
12309 | refExpressionErrors = new ExpressionErrors();
|
12310 | ownExpressionErrors = true;
|
12311 | }
|
12312 |
|
12313 | const {
|
12314 | type
|
12315 | } = this.state;
|
12316 |
|
12317 | if (type === 10 || tokenIsIdentifier(type)) {
|
12318 | this.state.potentialArrowAt = this.state.start;
|
12319 | }
|
12320 |
|
12321 | let left = this.parseMaybeConditional(refExpressionErrors);
|
12322 |
|
12323 | if (afterLeftParse) {
|
12324 | left = afterLeftParse.call(this, left, startPos, startLoc);
|
12325 | }
|
12326 |
|
12327 | if (tokenIsAssignment(this.state.type)) {
|
12328 | const node = this.startNodeAt(startPos, startLoc);
|
12329 | const operator = this.state.value;
|
12330 | node.operator = operator;
|
12331 |
|
12332 | if (this.match(29)) {
|
12333 | this.toAssignable(left, true);
|
12334 | node.left = left;
|
12335 |
|
12336 | if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startPos) {
|
12337 | refExpressionErrors.doubleProtoLoc = null;
|
12338 | }
|
12339 |
|
12340 | if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startPos) {
|
12341 | refExpressionErrors.shorthandAssignLoc = null;
|
12342 | }
|
12343 |
|
12344 | if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startPos) {
|
12345 | this.checkDestructuringPrivate(refExpressionErrors);
|
12346 | refExpressionErrors.privateKeyLoc = null;
|
12347 | }
|
12348 | } else {
|
12349 | node.left = left;
|
12350 | }
|
12351 |
|
12352 | this.next();
|
12353 | node.right = this.parseMaybeAssign();
|
12354 | this.checkLVal(left, {
|
12355 | in: this.finishNode(node, "AssignmentExpression")
|
12356 | });
|
12357 | return node;
|
12358 | } else if (ownExpressionErrors) {
|
12359 | this.checkExpressionErrors(refExpressionErrors, true);
|
12360 | }
|
12361 |
|
12362 | return left;
|
12363 | }
|
12364 |
|
12365 | parseMaybeConditional(refExpressionErrors) {
|
12366 | const startPos = this.state.start;
|
12367 | const startLoc = this.state.startLoc;
|
12368 | const potentialArrowAt = this.state.potentialArrowAt;
|
12369 | const expr = this.parseExprOps(refExpressionErrors);
|
12370 |
|
12371 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
12372 | return expr;
|
12373 | }
|
12374 |
|
12375 | return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
|
12376 | }
|
12377 |
|
12378 | parseConditional(expr, startPos, startLoc, refExpressionErrors) {
|
12379 | if (this.eat(17)) {
|
12380 | const node = this.startNodeAt(startPos, startLoc);
|
12381 | node.test = expr;
|
12382 | node.consequent = this.parseMaybeAssignAllowIn();
|
12383 | this.expect(14);
|
12384 | node.alternate = this.parseMaybeAssign();
|
12385 | return this.finishNode(node, "ConditionalExpression");
|
12386 | }
|
12387 |
|
12388 | return expr;
|
12389 | }
|
12390 |
|
12391 | parseMaybeUnaryOrPrivate(refExpressionErrors) {
|
12392 | return this.match(134) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
|
12393 | }
|
12394 |
|
12395 | parseExprOps(refExpressionErrors) {
|
12396 | const startPos = this.state.start;
|
12397 | const startLoc = this.state.startLoc;
|
12398 | const potentialArrowAt = this.state.potentialArrowAt;
|
12399 | const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
|
12400 |
|
12401 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
12402 | return expr;
|
12403 | }
|
12404 |
|
12405 | return this.parseExprOp(expr, startPos, startLoc, -1);
|
12406 | }
|
12407 |
|
12408 | parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
|
12409 | if (this.isPrivateName(left)) {
|
12410 | const value = this.getPrivateNameSV(left);
|
12411 |
|
12412 | if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
|
12413 | this.raise(Errors.PrivateInExpectedIn, {
|
12414 | at: left,
|
12415 | identifierName: value
|
12416 | });
|
12417 | }
|
12418 |
|
12419 | this.classScope.usePrivateName(value, left.loc.start);
|
12420 | }
|
12421 |
|
12422 | const op = this.state.type;
|
12423 |
|
12424 | if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
|
12425 | let prec = tokenOperatorPrecedence(op);
|
12426 |
|
12427 | if (prec > minPrec) {
|
12428 | if (op === 39) {
|
12429 | this.expectPlugin("pipelineOperator");
|
12430 |
|
12431 | if (this.state.inFSharpPipelineDirectBody) {
|
12432 | return left;
|
12433 | }
|
12434 |
|
12435 | this.checkPipelineAtInfixOperator(left, leftStartLoc);
|
12436 | }
|
12437 |
|
12438 | const node = this.startNodeAt(leftStartPos, leftStartLoc);
|
12439 | node.left = left;
|
12440 | node.operator = this.state.value;
|
12441 | const logical = op === 41 || op === 42;
|
12442 | const coalesce = op === 40;
|
12443 |
|
12444 | if (coalesce) {
|
12445 | prec = tokenOperatorPrecedence(42);
|
12446 | }
|
12447 |
|
12448 | this.next();
|
12449 |
|
12450 | if (op === 39 && this.hasPlugin(["pipelineOperator", {
|
12451 | proposal: "minimal"
|
12452 | }])) {
|
12453 | if (this.state.type === 96 && this.prodParam.hasAwait) {
|
12454 | throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
|
12455 | at: this.state.startLoc
|
12456 | });
|
12457 | }
|
12458 | }
|
12459 |
|
12460 | node.right = this.parseExprOpRightExpr(op, prec);
|
12461 | this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
|
12462 | const nextOp = this.state.type;
|
12463 |
|
12464 | if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
|
12465 | throw this.raise(Errors.MixingCoalesceWithLogical, {
|
12466 | at: this.state.startLoc
|
12467 | });
|
12468 | }
|
12469 |
|
12470 | return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
|
12471 | }
|
12472 | }
|
12473 |
|
12474 | return left;
|
12475 | }
|
12476 |
|
12477 | parseExprOpRightExpr(op, prec) {
|
12478 | const startPos = this.state.start;
|
12479 | const startLoc = this.state.startLoc;
|
12480 |
|
12481 | switch (op) {
|
12482 | case 39:
|
12483 | switch (this.getPluginOption("pipelineOperator", "proposal")) {
|
12484 | case "hack":
|
12485 | return this.withTopicBindingContext(() => {
|
12486 | return this.parseHackPipeBody();
|
12487 | });
|
12488 |
|
12489 | case "smart":
|
12490 | return this.withTopicBindingContext(() => {
|
12491 | if (this.prodParam.hasYield && this.isContextual(105)) {
|
12492 | throw this.raise(Errors.PipeBodyIsTighter, {
|
12493 | at: this.state.startLoc
|
12494 | });
|
12495 | }
|
12496 |
|
12497 | return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
|
12498 | });
|
12499 |
|
12500 | case "fsharp":
|
12501 | return this.withSoloAwaitPermittingContext(() => {
|
12502 | return this.parseFSharpPipelineBody(prec);
|
12503 | });
|
12504 | }
|
12505 |
|
12506 | default:
|
12507 | return this.parseExprOpBaseRightExpr(op, prec);
|
12508 | }
|
12509 | }
|
12510 |
|
12511 | parseExprOpBaseRightExpr(op, prec) {
|
12512 | const startPos = this.state.start;
|
12513 | const startLoc = this.state.startLoc;
|
12514 | return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
|
12515 | }
|
12516 |
|
12517 | parseHackPipeBody() {
|
12518 | var _body$extra;
|
12519 |
|
12520 | const {
|
12521 | startLoc
|
12522 | } = this.state;
|
12523 | const body = this.parseMaybeAssign();
|
12524 | const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
|
12525 |
|
12526 | if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
|
12527 | this.raise(Errors.PipeUnparenthesizedBody, {
|
12528 | at: startLoc,
|
12529 | type: body.type
|
12530 | });
|
12531 | }
|
12532 |
|
12533 | if (!this.topicReferenceWasUsedInCurrentContext()) {
|
12534 | this.raise(Errors.PipeTopicUnused, {
|
12535 | at: startLoc
|
12536 | });
|
12537 | }
|
12538 |
|
12539 | return body;
|
12540 | }
|
12541 |
|
12542 | checkExponentialAfterUnary(node) {
|
12543 | if (this.match(57)) {
|
12544 | this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
|
12545 | at: node.argument
|
12546 | });
|
12547 | }
|
12548 | }
|
12549 |
|
12550 | parseMaybeUnary(refExpressionErrors, sawUnary) {
|
12551 | const startPos = this.state.start;
|
12552 | const startLoc = this.state.startLoc;
|
12553 | const isAwait = this.isContextual(96);
|
12554 |
|
12555 | if (isAwait && this.isAwaitAllowed()) {
|
12556 | this.next();
|
12557 | const expr = this.parseAwait(startPos, startLoc);
|
12558 | if (!sawUnary) this.checkExponentialAfterUnary(expr);
|
12559 | return expr;
|
12560 | }
|
12561 |
|
12562 | const update = this.match(34);
|
12563 | const node = this.startNode();
|
12564 |
|
12565 | if (tokenIsPrefix(this.state.type)) {
|
12566 | node.operator = this.state.value;
|
12567 | node.prefix = true;
|
12568 |
|
12569 | if (this.match(72)) {
|
12570 | this.expectPlugin("throwExpressions");
|
12571 | }
|
12572 |
|
12573 | const isDelete = this.match(89);
|
12574 | this.next();
|
12575 | node.argument = this.parseMaybeUnary(null, true);
|
12576 | this.checkExpressionErrors(refExpressionErrors, true);
|
12577 |
|
12578 | if (this.state.strict && isDelete) {
|
12579 | const arg = node.argument;
|
12580 |
|
12581 | if (arg.type === "Identifier") {
|
12582 | this.raise(Errors.StrictDelete, {
|
12583 | at: node
|
12584 | });
|
12585 | } else if (this.hasPropertyAsPrivateName(arg)) {
|
12586 | this.raise(Errors.DeletePrivateField, {
|
12587 | at: node
|
12588 | });
|
12589 | }
|
12590 | }
|
12591 |
|
12592 | if (!update) {
|
12593 | if (!sawUnary) this.checkExponentialAfterUnary(node);
|
12594 | return this.finishNode(node, "UnaryExpression");
|
12595 | }
|
12596 | }
|
12597 |
|
12598 | const expr = this.parseUpdate(node, update, refExpressionErrors);
|
12599 |
|
12600 | if (isAwait) {
|
12601 | const {
|
12602 | type
|
12603 | } = this.state;
|
12604 | const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
|
12605 |
|
12606 | if (startsExpr && !this.isAmbiguousAwait()) {
|
12607 | this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
|
12608 | at: startLoc
|
12609 | });
|
12610 | return this.parseAwait(startPos, startLoc);
|
12611 | }
|
12612 | }
|
12613 |
|
12614 | return expr;
|
12615 | }
|
12616 |
|
12617 | parseUpdate(node, update, refExpressionErrors) {
|
12618 | if (update) {
|
12619 | this.checkLVal(node.argument, {
|
12620 | in: this.finishNode(node, "UpdateExpression")
|
12621 | });
|
12622 | return node;
|
12623 | }
|
12624 |
|
12625 | const startPos = this.state.start;
|
12626 | const startLoc = this.state.startLoc;
|
12627 | let expr = this.parseExprSubscripts(refExpressionErrors);
|
12628 | if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
|
12629 |
|
12630 | while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
|
12631 | const node = this.startNodeAt(startPos, startLoc);
|
12632 | node.operator = this.state.value;
|
12633 | node.prefix = false;
|
12634 | node.argument = expr;
|
12635 | this.next();
|
12636 | this.checkLVal(expr, {
|
12637 | in: expr = this.finishNode(node, "UpdateExpression")
|
12638 | });
|
12639 | }
|
12640 |
|
12641 | return expr;
|
12642 | }
|
12643 |
|
12644 | parseExprSubscripts(refExpressionErrors) {
|
12645 | const startPos = this.state.start;
|
12646 | const startLoc = this.state.startLoc;
|
12647 | const potentialArrowAt = this.state.potentialArrowAt;
|
12648 | const expr = this.parseExprAtom(refExpressionErrors);
|
12649 |
|
12650 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
12651 | return expr;
|
12652 | }
|
12653 |
|
12654 | return this.parseSubscripts(expr, startPos, startLoc);
|
12655 | }
|
12656 |
|
12657 | parseSubscripts(base, startPos, startLoc, noCalls) {
|
12658 | const state = {
|
12659 | optionalChainMember: false,
|
12660 | maybeAsyncArrow: this.atPossibleAsyncArrow(base),
|
12661 | stop: false
|
12662 | };
|
12663 |
|
12664 | do {
|
12665 | base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
|
12666 | state.maybeAsyncArrow = false;
|
12667 | } while (!state.stop);
|
12668 |
|
12669 | return base;
|
12670 | }
|
12671 |
|
12672 | parseSubscript(base, startPos, startLoc, noCalls, state) {
|
12673 | const {
|
12674 | type
|
12675 | } = this.state;
|
12676 |
|
12677 | if (!noCalls && type === 15) {
|
12678 | return this.parseBind(base, startPos, startLoc, noCalls, state);
|
12679 | } else if (tokenIsTemplate(type)) {
|
12680 | return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
|
12681 | }
|
12682 |
|
12683 | let optional = false;
|
12684 |
|
12685 | if (type === 18) {
|
12686 | if (noCalls && this.lookaheadCharCode() === 40) {
|
12687 | state.stop = true;
|
12688 | return base;
|
12689 | }
|
12690 |
|
12691 | state.optionalChainMember = optional = true;
|
12692 | this.next();
|
12693 | }
|
12694 |
|
12695 | if (!noCalls && this.match(10)) {
|
12696 | return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
|
12697 | } else {
|
12698 | const computed = this.eat(0);
|
12699 |
|
12700 | if (computed || optional || this.eat(16)) {
|
12701 | return this.parseMember(base, startPos, startLoc, state, computed, optional);
|
12702 | } else {
|
12703 | state.stop = true;
|
12704 | return base;
|
12705 | }
|
12706 | }
|
12707 | }
|
12708 |
|
12709 | parseMember(base, startPos, startLoc, state, computed, optional) {
|
12710 | const node = this.startNodeAt(startPos, startLoc);
|
12711 | node.object = base;
|
12712 | node.computed = computed;
|
12713 |
|
12714 | if (computed) {
|
12715 | node.property = this.parseExpression();
|
12716 | this.expect(3);
|
12717 | } else if (this.match(134)) {
|
12718 | if (base.type === "Super") {
|
12719 | this.raise(Errors.SuperPrivateField, {
|
12720 | at: startLoc
|
12721 | });
|
12722 | }
|
12723 |
|
12724 | this.classScope.usePrivateName(this.state.value, this.state.startLoc);
|
12725 | node.property = this.parsePrivateName();
|
12726 | } else {
|
12727 | node.property = this.parseIdentifier(true);
|
12728 | }
|
12729 |
|
12730 | if (state.optionalChainMember) {
|
12731 | node.optional = optional;
|
12732 | return this.finishNode(node, "OptionalMemberExpression");
|
12733 | } else {
|
12734 | return this.finishNode(node, "MemberExpression");
|
12735 | }
|
12736 | }
|
12737 |
|
12738 | parseBind(base, startPos, startLoc, noCalls, state) {
|
12739 | const node = this.startNodeAt(startPos, startLoc);
|
12740 | node.object = base;
|
12741 | this.next();
|
12742 | node.callee = this.parseNoCallExpr();
|
12743 | state.stop = true;
|
12744 | return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
|
12745 | }
|
12746 |
|
12747 | parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
|
12748 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
12749 | let refExpressionErrors = null;
|
12750 | this.state.maybeInArrowParameters = true;
|
12751 | this.next();
|
12752 | let node = this.startNodeAt(startPos, startLoc);
|
12753 | node.callee = base;
|
12754 | const {
|
12755 | maybeAsyncArrow,
|
12756 | optionalChainMember
|
12757 | } = state;
|
12758 |
|
12759 | if (maybeAsyncArrow) {
|
12760 | this.expressionScope.enter(newAsyncArrowScope());
|
12761 | refExpressionErrors = new ExpressionErrors();
|
12762 | }
|
12763 |
|
12764 | if (optionalChainMember) {
|
12765 | node.optional = optional;
|
12766 | }
|
12767 |
|
12768 | if (optional) {
|
12769 | node.arguments = this.parseCallExpressionArguments(11);
|
12770 | } else {
|
12771 | node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
|
12772 | }
|
12773 |
|
12774 | this.finishCallExpression(node, optionalChainMember);
|
12775 |
|
12776 | if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
|
12777 | state.stop = true;
|
12778 | this.checkDestructuringPrivate(refExpressionErrors);
|
12779 | this.expressionScope.validateAsPattern();
|
12780 | this.expressionScope.exit();
|
12781 | node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
|
12782 | } else {
|
12783 | if (maybeAsyncArrow) {
|
12784 | this.checkExpressionErrors(refExpressionErrors, true);
|
12785 | this.expressionScope.exit();
|
12786 | }
|
12787 |
|
12788 | this.toReferencedArguments(node);
|
12789 | }
|
12790 |
|
12791 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
12792 | return node;
|
12793 | }
|
12794 |
|
12795 | toReferencedArguments(node, isParenthesizedExpr) {
|
12796 | this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
|
12797 | }
|
12798 |
|
12799 | parseTaggedTemplateExpression(base, startPos, startLoc, state) {
|
12800 | const node = this.startNodeAt(startPos, startLoc);
|
12801 | node.tag = base;
|
12802 | node.quasi = this.parseTemplate(true);
|
12803 |
|
12804 | if (state.optionalChainMember) {
|
12805 | this.raise(Errors.OptionalChainingNoTemplate, {
|
12806 | at: startLoc
|
12807 | });
|
12808 | }
|
12809 |
|
12810 | return this.finishNode(node, "TaggedTemplateExpression");
|
12811 | }
|
12812 |
|
12813 | atPossibleAsyncArrow(base) {
|
12814 | return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
|
12815 | }
|
12816 |
|
12817 | finishCallExpression(node, optional) {
|
12818 | if (node.callee.type === "Import") {
|
12819 | if (node.arguments.length === 2) {
|
12820 | {
|
12821 | if (!this.hasPlugin("moduleAttributes")) {
|
12822 | this.expectPlugin("importAssertions");
|
12823 | }
|
12824 | }
|
12825 | }
|
12826 |
|
12827 | if (node.arguments.length === 0 || node.arguments.length > 2) {
|
12828 | this.raise(Errors.ImportCallArity, {
|
12829 | at: node,
|
12830 | maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
|
12831 | });
|
12832 | } else {
|
12833 | for (const arg of node.arguments) {
|
12834 | if (arg.type === "SpreadElement") {
|
12835 | this.raise(Errors.ImportCallSpreadArgument, {
|
12836 | at: arg
|
12837 | });
|
12838 | }
|
12839 | }
|
12840 | }
|
12841 | }
|
12842 |
|
12843 | return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
|
12844 | }
|
12845 |
|
12846 | parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
|
12847 | const elts = [];
|
12848 | let first = true;
|
12849 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
12850 | this.state.inFSharpPipelineDirectBody = false;
|
12851 |
|
12852 | while (!this.eat(close)) {
|
12853 | if (first) {
|
12854 | first = false;
|
12855 | } else {
|
12856 | this.expect(12);
|
12857 |
|
12858 | if (this.match(close)) {
|
12859 | if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
|
12860 | this.raise(Errors.ImportCallArgumentTrailingComma, {
|
12861 | at: this.state.lastTokStartLoc
|
12862 | });
|
12863 | }
|
12864 |
|
12865 | if (nodeForExtra) {
|
12866 | this.addTrailingCommaExtraToNode(nodeForExtra);
|
12867 | }
|
12868 |
|
12869 | this.next();
|
12870 | break;
|
12871 | }
|
12872 | }
|
12873 |
|
12874 | elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
|
12875 | }
|
12876 |
|
12877 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
12878 | return elts;
|
12879 | }
|
12880 |
|
12881 | shouldParseAsyncArrow() {
|
12882 | return this.match(19) && !this.canInsertSemicolon();
|
12883 | }
|
12884 |
|
12885 | parseAsyncArrowFromCallExpression(node, call) {
|
12886 | var _call$extra;
|
12887 |
|
12888 | this.resetPreviousNodeTrailingComments(call);
|
12889 | this.expect(19);
|
12890 | this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
|
12891 |
|
12892 | if (call.innerComments) {
|
12893 | setInnerComments(node, call.innerComments);
|
12894 | }
|
12895 |
|
12896 | if (call.callee.trailingComments) {
|
12897 | setInnerComments(node, call.callee.trailingComments);
|
12898 | }
|
12899 |
|
12900 | return node;
|
12901 | }
|
12902 |
|
12903 | parseNoCallExpr() {
|
12904 | const startPos = this.state.start;
|
12905 | const startLoc = this.state.startLoc;
|
12906 | return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
|
12907 | }
|
12908 |
|
12909 | parseExprAtom(refExpressionErrors) {
|
12910 | let node;
|
12911 | const {
|
12912 | type
|
12913 | } = this.state;
|
12914 |
|
12915 | switch (type) {
|
12916 | case 79:
|
12917 | return this.parseSuper();
|
12918 |
|
12919 | case 83:
|
12920 | node = this.startNode();
|
12921 | this.next();
|
12922 |
|
12923 | if (this.match(16)) {
|
12924 | return this.parseImportMetaProperty(node);
|
12925 | }
|
12926 |
|
12927 | if (!this.match(10)) {
|
12928 | this.raise(Errors.UnsupportedImport, {
|
12929 | at: this.state.lastTokStartLoc
|
12930 | });
|
12931 | }
|
12932 |
|
12933 | return this.finishNode(node, "Import");
|
12934 |
|
12935 | case 78:
|
12936 | node = this.startNode();
|
12937 | this.next();
|
12938 | return this.finishNode(node, "ThisExpression");
|
12939 |
|
12940 | case 90:
|
12941 | {
|
12942 | return this.parseDo(this.startNode(), false);
|
12943 | }
|
12944 |
|
12945 | case 56:
|
12946 | case 31:
|
12947 | {
|
12948 | this.readRegexp();
|
12949 | return this.parseRegExpLiteral(this.state.value);
|
12950 | }
|
12951 |
|
12952 | case 130:
|
12953 | return this.parseNumericLiteral(this.state.value);
|
12954 |
|
12955 | case 131:
|
12956 | return this.parseBigIntLiteral(this.state.value);
|
12957 |
|
12958 | case 132:
|
12959 | return this.parseDecimalLiteral(this.state.value);
|
12960 |
|
12961 | case 129:
|
12962 | return this.parseStringLiteral(this.state.value);
|
12963 |
|
12964 | case 84:
|
12965 | return this.parseNullLiteral();
|
12966 |
|
12967 | case 85:
|
12968 | return this.parseBooleanLiteral(true);
|
12969 |
|
12970 | case 86:
|
12971 | return this.parseBooleanLiteral(false);
|
12972 |
|
12973 | case 10:
|
12974 | {
|
12975 | const canBeArrow = this.state.potentialArrowAt === this.state.start;
|
12976 | return this.parseParenAndDistinguishExpression(canBeArrow);
|
12977 | }
|
12978 |
|
12979 | case 2:
|
12980 | case 1:
|
12981 | {
|
12982 | return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
|
12983 | }
|
12984 |
|
12985 | case 0:
|
12986 | {
|
12987 | return this.parseArrayLike(3, true, false, refExpressionErrors);
|
12988 | }
|
12989 |
|
12990 | case 6:
|
12991 | case 7:
|
12992 | {
|
12993 | return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
|
12994 | }
|
12995 |
|
12996 | case 5:
|
12997 | {
|
12998 | return this.parseObjectLike(8, false, false, refExpressionErrors);
|
12999 | }
|
13000 |
|
13001 | case 68:
|
13002 | return this.parseFunctionOrFunctionSent();
|
13003 |
|
13004 | case 26:
|
13005 | this.parseDecorators();
|
13006 |
|
13007 | case 80:
|
13008 | node = this.startNode();
|
13009 | this.takeDecorators(node);
|
13010 | return this.parseClass(node, false);
|
13011 |
|
13012 | case 77:
|
13013 | return this.parseNewOrNewTarget();
|
13014 |
|
13015 | case 25:
|
13016 | case 24:
|
13017 | return this.parseTemplate(false);
|
13018 |
|
13019 | case 15:
|
13020 | {
|
13021 | node = this.startNode();
|
13022 | this.next();
|
13023 | node.object = null;
|
13024 | const callee = node.callee = this.parseNoCallExpr();
|
13025 |
|
13026 | if (callee.type === "MemberExpression") {
|
13027 | return this.finishNode(node, "BindExpression");
|
13028 | } else {
|
13029 | throw this.raise(Errors.UnsupportedBind, {
|
13030 | at: callee
|
13031 | });
|
13032 | }
|
13033 | }
|
13034 |
|
13035 | case 134:
|
13036 | {
|
13037 | this.raise(Errors.PrivateInExpectedIn, {
|
13038 | at: this.state.startLoc,
|
13039 | identifierName: this.state.value
|
13040 | });
|
13041 | return this.parsePrivateName();
|
13042 | }
|
13043 |
|
13044 | case 33:
|
13045 | {
|
13046 | return this.parseTopicReferenceThenEqualsSign(54, "%");
|
13047 | }
|
13048 |
|
13049 | case 32:
|
13050 | {
|
13051 | return this.parseTopicReferenceThenEqualsSign(44, "^");
|
13052 | }
|
13053 |
|
13054 | case 37:
|
13055 | case 38:
|
13056 | {
|
13057 | return this.parseTopicReference("hack");
|
13058 | }
|
13059 |
|
13060 | case 44:
|
13061 | case 54:
|
13062 | case 27:
|
13063 | {
|
13064 | const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
|
13065 |
|
13066 | if (pipeProposal) {
|
13067 | return this.parseTopicReference(pipeProposal);
|
13068 | } else {
|
13069 | throw this.unexpected();
|
13070 | }
|
13071 | }
|
13072 |
|
13073 | case 47:
|
13074 | {
|
13075 | const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
|
13076 |
|
13077 | if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
|
13078 | this.expectOnePlugin(["jsx", "flow", "typescript"]);
|
13079 | break;
|
13080 | } else {
|
13081 | throw this.unexpected();
|
13082 | }
|
13083 | }
|
13084 |
|
13085 | default:
|
13086 | if (tokenIsIdentifier(type)) {
|
13087 | if (this.isContextual(123) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
|
13088 | return this.parseModuleExpression();
|
13089 | }
|
13090 |
|
13091 | const canBeArrow = this.state.potentialArrowAt === this.state.start;
|
13092 | const containsEsc = this.state.containsEsc;
|
13093 | const id = this.parseIdentifier();
|
13094 |
|
13095 | if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
|
13096 | const {
|
13097 | type
|
13098 | } = this.state;
|
13099 |
|
13100 | if (type === 68) {
|
13101 | this.resetPreviousNodeTrailingComments(id);
|
13102 | this.next();
|
13103 | return this.parseFunction(this.startNodeAtNode(id), undefined, true);
|
13104 | } else if (tokenIsIdentifier(type)) {
|
13105 | if (this.lookaheadCharCode() === 61) {
|
13106 | return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
|
13107 | } else {
|
13108 | return id;
|
13109 | }
|
13110 | } else if (type === 90) {
|
13111 | this.resetPreviousNodeTrailingComments(id);
|
13112 | return this.parseDo(this.startNodeAtNode(id), true);
|
13113 | }
|
13114 | }
|
13115 |
|
13116 | if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
|
13117 | this.next();
|
13118 | return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
|
13119 | }
|
13120 |
|
13121 | return id;
|
13122 | } else {
|
13123 | throw this.unexpected();
|
13124 | }
|
13125 |
|
13126 | }
|
13127 | }
|
13128 |
|
13129 | parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
|
13130 | const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
|
13131 |
|
13132 | if (pipeProposal) {
|
13133 | this.state.type = topicTokenType;
|
13134 | this.state.value = topicTokenValue;
|
13135 | this.state.pos--;
|
13136 | this.state.end--;
|
13137 | this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
|
13138 | return this.parseTopicReference(pipeProposal);
|
13139 | } else {
|
13140 | throw this.unexpected();
|
13141 | }
|
13142 | }
|
13143 |
|
13144 | parseTopicReference(pipeProposal) {
|
13145 | const node = this.startNode();
|
13146 | const startLoc = this.state.startLoc;
|
13147 | const tokenType = this.state.type;
|
13148 | this.next();
|
13149 | return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
|
13150 | }
|
13151 |
|
13152 | finishTopicReference(node, startLoc, pipeProposal, tokenType) {
|
13153 | if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
|
13154 | const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
|
13155 |
|
13156 | if (!this.topicReferenceIsAllowedInCurrentContext()) {
|
13157 | this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
|
13158 | at: startLoc
|
13159 | });
|
13160 | }
|
13161 |
|
13162 | this.registerTopicReference();
|
13163 | return this.finishNode(node, nodeType);
|
13164 | } else {
|
13165 | throw this.raise(Errors.PipeTopicUnconfiguredToken, {
|
13166 | at: startLoc,
|
13167 | token: tokenLabelName(tokenType)
|
13168 | });
|
13169 | }
|
13170 | }
|
13171 |
|
13172 | testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
|
13173 | switch (pipeProposal) {
|
13174 | case "hack":
|
13175 | {
|
13176 | return this.hasPlugin(["pipelineOperator", {
|
13177 | topicToken: tokenLabelName(tokenType)
|
13178 | }]);
|
13179 | }
|
13180 |
|
13181 | case "smart":
|
13182 | return tokenType === 27;
|
13183 |
|
13184 | default:
|
13185 | throw this.raise(Errors.PipeTopicRequiresHackPipes, {
|
13186 | at: startLoc
|
13187 | });
|
13188 | }
|
13189 | }
|
13190 |
|
13191 | parseAsyncArrowUnaryFunction(node) {
|
13192 | this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
|
13193 | const params = [this.parseIdentifier()];
|
13194 | this.prodParam.exit();
|
13195 |
|
13196 | if (this.hasPrecedingLineBreak()) {
|
13197 | this.raise(Errors.LineTerminatorBeforeArrow, {
|
13198 | at: this.state.curPosition()
|
13199 | });
|
13200 | }
|
13201 |
|
13202 | this.expect(19);
|
13203 | this.parseArrowExpression(node, params, true);
|
13204 | return node;
|
13205 | }
|
13206 |
|
13207 | parseDo(node, isAsync) {
|
13208 | this.expectPlugin("doExpressions");
|
13209 |
|
13210 | if (isAsync) {
|
13211 | this.expectPlugin("asyncDoExpressions");
|
13212 | }
|
13213 |
|
13214 | node.async = isAsync;
|
13215 | this.next();
|
13216 | const oldLabels = this.state.labels;
|
13217 | this.state.labels = [];
|
13218 |
|
13219 | if (isAsync) {
|
13220 | this.prodParam.enter(PARAM_AWAIT);
|
13221 | node.body = this.parseBlock();
|
13222 | this.prodParam.exit();
|
13223 | } else {
|
13224 | node.body = this.parseBlock();
|
13225 | }
|
13226 |
|
13227 | this.state.labels = oldLabels;
|
13228 | return this.finishNode(node, "DoExpression");
|
13229 | }
|
13230 |
|
13231 | parseSuper() {
|
13232 | const node = this.startNode();
|
13233 | this.next();
|
13234 |
|
13235 | if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
|
13236 | this.raise(Errors.SuperNotAllowed, {
|
13237 | at: node
|
13238 | });
|
13239 | } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
|
13240 | this.raise(Errors.UnexpectedSuper, {
|
13241 | at: node
|
13242 | });
|
13243 | }
|
13244 |
|
13245 | if (!this.match(10) && !this.match(0) && !this.match(16)) {
|
13246 | this.raise(Errors.UnsupportedSuper, {
|
13247 | at: node
|
13248 | });
|
13249 | }
|
13250 |
|
13251 | return this.finishNode(node, "Super");
|
13252 | }
|
13253 |
|
13254 | parsePrivateName() {
|
13255 | const node = this.startNode();
|
13256 | const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
|
13257 | const name = this.state.value;
|
13258 | this.next();
|
13259 | node.id = this.createIdentifier(id, name);
|
13260 | return this.finishNode(node, "PrivateName");
|
13261 | }
|
13262 |
|
13263 | parseFunctionOrFunctionSent() {
|
13264 | const node = this.startNode();
|
13265 | this.next();
|
13266 |
|
13267 | if (this.prodParam.hasYield && this.match(16)) {
|
13268 | const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
|
13269 | this.next();
|
13270 |
|
13271 | if (this.match(102)) {
|
13272 | this.expectPlugin("functionSent");
|
13273 | } else if (!this.hasPlugin("functionSent")) {
|
13274 | this.unexpected();
|
13275 | }
|
13276 |
|
13277 | return this.parseMetaProperty(node, meta, "sent");
|
13278 | }
|
13279 |
|
13280 | return this.parseFunction(node);
|
13281 | }
|
13282 |
|
13283 | parseMetaProperty(node, meta, propertyName) {
|
13284 | node.meta = meta;
|
13285 | const containsEsc = this.state.containsEsc;
|
13286 | node.property = this.parseIdentifier(true);
|
13287 |
|
13288 | if (node.property.name !== propertyName || containsEsc) {
|
13289 | this.raise(Errors.UnsupportedMetaProperty, {
|
13290 | at: node.property,
|
13291 | target: meta.name,
|
13292 | onlyValidPropertyName: propertyName
|
13293 | });
|
13294 | }
|
13295 |
|
13296 | return this.finishNode(node, "MetaProperty");
|
13297 | }
|
13298 |
|
13299 | parseImportMetaProperty(node) {
|
13300 | const id = this.createIdentifier(this.startNodeAtNode(node), "import");
|
13301 | this.next();
|
13302 |
|
13303 | if (this.isContextual(100)) {
|
13304 | if (!this.inModule) {
|
13305 | this.raise(Errors.ImportMetaOutsideModule, {
|
13306 | at: id
|
13307 | });
|
13308 | }
|
13309 |
|
13310 | this.sawUnambiguousESM = true;
|
13311 | }
|
13312 |
|
13313 | return this.parseMetaProperty(node, id, "meta");
|
13314 | }
|
13315 |
|
13316 | parseLiteralAtNode(value, type, node) {
|
13317 | this.addExtra(node, "rawValue", value);
|
13318 | this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
|
13319 | node.value = value;
|
13320 | this.next();
|
13321 | return this.finishNode(node, type);
|
13322 | }
|
13323 |
|
13324 | parseLiteral(value, type) {
|
13325 | const node = this.startNode();
|
13326 | return this.parseLiteralAtNode(value, type, node);
|
13327 | }
|
13328 |
|
13329 | parseStringLiteral(value) {
|
13330 | return this.parseLiteral(value, "StringLiteral");
|
13331 | }
|
13332 |
|
13333 | parseNumericLiteral(value) {
|
13334 | return this.parseLiteral(value, "NumericLiteral");
|
13335 | }
|
13336 |
|
13337 | parseBigIntLiteral(value) {
|
13338 | return this.parseLiteral(value, "BigIntLiteral");
|
13339 | }
|
13340 |
|
13341 | parseDecimalLiteral(value) {
|
13342 | return this.parseLiteral(value, "DecimalLiteral");
|
13343 | }
|
13344 |
|
13345 | parseRegExpLiteral(value) {
|
13346 | const node = this.parseLiteral(value.value, "RegExpLiteral");
|
13347 | node.pattern = value.pattern;
|
13348 | node.flags = value.flags;
|
13349 | return node;
|
13350 | }
|
13351 |
|
13352 | parseBooleanLiteral(value) {
|
13353 | const node = this.startNode();
|
13354 | node.value = value;
|
13355 | this.next();
|
13356 | return this.finishNode(node, "BooleanLiteral");
|
13357 | }
|
13358 |
|
13359 | parseNullLiteral() {
|
13360 | const node = this.startNode();
|
13361 | this.next();
|
13362 | return this.finishNode(node, "NullLiteral");
|
13363 | }
|
13364 |
|
13365 | parseParenAndDistinguishExpression(canBeArrow) {
|
13366 | const startPos = this.state.start;
|
13367 | const startLoc = this.state.startLoc;
|
13368 | let val;
|
13369 | this.next();
|
13370 | this.expressionScope.enter(newArrowHeadScope());
|
13371 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
13372 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
13373 | this.state.maybeInArrowParameters = true;
|
13374 | this.state.inFSharpPipelineDirectBody = false;
|
13375 | const innerStartPos = this.state.start;
|
13376 | const innerStartLoc = this.state.startLoc;
|
13377 | const exprList = [];
|
13378 | const refExpressionErrors = new ExpressionErrors();
|
13379 | let first = true;
|
13380 | let spreadStartLoc;
|
13381 | let optionalCommaStartLoc;
|
13382 |
|
13383 | while (!this.match(11)) {
|
13384 | if (first) {
|
13385 | first = false;
|
13386 | } else {
|
13387 | this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
|
13388 |
|
13389 | if (this.match(11)) {
|
13390 | optionalCommaStartLoc = this.state.startLoc;
|
13391 | break;
|
13392 | }
|
13393 | }
|
13394 |
|
13395 | if (this.match(21)) {
|
13396 | const spreadNodeStartPos = this.state.start;
|
13397 | const spreadNodeStartLoc = this.state.startLoc;
|
13398 | spreadStartLoc = this.state.startLoc;
|
13399 | exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
|
13400 |
|
13401 | if (!this.checkCommaAfterRest(41)) {
|
13402 | break;
|
13403 | }
|
13404 | } else {
|
13405 | exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
|
13406 | }
|
13407 | }
|
13408 |
|
13409 | const innerEndLoc = this.state.lastTokEndLoc;
|
13410 | this.expect(11);
|
13411 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
13412 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
13413 | let arrowNode = this.startNodeAt(startPos, startLoc);
|
13414 |
|
13415 | if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
|
13416 | this.checkDestructuringPrivate(refExpressionErrors);
|
13417 | this.expressionScope.validateAsPattern();
|
13418 | this.expressionScope.exit();
|
13419 | this.parseArrowExpression(arrowNode, exprList, false);
|
13420 | return arrowNode;
|
13421 | }
|
13422 |
|
13423 | this.expressionScope.exit();
|
13424 |
|
13425 | if (!exprList.length) {
|
13426 | this.unexpected(this.state.lastTokStartLoc);
|
13427 | }
|
13428 |
|
13429 | if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
|
13430 | if (spreadStartLoc) this.unexpected(spreadStartLoc);
|
13431 | this.checkExpressionErrors(refExpressionErrors, true);
|
13432 | this.toReferencedListDeep(exprList, true);
|
13433 |
|
13434 | if (exprList.length > 1) {
|
13435 | val = this.startNodeAt(innerStartPos, innerStartLoc);
|
13436 | val.expressions = exprList;
|
13437 | this.finishNode(val, "SequenceExpression");
|
13438 | this.resetEndLocation(val, innerEndLoc);
|
13439 | } else {
|
13440 | val = exprList[0];
|
13441 | }
|
13442 |
|
13443 | return this.wrapParenthesis(startPos, startLoc, val);
|
13444 | }
|
13445 |
|
13446 | wrapParenthesis(startPos, startLoc, expression) {
|
13447 | if (!this.options.createParenthesizedExpressions) {
|
13448 | this.addExtra(expression, "parenthesized", true);
|
13449 | this.addExtra(expression, "parenStart", startPos);
|
13450 | this.takeSurroundingComments(expression, startPos, this.state.lastTokEndLoc.index);
|
13451 | return expression;
|
13452 | }
|
13453 |
|
13454 | const parenExpression = this.startNodeAt(startPos, startLoc);
|
13455 | parenExpression.expression = expression;
|
13456 | this.finishNode(parenExpression, "ParenthesizedExpression");
|
13457 | return parenExpression;
|
13458 | }
|
13459 |
|
13460 | shouldParseArrow(params) {
|
13461 | return !this.canInsertSemicolon();
|
13462 | }
|
13463 |
|
13464 | parseArrow(node) {
|
13465 | if (this.eat(19)) {
|
13466 | return node;
|
13467 | }
|
13468 | }
|
13469 |
|
13470 | parseParenItem(node, startPos, startLoc) {
|
13471 | return node;
|
13472 | }
|
13473 |
|
13474 | parseNewOrNewTarget() {
|
13475 | const node = this.startNode();
|
13476 | this.next();
|
13477 |
|
13478 | if (this.match(16)) {
|
13479 | const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
|
13480 | this.next();
|
13481 | const metaProp = this.parseMetaProperty(node, meta, "target");
|
13482 |
|
13483 | if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
|
13484 | this.raise(Errors.UnexpectedNewTarget, {
|
13485 | at: metaProp
|
13486 | });
|
13487 | }
|
13488 |
|
13489 | return metaProp;
|
13490 | }
|
13491 |
|
13492 | return this.parseNew(node);
|
13493 | }
|
13494 |
|
13495 | parseNew(node) {
|
13496 | this.parseNewCallee(node);
|
13497 |
|
13498 | if (this.eat(10)) {
|
13499 | const args = this.parseExprList(11);
|
13500 | this.toReferencedList(args);
|
13501 | node.arguments = args;
|
13502 | } else {
|
13503 | node.arguments = [];
|
13504 | }
|
13505 |
|
13506 | return this.finishNode(node, "NewExpression");
|
13507 | }
|
13508 |
|
13509 | parseNewCallee(node) {
|
13510 | node.callee = this.parseNoCallExpr();
|
13511 |
|
13512 | if (node.callee.type === "Import") {
|
13513 | this.raise(Errors.ImportCallNotNewExpression, {
|
13514 | at: node.callee
|
13515 | });
|
13516 | } else if (this.isOptionalChain(node.callee)) {
|
13517 | this.raise(Errors.OptionalChainingNoNew, {
|
13518 | at: this.state.lastTokEndLoc
|
13519 | });
|
13520 | } else if (this.eat(18)) {
|
13521 | this.raise(Errors.OptionalChainingNoNew, {
|
13522 | at: this.state.startLoc
|
13523 | });
|
13524 | }
|
13525 | }
|
13526 |
|
13527 | parseTemplateElement(isTagged) {
|
13528 | const {
|
13529 | start,
|
13530 | startLoc,
|
13531 | end,
|
13532 | value
|
13533 | } = this.state;
|
13534 | const elemStart = start + 1;
|
13535 | const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
|
13536 |
|
13537 | if (value === null) {
|
13538 | if (!isTagged) {
|
13539 | this.raise(Errors.InvalidEscapeSequenceTemplate, {
|
13540 | at: createPositionWithColumnOffset(startLoc, 2)
|
13541 | });
|
13542 | }
|
13543 | }
|
13544 |
|
13545 | const isTail = this.match(24);
|
13546 | const endOffset = isTail ? -1 : -2;
|
13547 | const elemEnd = end + endOffset;
|
13548 | elem.value = {
|
13549 | raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
|
13550 | cooked: value === null ? null : value.slice(1, endOffset)
|
13551 | };
|
13552 | elem.tail = isTail;
|
13553 | this.next();
|
13554 | this.finishNode(elem, "TemplateElement");
|
13555 | this.resetEndLocation(elem, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
|
13556 | return elem;
|
13557 | }
|
13558 |
|
13559 | parseTemplate(isTagged) {
|
13560 | const node = this.startNode();
|
13561 | node.expressions = [];
|
13562 | let curElt = this.parseTemplateElement(isTagged);
|
13563 | node.quasis = [curElt];
|
13564 |
|
13565 | while (!curElt.tail) {
|
13566 | node.expressions.push(this.parseTemplateSubstitution());
|
13567 | this.readTemplateContinuation();
|
13568 | node.quasis.push(curElt = this.parseTemplateElement(isTagged));
|
13569 | }
|
13570 |
|
13571 | return this.finishNode(node, "TemplateLiteral");
|
13572 | }
|
13573 |
|
13574 | parseTemplateSubstitution() {
|
13575 | return this.parseExpression();
|
13576 | }
|
13577 |
|
13578 | parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
|
13579 | if (isRecord) {
|
13580 | this.expectPlugin("recordAndTuple");
|
13581 | }
|
13582 |
|
13583 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
13584 | this.state.inFSharpPipelineDirectBody = false;
|
13585 | const propHash = Object.create(null);
|
13586 | let first = true;
|
13587 | const node = this.startNode();
|
13588 | node.properties = [];
|
13589 | this.next();
|
13590 |
|
13591 | while (!this.match(close)) {
|
13592 | if (first) {
|
13593 | first = false;
|
13594 | } else {
|
13595 | this.expect(12);
|
13596 |
|
13597 | if (this.match(close)) {
|
13598 | this.addTrailingCommaExtraToNode(node);
|
13599 | break;
|
13600 | }
|
13601 | }
|
13602 |
|
13603 | let prop;
|
13604 |
|
13605 | if (isPattern) {
|
13606 | prop = this.parseBindingProperty();
|
13607 | } else {
|
13608 | prop = this.parsePropertyDefinition(refExpressionErrors);
|
13609 | this.checkProto(prop, isRecord, propHash, refExpressionErrors);
|
13610 | }
|
13611 |
|
13612 | if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
|
13613 | this.raise(Errors.InvalidRecordProperty, {
|
13614 | at: prop
|
13615 | });
|
13616 | }
|
13617 |
|
13618 | if (prop.shorthand) {
|
13619 | this.addExtra(prop, "shorthand", true);
|
13620 | }
|
13621 |
|
13622 | node.properties.push(prop);
|
13623 | }
|
13624 |
|
13625 | this.next();
|
13626 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
13627 | let type = "ObjectExpression";
|
13628 |
|
13629 | if (isPattern) {
|
13630 | type = "ObjectPattern";
|
13631 | } else if (isRecord) {
|
13632 | type = "RecordExpression";
|
13633 | }
|
13634 |
|
13635 | return this.finishNode(node, type);
|
13636 | }
|
13637 |
|
13638 | addTrailingCommaExtraToNode(node) {
|
13639 | this.addExtra(node, "trailingComma", this.state.lastTokStart);
|
13640 | this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
|
13641 | }
|
13642 |
|
13643 | maybeAsyncOrAccessorProp(prop) {
|
13644 | return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
|
13645 | }
|
13646 |
|
13647 | parsePropertyDefinition(refExpressionErrors) {
|
13648 | let decorators = [];
|
13649 |
|
13650 | if (this.match(26)) {
|
13651 | if (this.hasPlugin("decorators")) {
|
13652 | this.raise(Errors.UnsupportedPropertyDecorator, {
|
13653 | at: this.state.startLoc
|
13654 | });
|
13655 | }
|
13656 |
|
13657 | while (this.match(26)) {
|
13658 | decorators.push(this.parseDecorator());
|
13659 | }
|
13660 | }
|
13661 |
|
13662 | const prop = this.startNode();
|
13663 | let isAsync = false;
|
13664 | let isAccessor = false;
|
13665 | let startPos;
|
13666 | let startLoc;
|
13667 |
|
13668 | if (this.match(21)) {
|
13669 | if (decorators.length) this.unexpected();
|
13670 | return this.parseSpread();
|
13671 | }
|
13672 |
|
13673 | if (decorators.length) {
|
13674 | prop.decorators = decorators;
|
13675 | decorators = [];
|
13676 | }
|
13677 |
|
13678 | prop.method = false;
|
13679 |
|
13680 | if (refExpressionErrors) {
|
13681 | startPos = this.state.start;
|
13682 | startLoc = this.state.startLoc;
|
13683 | }
|
13684 |
|
13685 | let isGenerator = this.eat(55);
|
13686 | this.parsePropertyNamePrefixOperator(prop);
|
13687 | const containsEsc = this.state.containsEsc;
|
13688 | const key = this.parsePropertyName(prop, refExpressionErrors);
|
13689 |
|
13690 | if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
|
13691 | const keyName = key.name;
|
13692 |
|
13693 | if (keyName === "async" && !this.hasPrecedingLineBreak()) {
|
13694 | isAsync = true;
|
13695 | this.resetPreviousNodeTrailingComments(key);
|
13696 | isGenerator = this.eat(55);
|
13697 | this.parsePropertyName(prop);
|
13698 | }
|
13699 |
|
13700 | if (keyName === "get" || keyName === "set") {
|
13701 | isAccessor = true;
|
13702 | this.resetPreviousNodeTrailingComments(key);
|
13703 | prop.kind = keyName;
|
13704 |
|
13705 | if (this.match(55)) {
|
13706 | isGenerator = true;
|
13707 | this.raise(Errors.AccessorIsGenerator, {
|
13708 | at: this.state.curPosition(),
|
13709 | kind: keyName
|
13710 | });
|
13711 | this.next();
|
13712 | }
|
13713 |
|
13714 | this.parsePropertyName(prop);
|
13715 | }
|
13716 | }
|
13717 |
|
13718 | this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
|
13719 | return prop;
|
13720 | }
|
13721 |
|
13722 | getGetterSetterExpectedParamCount(method) {
|
13723 | return method.kind === "get" ? 0 : 1;
|
13724 | }
|
13725 |
|
13726 | getObjectOrClassMethodParams(method) {
|
13727 | return method.params;
|
13728 | }
|
13729 |
|
13730 | checkGetterSetterParams(method) {
|
13731 | var _params;
|
13732 |
|
13733 | const paramCount = this.getGetterSetterExpectedParamCount(method);
|
13734 | const params = this.getObjectOrClassMethodParams(method);
|
13735 |
|
13736 | if (params.length !== paramCount) {
|
13737 | this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
|
13738 | at: method
|
13739 | });
|
13740 | }
|
13741 |
|
13742 | if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
|
13743 | this.raise(Errors.BadSetterRestParameter, {
|
13744 | at: method
|
13745 | });
|
13746 | }
|
13747 | }
|
13748 |
|
13749 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
|
13750 | if (isAccessor) {
|
13751 | this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
|
13752 | this.checkGetterSetterParams(prop);
|
13753 | return prop;
|
13754 | }
|
13755 |
|
13756 | if (isAsync || isGenerator || this.match(10)) {
|
13757 | if (isPattern) this.unexpected();
|
13758 | prop.kind = "method";
|
13759 | prop.method = true;
|
13760 | return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
|
13761 | }
|
13762 | }
|
13763 |
|
13764 | parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
|
13765 | prop.shorthand = false;
|
13766 |
|
13767 | if (this.eat(14)) {
|
13768 | prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
|
13769 | return this.finishNode(prop, "ObjectProperty");
|
13770 | }
|
13771 |
|
13772 | if (!prop.computed && prop.key.type === "Identifier") {
|
13773 | this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
|
13774 |
|
13775 | if (isPattern) {
|
13776 | prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
|
13777 | } else if (this.match(29)) {
|
13778 | const shorthandAssignLoc = this.state.startLoc;
|
13779 |
|
13780 | if (refExpressionErrors != null) {
|
13781 | if (refExpressionErrors.shorthandAssignLoc === null) {
|
13782 | refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
|
13783 | }
|
13784 | } else {
|
13785 | this.raise(Errors.InvalidCoverInitializedName, {
|
13786 | at: shorthandAssignLoc
|
13787 | });
|
13788 | }
|
13789 |
|
13790 | prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
|
13791 | } else {
|
13792 | prop.value = cloneIdentifier(prop.key);
|
13793 | }
|
13794 |
|
13795 | prop.shorthand = true;
|
13796 | return this.finishNode(prop, "ObjectProperty");
|
13797 | }
|
13798 | }
|
13799 |
|
13800 | parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
|
13801 | const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
|
13802 | if (!node) this.unexpected();
|
13803 | return node;
|
13804 | }
|
13805 |
|
13806 | parsePropertyName(prop, refExpressionErrors) {
|
13807 | if (this.eat(0)) {
|
13808 | prop.computed = true;
|
13809 | prop.key = this.parseMaybeAssignAllowIn();
|
13810 | this.expect(3);
|
13811 | } else {
|
13812 | const {
|
13813 | type,
|
13814 | value
|
13815 | } = this.state;
|
13816 | let key;
|
13817 |
|
13818 | if (tokenIsKeywordOrIdentifier(type)) {
|
13819 | key = this.parseIdentifier(true);
|
13820 | } else {
|
13821 | switch (type) {
|
13822 | case 130:
|
13823 | key = this.parseNumericLiteral(value);
|
13824 | break;
|
13825 |
|
13826 | case 129:
|
13827 | key = this.parseStringLiteral(value);
|
13828 | break;
|
13829 |
|
13830 | case 131:
|
13831 | key = this.parseBigIntLiteral(value);
|
13832 | break;
|
13833 |
|
13834 | case 132:
|
13835 | key = this.parseDecimalLiteral(value);
|
13836 | break;
|
13837 |
|
13838 | case 134:
|
13839 | {
|
13840 | const privateKeyLoc = this.state.startLoc;
|
13841 |
|
13842 | if (refExpressionErrors != null) {
|
13843 | if (refExpressionErrors.privateKeyLoc === null) {
|
13844 | refExpressionErrors.privateKeyLoc = privateKeyLoc;
|
13845 | }
|
13846 | } else {
|
13847 | this.raise(Errors.UnexpectedPrivateField, {
|
13848 | at: privateKeyLoc
|
13849 | });
|
13850 | }
|
13851 |
|
13852 | key = this.parsePrivateName();
|
13853 | break;
|
13854 | }
|
13855 |
|
13856 | default:
|
13857 | throw this.unexpected();
|
13858 | }
|
13859 | }
|
13860 |
|
13861 | prop.key = key;
|
13862 |
|
13863 | if (type !== 134) {
|
13864 | prop.computed = false;
|
13865 | }
|
13866 | }
|
13867 |
|
13868 | return prop.key;
|
13869 | }
|
13870 |
|
13871 | initFunction(node, isAsync) {
|
13872 | node.id = null;
|
13873 | node.generator = false;
|
13874 | node.async = !!isAsync;
|
13875 | }
|
13876 |
|
13877 | parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
|
13878 | this.initFunction(node, isAsync);
|
13879 | node.generator = !!isGenerator;
|
13880 | const allowModifiers = isConstructor;
|
13881 | this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
|
13882 | this.prodParam.enter(functionFlags(isAsync, node.generator));
|
13883 | this.parseFunctionParams(node, allowModifiers);
|
13884 | this.parseFunctionBodyAndFinish(node, type, true);
|
13885 | this.prodParam.exit();
|
13886 | this.scope.exit();
|
13887 | return node;
|
13888 | }
|
13889 |
|
13890 | parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
|
13891 | if (isTuple) {
|
13892 | this.expectPlugin("recordAndTuple");
|
13893 | }
|
13894 |
|
13895 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
13896 | this.state.inFSharpPipelineDirectBody = false;
|
13897 | const node = this.startNode();
|
13898 | this.next();
|
13899 | node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
|
13900 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
13901 | return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
|
13902 | }
|
13903 |
|
13904 | parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
|
13905 | this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
|
13906 | let flags = functionFlags(isAsync, false);
|
13907 |
|
13908 | if (!this.match(5) && this.prodParam.hasIn) {
|
13909 | flags |= PARAM_IN;
|
13910 | }
|
13911 |
|
13912 | this.prodParam.enter(flags);
|
13913 | this.initFunction(node, isAsync);
|
13914 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
13915 |
|
13916 | if (params) {
|
13917 | this.state.maybeInArrowParameters = true;
|
13918 | this.setArrowFunctionParameters(node, params, trailingCommaLoc);
|
13919 | }
|
13920 |
|
13921 | this.state.maybeInArrowParameters = false;
|
13922 | this.parseFunctionBody(node, true);
|
13923 | this.prodParam.exit();
|
13924 | this.scope.exit();
|
13925 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
13926 | return this.finishNode(node, "ArrowFunctionExpression");
|
13927 | }
|
13928 |
|
13929 | setArrowFunctionParameters(node, params, trailingCommaLoc) {
|
13930 | this.toAssignableList(params, trailingCommaLoc, false);
|
13931 | node.params = params;
|
13932 | }
|
13933 |
|
13934 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
13935 | this.parseFunctionBody(node, false, isMethod);
|
13936 | this.finishNode(node, type);
|
13937 | }
|
13938 |
|
13939 | parseFunctionBody(node, allowExpression, isMethod = false) {
|
13940 | const isExpression = allowExpression && !this.match(5);
|
13941 | this.expressionScope.enter(newExpressionScope());
|
13942 |
|
13943 | if (isExpression) {
|
13944 | node.body = this.parseMaybeAssign();
|
13945 | this.checkParams(node, false, allowExpression, false);
|
13946 | } else {
|
13947 | const oldStrict = this.state.strict;
|
13948 | const oldLabels = this.state.labels;
|
13949 | this.state.labels = [];
|
13950 | this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
|
13951 | node.body = this.parseBlock(true, false, hasStrictModeDirective => {
|
13952 | const nonSimple = !this.isSimpleParamList(node.params);
|
13953 |
|
13954 | if (hasStrictModeDirective && nonSimple) {
|
13955 | this.raise(Errors.IllegalLanguageModeDirective, {
|
13956 | at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
|
13957 | });
|
13958 | }
|
13959 |
|
13960 | const strictModeChanged = !oldStrict && this.state.strict;
|
13961 | this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
|
13962 |
|
13963 | if (this.state.strict && node.id) {
|
13964 | this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
|
13965 | }
|
13966 | });
|
13967 | this.prodParam.exit();
|
13968 | this.state.labels = oldLabels;
|
13969 | }
|
13970 |
|
13971 | this.expressionScope.exit();
|
13972 | }
|
13973 |
|
13974 | isSimpleParameter(node) {
|
13975 | return node.type === "Identifier";
|
13976 | }
|
13977 |
|
13978 | isSimpleParamList(params) {
|
13979 | for (let i = 0, len = params.length; i < len; i++) {
|
13980 | if (!this.isSimpleParameter(params[i])) return false;
|
13981 | }
|
13982 |
|
13983 | return true;
|
13984 | }
|
13985 |
|
13986 | checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
|
13987 | const checkClashes = !allowDuplicates && new Set();
|
13988 | const formalParameters = {
|
13989 | type: "FormalParameters"
|
13990 | };
|
13991 |
|
13992 | for (const param of node.params) {
|
13993 | this.checkLVal(param, {
|
13994 | in: formalParameters,
|
13995 | binding: BIND_VAR,
|
13996 | checkClashes,
|
13997 | strictModeChanged
|
13998 | });
|
13999 | }
|
14000 | }
|
14001 |
|
14002 | parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
|
14003 | const elts = [];
|
14004 | let first = true;
|
14005 |
|
14006 | while (!this.eat(close)) {
|
14007 | if (first) {
|
14008 | first = false;
|
14009 | } else {
|
14010 | this.expect(12);
|
14011 |
|
14012 | if (this.match(close)) {
|
14013 | if (nodeForExtra) {
|
14014 | this.addTrailingCommaExtraToNode(nodeForExtra);
|
14015 | }
|
14016 |
|
14017 | this.next();
|
14018 | break;
|
14019 | }
|
14020 | }
|
14021 |
|
14022 | elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
|
14023 | }
|
14024 |
|
14025 | return elts;
|
14026 | }
|
14027 |
|
14028 | parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
|
14029 | let elt;
|
14030 |
|
14031 | if (this.match(12)) {
|
14032 | if (!allowEmpty) {
|
14033 | this.raise(Errors.UnexpectedToken, {
|
14034 | at: this.state.curPosition(),
|
14035 | unexpected: ","
|
14036 | });
|
14037 | }
|
14038 |
|
14039 | elt = null;
|
14040 | } else if (this.match(21)) {
|
14041 | const spreadNodeStartPos = this.state.start;
|
14042 | const spreadNodeStartLoc = this.state.startLoc;
|
14043 | elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
|
14044 | } else if (this.match(17)) {
|
14045 | this.expectPlugin("partialApplication");
|
14046 |
|
14047 | if (!allowPlaceholder) {
|
14048 | this.raise(Errors.UnexpectedArgumentPlaceholder, {
|
14049 | at: this.state.startLoc
|
14050 | });
|
14051 | }
|
14052 |
|
14053 | const node = this.startNode();
|
14054 | this.next();
|
14055 | elt = this.finishNode(node, "ArgumentPlaceholder");
|
14056 | } else {
|
14057 | elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
|
14058 | }
|
14059 |
|
14060 | return elt;
|
14061 | }
|
14062 |
|
14063 | parseIdentifier(liberal) {
|
14064 | const node = this.startNode();
|
14065 | const name = this.parseIdentifierName(node.start, liberal);
|
14066 | return this.createIdentifier(node, name);
|
14067 | }
|
14068 |
|
14069 | createIdentifier(node, name) {
|
14070 | node.name = name;
|
14071 | node.loc.identifierName = name;
|
14072 | return this.finishNode(node, "Identifier");
|
14073 | }
|
14074 |
|
14075 | parseIdentifierName(pos, liberal) {
|
14076 | let name;
|
14077 | const {
|
14078 | startLoc,
|
14079 | type
|
14080 | } = this.state;
|
14081 |
|
14082 | if (tokenIsKeywordOrIdentifier(type)) {
|
14083 | name = this.state.value;
|
14084 | } else {
|
14085 | throw this.unexpected();
|
14086 | }
|
14087 |
|
14088 | const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
|
14089 |
|
14090 | if (liberal) {
|
14091 | if (tokenIsKeyword) {
|
14092 | this.replaceToken(128);
|
14093 | }
|
14094 | } else {
|
14095 | this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
|
14096 | }
|
14097 |
|
14098 | this.next();
|
14099 | return name;
|
14100 | }
|
14101 |
|
14102 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
14103 | if (word.length > 10) {
|
14104 | return;
|
14105 | }
|
14106 |
|
14107 | if (!canBeReservedWord(word)) {
|
14108 | return;
|
14109 | }
|
14110 |
|
14111 | if (word === "yield") {
|
14112 | if (this.prodParam.hasYield) {
|
14113 | this.raise(Errors.YieldBindingIdentifier, {
|
14114 | at: startLoc
|
14115 | });
|
14116 | return;
|
14117 | }
|
14118 | } else if (word === "await") {
|
14119 | if (this.prodParam.hasAwait) {
|
14120 | this.raise(Errors.AwaitBindingIdentifier, {
|
14121 | at: startLoc
|
14122 | });
|
14123 | return;
|
14124 | }
|
14125 |
|
14126 | if (this.scope.inStaticBlock) {
|
14127 | this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
|
14128 | at: startLoc
|
14129 | });
|
14130 | return;
|
14131 | }
|
14132 |
|
14133 | this.expressionScope.recordAsyncArrowParametersError({
|
14134 | at: startLoc
|
14135 | });
|
14136 | } else if (word === "arguments") {
|
14137 | if (this.scope.inClassAndNotInNonArrowFunction) {
|
14138 | this.raise(Errors.ArgumentsInClass, {
|
14139 | at: startLoc
|
14140 | });
|
14141 | return;
|
14142 | }
|
14143 | }
|
14144 |
|
14145 | if (checkKeywords && isKeyword(word)) {
|
14146 | this.raise(Errors.UnexpectedKeyword, {
|
14147 | at: startLoc,
|
14148 | keyword: word
|
14149 | });
|
14150 | return;
|
14151 | }
|
14152 |
|
14153 | const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
|
14154 |
|
14155 | if (reservedTest(word, this.inModule)) {
|
14156 | this.raise(Errors.UnexpectedReservedWord, {
|
14157 | at: startLoc,
|
14158 | reservedWord: word
|
14159 | });
|
14160 | }
|
14161 | }
|
14162 |
|
14163 | isAwaitAllowed() {
|
14164 | if (this.prodParam.hasAwait) return true;
|
14165 |
|
14166 | if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
|
14167 | return true;
|
14168 | }
|
14169 |
|
14170 | return false;
|
14171 | }
|
14172 |
|
14173 | parseAwait(startPos, startLoc) {
|
14174 | const node = this.startNodeAt(startPos, startLoc);
|
14175 | this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
|
14176 | at: node
|
14177 | });
|
14178 |
|
14179 | if (this.eat(55)) {
|
14180 | this.raise(Errors.ObsoleteAwaitStar, {
|
14181 | at: node
|
14182 | });
|
14183 | }
|
14184 |
|
14185 | if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
|
14186 | if (this.isAmbiguousAwait()) {
|
14187 | this.ambiguousScriptDifferentAst = true;
|
14188 | } else {
|
14189 | this.sawUnambiguousESM = true;
|
14190 | }
|
14191 | }
|
14192 |
|
14193 | if (!this.state.soloAwait) {
|
14194 | node.argument = this.parseMaybeUnary(null, true);
|
14195 | }
|
14196 |
|
14197 | return this.finishNode(node, "AwaitExpression");
|
14198 | }
|
14199 |
|
14200 | isAmbiguousAwait() {
|
14201 | if (this.hasPrecedingLineBreak()) return true;
|
14202 | const {
|
14203 | type
|
14204 | } = this.state;
|
14205 | return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 133 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
|
14206 | }
|
14207 |
|
14208 | parseYield() {
|
14209 | const node = this.startNode();
|
14210 | this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
|
14211 | at: node
|
14212 | });
|
14213 | this.next();
|
14214 | let delegating = false;
|
14215 | let argument = null;
|
14216 |
|
14217 | if (!this.hasPrecedingLineBreak()) {
|
14218 | delegating = this.eat(55);
|
14219 |
|
14220 | switch (this.state.type) {
|
14221 | case 13:
|
14222 | case 135:
|
14223 | case 8:
|
14224 | case 11:
|
14225 | case 3:
|
14226 | case 9:
|
14227 | case 14:
|
14228 | case 12:
|
14229 | if (!delegating) break;
|
14230 |
|
14231 | default:
|
14232 | argument = this.parseMaybeAssign();
|
14233 | }
|
14234 | }
|
14235 |
|
14236 | node.delegate = delegating;
|
14237 | node.argument = argument;
|
14238 | return this.finishNode(node, "YieldExpression");
|
14239 | }
|
14240 |
|
14241 | checkPipelineAtInfixOperator(left, leftStartLoc) {
|
14242 | if (this.hasPlugin(["pipelineOperator", {
|
14243 | proposal: "smart"
|
14244 | }])) {
|
14245 | if (left.type === "SequenceExpression") {
|
14246 | this.raise(Errors.PipelineHeadSequenceExpression, {
|
14247 | at: leftStartLoc
|
14248 | });
|
14249 | }
|
14250 | }
|
14251 | }
|
14252 |
|
14253 | parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
|
14254 | const bodyNode = this.startNodeAt(startPos, startLoc);
|
14255 |
|
14256 | if (this.isSimpleReference(childExpr)) {
|
14257 | bodyNode.callee = childExpr;
|
14258 | return this.finishNode(bodyNode, "PipelineBareFunction");
|
14259 | } else {
|
14260 | this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
|
14261 | bodyNode.expression = childExpr;
|
14262 | return this.finishNode(bodyNode, "PipelineTopicExpression");
|
14263 | }
|
14264 | }
|
14265 |
|
14266 | isSimpleReference(expression) {
|
14267 | switch (expression.type) {
|
14268 | case "MemberExpression":
|
14269 | return !expression.computed && this.isSimpleReference(expression.object);
|
14270 |
|
14271 | case "Identifier":
|
14272 | return true;
|
14273 |
|
14274 | default:
|
14275 | return false;
|
14276 | }
|
14277 | }
|
14278 |
|
14279 | checkSmartPipeTopicBodyEarlyErrors(startLoc) {
|
14280 | if (this.match(19)) {
|
14281 | throw this.raise(Errors.PipelineBodyNoArrow, {
|
14282 | at: this.state.startLoc
|
14283 | });
|
14284 | }
|
14285 |
|
14286 | if (!this.topicReferenceWasUsedInCurrentContext()) {
|
14287 | this.raise(Errors.PipelineTopicUnused, {
|
14288 | at: startLoc
|
14289 | });
|
14290 | }
|
14291 | }
|
14292 |
|
14293 | withTopicBindingContext(callback) {
|
14294 | const outerContextTopicState = this.state.topicContext;
|
14295 | this.state.topicContext = {
|
14296 | maxNumOfResolvableTopics: 1,
|
14297 | maxTopicIndex: null
|
14298 | };
|
14299 |
|
14300 | try {
|
14301 | return callback();
|
14302 | } finally {
|
14303 | this.state.topicContext = outerContextTopicState;
|
14304 | }
|
14305 | }
|
14306 |
|
14307 | withSmartMixTopicForbiddingContext(callback) {
|
14308 | if (this.hasPlugin(["pipelineOperator", {
|
14309 | proposal: "smart"
|
14310 | }])) {
|
14311 | const outerContextTopicState = this.state.topicContext;
|
14312 | this.state.topicContext = {
|
14313 | maxNumOfResolvableTopics: 0,
|
14314 | maxTopicIndex: null
|
14315 | };
|
14316 |
|
14317 | try {
|
14318 | return callback();
|
14319 | } finally {
|
14320 | this.state.topicContext = outerContextTopicState;
|
14321 | }
|
14322 | } else {
|
14323 | return callback();
|
14324 | }
|
14325 | }
|
14326 |
|
14327 | withSoloAwaitPermittingContext(callback) {
|
14328 | const outerContextSoloAwaitState = this.state.soloAwait;
|
14329 | this.state.soloAwait = true;
|
14330 |
|
14331 | try {
|
14332 | return callback();
|
14333 | } finally {
|
14334 | this.state.soloAwait = outerContextSoloAwaitState;
|
14335 | }
|
14336 | }
|
14337 |
|
14338 | allowInAnd(callback) {
|
14339 | const flags = this.prodParam.currentFlags();
|
14340 | const prodParamToSet = PARAM_IN & ~flags;
|
14341 |
|
14342 | if (prodParamToSet) {
|
14343 | this.prodParam.enter(flags | PARAM_IN);
|
14344 |
|
14345 | try {
|
14346 | return callback();
|
14347 | } finally {
|
14348 | this.prodParam.exit();
|
14349 | }
|
14350 | }
|
14351 |
|
14352 | return callback();
|
14353 | }
|
14354 |
|
14355 | disallowInAnd(callback) {
|
14356 | const flags = this.prodParam.currentFlags();
|
14357 | const prodParamToClear = PARAM_IN & flags;
|
14358 |
|
14359 | if (prodParamToClear) {
|
14360 | this.prodParam.enter(flags & ~PARAM_IN);
|
14361 |
|
14362 | try {
|
14363 | return callback();
|
14364 | } finally {
|
14365 | this.prodParam.exit();
|
14366 | }
|
14367 | }
|
14368 |
|
14369 | return callback();
|
14370 | }
|
14371 |
|
14372 | registerTopicReference() {
|
14373 | this.state.topicContext.maxTopicIndex = 0;
|
14374 | }
|
14375 |
|
14376 | topicReferenceIsAllowedInCurrentContext() {
|
14377 | return this.state.topicContext.maxNumOfResolvableTopics >= 1;
|
14378 | }
|
14379 |
|
14380 | topicReferenceWasUsedInCurrentContext() {
|
14381 | return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
|
14382 | }
|
14383 |
|
14384 | parseFSharpPipelineBody(prec) {
|
14385 | const startPos = this.state.start;
|
14386 | const startLoc = this.state.startLoc;
|
14387 | this.state.potentialArrowAt = this.state.start;
|
14388 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
14389 | this.state.inFSharpPipelineDirectBody = true;
|
14390 | const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
|
14391 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
14392 | return ret;
|
14393 | }
|
14394 |
|
14395 | parseModuleExpression() {
|
14396 | this.expectPlugin("moduleBlocks");
|
14397 | const node = this.startNode();
|
14398 | this.next();
|
14399 | this.eat(5);
|
14400 | const revertScopes = this.initializeScopes(true);
|
14401 | this.enterInitialScopes();
|
14402 | const program = this.startNode();
|
14403 |
|
14404 | try {
|
14405 | node.body = this.parseProgram(program, 8, "module");
|
14406 | } finally {
|
14407 | revertScopes();
|
14408 | }
|
14409 |
|
14410 | this.eat(8);
|
14411 | return this.finishNode(node, "ModuleExpression");
|
14412 | }
|
14413 |
|
14414 | parsePropertyNamePrefixOperator(prop) {}
|
14415 |
|
14416 | }
|
14417 |
|
14418 | const loopLabel = {
|
14419 | kind: "loop"
|
14420 | },
|
14421 | switchLabel = {
|
14422 | kind: "switch"
|
14423 | };
|
14424 | const FUNC_NO_FLAGS = 0b000,
|
14425 | FUNC_STATEMENT = 0b001,
|
14426 | FUNC_HANGING_STATEMENT = 0b010,
|
14427 | FUNC_NULLABLE_ID = 0b100;
|
14428 | const loneSurrogate = /[\uD800-\uDFFF]/u;
|
14429 | const keywordRelationalOperator = /in(?:stanceof)?/y;
|
14430 |
|
14431 | function babel7CompatTokens(tokens, input) {
|
14432 | for (let i = 0; i < tokens.length; i++) {
|
14433 | const token = tokens[i];
|
14434 | const {
|
14435 | type
|
14436 | } = token;
|
14437 |
|
14438 | if (typeof type === "number") {
|
14439 | {
|
14440 | if (type === 134) {
|
14441 | const {
|
14442 | loc,
|
14443 | start,
|
14444 | value,
|
14445 | end
|
14446 | } = token;
|
14447 | const hashEndPos = start + 1;
|
14448 | const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
|
14449 | tokens.splice(i, 1, new Token({
|
14450 | type: getExportedToken(27),
|
14451 | value: "#",
|
14452 | start: start,
|
14453 | end: hashEndPos,
|
14454 | startLoc: loc.start,
|
14455 | endLoc: hashEndLoc
|
14456 | }), new Token({
|
14457 | type: getExportedToken(128),
|
14458 | value: value,
|
14459 | start: hashEndPos,
|
14460 | end: end,
|
14461 | startLoc: hashEndLoc,
|
14462 | endLoc: loc.end
|
14463 | }));
|
14464 | i++;
|
14465 | continue;
|
14466 | }
|
14467 |
|
14468 | if (tokenIsTemplate(type)) {
|
14469 | const {
|
14470 | loc,
|
14471 | start,
|
14472 | value,
|
14473 | end
|
14474 | } = token;
|
14475 | const backquoteEnd = start + 1;
|
14476 | const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
|
14477 | let startToken;
|
14478 |
|
14479 | if (input.charCodeAt(start) === 96) {
|
14480 | startToken = new Token({
|
14481 | type: getExportedToken(22),
|
14482 | value: "`",
|
14483 | start: start,
|
14484 | end: backquoteEnd,
|
14485 | startLoc: loc.start,
|
14486 | endLoc: backquoteEndLoc
|
14487 | });
|
14488 | } else {
|
14489 | startToken = new Token({
|
14490 | type: getExportedToken(8),
|
14491 | value: "}",
|
14492 | start: start,
|
14493 | end: backquoteEnd,
|
14494 | startLoc: loc.start,
|
14495 | endLoc: backquoteEndLoc
|
14496 | });
|
14497 | }
|
14498 |
|
14499 | let templateValue, templateElementEnd, templateElementEndLoc, endToken;
|
14500 |
|
14501 | if (type === 24) {
|
14502 | templateElementEnd = end - 1;
|
14503 | templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
|
14504 | templateValue = value === null ? null : value.slice(1, -1);
|
14505 | endToken = new Token({
|
14506 | type: getExportedToken(22),
|
14507 | value: "`",
|
14508 | start: templateElementEnd,
|
14509 | end: end,
|
14510 | startLoc: templateElementEndLoc,
|
14511 | endLoc: loc.end
|
14512 | });
|
14513 | } else {
|
14514 | templateElementEnd = end - 2;
|
14515 | templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
|
14516 | templateValue = value === null ? null : value.slice(1, -2);
|
14517 | endToken = new Token({
|
14518 | type: getExportedToken(23),
|
14519 | value: "${",
|
14520 | start: templateElementEnd,
|
14521 | end: end,
|
14522 | startLoc: templateElementEndLoc,
|
14523 | endLoc: loc.end
|
14524 | });
|
14525 | }
|
14526 |
|
14527 | tokens.splice(i, 1, startToken, new Token({
|
14528 | type: getExportedToken(20),
|
14529 | value: templateValue,
|
14530 | start: backquoteEnd,
|
14531 | end: templateElementEnd,
|
14532 | startLoc: backquoteEndLoc,
|
14533 | endLoc: templateElementEndLoc
|
14534 | }), endToken);
|
14535 | i += 2;
|
14536 | continue;
|
14537 | }
|
14538 | }
|
14539 | token.type = getExportedToken(type);
|
14540 | }
|
14541 | }
|
14542 |
|
14543 | return tokens;
|
14544 | }
|
14545 |
|
14546 | class StatementParser extends ExpressionParser {
|
14547 | parseTopLevel(file, program) {
|
14548 | file.program = this.parseProgram(program);
|
14549 | file.comments = this.state.comments;
|
14550 |
|
14551 | if (this.options.tokens) {
|
14552 | file.tokens = babel7CompatTokens(this.tokens, this.input);
|
14553 | }
|
14554 |
|
14555 | return this.finishNode(file, "File");
|
14556 | }
|
14557 |
|
14558 | parseProgram(program, end = 135, sourceType = this.options.sourceType) {
|
14559 | program.sourceType = sourceType;
|
14560 | program.interpreter = this.parseInterpreterDirective();
|
14561 | this.parseBlockBody(program, true, true, end);
|
14562 |
|
14563 | if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
|
14564 | for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
|
14565 | this.raise(Errors.ModuleExportUndefined, {
|
14566 | at,
|
14567 | localName
|
14568 | });
|
14569 | }
|
14570 | }
|
14571 |
|
14572 | return this.finishNode(program, "Program");
|
14573 | }
|
14574 |
|
14575 | stmtToDirective(stmt) {
|
14576 | const directive = stmt;
|
14577 | directive.type = "Directive";
|
14578 | directive.value = directive.expression;
|
14579 | delete directive.expression;
|
14580 | const directiveLiteral = directive.value;
|
14581 | const expressionValue = directiveLiteral.value;
|
14582 | const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
|
14583 | const val = directiveLiteral.value = raw.slice(1, -1);
|
14584 | this.addExtra(directiveLiteral, "raw", raw);
|
14585 | this.addExtra(directiveLiteral, "rawValue", val);
|
14586 | this.addExtra(directiveLiteral, "expressionValue", expressionValue);
|
14587 | directiveLiteral.type = "DirectiveLiteral";
|
14588 | return directive;
|
14589 | }
|
14590 |
|
14591 | parseInterpreterDirective() {
|
14592 | if (!this.match(28)) {
|
14593 | return null;
|
14594 | }
|
14595 |
|
14596 | const node = this.startNode();
|
14597 | node.value = this.state.value;
|
14598 | this.next();
|
14599 | return this.finishNode(node, "InterpreterDirective");
|
14600 | }
|
14601 |
|
14602 | isLet(context) {
|
14603 | if (!this.isContextual(99)) {
|
14604 | return false;
|
14605 | }
|
14606 |
|
14607 | return this.isLetKeyword(context);
|
14608 | }
|
14609 |
|
14610 | isLetKeyword(context) {
|
14611 | const next = this.nextTokenStart();
|
14612 | const nextCh = this.codePointAtPos(next);
|
14613 |
|
14614 | if (nextCh === 92 || nextCh === 91) {
|
14615 | return true;
|
14616 | }
|
14617 |
|
14618 | if (context) return false;
|
14619 | if (nextCh === 123) return true;
|
14620 |
|
14621 | if (isIdentifierStart(nextCh)) {
|
14622 | keywordRelationalOperator.lastIndex = next;
|
14623 |
|
14624 | if (keywordRelationalOperator.test(this.input)) {
|
14625 | const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
|
14626 |
|
14627 | if (!isIdentifierChar(endCh) && endCh !== 92) {
|
14628 | return false;
|
14629 | }
|
14630 | }
|
14631 |
|
14632 | return true;
|
14633 | }
|
14634 |
|
14635 | return false;
|
14636 | }
|
14637 |
|
14638 | parseStatement(context, topLevel) {
|
14639 | if (this.match(26)) {
|
14640 | this.parseDecorators(true);
|
14641 | }
|
14642 |
|
14643 | return this.parseStatementContent(context, topLevel);
|
14644 | }
|
14645 |
|
14646 | parseStatementContent(context, topLevel) {
|
14647 | let starttype = this.state.type;
|
14648 | const node = this.startNode();
|
14649 | let kind;
|
14650 |
|
14651 | if (this.isLet(context)) {
|
14652 | starttype = 74;
|
14653 | kind = "let";
|
14654 | }
|
14655 |
|
14656 | switch (starttype) {
|
14657 | case 60:
|
14658 | return this.parseBreakContinueStatement(node, true);
|
14659 |
|
14660 | case 63:
|
14661 | return this.parseBreakContinueStatement(node, false);
|
14662 |
|
14663 | case 64:
|
14664 | return this.parseDebuggerStatement(node);
|
14665 |
|
14666 | case 90:
|
14667 | return this.parseDoStatement(node);
|
14668 |
|
14669 | case 91:
|
14670 | return this.parseForStatement(node);
|
14671 |
|
14672 | case 68:
|
14673 | if (this.lookaheadCharCode() === 46) break;
|
14674 |
|
14675 | if (context) {
|
14676 | if (this.state.strict) {
|
14677 | this.raise(Errors.StrictFunction, {
|
14678 | at: this.state.startLoc
|
14679 | });
|
14680 | } else if (context !== "if" && context !== "label") {
|
14681 | this.raise(Errors.SloppyFunction, {
|
14682 | at: this.state.startLoc
|
14683 | });
|
14684 | }
|
14685 | }
|
14686 |
|
14687 | return this.parseFunctionStatement(node, false, !context);
|
14688 |
|
14689 | case 80:
|
14690 | if (context) this.unexpected();
|
14691 | return this.parseClass(node, true);
|
14692 |
|
14693 | case 69:
|
14694 | return this.parseIfStatement(node);
|
14695 |
|
14696 | case 70:
|
14697 | return this.parseReturnStatement(node);
|
14698 |
|
14699 | case 71:
|
14700 | return this.parseSwitchStatement(node);
|
14701 |
|
14702 | case 72:
|
14703 | return this.parseThrowStatement(node);
|
14704 |
|
14705 | case 73:
|
14706 | return this.parseTryStatement(node);
|
14707 |
|
14708 | case 75:
|
14709 | case 74:
|
14710 | kind = kind || this.state.value;
|
14711 |
|
14712 | if (context && kind !== "var") {
|
14713 | this.raise(Errors.UnexpectedLexicalDeclaration, {
|
14714 | at: this.state.startLoc
|
14715 | });
|
14716 | }
|
14717 |
|
14718 | return this.parseVarStatement(node, kind);
|
14719 |
|
14720 | case 92:
|
14721 | return this.parseWhileStatement(node);
|
14722 |
|
14723 | case 76:
|
14724 | return this.parseWithStatement(node);
|
14725 |
|
14726 | case 5:
|
14727 | return this.parseBlock();
|
14728 |
|
14729 | case 13:
|
14730 | return this.parseEmptyStatement(node);
|
14731 |
|
14732 | case 83:
|
14733 | {
|
14734 | const nextTokenCharCode = this.lookaheadCharCode();
|
14735 |
|
14736 | if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
|
14737 | break;
|
14738 | }
|
14739 | }
|
14740 |
|
14741 | case 82:
|
14742 | {
|
14743 | if (!this.options.allowImportExportEverywhere && !topLevel) {
|
14744 | this.raise(Errors.UnexpectedImportExport, {
|
14745 | at: this.state.startLoc
|
14746 | });
|
14747 | }
|
14748 |
|
14749 | this.next();
|
14750 | let result;
|
14751 |
|
14752 | if (starttype === 83) {
|
14753 | result = this.parseImport(node);
|
14754 |
|
14755 | if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
|
14756 | this.sawUnambiguousESM = true;
|
14757 | }
|
14758 | } else {
|
14759 | result = this.parseExport(node);
|
14760 |
|
14761 | if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
|
14762 | this.sawUnambiguousESM = true;
|
14763 | }
|
14764 | }
|
14765 |
|
14766 | this.assertModuleNodeAllowed(node);
|
14767 | return result;
|
14768 | }
|
14769 |
|
14770 | default:
|
14771 | {
|
14772 | if (this.isAsyncFunction()) {
|
14773 | if (context) {
|
14774 | this.raise(Errors.AsyncFunctionInSingleStatementContext, {
|
14775 | at: this.state.startLoc
|
14776 | });
|
14777 | }
|
14778 |
|
14779 | this.next();
|
14780 | return this.parseFunctionStatement(node, true, !context);
|
14781 | }
|
14782 | }
|
14783 | }
|
14784 |
|
14785 | const maybeName = this.state.value;
|
14786 | const expr = this.parseExpression();
|
14787 |
|
14788 | if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
|
14789 | return this.parseLabeledStatement(node, maybeName, expr, context);
|
14790 | } else {
|
14791 | return this.parseExpressionStatement(node, expr);
|
14792 | }
|
14793 | }
|
14794 |
|
14795 | assertModuleNodeAllowed(node) {
|
14796 | if (!this.options.allowImportExportEverywhere && !this.inModule) {
|
14797 | this.raise(Errors.ImportOutsideModule, {
|
14798 | at: node
|
14799 | });
|
14800 | }
|
14801 | }
|
14802 |
|
14803 | takeDecorators(node) {
|
14804 | const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
14805 |
|
14806 | if (decorators.length) {
|
14807 | node.decorators = decorators;
|
14808 | this.resetStartLocationFromNode(node, decorators[0]);
|
14809 | this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
|
14810 | }
|
14811 | }
|
14812 |
|
14813 | canHaveLeadingDecorator() {
|
14814 | return this.match(80);
|
14815 | }
|
14816 |
|
14817 | parseDecorators(allowExport) {
|
14818 | const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
14819 |
|
14820 | while (this.match(26)) {
|
14821 | const decorator = this.parseDecorator();
|
14822 | currentContextDecorators.push(decorator);
|
14823 | }
|
14824 |
|
14825 | if (this.match(82)) {
|
14826 | if (!allowExport) {
|
14827 | this.unexpected();
|
14828 | }
|
14829 |
|
14830 | if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
14831 | this.raise(Errors.DecoratorExportClass, {
|
14832 | at: this.state.startLoc
|
14833 | });
|
14834 | }
|
14835 | } else if (!this.canHaveLeadingDecorator()) {
|
14836 | throw this.raise(Errors.UnexpectedLeadingDecorator, {
|
14837 | at: this.state.startLoc
|
14838 | });
|
14839 | }
|
14840 | }
|
14841 |
|
14842 | parseDecorator() {
|
14843 | this.expectOnePlugin(["decorators-legacy", "decorators"]);
|
14844 | const node = this.startNode();
|
14845 | this.next();
|
14846 |
|
14847 | if (this.hasPlugin("decorators")) {
|
14848 | this.state.decoratorStack.push([]);
|
14849 | const startPos = this.state.start;
|
14850 | const startLoc = this.state.startLoc;
|
14851 | let expr;
|
14852 |
|
14853 | if (this.match(10)) {
|
14854 | const startPos = this.state.start;
|
14855 | const startLoc = this.state.startLoc;
|
14856 | this.next();
|
14857 | expr = this.parseExpression();
|
14858 | this.expect(11);
|
14859 | expr = this.wrapParenthesis(startPos, startLoc, expr);
|
14860 | } else {
|
14861 | expr = this.parseIdentifier(false);
|
14862 |
|
14863 | while (this.eat(16)) {
|
14864 | const node = this.startNodeAt(startPos, startLoc);
|
14865 | node.object = expr;
|
14866 | node.property = this.parseIdentifier(true);
|
14867 | node.computed = false;
|
14868 | expr = this.finishNode(node, "MemberExpression");
|
14869 | }
|
14870 | }
|
14871 |
|
14872 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
14873 | this.state.decoratorStack.pop();
|
14874 | } else {
|
14875 | node.expression = this.parseExprSubscripts();
|
14876 | }
|
14877 |
|
14878 | return this.finishNode(node, "Decorator");
|
14879 | }
|
14880 |
|
14881 | parseMaybeDecoratorArguments(expr) {
|
14882 | if (this.eat(10)) {
|
14883 | const node = this.startNodeAtNode(expr);
|
14884 | node.callee = expr;
|
14885 | node.arguments = this.parseCallExpressionArguments(11, false);
|
14886 | this.toReferencedList(node.arguments);
|
14887 | return this.finishNode(node, "CallExpression");
|
14888 | }
|
14889 |
|
14890 | return expr;
|
14891 | }
|
14892 |
|
14893 | parseBreakContinueStatement(node, isBreak) {
|
14894 | this.next();
|
14895 |
|
14896 | if (this.isLineTerminator()) {
|
14897 | node.label = null;
|
14898 | } else {
|
14899 | node.label = this.parseIdentifier();
|
14900 | this.semicolon();
|
14901 | }
|
14902 |
|
14903 | this.verifyBreakContinue(node, isBreak);
|
14904 | return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
14905 | }
|
14906 |
|
14907 | verifyBreakContinue(node, isBreak) {
|
14908 | let i;
|
14909 |
|
14910 | for (i = 0; i < this.state.labels.length; ++i) {
|
14911 | const lab = this.state.labels[i];
|
14912 |
|
14913 | if (node.label == null || lab.name === node.label.name) {
|
14914 | if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
|
14915 | if (node.label && isBreak) break;
|
14916 | }
|
14917 | }
|
14918 |
|
14919 | if (i === this.state.labels.length) {
|
14920 | const type = isBreak ? "BreakStatement" : "ContinueStatement";
|
14921 | this.raise(Errors.IllegalBreakContinue, {
|
14922 | at: node,
|
14923 | type
|
14924 | });
|
14925 | }
|
14926 | }
|
14927 |
|
14928 | parseDebuggerStatement(node) {
|
14929 | this.next();
|
14930 | this.semicolon();
|
14931 | return this.finishNode(node, "DebuggerStatement");
|
14932 | }
|
14933 |
|
14934 | parseHeaderExpression() {
|
14935 | this.expect(10);
|
14936 | const val = this.parseExpression();
|
14937 | this.expect(11);
|
14938 | return val;
|
14939 | }
|
14940 |
|
14941 | parseDoStatement(node) {
|
14942 | this.next();
|
14943 | this.state.labels.push(loopLabel);
|
14944 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
|
14945 | this.state.labels.pop();
|
14946 | this.expect(92);
|
14947 | node.test = this.parseHeaderExpression();
|
14948 | this.eat(13);
|
14949 | return this.finishNode(node, "DoWhileStatement");
|
14950 | }
|
14951 |
|
14952 | parseForStatement(node) {
|
14953 | this.next();
|
14954 | this.state.labels.push(loopLabel);
|
14955 | let awaitAt = null;
|
14956 |
|
14957 | if (this.isAwaitAllowed() && this.eatContextual(96)) {
|
14958 | awaitAt = this.state.lastTokStartLoc;
|
14959 | }
|
14960 |
|
14961 | this.scope.enter(SCOPE_OTHER);
|
14962 | this.expect(10);
|
14963 |
|
14964 | if (this.match(13)) {
|
14965 | if (awaitAt !== null) {
|
14966 | this.unexpected(awaitAt);
|
14967 | }
|
14968 |
|
14969 | return this.parseFor(node, null);
|
14970 | }
|
14971 |
|
14972 | const startsWithLet = this.isContextual(99);
|
14973 | const isLet = startsWithLet && this.isLetKeyword();
|
14974 |
|
14975 | if (this.match(74) || this.match(75) || isLet) {
|
14976 | const init = this.startNode();
|
14977 | const kind = isLet ? "let" : this.state.value;
|
14978 | this.next();
|
14979 | this.parseVar(init, true, kind);
|
14980 | this.finishNode(init, "VariableDeclaration");
|
14981 |
|
14982 | if ((this.match(58) || this.isContextual(101)) && init.declarations.length === 1) {
|
14983 | return this.parseForIn(node, init, awaitAt);
|
14984 | }
|
14985 |
|
14986 | if (awaitAt !== null) {
|
14987 | this.unexpected(awaitAt);
|
14988 | }
|
14989 |
|
14990 | return this.parseFor(node, init);
|
14991 | }
|
14992 |
|
14993 | const startsWithAsync = this.isContextual(95);
|
14994 | const refExpressionErrors = new ExpressionErrors();
|
14995 | const init = this.parseExpression(true, refExpressionErrors);
|
14996 | const isForOf = this.isContextual(101);
|
14997 |
|
14998 | if (isForOf) {
|
14999 | if (startsWithLet) {
|
15000 | this.raise(Errors.ForOfLet, {
|
15001 | at: init
|
15002 | });
|
15003 | }
|
15004 |
|
15005 | if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
|
15006 | this.raise(Errors.ForOfAsync, {
|
15007 | at: init
|
15008 | });
|
15009 | }
|
15010 | }
|
15011 |
|
15012 | if (isForOf || this.match(58)) {
|
15013 | this.checkDestructuringPrivate(refExpressionErrors);
|
15014 | this.toAssignable(init, true);
|
15015 | const type = isForOf ? "ForOfStatement" : "ForInStatement";
|
15016 | this.checkLVal(init, {
|
15017 | in: {
|
15018 | type
|
15019 | }
|
15020 | });
|
15021 | return this.parseForIn(node, init, awaitAt);
|
15022 | } else {
|
15023 | this.checkExpressionErrors(refExpressionErrors, true);
|
15024 | }
|
15025 |
|
15026 | if (awaitAt !== null) {
|
15027 | this.unexpected(awaitAt);
|
15028 | }
|
15029 |
|
15030 | return this.parseFor(node, init);
|
15031 | }
|
15032 |
|
15033 | parseFunctionStatement(node, isAsync, declarationPosition) {
|
15034 | this.next();
|
15035 | return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
|
15036 | }
|
15037 |
|
15038 | parseIfStatement(node) {
|
15039 | this.next();
|
15040 | node.test = this.parseHeaderExpression();
|
15041 | node.consequent = this.parseStatement("if");
|
15042 | node.alternate = this.eat(66) ? this.parseStatement("if") : null;
|
15043 | return this.finishNode(node, "IfStatement");
|
15044 | }
|
15045 |
|
15046 | parseReturnStatement(node) {
|
15047 | if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
|
15048 | this.raise(Errors.IllegalReturn, {
|
15049 | at: this.state.startLoc
|
15050 | });
|
15051 | }
|
15052 |
|
15053 | this.next();
|
15054 |
|
15055 | if (this.isLineTerminator()) {
|
15056 | node.argument = null;
|
15057 | } else {
|
15058 | node.argument = this.parseExpression();
|
15059 | this.semicolon();
|
15060 | }
|
15061 |
|
15062 | return this.finishNode(node, "ReturnStatement");
|
15063 | }
|
15064 |
|
15065 | parseSwitchStatement(node) {
|
15066 | this.next();
|
15067 | node.discriminant = this.parseHeaderExpression();
|
15068 | const cases = node.cases = [];
|
15069 | this.expect(5);
|
15070 | this.state.labels.push(switchLabel);
|
15071 | this.scope.enter(SCOPE_OTHER);
|
15072 | let cur;
|
15073 |
|
15074 | for (let sawDefault; !this.match(8);) {
|
15075 | if (this.match(61) || this.match(65)) {
|
15076 | const isCase = this.match(61);
|
15077 | if (cur) this.finishNode(cur, "SwitchCase");
|
15078 | cases.push(cur = this.startNode());
|
15079 | cur.consequent = [];
|
15080 | this.next();
|
15081 |
|
15082 | if (isCase) {
|
15083 | cur.test = this.parseExpression();
|
15084 | } else {
|
15085 | if (sawDefault) {
|
15086 | this.raise(Errors.MultipleDefaultsInSwitch, {
|
15087 | at: this.state.lastTokStartLoc
|
15088 | });
|
15089 | }
|
15090 |
|
15091 | sawDefault = true;
|
15092 | cur.test = null;
|
15093 | }
|
15094 |
|
15095 | this.expect(14);
|
15096 | } else {
|
15097 | if (cur) {
|
15098 | cur.consequent.push(this.parseStatement(null));
|
15099 | } else {
|
15100 | this.unexpected();
|
15101 | }
|
15102 | }
|
15103 | }
|
15104 |
|
15105 | this.scope.exit();
|
15106 | if (cur) this.finishNode(cur, "SwitchCase");
|
15107 | this.next();
|
15108 | this.state.labels.pop();
|
15109 | return this.finishNode(node, "SwitchStatement");
|
15110 | }
|
15111 |
|
15112 | parseThrowStatement(node) {
|
15113 | this.next();
|
15114 |
|
15115 | if (this.hasPrecedingLineBreak()) {
|
15116 | this.raise(Errors.NewlineAfterThrow, {
|
15117 | at: this.state.lastTokEndLoc
|
15118 | });
|
15119 | }
|
15120 |
|
15121 | node.argument = this.parseExpression();
|
15122 | this.semicolon();
|
15123 | return this.finishNode(node, "ThrowStatement");
|
15124 | }
|
15125 |
|
15126 | parseCatchClauseParam() {
|
15127 | const param = this.parseBindingAtom();
|
15128 | const simple = param.type === "Identifier";
|
15129 | this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
|
15130 | this.checkLVal(param, {
|
15131 | in: {
|
15132 | type: "CatchClause"
|
15133 | },
|
15134 | binding: BIND_LEXICAL,
|
15135 | allowingSloppyLetBinding: true
|
15136 | });
|
15137 | return param;
|
15138 | }
|
15139 |
|
15140 | parseTryStatement(node) {
|
15141 | this.next();
|
15142 | node.block = this.parseBlock();
|
15143 | node.handler = null;
|
15144 |
|
15145 | if (this.match(62)) {
|
15146 | const clause = this.startNode();
|
15147 | this.next();
|
15148 |
|
15149 | if (this.match(10)) {
|
15150 | this.expect(10);
|
15151 | clause.param = this.parseCatchClauseParam();
|
15152 | this.expect(11);
|
15153 | } else {
|
15154 | clause.param = null;
|
15155 | this.scope.enter(SCOPE_OTHER);
|
15156 | }
|
15157 |
|
15158 | clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
|
15159 | this.scope.exit();
|
15160 | node.handler = this.finishNode(clause, "CatchClause");
|
15161 | }
|
15162 |
|
15163 | node.finalizer = this.eat(67) ? this.parseBlock() : null;
|
15164 |
|
15165 | if (!node.handler && !node.finalizer) {
|
15166 | this.raise(Errors.NoCatchOrFinally, {
|
15167 | at: node
|
15168 | });
|
15169 | }
|
15170 |
|
15171 | return this.finishNode(node, "TryStatement");
|
15172 | }
|
15173 |
|
15174 | parseVarStatement(node, kind, allowMissingInitializer = false) {
|
15175 | this.next();
|
15176 | this.parseVar(node, false, kind, allowMissingInitializer);
|
15177 | this.semicolon();
|
15178 | return this.finishNode(node, "VariableDeclaration");
|
15179 | }
|
15180 |
|
15181 | parseWhileStatement(node) {
|
15182 | this.next();
|
15183 | node.test = this.parseHeaderExpression();
|
15184 | this.state.labels.push(loopLabel);
|
15185 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
|
15186 | this.state.labels.pop();
|
15187 | return this.finishNode(node, "WhileStatement");
|
15188 | }
|
15189 |
|
15190 | parseWithStatement(node) {
|
15191 | if (this.state.strict) {
|
15192 | this.raise(Errors.StrictWith, {
|
15193 | at: this.state.startLoc
|
15194 | });
|
15195 | }
|
15196 |
|
15197 | this.next();
|
15198 | node.object = this.parseHeaderExpression();
|
15199 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
|
15200 | return this.finishNode(node, "WithStatement");
|
15201 | }
|
15202 |
|
15203 | parseEmptyStatement(node) {
|
15204 | this.next();
|
15205 | return this.finishNode(node, "EmptyStatement");
|
15206 | }
|
15207 |
|
15208 | parseLabeledStatement(node, maybeName, expr, context) {
|
15209 | for (const label of this.state.labels) {
|
15210 | if (label.name === maybeName) {
|
15211 | this.raise(Errors.LabelRedeclaration, {
|
15212 | at: expr,
|
15213 | labelName: maybeName
|
15214 | });
|
15215 | }
|
15216 | }
|
15217 |
|
15218 | const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
|
15219 |
|
15220 | for (let i = this.state.labels.length - 1; i >= 0; i--) {
|
15221 | const label = this.state.labels[i];
|
15222 |
|
15223 | if (label.statementStart === node.start) {
|
15224 | label.statementStart = this.state.start;
|
15225 | label.kind = kind;
|
15226 | } else {
|
15227 | break;
|
15228 | }
|
15229 | }
|
15230 |
|
15231 | this.state.labels.push({
|
15232 | name: maybeName,
|
15233 | kind: kind,
|
15234 | statementStart: this.state.start
|
15235 | });
|
15236 | node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
|
15237 | this.state.labels.pop();
|
15238 | node.label = expr;
|
15239 | return this.finishNode(node, "LabeledStatement");
|
15240 | }
|
15241 |
|
15242 | parseExpressionStatement(node, expr) {
|
15243 | node.expression = expr;
|
15244 | this.semicolon();
|
15245 | return this.finishNode(node, "ExpressionStatement");
|
15246 | }
|
15247 |
|
15248 | parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
|
15249 | const node = this.startNode();
|
15250 |
|
15251 | if (allowDirectives) {
|
15252 | this.state.strictErrors.clear();
|
15253 | }
|
15254 |
|
15255 | this.expect(5);
|
15256 |
|
15257 | if (createNewLexicalScope) {
|
15258 | this.scope.enter(SCOPE_OTHER);
|
15259 | }
|
15260 |
|
15261 | this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
|
15262 |
|
15263 | if (createNewLexicalScope) {
|
15264 | this.scope.exit();
|
15265 | }
|
15266 |
|
15267 | return this.finishNode(node, "BlockStatement");
|
15268 | }
|
15269 |
|
15270 | isValidDirective(stmt) {
|
15271 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
|
15272 | }
|
15273 |
|
15274 | parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
|
15275 | const body = node.body = [];
|
15276 | const directives = node.directives = [];
|
15277 | this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
|
15278 | }
|
15279 |
|
15280 | parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
|
15281 | const oldStrict = this.state.strict;
|
15282 | let hasStrictModeDirective = false;
|
15283 | let parsedNonDirective = false;
|
15284 |
|
15285 | while (!this.match(end)) {
|
15286 | const stmt = this.parseStatement(null, topLevel);
|
15287 |
|
15288 | if (directives && !parsedNonDirective) {
|
15289 | if (this.isValidDirective(stmt)) {
|
15290 | const directive = this.stmtToDirective(stmt);
|
15291 | directives.push(directive);
|
15292 |
|
15293 | if (!hasStrictModeDirective && directive.value.value === "use strict") {
|
15294 | hasStrictModeDirective = true;
|
15295 | this.setStrict(true);
|
15296 | }
|
15297 |
|
15298 | continue;
|
15299 | }
|
15300 |
|
15301 | parsedNonDirective = true;
|
15302 | this.state.strictErrors.clear();
|
15303 | }
|
15304 |
|
15305 | body.push(stmt);
|
15306 | }
|
15307 |
|
15308 | if (afterBlockParse) {
|
15309 | afterBlockParse.call(this, hasStrictModeDirective);
|
15310 | }
|
15311 |
|
15312 | if (!oldStrict) {
|
15313 | this.setStrict(false);
|
15314 | }
|
15315 |
|
15316 | this.next();
|
15317 | }
|
15318 |
|
15319 | parseFor(node, init) {
|
15320 | node.init = init;
|
15321 | this.semicolon(false);
|
15322 | node.test = this.match(13) ? null : this.parseExpression();
|
15323 | this.semicolon(false);
|
15324 | node.update = this.match(11) ? null : this.parseExpression();
|
15325 | this.expect(11);
|
15326 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
|
15327 | this.scope.exit();
|
15328 | this.state.labels.pop();
|
15329 | return this.finishNode(node, "ForStatement");
|
15330 | }
|
15331 |
|
15332 | parseForIn(node, init, awaitAt) {
|
15333 | const isForIn = this.match(58);
|
15334 | this.next();
|
15335 |
|
15336 | if (isForIn) {
|
15337 | if (awaitAt !== null) this.unexpected(awaitAt);
|
15338 | } else {
|
15339 | node.await = awaitAt !== null;
|
15340 | }
|
15341 |
|
15342 | if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
|
15343 | this.raise(Errors.ForInOfLoopInitializer, {
|
15344 | at: init,
|
15345 | type: isForIn ? "ForInStatement" : "ForOfStatement"
|
15346 | });
|
15347 | }
|
15348 |
|
15349 | if (init.type === "AssignmentPattern") {
|
15350 | this.raise(Errors.InvalidLhs, {
|
15351 | at: init,
|
15352 | ancestor: {
|
15353 | type: "ForStatement"
|
15354 | }
|
15355 | });
|
15356 | }
|
15357 |
|
15358 | node.left = init;
|
15359 | node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
|
15360 | this.expect(11);
|
15361 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
|
15362 | this.scope.exit();
|
15363 | this.state.labels.pop();
|
15364 | return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
|
15365 | }
|
15366 |
|
15367 | parseVar(node, isFor, kind, allowMissingInitializer = false) {
|
15368 | const declarations = node.declarations = [];
|
15369 | node.kind = kind;
|
15370 |
|
15371 | for (;;) {
|
15372 | const decl = this.startNode();
|
15373 | this.parseVarId(decl, kind);
|
15374 | decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
|
15375 |
|
15376 | if (decl.init === null && !allowMissingInitializer) {
|
15377 | if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
|
15378 | this.raise(Errors.DeclarationMissingInitializer, {
|
15379 | at: this.state.lastTokEndLoc,
|
15380 | kind: "destructuring"
|
15381 | });
|
15382 | } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
|
15383 | this.raise(Errors.DeclarationMissingInitializer, {
|
15384 | at: this.state.lastTokEndLoc,
|
15385 | kind: "const"
|
15386 | });
|
15387 | }
|
15388 | }
|
15389 |
|
15390 | declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
15391 | if (!this.eat(12)) break;
|
15392 | }
|
15393 |
|
15394 | return node;
|
15395 | }
|
15396 |
|
15397 | parseVarId(decl, kind) {
|
15398 | decl.id = this.parseBindingAtom();
|
15399 | this.checkLVal(decl.id, {
|
15400 | in: {
|
15401 | type: "VariableDeclarator"
|
15402 | },
|
15403 | binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
|
15404 | });
|
15405 | }
|
15406 |
|
15407 | parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
|
15408 | const isStatement = statement & FUNC_STATEMENT;
|
15409 | const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
|
15410 | const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
|
15411 | this.initFunction(node, isAsync);
|
15412 |
|
15413 | if (this.match(55) && isHangingStatement) {
|
15414 | this.raise(Errors.GeneratorInSingleStatementContext, {
|
15415 | at: this.state.startLoc
|
15416 | });
|
15417 | }
|
15418 |
|
15419 | node.generator = this.eat(55);
|
15420 |
|
15421 | if (isStatement) {
|
15422 | node.id = this.parseFunctionId(requireId);
|
15423 | }
|
15424 |
|
15425 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
15426 | this.state.maybeInArrowParameters = false;
|
15427 | this.scope.enter(SCOPE_FUNCTION);
|
15428 | this.prodParam.enter(functionFlags(isAsync, node.generator));
|
15429 |
|
15430 | if (!isStatement) {
|
15431 | node.id = this.parseFunctionId();
|
15432 | }
|
15433 |
|
15434 | this.parseFunctionParams(node, false);
|
15435 | this.withSmartMixTopicForbiddingContext(() => {
|
15436 | this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
|
15437 | });
|
15438 | this.prodParam.exit();
|
15439 | this.scope.exit();
|
15440 |
|
15441 | if (isStatement && !isHangingStatement) {
|
15442 | this.registerFunctionStatementId(node);
|
15443 | }
|
15444 |
|
15445 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
15446 | return node;
|
15447 | }
|
15448 |
|
15449 | parseFunctionId(requireId) {
|
15450 | return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
|
15451 | }
|
15452 |
|
15453 | parseFunctionParams(node, allowModifiers) {
|
15454 | this.expect(10);
|
15455 | this.expressionScope.enter(newParameterDeclarationScope());
|
15456 | node.params = this.parseBindingList(11, 41, false, allowModifiers);
|
15457 | this.expressionScope.exit();
|
15458 | }
|
15459 |
|
15460 | registerFunctionStatementId(node) {
|
15461 | if (!node.id) return;
|
15462 | this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
|
15463 | }
|
15464 |
|
15465 | parseClass(node, isStatement, optionalId) {
|
15466 | this.next();
|
15467 | this.takeDecorators(node);
|
15468 | const oldStrict = this.state.strict;
|
15469 | this.state.strict = true;
|
15470 | this.parseClassId(node, isStatement, optionalId);
|
15471 | this.parseClassSuper(node);
|
15472 | node.body = this.parseClassBody(!!node.superClass, oldStrict);
|
15473 | return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
|
15474 | }
|
15475 |
|
15476 | isClassProperty() {
|
15477 | return this.match(29) || this.match(13) || this.match(8);
|
15478 | }
|
15479 |
|
15480 | isClassMethod() {
|
15481 | return this.match(10);
|
15482 | }
|
15483 |
|
15484 | isNonstaticConstructor(method) {
|
15485 | return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
|
15486 | }
|
15487 |
|
15488 | parseClassBody(hadSuperClass, oldStrict) {
|
15489 | this.classScope.enter();
|
15490 | const state = {
|
15491 | hadConstructor: false,
|
15492 | hadSuperClass
|
15493 | };
|
15494 | let decorators = [];
|
15495 | const classBody = this.startNode();
|
15496 | classBody.body = [];
|
15497 | this.expect(5);
|
15498 | this.withSmartMixTopicForbiddingContext(() => {
|
15499 | while (!this.match(8)) {
|
15500 | if (this.eat(13)) {
|
15501 | if (decorators.length > 0) {
|
15502 | throw this.raise(Errors.DecoratorSemicolon, {
|
15503 | at: this.state.lastTokEndLoc
|
15504 | });
|
15505 | }
|
15506 |
|
15507 | continue;
|
15508 | }
|
15509 |
|
15510 | if (this.match(26)) {
|
15511 | decorators.push(this.parseDecorator());
|
15512 | continue;
|
15513 | }
|
15514 |
|
15515 | const member = this.startNode();
|
15516 |
|
15517 | if (decorators.length) {
|
15518 | member.decorators = decorators;
|
15519 | this.resetStartLocationFromNode(member, decorators[0]);
|
15520 | decorators = [];
|
15521 | }
|
15522 |
|
15523 | this.parseClassMember(classBody, member, state);
|
15524 |
|
15525 | if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
|
15526 | this.raise(Errors.DecoratorConstructor, {
|
15527 | at: member
|
15528 | });
|
15529 | }
|
15530 | }
|
15531 | });
|
15532 | this.state.strict = oldStrict;
|
15533 | this.next();
|
15534 |
|
15535 | if (decorators.length) {
|
15536 | throw this.raise(Errors.TrailingDecorator, {
|
15537 | at: this.state.startLoc
|
15538 | });
|
15539 | }
|
15540 |
|
15541 | this.classScope.exit();
|
15542 | return this.finishNode(classBody, "ClassBody");
|
15543 | }
|
15544 |
|
15545 | parseClassMemberFromModifier(classBody, member) {
|
15546 | const key = this.parseIdentifier(true);
|
15547 |
|
15548 | if (this.isClassMethod()) {
|
15549 | const method = member;
|
15550 | method.kind = "method";
|
15551 | method.computed = false;
|
15552 | method.key = key;
|
15553 | method.static = false;
|
15554 | this.pushClassMethod(classBody, method, false, false, false, false);
|
15555 | return true;
|
15556 | } else if (this.isClassProperty()) {
|
15557 | const prop = member;
|
15558 | prop.computed = false;
|
15559 | prop.key = key;
|
15560 | prop.static = false;
|
15561 | classBody.body.push(this.parseClassProperty(prop));
|
15562 | return true;
|
15563 | }
|
15564 |
|
15565 | this.resetPreviousNodeTrailingComments(key);
|
15566 | return false;
|
15567 | }
|
15568 |
|
15569 | parseClassMember(classBody, member, state) {
|
15570 | const isStatic = this.isContextual(104);
|
15571 |
|
15572 | if (isStatic) {
|
15573 | if (this.parseClassMemberFromModifier(classBody, member)) {
|
15574 | return;
|
15575 | }
|
15576 |
|
15577 | if (this.eat(5)) {
|
15578 | this.parseClassStaticBlock(classBody, member);
|
15579 | return;
|
15580 | }
|
15581 | }
|
15582 |
|
15583 | this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
15584 | }
|
15585 |
|
15586 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
15587 | const publicMethod = member;
|
15588 | const privateMethod = member;
|
15589 | const publicProp = member;
|
15590 | const privateProp = member;
|
15591 | const accessorProp = member;
|
15592 | const method = publicMethod;
|
15593 | const publicMember = publicMethod;
|
15594 | member.static = isStatic;
|
15595 | this.parsePropertyNamePrefixOperator(member);
|
15596 |
|
15597 | if (this.eat(55)) {
|
15598 | method.kind = "method";
|
15599 | const isPrivateName = this.match(134);
|
15600 | this.parseClassElementName(method);
|
15601 |
|
15602 | if (isPrivateName) {
|
15603 | this.pushClassPrivateMethod(classBody, privateMethod, true, false);
|
15604 | return;
|
15605 | }
|
15606 |
|
15607 | if (this.isNonstaticConstructor(publicMethod)) {
|
15608 | this.raise(Errors.ConstructorIsGenerator, {
|
15609 | at: publicMethod.key
|
15610 | });
|
15611 | }
|
15612 |
|
15613 | this.pushClassMethod(classBody, publicMethod, true, false, false, false);
|
15614 | return;
|
15615 | }
|
15616 |
|
15617 | const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
|
15618 | const isPrivate = this.match(134);
|
15619 | const key = this.parseClassElementName(member);
|
15620 | const maybeQuestionTokenStartLoc = this.state.startLoc;
|
15621 | this.parsePostMemberNameModifiers(publicMember);
|
15622 |
|
15623 | if (this.isClassMethod()) {
|
15624 | method.kind = "method";
|
15625 |
|
15626 | if (isPrivate) {
|
15627 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
15628 | return;
|
15629 | }
|
15630 |
|
15631 | const isConstructor = this.isNonstaticConstructor(publicMethod);
|
15632 | let allowsDirectSuper = false;
|
15633 |
|
15634 | if (isConstructor) {
|
15635 | publicMethod.kind = "constructor";
|
15636 |
|
15637 | if (state.hadConstructor && !this.hasPlugin("typescript")) {
|
15638 | this.raise(Errors.DuplicateConstructor, {
|
15639 | at: key
|
15640 | });
|
15641 | }
|
15642 |
|
15643 | if (isConstructor && this.hasPlugin("typescript") && member.override) {
|
15644 | this.raise(Errors.OverrideOnConstructor, {
|
15645 | at: key
|
15646 | });
|
15647 | }
|
15648 |
|
15649 | state.hadConstructor = true;
|
15650 | allowsDirectSuper = state.hadSuperClass;
|
15651 | }
|
15652 |
|
15653 | this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
|
15654 | } else if (this.isClassProperty()) {
|
15655 | if (isPrivate) {
|
15656 | this.pushClassPrivateProperty(classBody, privateProp);
|
15657 | } else {
|
15658 | this.pushClassProperty(classBody, publicProp);
|
15659 | }
|
15660 | } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
|
15661 | this.resetPreviousNodeTrailingComments(key);
|
15662 | const isGenerator = this.eat(55);
|
15663 |
|
15664 | if (publicMember.optional) {
|
15665 | this.unexpected(maybeQuestionTokenStartLoc);
|
15666 | }
|
15667 |
|
15668 | method.kind = "method";
|
15669 | const isPrivate = this.match(134);
|
15670 | this.parseClassElementName(method);
|
15671 | this.parsePostMemberNameModifiers(publicMember);
|
15672 |
|
15673 | if (isPrivate) {
|
15674 | this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
|
15675 | } else {
|
15676 | if (this.isNonstaticConstructor(publicMethod)) {
|
15677 | this.raise(Errors.ConstructorIsAsync, {
|
15678 | at: publicMethod.key
|
15679 | });
|
15680 | }
|
15681 |
|
15682 | this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
|
15683 | }
|
15684 | } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
|
15685 | this.resetPreviousNodeTrailingComments(key);
|
15686 | method.kind = key.name;
|
15687 | const isPrivate = this.match(134);
|
15688 | this.parseClassElementName(publicMethod);
|
15689 |
|
15690 | if (isPrivate) {
|
15691 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
15692 | } else {
|
15693 | if (this.isNonstaticConstructor(publicMethod)) {
|
15694 | this.raise(Errors.ConstructorIsAccessor, {
|
15695 | at: publicMethod.key
|
15696 | });
|
15697 | }
|
15698 |
|
15699 | this.pushClassMethod(classBody, publicMethod, false, false, false, false);
|
15700 | }
|
15701 |
|
15702 | this.checkGetterSetterParams(publicMethod);
|
15703 | } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
|
15704 | this.expectPlugin("decoratorAutoAccessors");
|
15705 | this.resetPreviousNodeTrailingComments(key);
|
15706 | const isPrivate = this.match(134);
|
15707 | this.parseClassElementName(publicProp);
|
15708 | this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
|
15709 | } else if (this.isLineTerminator()) {
|
15710 | if (isPrivate) {
|
15711 | this.pushClassPrivateProperty(classBody, privateProp);
|
15712 | } else {
|
15713 | this.pushClassProperty(classBody, publicProp);
|
15714 | }
|
15715 | } else {
|
15716 | this.unexpected();
|
15717 | }
|
15718 | }
|
15719 |
|
15720 | parseClassElementName(member) {
|
15721 | const {
|
15722 | type,
|
15723 | value
|
15724 | } = this.state;
|
15725 |
|
15726 | if ((type === 128 || type === 129) && member.static && value === "prototype") {
|
15727 | this.raise(Errors.StaticPrototype, {
|
15728 | at: this.state.startLoc
|
15729 | });
|
15730 | }
|
15731 |
|
15732 | if (type === 134) {
|
15733 | if (value === "constructor") {
|
15734 | this.raise(Errors.ConstructorClassPrivateField, {
|
15735 | at: this.state.startLoc
|
15736 | });
|
15737 | }
|
15738 |
|
15739 | const key = this.parsePrivateName();
|
15740 | member.key = key;
|
15741 | return key;
|
15742 | }
|
15743 |
|
15744 | return this.parsePropertyName(member);
|
15745 | }
|
15746 |
|
15747 | parseClassStaticBlock(classBody, member) {
|
15748 | var _member$decorators;
|
15749 |
|
15750 | this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
|
15751 | const oldLabels = this.state.labels;
|
15752 | this.state.labels = [];
|
15753 | this.prodParam.enter(PARAM);
|
15754 | const body = member.body = [];
|
15755 | this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
|
15756 | this.prodParam.exit();
|
15757 | this.scope.exit();
|
15758 | this.state.labels = oldLabels;
|
15759 | classBody.body.push(this.finishNode(member, "StaticBlock"));
|
15760 |
|
15761 | if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
|
15762 | this.raise(Errors.DecoratorStaticBlock, {
|
15763 | at: member
|
15764 | });
|
15765 | }
|
15766 | }
|
15767 |
|
15768 | pushClassProperty(classBody, prop) {
|
15769 | if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
|
15770 | this.raise(Errors.ConstructorClassField, {
|
15771 | at: prop.key
|
15772 | });
|
15773 | }
|
15774 |
|
15775 | classBody.body.push(this.parseClassProperty(prop));
|
15776 | }
|
15777 |
|
15778 | pushClassPrivateProperty(classBody, prop) {
|
15779 | const node = this.parseClassPrivateProperty(prop);
|
15780 | classBody.body.push(node);
|
15781 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
|
15782 | }
|
15783 |
|
15784 | pushClassAccessorProperty(classBody, prop, isPrivate) {
|
15785 | if (!isPrivate && !prop.computed) {
|
15786 | const key = prop.key;
|
15787 |
|
15788 | if (key.name === "constructor" || key.value === "constructor") {
|
15789 | this.raise(Errors.ConstructorClassField, {
|
15790 | at: key
|
15791 | });
|
15792 | }
|
15793 | }
|
15794 |
|
15795 | const node = this.parseClassAccessorProperty(prop);
|
15796 | classBody.body.push(node);
|
15797 |
|
15798 | if (isPrivate) {
|
15799 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
|
15800 | }
|
15801 | }
|
15802 |
|
15803 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
15804 | classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
|
15805 | }
|
15806 |
|
15807 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
15808 | const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
|
15809 | classBody.body.push(node);
|
15810 | const kind = node.kind === "get" ? node.static ? CLASS_ELEMENT_STATIC_GETTER : CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? CLASS_ELEMENT_STATIC_SETTER : CLASS_ELEMENT_INSTANCE_SETTER : CLASS_ELEMENT_OTHER;
|
15811 | this.declareClassPrivateMethodInScope(node, kind);
|
15812 | }
|
15813 |
|
15814 | declareClassPrivateMethodInScope(node, kind) {
|
15815 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
|
15816 | }
|
15817 |
|
15818 | parsePostMemberNameModifiers(methodOrProp) {}
|
15819 |
|
15820 | parseClassPrivateProperty(node) {
|
15821 | this.parseInitializer(node);
|
15822 | this.semicolon();
|
15823 | return this.finishNode(node, "ClassPrivateProperty");
|
15824 | }
|
15825 |
|
15826 | parseClassProperty(node) {
|
15827 | this.parseInitializer(node);
|
15828 | this.semicolon();
|
15829 | return this.finishNode(node, "ClassProperty");
|
15830 | }
|
15831 |
|
15832 | parseClassAccessorProperty(node) {
|
15833 | this.parseInitializer(node);
|
15834 | this.semicolon();
|
15835 | return this.finishNode(node, "ClassAccessorProperty");
|
15836 | }
|
15837 |
|
15838 | parseInitializer(node) {
|
15839 | this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
|
15840 | this.expressionScope.enter(newExpressionScope());
|
15841 | this.prodParam.enter(PARAM);
|
15842 | node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
|
15843 | this.expressionScope.exit();
|
15844 | this.prodParam.exit();
|
15845 | this.scope.exit();
|
15846 | }
|
15847 |
|
15848 | parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
|
15849 | if (tokenIsIdentifier(this.state.type)) {
|
15850 | node.id = this.parseIdentifier();
|
15851 |
|
15852 | if (isStatement) {
|
15853 | this.declareNameFromIdentifier(node.id, bindingType);
|
15854 | }
|
15855 | } else {
|
15856 | if (optionalId || !isStatement) {
|
15857 | node.id = null;
|
15858 | } else {
|
15859 | throw this.raise(Errors.MissingClassName, {
|
15860 | at: this.state.startLoc
|
15861 | });
|
15862 | }
|
15863 | }
|
15864 | }
|
15865 |
|
15866 | parseClassSuper(node) {
|
15867 | node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
|
15868 | }
|
15869 |
|
15870 | parseExport(node) {
|
15871 | const hasDefault = this.maybeParseExportDefaultSpecifier(node);
|
15872 | const parseAfterDefault = !hasDefault || this.eat(12);
|
15873 | const hasStar = parseAfterDefault && this.eatExportStar(node);
|
15874 | const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
|
15875 | const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
|
15876 | const isFromRequired = hasDefault || hasStar;
|
15877 |
|
15878 | if (hasStar && !hasNamespace) {
|
15879 | if (hasDefault) this.unexpected();
|
15880 | this.parseExportFrom(node, true);
|
15881 | return this.finishNode(node, "ExportAllDeclaration");
|
15882 | }
|
15883 |
|
15884 | const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
|
15885 |
|
15886 | if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
|
15887 | throw this.unexpected(null, 5);
|
15888 | }
|
15889 |
|
15890 | let hasDeclaration;
|
15891 |
|
15892 | if (isFromRequired || hasSpecifiers) {
|
15893 | hasDeclaration = false;
|
15894 | this.parseExportFrom(node, isFromRequired);
|
15895 | } else {
|
15896 | hasDeclaration = this.maybeParseExportDeclaration(node);
|
15897 | }
|
15898 |
|
15899 | if (isFromRequired || hasSpecifiers || hasDeclaration) {
|
15900 | this.checkExport(node, true, false, !!node.source);
|
15901 | return this.finishNode(node, "ExportNamedDeclaration");
|
15902 | }
|
15903 |
|
15904 | if (this.eat(65)) {
|
15905 | node.declaration = this.parseExportDefaultExpression();
|
15906 | this.checkExport(node, true, true);
|
15907 | return this.finishNode(node, "ExportDefaultDeclaration");
|
15908 | }
|
15909 |
|
15910 | throw this.unexpected(null, 5);
|
15911 | }
|
15912 |
|
15913 | eatExportStar(node) {
|
15914 | return this.eat(55);
|
15915 | }
|
15916 |
|
15917 | maybeParseExportDefaultSpecifier(node) {
|
15918 | if (this.isExportDefaultSpecifier()) {
|
15919 | this.expectPlugin("exportDefaultFrom");
|
15920 | const specifier = this.startNode();
|
15921 | specifier.exported = this.parseIdentifier(true);
|
15922 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
15923 | return true;
|
15924 | }
|
15925 |
|
15926 | return false;
|
15927 | }
|
15928 |
|
15929 | maybeParseExportNamespaceSpecifier(node) {
|
15930 | if (this.isContextual(93)) {
|
15931 | if (!node.specifiers) node.specifiers = [];
|
15932 | const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
|
15933 | this.next();
|
15934 | specifier.exported = this.parseModuleExportName();
|
15935 | node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
|
15936 | return true;
|
15937 | }
|
15938 |
|
15939 | return false;
|
15940 | }
|
15941 |
|
15942 | maybeParseExportNamedSpecifiers(node) {
|
15943 | if (this.match(5)) {
|
15944 | if (!node.specifiers) node.specifiers = [];
|
15945 | const isTypeExport = node.exportKind === "type";
|
15946 | node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
|
15947 | node.source = null;
|
15948 | node.declaration = null;
|
15949 |
|
15950 | if (this.hasPlugin("importAssertions")) {
|
15951 | node.assertions = [];
|
15952 | }
|
15953 |
|
15954 | return true;
|
15955 | }
|
15956 |
|
15957 | return false;
|
15958 | }
|
15959 |
|
15960 | maybeParseExportDeclaration(node) {
|
15961 | if (this.shouldParseExportDeclaration()) {
|
15962 | node.specifiers = [];
|
15963 | node.source = null;
|
15964 |
|
15965 | if (this.hasPlugin("importAssertions")) {
|
15966 | node.assertions = [];
|
15967 | }
|
15968 |
|
15969 | node.declaration = this.parseExportDeclaration(node);
|
15970 | return true;
|
15971 | }
|
15972 |
|
15973 | return false;
|
15974 | }
|
15975 |
|
15976 | isAsyncFunction() {
|
15977 | if (!this.isContextual(95)) return false;
|
15978 | const next = this.nextTokenStart();
|
15979 | return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
|
15980 | }
|
15981 |
|
15982 | parseExportDefaultExpression() {
|
15983 | const expr = this.startNode();
|
15984 | const isAsync = this.isAsyncFunction();
|
15985 |
|
15986 | if (this.match(68) || isAsync) {
|
15987 | this.next();
|
15988 |
|
15989 | if (isAsync) {
|
15990 | this.next();
|
15991 | }
|
15992 |
|
15993 | return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
|
15994 | }
|
15995 |
|
15996 | if (this.match(80)) {
|
15997 | return this.parseClass(expr, true, true);
|
15998 | }
|
15999 |
|
16000 | if (this.match(26)) {
|
16001 | if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
16002 | this.raise(Errors.DecoratorBeforeExport, {
|
16003 | at: this.state.startLoc
|
16004 | });
|
16005 | }
|
16006 |
|
16007 | this.parseDecorators(false);
|
16008 | return this.parseClass(expr, true, true);
|
16009 | }
|
16010 |
|
16011 | if (this.match(75) || this.match(74) || this.isLet()) {
|
16012 | throw this.raise(Errors.UnsupportedDefaultExport, {
|
16013 | at: this.state.startLoc
|
16014 | });
|
16015 | }
|
16016 |
|
16017 | const res = this.parseMaybeAssignAllowIn();
|
16018 | this.semicolon();
|
16019 | return res;
|
16020 | }
|
16021 |
|
16022 | parseExportDeclaration(node) {
|
16023 | return this.parseStatement(null);
|
16024 | }
|
16025 |
|
16026 | isExportDefaultSpecifier() {
|
16027 | const {
|
16028 | type
|
16029 | } = this.state;
|
16030 |
|
16031 | if (tokenIsIdentifier(type)) {
|
16032 | if (type === 95 && !this.state.containsEsc || type === 99) {
|
16033 | return false;
|
16034 | }
|
16035 |
|
16036 | if ((type === 126 || type === 125) && !this.state.containsEsc) {
|
16037 | const {
|
16038 | type: nextType
|
16039 | } = this.lookahead();
|
16040 |
|
16041 | if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
|
16042 | this.expectOnePlugin(["flow", "typescript"]);
|
16043 | return false;
|
16044 | }
|
16045 | }
|
16046 | } else if (!this.match(65)) {
|
16047 | return false;
|
16048 | }
|
16049 |
|
16050 | const next = this.nextTokenStart();
|
16051 | const hasFrom = this.isUnparsedContextual(next, "from");
|
16052 |
|
16053 | if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
|
16054 | return true;
|
16055 | }
|
16056 |
|
16057 | if (this.match(65) && hasFrom) {
|
16058 | const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
|
16059 | return nextAfterFrom === 34 || nextAfterFrom === 39;
|
16060 | }
|
16061 |
|
16062 | return false;
|
16063 | }
|
16064 |
|
16065 | parseExportFrom(node, expect) {
|
16066 | if (this.eatContextual(97)) {
|
16067 | node.source = this.parseImportSource();
|
16068 | this.checkExport(node);
|
16069 | const assertions = this.maybeParseImportAssertions();
|
16070 |
|
16071 | if (assertions) {
|
16072 | node.assertions = assertions;
|
16073 | this.checkJSONModuleImport(node);
|
16074 | }
|
16075 | } else if (expect) {
|
16076 | this.unexpected();
|
16077 | }
|
16078 |
|
16079 | this.semicolon();
|
16080 | }
|
16081 |
|
16082 | shouldParseExportDeclaration() {
|
16083 | const {
|
16084 | type
|
16085 | } = this.state;
|
16086 |
|
16087 | if (type === 26) {
|
16088 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
16089 |
|
16090 | if (this.hasPlugin("decorators")) {
|
16091 | if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
|
16092 | throw this.raise(Errors.DecoratorBeforeExport, {
|
16093 | at: this.state.startLoc
|
16094 | });
|
16095 | }
|
16096 |
|
16097 | return true;
|
16098 | }
|
16099 | }
|
16100 |
|
16101 | return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
|
16102 | }
|
16103 |
|
16104 | checkExport(node, checkNames, isDefault, isFrom) {
|
16105 | if (checkNames) {
|
16106 | if (isDefault) {
|
16107 | this.checkDuplicateExports(node, "default");
|
16108 |
|
16109 | if (this.hasPlugin("exportDefaultFrom")) {
|
16110 | var _declaration$extra;
|
16111 |
|
16112 | const declaration = node.declaration;
|
16113 |
|
16114 | if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
|
16115 | this.raise(Errors.ExportDefaultFromAsIdentifier, {
|
16116 | at: declaration
|
16117 | });
|
16118 | }
|
16119 | }
|
16120 | } else if (node.specifiers && node.specifiers.length) {
|
16121 | for (const specifier of node.specifiers) {
|
16122 | const {
|
16123 | exported
|
16124 | } = specifier;
|
16125 | const exportName = exported.type === "Identifier" ? exported.name : exported.value;
|
16126 | this.checkDuplicateExports(specifier, exportName);
|
16127 |
|
16128 | if (!isFrom && specifier.local) {
|
16129 | const {
|
16130 | local
|
16131 | } = specifier;
|
16132 |
|
16133 | if (local.type !== "Identifier") {
|
16134 | this.raise(Errors.ExportBindingIsString, {
|
16135 | at: specifier,
|
16136 | localName: local.value,
|
16137 | exportName
|
16138 | });
|
16139 | } else {
|
16140 | this.checkReservedWord(local.name, local.loc.start, true, false);
|
16141 | this.scope.checkLocalExport(local);
|
16142 | }
|
16143 | }
|
16144 | }
|
16145 | } else if (node.declaration) {
|
16146 | if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
|
16147 | const id = node.declaration.id;
|
16148 | if (!id) throw new Error("Assertion failure");
|
16149 | this.checkDuplicateExports(node, id.name);
|
16150 | } else if (node.declaration.type === "VariableDeclaration") {
|
16151 | for (const declaration of node.declaration.declarations) {
|
16152 | this.checkDeclaration(declaration.id);
|
16153 | }
|
16154 | }
|
16155 | }
|
16156 | }
|
16157 |
|
16158 | const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
|
16159 |
|
16160 | if (currentContextDecorators.length) {
|
16161 | throw this.raise(Errors.UnsupportedDecoratorExport, {
|
16162 | at: node
|
16163 | });
|
16164 | }
|
16165 | }
|
16166 |
|
16167 | checkDeclaration(node) {
|
16168 | if (node.type === "Identifier") {
|
16169 | this.checkDuplicateExports(node, node.name);
|
16170 | } else if (node.type === "ObjectPattern") {
|
16171 | for (const prop of node.properties) {
|
16172 | this.checkDeclaration(prop);
|
16173 | }
|
16174 | } else if (node.type === "ArrayPattern") {
|
16175 | for (const elem of node.elements) {
|
16176 | if (elem) {
|
16177 | this.checkDeclaration(elem);
|
16178 | }
|
16179 | }
|
16180 | } else if (node.type === "ObjectProperty") {
|
16181 | this.checkDeclaration(node.value);
|
16182 | } else if (node.type === "RestElement") {
|
16183 | this.checkDeclaration(node.argument);
|
16184 | } else if (node.type === "AssignmentPattern") {
|
16185 | this.checkDeclaration(node.left);
|
16186 | }
|
16187 | }
|
16188 |
|
16189 | checkDuplicateExports(node, exportName) {
|
16190 | if (this.exportedIdentifiers.has(exportName)) {
|
16191 | if (exportName === "default") {
|
16192 | this.raise(Errors.DuplicateDefaultExport, {
|
16193 | at: node
|
16194 | });
|
16195 | } else {
|
16196 | this.raise(Errors.DuplicateExport, {
|
16197 | at: node,
|
16198 | exportName
|
16199 | });
|
16200 | }
|
16201 | }
|
16202 |
|
16203 | this.exportedIdentifiers.add(exportName);
|
16204 | }
|
16205 |
|
16206 | parseExportSpecifiers(isInTypeExport) {
|
16207 | const nodes = [];
|
16208 | let first = true;
|
16209 | this.expect(5);
|
16210 |
|
16211 | while (!this.eat(8)) {
|
16212 | if (first) {
|
16213 | first = false;
|
16214 | } else {
|
16215 | this.expect(12);
|
16216 | if (this.eat(8)) break;
|
16217 | }
|
16218 |
|
16219 | const isMaybeTypeOnly = this.isContextual(126);
|
16220 | const isString = this.match(129);
|
16221 | const node = this.startNode();
|
16222 | node.local = this.parseModuleExportName();
|
16223 | nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
|
16224 | }
|
16225 |
|
16226 | return nodes;
|
16227 | }
|
16228 |
|
16229 | parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
|
16230 | if (this.eatContextual(93)) {
|
16231 | node.exported = this.parseModuleExportName();
|
16232 | } else if (isString) {
|
16233 | node.exported = cloneStringLiteral(node.local);
|
16234 | } else if (!node.exported) {
|
16235 | node.exported = cloneIdentifier(node.local);
|
16236 | }
|
16237 |
|
16238 | return this.finishNode(node, "ExportSpecifier");
|
16239 | }
|
16240 |
|
16241 | parseModuleExportName() {
|
16242 | if (this.match(129)) {
|
16243 | const result = this.parseStringLiteral(this.state.value);
|
16244 | const surrogate = result.value.match(loneSurrogate);
|
16245 |
|
16246 | if (surrogate) {
|
16247 | this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
|
16248 | at: result,
|
16249 | surrogateCharCode: surrogate[0].charCodeAt(0)
|
16250 | });
|
16251 | }
|
16252 |
|
16253 | return result;
|
16254 | }
|
16255 |
|
16256 | return this.parseIdentifier(true);
|
16257 | }
|
16258 |
|
16259 | isJSONModuleImport(node) {
|
16260 | if (node.assertions != null) {
|
16261 | return node.assertions.some(({
|
16262 | key,
|
16263 | value
|
16264 | }) => {
|
16265 | return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
|
16266 | });
|
16267 | }
|
16268 |
|
16269 | return false;
|
16270 | }
|
16271 |
|
16272 | checkJSONModuleImport(node) {
|
16273 | if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
|
16274 | const {
|
16275 | specifiers
|
16276 | } = node;
|
16277 |
|
16278 | if (node.specifiers != null) {
|
16279 | const nonDefaultNamedSpecifier = specifiers.find(specifier => {
|
16280 | let imported;
|
16281 |
|
16282 | if (specifier.type === "ExportSpecifier") {
|
16283 | imported = specifier.local;
|
16284 | } else if (specifier.type === "ImportSpecifier") {
|
16285 | imported = specifier.imported;
|
16286 | }
|
16287 |
|
16288 | if (imported !== undefined) {
|
16289 | return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
|
16290 | }
|
16291 | });
|
16292 |
|
16293 | if (nonDefaultNamedSpecifier !== undefined) {
|
16294 | this.raise(Errors.ImportJSONBindingNotDefault, {
|
16295 | at: nonDefaultNamedSpecifier.loc.start
|
16296 | });
|
16297 | }
|
16298 | }
|
16299 | }
|
16300 | }
|
16301 |
|
16302 | parseImport(node) {
|
16303 | node.specifiers = [];
|
16304 |
|
16305 | if (!this.match(129)) {
|
16306 | const hasDefault = this.maybeParseDefaultImportSpecifier(node);
|
16307 | const parseNext = !hasDefault || this.eat(12);
|
16308 | const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
|
16309 | if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
|
16310 | this.expectContextual(97);
|
16311 | }
|
16312 |
|
16313 | node.source = this.parseImportSource();
|
16314 | const assertions = this.maybeParseImportAssertions();
|
16315 |
|
16316 | if (assertions) {
|
16317 | node.assertions = assertions;
|
16318 | } else {
|
16319 | const attributes = this.maybeParseModuleAttributes();
|
16320 |
|
16321 | if (attributes) {
|
16322 | node.attributes = attributes;
|
16323 | }
|
16324 | }
|
16325 |
|
16326 | this.checkJSONModuleImport(node);
|
16327 | this.semicolon();
|
16328 | return this.finishNode(node, "ImportDeclaration");
|
16329 | }
|
16330 |
|
16331 | parseImportSource() {
|
16332 | if (!this.match(129)) this.unexpected();
|
16333 | return this.parseExprAtom();
|
16334 | }
|
16335 |
|
16336 | shouldParseDefaultImport(node) {
|
16337 | return tokenIsIdentifier(this.state.type);
|
16338 | }
|
16339 |
|
16340 | parseImportSpecifierLocal(node, specifier, type) {
|
16341 | specifier.local = this.parseIdentifier();
|
16342 | node.specifiers.push(this.finishImportSpecifier(specifier, type));
|
16343 | }
|
16344 |
|
16345 | finishImportSpecifier(specifier, type) {
|
16346 | this.checkLVal(specifier.local, {
|
16347 | in: specifier,
|
16348 | binding: BIND_LEXICAL
|
16349 | });
|
16350 | return this.finishNode(specifier, type);
|
16351 | }
|
16352 |
|
16353 | parseAssertEntries() {
|
16354 | const attrs = [];
|
16355 | const attrNames = new Set();
|
16356 |
|
16357 | do {
|
16358 | if (this.match(8)) {
|
16359 | break;
|
16360 | }
|
16361 |
|
16362 | const node = this.startNode();
|
16363 | const keyName = this.state.value;
|
16364 |
|
16365 | if (attrNames.has(keyName)) {
|
16366 | this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
|
16367 | at: this.state.startLoc,
|
16368 | key: keyName
|
16369 | });
|
16370 | }
|
16371 |
|
16372 | attrNames.add(keyName);
|
16373 |
|
16374 | if (this.match(129)) {
|
16375 | node.key = this.parseStringLiteral(keyName);
|
16376 | } else {
|
16377 | node.key = this.parseIdentifier(true);
|
16378 | }
|
16379 |
|
16380 | this.expect(14);
|
16381 |
|
16382 | if (!this.match(129)) {
|
16383 | throw this.raise(Errors.ModuleAttributeInvalidValue, {
|
16384 | at: this.state.startLoc
|
16385 | });
|
16386 | }
|
16387 |
|
16388 | node.value = this.parseStringLiteral(this.state.value);
|
16389 | this.finishNode(node, "ImportAttribute");
|
16390 | attrs.push(node);
|
16391 | } while (this.eat(12));
|
16392 |
|
16393 | return attrs;
|
16394 | }
|
16395 |
|
16396 | maybeParseModuleAttributes() {
|
16397 | if (this.match(76) && !this.hasPrecedingLineBreak()) {
|
16398 | this.expectPlugin("moduleAttributes");
|
16399 | this.next();
|
16400 | } else {
|
16401 | if (this.hasPlugin("moduleAttributes")) return [];
|
16402 | return null;
|
16403 | }
|
16404 |
|
16405 | const attrs = [];
|
16406 | const attributes = new Set();
|
16407 |
|
16408 | do {
|
16409 | const node = this.startNode();
|
16410 | node.key = this.parseIdentifier(true);
|
16411 |
|
16412 | if (node.key.name !== "type") {
|
16413 | this.raise(Errors.ModuleAttributeDifferentFromType, {
|
16414 | at: node.key
|
16415 | });
|
16416 | }
|
16417 |
|
16418 | if (attributes.has(node.key.name)) {
|
16419 | this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
|
16420 | at: node.key,
|
16421 | key: node.key.name
|
16422 | });
|
16423 | }
|
16424 |
|
16425 | attributes.add(node.key.name);
|
16426 | this.expect(14);
|
16427 |
|
16428 | if (!this.match(129)) {
|
16429 | throw this.raise(Errors.ModuleAttributeInvalidValue, {
|
16430 | at: this.state.startLoc
|
16431 | });
|
16432 | }
|
16433 |
|
16434 | node.value = this.parseStringLiteral(this.state.value);
|
16435 | this.finishNode(node, "ImportAttribute");
|
16436 | attrs.push(node);
|
16437 | } while (this.eat(12));
|
16438 |
|
16439 | return attrs;
|
16440 | }
|
16441 |
|
16442 | maybeParseImportAssertions() {
|
16443 | if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
|
16444 | this.expectPlugin("importAssertions");
|
16445 | this.next();
|
16446 | } else {
|
16447 | if (this.hasPlugin("importAssertions")) return [];
|
16448 | return null;
|
16449 | }
|
16450 |
|
16451 | this.eat(5);
|
16452 | const attrs = this.parseAssertEntries();
|
16453 | this.eat(8);
|
16454 | return attrs;
|
16455 | }
|
16456 |
|
16457 | maybeParseDefaultImportSpecifier(node) {
|
16458 | if (this.shouldParseDefaultImport(node)) {
|
16459 | this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
|
16460 | return true;
|
16461 | }
|
16462 |
|
16463 | return false;
|
16464 | }
|
16465 |
|
16466 | maybeParseStarImportSpecifier(node) {
|
16467 | if (this.match(55)) {
|
16468 | const specifier = this.startNode();
|
16469 | this.next();
|
16470 | this.expectContextual(93);
|
16471 | this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
|
16472 | return true;
|
16473 | }
|
16474 |
|
16475 | return false;
|
16476 | }
|
16477 |
|
16478 | parseNamedImportSpecifiers(node) {
|
16479 | let first = true;
|
16480 | this.expect(5);
|
16481 |
|
16482 | while (!this.eat(8)) {
|
16483 | if (first) {
|
16484 | first = false;
|
16485 | } else {
|
16486 | if (this.eat(14)) {
|
16487 | throw this.raise(Errors.DestructureNamedImport, {
|
16488 | at: this.state.startLoc
|
16489 | });
|
16490 | }
|
16491 |
|
16492 | this.expect(12);
|
16493 | if (this.eat(8)) break;
|
16494 | }
|
16495 |
|
16496 | const specifier = this.startNode();
|
16497 | const importedIsString = this.match(129);
|
16498 | const isMaybeTypeOnly = this.isContextual(126);
|
16499 | specifier.imported = this.parseModuleExportName();
|
16500 | const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
|
16501 | node.specifiers.push(importSpecifier);
|
16502 | }
|
16503 | }
|
16504 |
|
16505 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
|
16506 | if (this.eatContextual(93)) {
|
16507 | specifier.local = this.parseIdentifier();
|
16508 | } else {
|
16509 | const {
|
16510 | imported
|
16511 | } = specifier;
|
16512 |
|
16513 | if (importedIsString) {
|
16514 | throw this.raise(Errors.ImportBindingIsString, {
|
16515 | at: specifier,
|
16516 | importName: imported.value
|
16517 | });
|
16518 | }
|
16519 |
|
16520 | this.checkReservedWord(imported.name, specifier.loc.start, true, true);
|
16521 |
|
16522 | if (!specifier.local) {
|
16523 | specifier.local = cloneIdentifier(imported);
|
16524 | }
|
16525 | }
|
16526 |
|
16527 | return this.finishImportSpecifier(specifier, "ImportSpecifier");
|
16528 | }
|
16529 |
|
16530 | isThisParam(param) {
|
16531 | return param.type === "Identifier" && param.name === "this";
|
16532 | }
|
16533 |
|
16534 | }
|
16535 |
|
16536 | class Parser extends StatementParser {
|
16537 | constructor(options, input) {
|
16538 | options = getOptions(options);
|
16539 | super(options, input);
|
16540 | this.options = options;
|
16541 | this.initializeScopes();
|
16542 | this.plugins = pluginsMap(this.options.plugins);
|
16543 | this.filename = options.sourceFilename;
|
16544 | }
|
16545 |
|
16546 | getScopeHandler() {
|
16547 | return ScopeHandler;
|
16548 | }
|
16549 |
|
16550 | parse() {
|
16551 | this.enterInitialScopes();
|
16552 | const file = this.startNode();
|
16553 | const program = this.startNode();
|
16554 | this.nextToken();
|
16555 | file.errors = null;
|
16556 | this.parseTopLevel(file, program);
|
16557 | file.errors = this.state.errors;
|
16558 | return file;
|
16559 | }
|
16560 |
|
16561 | }
|
16562 |
|
16563 | function pluginsMap(plugins) {
|
16564 | const pluginMap = new Map();
|
16565 |
|
16566 | for (const plugin of plugins) {
|
16567 | const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
|
16568 | if (!pluginMap.has(name)) pluginMap.set(name, options || {});
|
16569 | }
|
16570 |
|
16571 | return pluginMap;
|
16572 | }
|
16573 |
|
16574 | function parse(input, options) {
|
16575 | var _options;
|
16576 |
|
16577 | if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
|
16578 | options = Object.assign({}, options);
|
16579 |
|
16580 | try {
|
16581 | options.sourceType = "module";
|
16582 | const parser = getParser(options, input);
|
16583 | const ast = parser.parse();
|
16584 |
|
16585 | if (parser.sawUnambiguousESM) {
|
16586 | return ast;
|
16587 | }
|
16588 |
|
16589 | if (parser.ambiguousScriptDifferentAst) {
|
16590 | try {
|
16591 | options.sourceType = "script";
|
16592 | return getParser(options, input).parse();
|
16593 | } catch (_unused) {}
|
16594 | } else {
|
16595 | ast.program.sourceType = "script";
|
16596 | }
|
16597 |
|
16598 | return ast;
|
16599 | } catch (moduleError) {
|
16600 | try {
|
16601 | options.sourceType = "script";
|
16602 | return getParser(options, input).parse();
|
16603 | } catch (_unused2) {}
|
16604 |
|
16605 | throw moduleError;
|
16606 | }
|
16607 | } else {
|
16608 | return getParser(options, input).parse();
|
16609 | }
|
16610 | }
|
16611 | function parseExpression(input, options) {
|
16612 | const parser = getParser(options, input);
|
16613 |
|
16614 | if (parser.options.strictMode) {
|
16615 | parser.state.strict = true;
|
16616 | }
|
16617 |
|
16618 | return parser.getExpression();
|
16619 | }
|
16620 |
|
16621 | function generateExportedTokenTypes(internalTokenTypes) {
|
16622 | const tokenTypes = {};
|
16623 |
|
16624 | for (const typeName of Object.keys(internalTokenTypes)) {
|
16625 | tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
|
16626 | }
|
16627 |
|
16628 | return tokenTypes;
|
16629 | }
|
16630 |
|
16631 | const tokTypes = generateExportedTokenTypes(tt);
|
16632 |
|
16633 | function getParser(options, input) {
|
16634 | let cls = Parser;
|
16635 |
|
16636 | if (options != null && options.plugins) {
|
16637 | validatePlugins(options.plugins);
|
16638 | cls = getParserClass(options.plugins);
|
16639 | }
|
16640 |
|
16641 | return new cls(options, input);
|
16642 | }
|
16643 |
|
16644 | const parserClassCache = {};
|
16645 |
|
16646 | function getParserClass(pluginsFromOptions) {
|
16647 | const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
|
16648 | const key = pluginList.join("/");
|
16649 | let cls = parserClassCache[key];
|
16650 |
|
16651 | if (!cls) {
|
16652 | cls = Parser;
|
16653 |
|
16654 | for (const plugin of pluginList) {
|
16655 | cls = mixinPlugins[plugin](cls);
|
16656 | }
|
16657 |
|
16658 | parserClassCache[key] = cls;
|
16659 | }
|
16660 |
|
16661 | return cls;
|
16662 | }
|
16663 |
|
16664 | exports.parse = parse;
|
16665 | exports.parseExpression = parseExpression;
|
16666 | exports.tokTypes = tokTypes;
|
16667 | //# sourceMappingURL=index.js.map
|