1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | function _objectWithoutPropertiesLoose(source, excluded) {
|
7 | if (source == null) return {};
|
8 | var target = {};
|
9 | var sourceKeys = Object.keys(source);
|
10 | var key, i;
|
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 | return target;
|
17 | }
|
18 | class Position {
|
19 | constructor(line, col, index) {
|
20 | this.line = void 0;
|
21 | this.column = void 0;
|
22 | this.index = void 0;
|
23 | this.line = line;
|
24 | this.column = col;
|
25 | this.index = index;
|
26 | }
|
27 | }
|
28 | class SourceLocation {
|
29 | constructor(start, end) {
|
30 | this.start = void 0;
|
31 | this.end = void 0;
|
32 | this.filename = void 0;
|
33 | this.identifierName = void 0;
|
34 | this.start = start;
|
35 | this.end = end;
|
36 | }
|
37 | }
|
38 | function createPositionWithColumnOffset(position, columnOffset) {
|
39 | const {
|
40 | line,
|
41 | column,
|
42 | index
|
43 | } = position;
|
44 | return new Position(line, column + columnOffset, index + columnOffset);
|
45 | }
|
46 | var ParseErrorCode = {
|
47 | SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
|
48 | SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
|
49 | };
|
50 | const reflect = (keys, last = keys.length - 1) => ({
|
51 | get() {
|
52 | return keys.reduce((object, key) => object[key], this);
|
53 | },
|
54 | set(value) {
|
55 | keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
|
56 | }
|
57 | });
|
58 | const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
|
59 | value: descriptor,
|
60 | enumerable: false
|
61 | } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
|
62 | configurable: true
|
63 | }, descriptor)), Object.assign(new constructor(), properties));
|
64 | var ModuleErrors = {
|
65 | ImportMetaOutsideModule: {
|
66 | message: `import.meta may appear only with 'sourceType: "module"'`,
|
67 | code: ParseErrorCode.SourceTypeModuleError
|
68 | },
|
69 | ImportOutsideModule: {
|
70 | message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
|
71 | code: ParseErrorCode.SourceTypeModuleError
|
72 | }
|
73 | };
|
74 | const NodeDescriptions = {
|
75 | ArrayPattern: "array destructuring pattern",
|
76 | AssignmentExpression: "assignment expression",
|
77 | AssignmentPattern: "assignment expression",
|
78 | ArrowFunctionExpression: "arrow function expression",
|
79 | ConditionalExpression: "conditional expression",
|
80 | CatchClause: "catch clause",
|
81 | ForOfStatement: "for-of statement",
|
82 | ForInStatement: "for-in statement",
|
83 | ForStatement: "for-loop",
|
84 | FormalParameters: "function parameter list",
|
85 | Identifier: "identifier",
|
86 | ImportSpecifier: "import specifier",
|
87 | ImportDefaultSpecifier: "import default specifier",
|
88 | ImportNamespaceSpecifier: "import namespace specifier",
|
89 | ObjectPattern: "object destructuring pattern",
|
90 | ParenthesizedExpression: "parenthesized expression",
|
91 | RestElement: "rest element",
|
92 | UpdateExpression: {
|
93 | true: "prefix operation",
|
94 | false: "postfix operation"
|
95 | },
|
96 | VariableDeclarator: "variable declaration",
|
97 | YieldExpression: "yield expression"
|
98 | };
|
99 | const toNodeDescription = ({
|
100 | type,
|
101 | prefix
|
102 | }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
|
103 | var StandardErrors = {
|
104 | AccessorIsGenerator: ({
|
105 | kind
|
106 | }) => `A ${kind}ter cannot be a generator.`,
|
107 | ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
|
108 | AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
|
109 | AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
|
110 | AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
|
111 | AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
|
112 | AwaitUsingNotInAsyncContext: "'await using' is only allowed within async functions and at the top levels of modules.",
|
113 | AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
|
114 | AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
|
115 | BadGetterArity: "A 'get' accessor must not have any formal parameters.",
|
116 | BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
|
117 | BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
|
118 | ConstructorClassField: "Classes may not have a field named 'constructor'.",
|
119 | ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
|
120 | ConstructorIsAccessor: "Class constructor may not be an accessor.",
|
121 | ConstructorIsAsync: "Constructor can't be an async function.",
|
122 | ConstructorIsGenerator: "Constructor can't be a generator.",
|
123 | DeclarationMissingInitializer: ({
|
124 | kind
|
125 | }) => `Missing initializer in ${kind} declaration.`,
|
126 | DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
|
127 | DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
|
128 | DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
|
129 | DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
|
130 | DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
|
131 | DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
|
132 | DecoratorStaticBlock: "Decorators can't be used with a static block.",
|
133 | DeletePrivateField: "Deleting a private field is not allowed.",
|
134 | DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
|
135 | DuplicateConstructor: "Duplicate constructor in the same class.",
|
136 | DuplicateDefaultExport: "Only one default export allowed per module.",
|
137 | DuplicateExport: ({
|
138 | exportName
|
139 | }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
|
140 | DuplicateProto: "Redefinition of __proto__ property.",
|
141 | DuplicateRegExpFlags: "Duplicate regular expression flag.",
|
142 | ElementAfterRest: "Rest element must be last element.",
|
143 | EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
|
144 | ExportBindingIsString: ({
|
145 | localName,
|
146 | exportName
|
147 | }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
|
148 | ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
|
149 | ForInOfLoopInitializer: ({
|
150 | type
|
151 | }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
|
152 | ForInUsing: "For-in loop may not start with 'using' declaration.",
|
153 | ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
|
154 | ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
|
155 | GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
|
156 | IllegalBreakContinue: ({
|
157 | type
|
158 | }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
|
159 | IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
|
160 | IllegalReturn: "'return' outside of function.",
|
161 | ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedAssertSyntax: true` option in the import attributes plugin to suppress this error.",
|
162 | ImportBindingIsString: ({
|
163 | importName
|
164 | }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
|
165 | ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
|
166 | ImportCallArity: ({
|
167 | maxArgumentCount
|
168 | }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
|
169 | ImportCallNotNewExpression: "Cannot use new with import(...).",
|
170 | ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
|
171 | ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
|
172 | ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
|
173 | ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
|
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 or inside a block.",
|
260 | SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
|
261 | StaticPrototype: "Classes may not have static property named prototype.",
|
262 | 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?",
|
263 | SuperPrivateField: "Private fields can't be accessed on super.",
|
264 | TrailingDecorator: "Decorators must be attached to a class element.",
|
265 | TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
|
266 | TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
|
267 | TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
|
268 | UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
|
269 | UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
|
270 | UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
|
271 | UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
|
272 | UnexpectedKeyword: ({
|
273 | keyword
|
274 | }) => `Unexpected keyword '${keyword}'.`,
|
275 | UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
|
276 | UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
|
277 | UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
|
278 | UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
|
279 | UnexpectedPrivateField: "Unexpected private name.",
|
280 | UnexpectedReservedWord: ({
|
281 | reservedWord
|
282 | }) => `Unexpected reserved word '${reservedWord}'.`,
|
283 | UnexpectedSuper: "'super' is only allowed in object methods and classes.",
|
284 | UnexpectedToken: ({
|
285 | expected,
|
286 | unexpected
|
287 | }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
|
288 | UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
|
289 | UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
|
290 | UnsupportedBind: "Binding should be performed on object property.",
|
291 | UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
|
292 | UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
|
293 | UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
|
294 | UnsupportedMetaProperty: ({
|
295 | target,
|
296 | onlyValidPropertyName
|
297 | }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
|
298 | UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
|
299 | UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
|
300 | UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
|
301 | UnterminatedComment: "Unterminated comment.",
|
302 | UnterminatedRegExp: "Unterminated regular expression.",
|
303 | UnterminatedString: "Unterminated string constant.",
|
304 | UnterminatedTemplate: "Unterminated template.",
|
305 | UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
|
306 | VarRedeclaration: ({
|
307 | identifierName
|
308 | }) => `Identifier '${identifierName}' has already been declared.`,
|
309 | YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
|
310 | YieldInParameter: "Yield expression is not allowed in formal parameters.",
|
311 | ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
|
312 | };
|
313 | var StrictModeErrors = {
|
314 | StrictDelete: "Deleting local variable in strict mode.",
|
315 | StrictEvalArguments: ({
|
316 | referenceName
|
317 | }) => `Assigning to '${referenceName}' in strict mode.`,
|
318 | StrictEvalArgumentsBinding: ({
|
319 | bindingName
|
320 | }) => `Binding '${bindingName}' in strict mode.`,
|
321 | StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
|
322 | StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
|
323 | StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
|
324 | StrictWith: "'with' in strict mode."
|
325 | };
|
326 | const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
|
327 | var PipelineOperatorErrors = {
|
328 | PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
|
329 | PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
|
330 | PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
|
331 | PipeTopicUnconfiguredToken: ({
|
332 | token
|
333 | }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
|
334 | PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
|
335 | PipeUnparenthesizedBody: ({
|
336 | type
|
337 | }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
|
338 | type
|
339 | })}; please wrap it in parentheses.`,
|
340 | PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
|
341 | PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
|
342 | PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
|
343 | PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
|
344 | PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
|
345 | PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
|
346 | };
|
347 | const _excluded$1 = ["toMessage"],
|
348 | _excluded2$1 = ["message"];
|
349 | function toParseErrorConstructor(_ref) {
|
350 | let {
|
351 | toMessage
|
352 | } = _ref,
|
353 | properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
354 | return function constructor({
|
355 | loc,
|
356 | details
|
357 | }) {
|
358 | return instantiate(SyntaxError, Object.assign({}, properties, {
|
359 | loc
|
360 | }), {
|
361 | clone(overrides = {}) {
|
362 | const loc = overrides.loc || {};
|
363 | return constructor({
|
364 | 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),
|
365 | details: Object.assign({}, this.details, overrides.details)
|
366 | });
|
367 | },
|
368 | details: {
|
369 | value: details,
|
370 | enumerable: false
|
371 | },
|
372 | message: {
|
373 | get() {
|
374 | return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
|
375 | },
|
376 | set(value) {
|
377 | Object.defineProperty(this, "message", {
|
378 | value
|
379 | });
|
380 | }
|
381 | },
|
382 | pos: {
|
383 | reflect: "loc.index",
|
384 | enumerable: true
|
385 | },
|
386 | missingPlugin: "missingPlugin" in details && {
|
387 | reflect: "details.missingPlugin",
|
388 | enumerable: true
|
389 | }
|
390 | });
|
391 | };
|
392 | }
|
393 | function ParseErrorEnum(argument, syntaxPlugin) {
|
394 | if (Array.isArray(argument)) {
|
395 | return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
|
396 | }
|
397 | const ParseErrorConstructors = {};
|
398 | for (const reasonCode of Object.keys(argument)) {
|
399 | const template = argument[reasonCode];
|
400 | const _ref2 = typeof template === "string" ? {
|
401 | message: () => template
|
402 | } : typeof template === "function" ? {
|
403 | message: template
|
404 | } : template,
|
405 | {
|
406 | message
|
407 | } = _ref2,
|
408 | rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
|
409 | const toMessage = typeof message === "string" ? () => message : message;
|
410 | ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
|
411 | code: ParseErrorCode.SyntaxError,
|
412 | reasonCode,
|
413 | toMessage
|
414 | }, syntaxPlugin ? {
|
415 | syntaxPlugin
|
416 | } : {}, rest));
|
417 | }
|
418 | return ParseErrorConstructors;
|
419 | }
|
420 | const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
|
421 | const {
|
422 | defineProperty
|
423 | } = Object;
|
424 | const toUnenumerable = (object, key) => defineProperty(object, key, {
|
425 | enumerable: false,
|
426 | value: object[key]
|
427 | });
|
428 | function toESTreeLocation(node) {
|
429 | node.loc.start && toUnenumerable(node.loc.start, "index");
|
430 | node.loc.end && toUnenumerable(node.loc.end, "index");
|
431 | return node;
|
432 | }
|
433 | var estree = superClass => class ESTreeParserMixin extends superClass {
|
434 | parse() {
|
435 | const file = toESTreeLocation(super.parse());
|
436 | if (this.options.tokens) {
|
437 | file.tokens = file.tokens.map(toESTreeLocation);
|
438 | }
|
439 | return file;
|
440 | }
|
441 | parseRegExpLiteral({
|
442 | pattern,
|
443 | flags
|
444 | }) {
|
445 | let regex = null;
|
446 | try {
|
447 | regex = new RegExp(pattern, flags);
|
448 | } catch (e) {}
|
449 | const node = this.estreeParseLiteral(regex);
|
450 | node.regex = {
|
451 | pattern,
|
452 | flags
|
453 | };
|
454 | return node;
|
455 | }
|
456 | parseBigIntLiteral(value) {
|
457 | let bigInt;
|
458 | try {
|
459 | bigInt = BigInt(value);
|
460 | } catch (_unused) {
|
461 | bigInt = null;
|
462 | }
|
463 | const node = this.estreeParseLiteral(bigInt);
|
464 | node.bigint = String(node.value || value);
|
465 | return node;
|
466 | }
|
467 | parseDecimalLiteral(value) {
|
468 | const decimal = null;
|
469 | const node = this.estreeParseLiteral(decimal);
|
470 | node.decimal = String(node.value || value);
|
471 | return node;
|
472 | }
|
473 | estreeParseLiteral(value) {
|
474 | return this.parseLiteral(value, "Literal");
|
475 | }
|
476 | parseStringLiteral(value) {
|
477 | return this.estreeParseLiteral(value);
|
478 | }
|
479 | parseNumericLiteral(value) {
|
480 | return this.estreeParseLiteral(value);
|
481 | }
|
482 | parseNullLiteral() {
|
483 | return this.estreeParseLiteral(null);
|
484 | }
|
485 | parseBooleanLiteral(value) {
|
486 | return this.estreeParseLiteral(value);
|
487 | }
|
488 | directiveToStmt(directive) {
|
489 | const expression = directive.value;
|
490 | delete directive.value;
|
491 | expression.type = "Literal";
|
492 | expression.raw = expression.extra.raw;
|
493 | expression.value = expression.extra.expressionValue;
|
494 | const stmt = directive;
|
495 | stmt.type = "ExpressionStatement";
|
496 | stmt.expression = expression;
|
497 | stmt.directive = expression.extra.rawValue;
|
498 | delete expression.extra;
|
499 | return stmt;
|
500 | }
|
501 | initFunction(node, isAsync) {
|
502 | super.initFunction(node, isAsync);
|
503 | node.expression = false;
|
504 | }
|
505 | checkDeclaration(node) {
|
506 | if (node != null && this.isObjectProperty(node)) {
|
507 | this.checkDeclaration(node.value);
|
508 | } else {
|
509 | super.checkDeclaration(node);
|
510 | }
|
511 | }
|
512 | getObjectOrClassMethodParams(method) {
|
513 | return method.value.params;
|
514 | }
|
515 | isValidDirective(stmt) {
|
516 | var _stmt$expression$extr;
|
517 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
|
518 | }
|
519 | parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
|
520 | super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
|
521 | const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
|
522 | node.body = directiveStatements.concat(node.body);
|
523 | delete node.directives;
|
524 | }
|
525 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
526 | this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
|
527 | if (method.typeParameters) {
|
528 | method.value.typeParameters = method.typeParameters;
|
529 | delete method.typeParameters;
|
530 | }
|
531 | classBody.body.push(method);
|
532 | }
|
533 | parsePrivateName() {
|
534 | const node = super.parsePrivateName();
|
535 | {
|
536 | if (!this.getPluginOption("estree", "classFeatures")) {
|
537 | return node;
|
538 | }
|
539 | }
|
540 | return this.convertPrivateNameToPrivateIdentifier(node);
|
541 | }
|
542 | convertPrivateNameToPrivateIdentifier(node) {
|
543 | const name = super.getPrivateNameSV(node);
|
544 | node = node;
|
545 | delete node.id;
|
546 | node.name = name;
|
547 | node.type = "PrivateIdentifier";
|
548 | return node;
|
549 | }
|
550 | isPrivateName(node) {
|
551 | {
|
552 | if (!this.getPluginOption("estree", "classFeatures")) {
|
553 | return super.isPrivateName(node);
|
554 | }
|
555 | }
|
556 | return node.type === "PrivateIdentifier";
|
557 | }
|
558 | getPrivateNameSV(node) {
|
559 | {
|
560 | if (!this.getPluginOption("estree", "classFeatures")) {
|
561 | return super.getPrivateNameSV(node);
|
562 | }
|
563 | }
|
564 | return node.name;
|
565 | }
|
566 | parseLiteral(value, type) {
|
567 | const node = super.parseLiteral(value, type);
|
568 | node.raw = node.extra.raw;
|
569 | delete node.extra;
|
570 | return node;
|
571 | }
|
572 | parseFunctionBody(node, allowExpression, isMethod = false) {
|
573 | super.parseFunctionBody(node, allowExpression, isMethod);
|
574 | node.expression = node.body.type !== "BlockStatement";
|
575 | }
|
576 | parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
|
577 | let funcNode = this.startNode();
|
578 | funcNode.kind = node.kind;
|
579 | funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
|
580 | funcNode.type = "FunctionExpression";
|
581 | delete funcNode.kind;
|
582 | node.value = funcNode;
|
583 | if (type === "ClassPrivateMethod") {
|
584 | node.computed = false;
|
585 | }
|
586 | return this.finishNode(node, "MethodDefinition");
|
587 | }
|
588 | parseClassProperty(...args) {
|
589 | const propertyNode = super.parseClassProperty(...args);
|
590 | {
|
591 | if (!this.getPluginOption("estree", "classFeatures")) {
|
592 | return propertyNode;
|
593 | }
|
594 | }
|
595 | propertyNode.type = "PropertyDefinition";
|
596 | return propertyNode;
|
597 | }
|
598 | parseClassPrivateProperty(...args) {
|
599 | const propertyNode = super.parseClassPrivateProperty(...args);
|
600 | {
|
601 | if (!this.getPluginOption("estree", "classFeatures")) {
|
602 | return propertyNode;
|
603 | }
|
604 | }
|
605 | propertyNode.type = "PropertyDefinition";
|
606 | propertyNode.computed = false;
|
607 | return propertyNode;
|
608 | }
|
609 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
|
610 | const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
|
611 | if (node) {
|
612 | node.type = "Property";
|
613 | if (node.kind === "method") {
|
614 | node.kind = "init";
|
615 | }
|
616 | node.shorthand = false;
|
617 | }
|
618 | return node;
|
619 | }
|
620 | parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
|
621 | const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
|
622 | if (node) {
|
623 | node.kind = "init";
|
624 | node.type = "Property";
|
625 | }
|
626 | return node;
|
627 | }
|
628 | isValidLVal(type, isUnparenthesizedInAssign, binding) {
|
629 | return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
|
630 | }
|
631 | isAssignable(node, isBinding) {
|
632 | if (node != null && this.isObjectProperty(node)) {
|
633 | return this.isAssignable(node.value, isBinding);
|
634 | }
|
635 | return super.isAssignable(node, isBinding);
|
636 | }
|
637 | toAssignable(node, isLHS = false) {
|
638 | if (node != null && this.isObjectProperty(node)) {
|
639 | const {
|
640 | key,
|
641 | value
|
642 | } = node;
|
643 | if (this.isPrivateName(key)) {
|
644 | this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
|
645 | }
|
646 | this.toAssignable(value, isLHS);
|
647 | } else {
|
648 | super.toAssignable(node, isLHS);
|
649 | }
|
650 | }
|
651 | toAssignableObjectExpressionProp(prop, isLast, isLHS) {
|
652 | if (prop.kind === "get" || prop.kind === "set") {
|
653 | this.raise(Errors.PatternHasAccessor, {
|
654 | at: prop.key
|
655 | });
|
656 | } else if (prop.method) {
|
657 | this.raise(Errors.PatternHasMethod, {
|
658 | at: prop.key
|
659 | });
|
660 | } else {
|
661 | super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
|
662 | }
|
663 | }
|
664 | finishCallExpression(unfinished, optional) {
|
665 | const node = super.finishCallExpression(unfinished, optional);
|
666 | if (node.callee.type === "Import") {
|
667 | node.type = "ImportExpression";
|
668 | node.source = node.arguments[0];
|
669 | if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
|
670 | var _node$arguments$;
|
671 | node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
|
672 | }
|
673 | delete node.arguments;
|
674 | delete node.callee;
|
675 | }
|
676 | return node;
|
677 | }
|
678 | toReferencedArguments(node) {
|
679 | if (node.type === "ImportExpression") {
|
680 | return;
|
681 | }
|
682 | super.toReferencedArguments(node);
|
683 | }
|
684 | parseExport(unfinished, decorators) {
|
685 | const exportStartLoc = this.state.lastTokStartLoc;
|
686 | const node = super.parseExport(unfinished, decorators);
|
687 | switch (node.type) {
|
688 | case "ExportAllDeclaration":
|
689 | node.exported = null;
|
690 | break;
|
691 | case "ExportNamedDeclaration":
|
692 | if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
|
693 | node.type = "ExportAllDeclaration";
|
694 | node.exported = node.specifiers[0].exported;
|
695 | delete node.specifiers;
|
696 | }
|
697 | case "ExportDefaultDeclaration":
|
698 | {
|
699 | var _declaration$decorato;
|
700 | const {
|
701 | declaration
|
702 | } = node;
|
703 | if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
|
704 | this.resetStartLocation(node, exportStartLoc);
|
705 | }
|
706 | }
|
707 | break;
|
708 | }
|
709 | return node;
|
710 | }
|
711 | parseSubscript(base, startLoc, noCalls, state) {
|
712 | const node = super.parseSubscript(base, startLoc, noCalls, state);
|
713 | if (state.optionalChainMember) {
|
714 | if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
|
715 | node.type = node.type.substring(8);
|
716 | }
|
717 | if (state.stop) {
|
718 | const chain = this.startNodeAtNode(node);
|
719 | chain.expression = node;
|
720 | return this.finishNode(chain, "ChainExpression");
|
721 | }
|
722 | } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
|
723 | node.optional = false;
|
724 | }
|
725 | return node;
|
726 | }
|
727 | hasPropertyAsPrivateName(node) {
|
728 | if (node.type === "ChainExpression") {
|
729 | node = node.expression;
|
730 | }
|
731 | return super.hasPropertyAsPrivateName(node);
|
732 | }
|
733 | isObjectProperty(node) {
|
734 | return node.type === "Property" && node.kind === "init" && !node.method;
|
735 | }
|
736 | isObjectMethod(node) {
|
737 | return node.method || node.kind === "get" || node.kind === "set";
|
738 | }
|
739 | finishNodeAt(node, type, endLoc) {
|
740 | return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
|
741 | }
|
742 | resetStartLocation(node, startLoc) {
|
743 | super.resetStartLocation(node, startLoc);
|
744 | toESTreeLocation(node);
|
745 | }
|
746 | resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
|
747 | super.resetEndLocation(node, endLoc);
|
748 | toESTreeLocation(node);
|
749 | }
|
750 | };
|
751 | class TokContext {
|
752 | constructor(token, preserveSpace) {
|
753 | this.token = void 0;
|
754 | this.preserveSpace = void 0;
|
755 | this.token = token;
|
756 | this.preserveSpace = !!preserveSpace;
|
757 | }
|
758 | }
|
759 | const types = {
|
760 | brace: new TokContext("{"),
|
761 | j_oTag: new TokContext("<tag"),
|
762 | j_cTag: new TokContext("</tag"),
|
763 | j_expr: new TokContext("<tag>...</tag>", true)
|
764 | };
|
765 | {
|
766 | types.template = new TokContext("`", true);
|
767 | }
|
768 | const beforeExpr = true;
|
769 | const startsExpr = true;
|
770 | const isLoop = true;
|
771 | const isAssign = true;
|
772 | const prefix = true;
|
773 | const postfix = true;
|
774 | class ExportedTokenType {
|
775 | constructor(label, conf = {}) {
|
776 | this.label = void 0;
|
777 | this.keyword = void 0;
|
778 | this.beforeExpr = void 0;
|
779 | this.startsExpr = void 0;
|
780 | this.rightAssociative = void 0;
|
781 | this.isLoop = void 0;
|
782 | this.isAssign = void 0;
|
783 | this.prefix = void 0;
|
784 | this.postfix = void 0;
|
785 | this.binop = void 0;
|
786 | this.label = label;
|
787 | this.keyword = conf.keyword;
|
788 | this.beforeExpr = !!conf.beforeExpr;
|
789 | this.startsExpr = !!conf.startsExpr;
|
790 | this.rightAssociative = !!conf.rightAssociative;
|
791 | this.isLoop = !!conf.isLoop;
|
792 | this.isAssign = !!conf.isAssign;
|
793 | this.prefix = !!conf.prefix;
|
794 | this.postfix = !!conf.postfix;
|
795 | this.binop = conf.binop != null ? conf.binop : null;
|
796 | {
|
797 | this.updateContext = null;
|
798 | }
|
799 | }
|
800 | }
|
801 | const keywords$1 = new Map();
|
802 | function createKeyword(name, options = {}) {
|
803 | options.keyword = name;
|
804 | const token = createToken(name, options);
|
805 | keywords$1.set(name, token);
|
806 | return token;
|
807 | }
|
808 | function createBinop(name, binop) {
|
809 | return createToken(name, {
|
810 | beforeExpr,
|
811 | binop
|
812 | });
|
813 | }
|
814 | let tokenTypeCounter = -1;
|
815 | const tokenTypes = [];
|
816 | const tokenLabels = [];
|
817 | const tokenBinops = [];
|
818 | const tokenBeforeExprs = [];
|
819 | const tokenStartsExprs = [];
|
820 | const tokenPrefixes = [];
|
821 | function createToken(name, options = {}) {
|
822 | var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
|
823 | ++tokenTypeCounter;
|
824 | tokenLabels.push(name);
|
825 | tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
|
826 | tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
|
827 | tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
|
828 | tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
|
829 | tokenTypes.push(new ExportedTokenType(name, options));
|
830 | return tokenTypeCounter;
|
831 | }
|
832 | function createKeywordLike(name, options = {}) {
|
833 | var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
|
834 | ++tokenTypeCounter;
|
835 | keywords$1.set(name, tokenTypeCounter);
|
836 | tokenLabels.push(name);
|
837 | tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
|
838 | tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
|
839 | tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
|
840 | tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
|
841 | tokenTypes.push(new ExportedTokenType("name", options));
|
842 | return tokenTypeCounter;
|
843 | }
|
844 | const tt = {
|
845 | bracketL: createToken("[", {
|
846 | beforeExpr,
|
847 | startsExpr
|
848 | }),
|
849 | bracketHashL: createToken("#[", {
|
850 | beforeExpr,
|
851 | startsExpr
|
852 | }),
|
853 | bracketBarL: createToken("[|", {
|
854 | beforeExpr,
|
855 | startsExpr
|
856 | }),
|
857 | bracketR: createToken("]"),
|
858 | bracketBarR: createToken("|]"),
|
859 | braceL: createToken("{", {
|
860 | beforeExpr,
|
861 | startsExpr
|
862 | }),
|
863 | braceBarL: createToken("{|", {
|
864 | beforeExpr,
|
865 | startsExpr
|
866 | }),
|
867 | braceHashL: createToken("#{", {
|
868 | beforeExpr,
|
869 | startsExpr
|
870 | }),
|
871 | braceR: createToken("}"),
|
872 | braceBarR: createToken("|}"),
|
873 | parenL: createToken("(", {
|
874 | beforeExpr,
|
875 | startsExpr
|
876 | }),
|
877 | parenR: createToken(")"),
|
878 | comma: createToken(",", {
|
879 | beforeExpr
|
880 | }),
|
881 | semi: createToken(";", {
|
882 | beforeExpr
|
883 | }),
|
884 | colon: createToken(":", {
|
885 | beforeExpr
|
886 | }),
|
887 | doubleColon: createToken("::", {
|
888 | beforeExpr
|
889 | }),
|
890 | dot: createToken("."),
|
891 | question: createToken("?", {
|
892 | beforeExpr
|
893 | }),
|
894 | questionDot: createToken("?."),
|
895 | arrow: createToken("=>", {
|
896 | beforeExpr
|
897 | }),
|
898 | template: createToken("template"),
|
899 | ellipsis: createToken("...", {
|
900 | beforeExpr
|
901 | }),
|
902 | backQuote: createToken("`", {
|
903 | startsExpr
|
904 | }),
|
905 | dollarBraceL: createToken("${", {
|
906 | beforeExpr,
|
907 | startsExpr
|
908 | }),
|
909 | templateTail: createToken("...`", {
|
910 | startsExpr
|
911 | }),
|
912 | templateNonTail: createToken("...${", {
|
913 | beforeExpr,
|
914 | startsExpr
|
915 | }),
|
916 | at: createToken("@"),
|
917 | hash: createToken("#", {
|
918 | startsExpr
|
919 | }),
|
920 | interpreterDirective: createToken("#!..."),
|
921 | eq: createToken("=", {
|
922 | beforeExpr,
|
923 | isAssign
|
924 | }),
|
925 | assign: createToken("_=", {
|
926 | beforeExpr,
|
927 | isAssign
|
928 | }),
|
929 | slashAssign: createToken("_=", {
|
930 | beforeExpr,
|
931 | isAssign
|
932 | }),
|
933 | xorAssign: createToken("_=", {
|
934 | beforeExpr,
|
935 | isAssign
|
936 | }),
|
937 | moduloAssign: createToken("_=", {
|
938 | beforeExpr,
|
939 | isAssign
|
940 | }),
|
941 | incDec: createToken("++/--", {
|
942 | prefix,
|
943 | postfix,
|
944 | startsExpr
|
945 | }),
|
946 | bang: createToken("!", {
|
947 | beforeExpr,
|
948 | prefix,
|
949 | startsExpr
|
950 | }),
|
951 | tilde: createToken("~", {
|
952 | beforeExpr,
|
953 | prefix,
|
954 | startsExpr
|
955 | }),
|
956 | doubleCaret: createToken("^^", {
|
957 | startsExpr
|
958 | }),
|
959 | doubleAt: createToken("@@", {
|
960 | startsExpr
|
961 | }),
|
962 | pipeline: createBinop("|>", 0),
|
963 | nullishCoalescing: createBinop("??", 1),
|
964 | logicalOR: createBinop("||", 1),
|
965 | logicalAND: createBinop("&&", 2),
|
966 | bitwiseOR: createBinop("|", 3),
|
967 | bitwiseXOR: createBinop("^", 4),
|
968 | bitwiseAND: createBinop("&", 5),
|
969 | equality: createBinop("==/!=/===/!==", 6),
|
970 | lt: createBinop("</>/<=/>=", 7),
|
971 | gt: createBinop("</>/<=/>=", 7),
|
972 | relational: createBinop("</>/<=/>=", 7),
|
973 | bitShift: createBinop("<</>>/>>>", 8),
|
974 | bitShiftL: createBinop("<</>>/>>>", 8),
|
975 | bitShiftR: createBinop("<</>>/>>>", 8),
|
976 | plusMin: createToken("+/-", {
|
977 | beforeExpr,
|
978 | binop: 9,
|
979 | prefix,
|
980 | startsExpr
|
981 | }),
|
982 | modulo: createToken("%", {
|
983 | binop: 10,
|
984 | startsExpr
|
985 | }),
|
986 | star: createToken("*", {
|
987 | binop: 10
|
988 | }),
|
989 | slash: createBinop("/", 10),
|
990 | exponent: createToken("**", {
|
991 | beforeExpr,
|
992 | binop: 11,
|
993 | rightAssociative: true
|
994 | }),
|
995 | _in: createKeyword("in", {
|
996 | beforeExpr,
|
997 | binop: 7
|
998 | }),
|
999 | _instanceof: createKeyword("instanceof", {
|
1000 | beforeExpr,
|
1001 | binop: 7
|
1002 | }),
|
1003 | _break: createKeyword("break"),
|
1004 | _case: createKeyword("case", {
|
1005 | beforeExpr
|
1006 | }),
|
1007 | _catch: createKeyword("catch"),
|
1008 | _continue: createKeyword("continue"),
|
1009 | _debugger: createKeyword("debugger"),
|
1010 | _default: createKeyword("default", {
|
1011 | beforeExpr
|
1012 | }),
|
1013 | _else: createKeyword("else", {
|
1014 | beforeExpr
|
1015 | }),
|
1016 | _finally: createKeyword("finally"),
|
1017 | _function: createKeyword("function", {
|
1018 | startsExpr
|
1019 | }),
|
1020 | _if: createKeyword("if"),
|
1021 | _return: createKeyword("return", {
|
1022 | beforeExpr
|
1023 | }),
|
1024 | _switch: createKeyword("switch"),
|
1025 | _throw: createKeyword("throw", {
|
1026 | beforeExpr,
|
1027 | prefix,
|
1028 | startsExpr
|
1029 | }),
|
1030 | _try: createKeyword("try"),
|
1031 | _var: createKeyword("var"),
|
1032 | _const: createKeyword("const"),
|
1033 | _with: createKeyword("with"),
|
1034 | _new: createKeyword("new", {
|
1035 | beforeExpr,
|
1036 | startsExpr
|
1037 | }),
|
1038 | _this: createKeyword("this", {
|
1039 | startsExpr
|
1040 | }),
|
1041 | _super: createKeyword("super", {
|
1042 | startsExpr
|
1043 | }),
|
1044 | _class: createKeyword("class", {
|
1045 | startsExpr
|
1046 | }),
|
1047 | _extends: createKeyword("extends", {
|
1048 | beforeExpr
|
1049 | }),
|
1050 | _export: createKeyword("export"),
|
1051 | _import: createKeyword("import", {
|
1052 | startsExpr
|
1053 | }),
|
1054 | _null: createKeyword("null", {
|
1055 | startsExpr
|
1056 | }),
|
1057 | _true: createKeyword("true", {
|
1058 | startsExpr
|
1059 | }),
|
1060 | _false: createKeyword("false", {
|
1061 | startsExpr
|
1062 | }),
|
1063 | _typeof: createKeyword("typeof", {
|
1064 | beforeExpr,
|
1065 | prefix,
|
1066 | startsExpr
|
1067 | }),
|
1068 | _void: createKeyword("void", {
|
1069 | beforeExpr,
|
1070 | prefix,
|
1071 | startsExpr
|
1072 | }),
|
1073 | _delete: createKeyword("delete", {
|
1074 | beforeExpr,
|
1075 | prefix,
|
1076 | startsExpr
|
1077 | }),
|
1078 | _do: createKeyword("do", {
|
1079 | isLoop,
|
1080 | beforeExpr
|
1081 | }),
|
1082 | _for: createKeyword("for", {
|
1083 | isLoop
|
1084 | }),
|
1085 | _while: createKeyword("while", {
|
1086 | isLoop
|
1087 | }),
|
1088 | _as: createKeywordLike("as", {
|
1089 | startsExpr
|
1090 | }),
|
1091 | _assert: createKeywordLike("assert", {
|
1092 | startsExpr
|
1093 | }),
|
1094 | _async: createKeywordLike("async", {
|
1095 | startsExpr
|
1096 | }),
|
1097 | _await: createKeywordLike("await", {
|
1098 | startsExpr
|
1099 | }),
|
1100 | _from: createKeywordLike("from", {
|
1101 | startsExpr
|
1102 | }),
|
1103 | _get: createKeywordLike("get", {
|
1104 | startsExpr
|
1105 | }),
|
1106 | _let: createKeywordLike("let", {
|
1107 | startsExpr
|
1108 | }),
|
1109 | _meta: createKeywordLike("meta", {
|
1110 | startsExpr
|
1111 | }),
|
1112 | _of: createKeywordLike("of", {
|
1113 | startsExpr
|
1114 | }),
|
1115 | _sent: createKeywordLike("sent", {
|
1116 | startsExpr
|
1117 | }),
|
1118 | _set: createKeywordLike("set", {
|
1119 | startsExpr
|
1120 | }),
|
1121 | _static: createKeywordLike("static", {
|
1122 | startsExpr
|
1123 | }),
|
1124 | _using: createKeywordLike("using", {
|
1125 | startsExpr
|
1126 | }),
|
1127 | _yield: createKeywordLike("yield", {
|
1128 | startsExpr
|
1129 | }),
|
1130 | _asserts: createKeywordLike("asserts", {
|
1131 | startsExpr
|
1132 | }),
|
1133 | _checks: createKeywordLike("checks", {
|
1134 | startsExpr
|
1135 | }),
|
1136 | _exports: createKeywordLike("exports", {
|
1137 | startsExpr
|
1138 | }),
|
1139 | _global: createKeywordLike("global", {
|
1140 | startsExpr
|
1141 | }),
|
1142 | _implements: createKeywordLike("implements", {
|
1143 | startsExpr
|
1144 | }),
|
1145 | _intrinsic: createKeywordLike("intrinsic", {
|
1146 | startsExpr
|
1147 | }),
|
1148 | _infer: createKeywordLike("infer", {
|
1149 | startsExpr
|
1150 | }),
|
1151 | _is: createKeywordLike("is", {
|
1152 | startsExpr
|
1153 | }),
|
1154 | _mixins: createKeywordLike("mixins", {
|
1155 | startsExpr
|
1156 | }),
|
1157 | _proto: createKeywordLike("proto", {
|
1158 | startsExpr
|
1159 | }),
|
1160 | _require: createKeywordLike("require", {
|
1161 | startsExpr
|
1162 | }),
|
1163 | _satisfies: createKeywordLike("satisfies", {
|
1164 | startsExpr
|
1165 | }),
|
1166 | _keyof: createKeywordLike("keyof", {
|
1167 | startsExpr
|
1168 | }),
|
1169 | _readonly: createKeywordLike("readonly", {
|
1170 | startsExpr
|
1171 | }),
|
1172 | _unique: createKeywordLike("unique", {
|
1173 | startsExpr
|
1174 | }),
|
1175 | _abstract: createKeywordLike("abstract", {
|
1176 | startsExpr
|
1177 | }),
|
1178 | _declare: createKeywordLike("declare", {
|
1179 | startsExpr
|
1180 | }),
|
1181 | _enum: createKeywordLike("enum", {
|
1182 | startsExpr
|
1183 | }),
|
1184 | _module: createKeywordLike("module", {
|
1185 | startsExpr
|
1186 | }),
|
1187 | _namespace: createKeywordLike("namespace", {
|
1188 | startsExpr
|
1189 | }),
|
1190 | _interface: createKeywordLike("interface", {
|
1191 | startsExpr
|
1192 | }),
|
1193 | _type: createKeywordLike("type", {
|
1194 | startsExpr
|
1195 | }),
|
1196 | _opaque: createKeywordLike("opaque", {
|
1197 | startsExpr
|
1198 | }),
|
1199 | name: createToken("name", {
|
1200 | startsExpr
|
1201 | }),
|
1202 | string: createToken("string", {
|
1203 | startsExpr
|
1204 | }),
|
1205 | num: createToken("num", {
|
1206 | startsExpr
|
1207 | }),
|
1208 | bigint: createToken("bigint", {
|
1209 | startsExpr
|
1210 | }),
|
1211 | decimal: createToken("decimal", {
|
1212 | startsExpr
|
1213 | }),
|
1214 | regexp: createToken("regexp", {
|
1215 | startsExpr
|
1216 | }),
|
1217 | privateName: createToken("#name", {
|
1218 | startsExpr
|
1219 | }),
|
1220 | eof: createToken("eof"),
|
1221 | jsxName: createToken("jsxName"),
|
1222 | jsxText: createToken("jsxText", {
|
1223 | beforeExpr: true
|
1224 | }),
|
1225 | jsxTagStart: createToken("jsxTagStart", {
|
1226 | startsExpr: true
|
1227 | }),
|
1228 | jsxTagEnd: createToken("jsxTagEnd"),
|
1229 | placeholder: createToken("%%", {
|
1230 | startsExpr: true
|
1231 | })
|
1232 | };
|
1233 | function tokenIsIdentifier(token) {
|
1234 | return token >= 93 && token <= 130;
|
1235 | }
|
1236 | function tokenKeywordOrIdentifierIsKeyword(token) {
|
1237 | return token <= 92;
|
1238 | }
|
1239 | function tokenIsKeywordOrIdentifier(token) {
|
1240 | return token >= 58 && token <= 130;
|
1241 | }
|
1242 | function tokenIsLiteralPropertyName(token) {
|
1243 | return token >= 58 && token <= 134;
|
1244 | }
|
1245 | function tokenComesBeforeExpression(token) {
|
1246 | return tokenBeforeExprs[token];
|
1247 | }
|
1248 | function tokenCanStartExpression(token) {
|
1249 | return tokenStartsExprs[token];
|
1250 | }
|
1251 | function tokenIsAssignment(token) {
|
1252 | return token >= 29 && token <= 33;
|
1253 | }
|
1254 | function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
|
1255 | return token >= 127 && token <= 129;
|
1256 | }
|
1257 | function tokenIsLoop(token) {
|
1258 | return token >= 90 && token <= 92;
|
1259 | }
|
1260 | function tokenIsKeyword(token) {
|
1261 | return token >= 58 && token <= 92;
|
1262 | }
|
1263 | function tokenIsOperator(token) {
|
1264 | return token >= 39 && token <= 59;
|
1265 | }
|
1266 | function tokenIsPostfix(token) {
|
1267 | return token === 34;
|
1268 | }
|
1269 | function tokenIsPrefix(token) {
|
1270 | return tokenPrefixes[token];
|
1271 | }
|
1272 | function tokenIsTSTypeOperator(token) {
|
1273 | return token >= 119 && token <= 121;
|
1274 | }
|
1275 | function tokenIsTSDeclarationStart(token) {
|
1276 | return token >= 122 && token <= 128;
|
1277 | }
|
1278 | function tokenLabelName(token) {
|
1279 | return tokenLabels[token];
|
1280 | }
|
1281 | function tokenOperatorPrecedence(token) {
|
1282 | return tokenBinops[token];
|
1283 | }
|
1284 | function tokenIsRightAssociative(token) {
|
1285 | return token === 57;
|
1286 | }
|
1287 | function tokenIsTemplate(token) {
|
1288 | return token >= 24 && token <= 25;
|
1289 | }
|
1290 | function getExportedToken(token) {
|
1291 | return tokenTypes[token];
|
1292 | }
|
1293 | {
|
1294 | tokenTypes[8].updateContext = context => {
|
1295 | context.pop();
|
1296 | };
|
1297 | tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
|
1298 | context.push(types.brace);
|
1299 | };
|
1300 | tokenTypes[22].updateContext = context => {
|
1301 | if (context[context.length - 1] === types.template) {
|
1302 | context.pop();
|
1303 | } else {
|
1304 | context.push(types.template);
|
1305 | }
|
1306 | };
|
1307 | tokenTypes[140].updateContext = context => {
|
1308 | context.push(types.j_expr, types.j_oTag);
|
1309 | };
|
1310 | }
|
1311 | 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";
|
1312 | 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\u0cf3\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-\u0ece\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";
|
1313 | const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
1314 | const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
1315 | nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
|
1316 | 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, 20, 1, 64, 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, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 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, 16, 0, 30, 2, 3, 0, 15, 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, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 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, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
|
1317 | 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, 81, 2, 71, 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, 3, 0, 158, 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, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 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, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
|
1318 | function isInAstralSet(code, set) {
|
1319 | let pos = 0x10000;
|
1320 | for (let i = 0, length = set.length; i < length; i += 2) {
|
1321 | pos += set[i];
|
1322 | if (pos > code) return false;
|
1323 | pos += set[i + 1];
|
1324 | if (pos >= code) return true;
|
1325 | }
|
1326 | return false;
|
1327 | }
|
1328 | function isIdentifierStart(code) {
|
1329 | if (code < 65) return code === 36;
|
1330 | if (code <= 90) return true;
|
1331 | if (code < 97) return code === 95;
|
1332 | if (code <= 122) return true;
|
1333 | if (code <= 0xffff) {
|
1334 | return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
|
1335 | }
|
1336 | return isInAstralSet(code, astralIdentifierStartCodes);
|
1337 | }
|
1338 | function isIdentifierChar(code) {
|
1339 | if (code < 48) return code === 36;
|
1340 | if (code < 58) return true;
|
1341 | if (code < 65) return false;
|
1342 | if (code <= 90) return true;
|
1343 | if (code < 97) return code === 95;
|
1344 | if (code <= 122) return true;
|
1345 | if (code <= 0xffff) {
|
1346 | return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
|
1347 | }
|
1348 | return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
|
1349 | }
|
1350 | const reservedWords = {
|
1351 | 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"],
|
1352 | strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
|
1353 | strictBind: ["eval", "arguments"]
|
1354 | };
|
1355 | const keywords = new Set(reservedWords.keyword);
|
1356 | const reservedWordsStrictSet = new Set(reservedWords.strict);
|
1357 | const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
|
1358 | function isReservedWord(word, inModule) {
|
1359 | return inModule && word === "await" || word === "enum";
|
1360 | }
|
1361 | function isStrictReservedWord(word, inModule) {
|
1362 | return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
|
1363 | }
|
1364 | function isStrictBindOnlyReservedWord(word) {
|
1365 | return reservedWordsStrictBindSet.has(word);
|
1366 | }
|
1367 | function isStrictBindReservedWord(word, inModule) {
|
1368 | return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
|
1369 | }
|
1370 | function isKeyword(word) {
|
1371 | return keywords.has(word);
|
1372 | }
|
1373 | function isIteratorStart(current, next, next2) {
|
1374 | return current === 64 && next === 64 && isIdentifierStart(next2);
|
1375 | }
|
1376 | 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"]);
|
1377 | function canBeReservedWord(word) {
|
1378 | return reservedWordLikeSet.has(word);
|
1379 | }
|
1380 | const SCOPE_OTHER = 0b000000000,
|
1381 | SCOPE_PROGRAM = 0b000000001,
|
1382 | SCOPE_FUNCTION = 0b000000010,
|
1383 | SCOPE_ARROW = 0b000000100,
|
1384 | SCOPE_SIMPLE_CATCH = 0b000001000,
|
1385 | SCOPE_SUPER = 0b000010000,
|
1386 | SCOPE_DIRECT_SUPER = 0b000100000,
|
1387 | SCOPE_CLASS = 0b001000000,
|
1388 | SCOPE_STATIC_BLOCK = 0b010000000,
|
1389 | SCOPE_TS_MODULE = 0b100000000,
|
1390 | SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
|
1391 | const BIND_KIND_VALUE = 0b0000000000001,
|
1392 | BIND_KIND_TYPE = 0b0000000000010,
|
1393 | BIND_SCOPE_VAR = 0b0000000000100,
|
1394 | BIND_SCOPE_LEXICAL = 0b0000000001000,
|
1395 | BIND_SCOPE_FUNCTION = 0b0000000010000,
|
1396 | BIND_FLAGS_NONE = 0b00000001000000,
|
1397 | BIND_FLAGS_CLASS = 0b00000010000000,
|
1398 | BIND_FLAGS_TS_ENUM = 0b00000100000000,
|
1399 | BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
|
1400 | BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
|
1401 | BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
|
1402 | BIND_FLAGS_TS_IMPORT = 0b01000000000000,
|
1403 | BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
|
1404 | const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
|
1405 | BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
|
1406 | BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
|
1407 | BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
|
1408 | BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
|
1409 | BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
|
1410 | BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
|
1411 | BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
|
1412 | BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
|
1413 | BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
|
1414 | BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
|
1415 | BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
|
1416 | BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
|
1417 | BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
|
1418 | BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
|
1419 | const CLASS_ELEMENT_FLAG_STATIC = 0b100,
|
1420 | CLASS_ELEMENT_KIND_GETTER = 0b010,
|
1421 | CLASS_ELEMENT_KIND_SETTER = 0b001,
|
1422 | CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
|
1423 | const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
|
1424 | CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
|
1425 | CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
|
1426 | CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
|
1427 | CLASS_ELEMENT_OTHER = 0;
|
1428 | class Scope {
|
1429 | constructor(flags) {
|
1430 | this.var = new Set();
|
1431 | this.lexical = new Set();
|
1432 | this.functions = new Set();
|
1433 | this.flags = flags;
|
1434 | }
|
1435 | }
|
1436 | class ScopeHandler {
|
1437 | constructor(parser, inModule) {
|
1438 | this.parser = void 0;
|
1439 | this.scopeStack = [];
|
1440 | this.inModule = void 0;
|
1441 | this.undefinedExports = new Map();
|
1442 | this.parser = parser;
|
1443 | this.inModule = inModule;
|
1444 | }
|
1445 | get inTopLevel() {
|
1446 | return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
|
1447 | }
|
1448 | get inFunction() {
|
1449 | return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
|
1450 | }
|
1451 | get allowSuper() {
|
1452 | return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
|
1453 | }
|
1454 | get allowDirectSuper() {
|
1455 | return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
|
1456 | }
|
1457 | get inClass() {
|
1458 | return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
|
1459 | }
|
1460 | get inClassAndNotInNonArrowFunction() {
|
1461 | const flags = this.currentThisScopeFlags();
|
1462 | return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
|
1463 | }
|
1464 | get inStaticBlock() {
|
1465 | for (let i = this.scopeStack.length - 1;; i--) {
|
1466 | const {
|
1467 | flags
|
1468 | } = this.scopeStack[i];
|
1469 | if (flags & SCOPE_STATIC_BLOCK) {
|
1470 | return true;
|
1471 | }
|
1472 | if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
|
1473 | return false;
|
1474 | }
|
1475 | }
|
1476 | }
|
1477 | get inNonArrowFunction() {
|
1478 | return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
|
1479 | }
|
1480 | get treatFunctionsAsVar() {
|
1481 | return this.treatFunctionsAsVarInScope(this.currentScope());
|
1482 | }
|
1483 | createScope(flags) {
|
1484 | return new Scope(flags);
|
1485 | }
|
1486 | enter(flags) {
|
1487 | this.scopeStack.push(this.createScope(flags));
|
1488 | }
|
1489 | exit() {
|
1490 | const scope = this.scopeStack.pop();
|
1491 | return scope.flags;
|
1492 | }
|
1493 | treatFunctionsAsVarInScope(scope) {
|
1494 | return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
|
1495 | }
|
1496 | declareName(name, bindingType, loc) {
|
1497 | let scope = this.currentScope();
|
1498 | if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
|
1499 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
1500 | if (bindingType & BIND_SCOPE_FUNCTION) {
|
1501 | scope.functions.add(name);
|
1502 | } else {
|
1503 | scope.lexical.add(name);
|
1504 | }
|
1505 | if (bindingType & BIND_SCOPE_LEXICAL) {
|
1506 | this.maybeExportDefined(scope, name);
|
1507 | }
|
1508 | } else if (bindingType & BIND_SCOPE_VAR) {
|
1509 | for (let i = this.scopeStack.length - 1; i >= 0; --i) {
|
1510 | scope = this.scopeStack[i];
|
1511 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
1512 | scope.var.add(name);
|
1513 | this.maybeExportDefined(scope, name);
|
1514 | if (scope.flags & SCOPE_VAR) break;
|
1515 | }
|
1516 | }
|
1517 | if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
|
1518 | this.undefinedExports.delete(name);
|
1519 | }
|
1520 | }
|
1521 | maybeExportDefined(scope, name) {
|
1522 | if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
|
1523 | this.undefinedExports.delete(name);
|
1524 | }
|
1525 | }
|
1526 | checkRedeclarationInScope(scope, name, bindingType, loc) {
|
1527 | if (this.isRedeclaredInScope(scope, name, bindingType)) {
|
1528 | this.parser.raise(Errors.VarRedeclaration, {
|
1529 | at: loc,
|
1530 | identifierName: name
|
1531 | });
|
1532 | }
|
1533 | }
|
1534 | isRedeclaredInScope(scope, name, bindingType) {
|
1535 | if (!(bindingType & BIND_KIND_VALUE)) return false;
|
1536 | if (bindingType & BIND_SCOPE_LEXICAL) {
|
1537 | return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
|
1538 | }
|
1539 | if (bindingType & BIND_SCOPE_FUNCTION) {
|
1540 | return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
|
1541 | }
|
1542 | return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
|
1543 | }
|
1544 | checkLocalExport(id) {
|
1545 | const {
|
1546 | name
|
1547 | } = id;
|
1548 | const topLevelScope = this.scopeStack[0];
|
1549 | if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
|
1550 | this.undefinedExports.set(name, id.loc.start);
|
1551 | }
|
1552 | }
|
1553 | currentScope() {
|
1554 | return this.scopeStack[this.scopeStack.length - 1];
|
1555 | }
|
1556 | currentVarScopeFlags() {
|
1557 | for (let i = this.scopeStack.length - 1;; i--) {
|
1558 | const {
|
1559 | flags
|
1560 | } = this.scopeStack[i];
|
1561 | if (flags & SCOPE_VAR) {
|
1562 | return flags;
|
1563 | }
|
1564 | }
|
1565 | }
|
1566 | currentThisScopeFlags() {
|
1567 | for (let i = this.scopeStack.length - 1;; i--) {
|
1568 | const {
|
1569 | flags
|
1570 | } = this.scopeStack[i];
|
1571 | if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
|
1572 | return flags;
|
1573 | }
|
1574 | }
|
1575 | }
|
1576 | }
|
1577 | class FlowScope extends Scope {
|
1578 | constructor(...args) {
|
1579 | super(...args);
|
1580 | this.declareFunctions = new Set();
|
1581 | }
|
1582 | }
|
1583 | class FlowScopeHandler extends ScopeHandler {
|
1584 | createScope(flags) {
|
1585 | return new FlowScope(flags);
|
1586 | }
|
1587 | declareName(name, bindingType, loc) {
|
1588 | const scope = this.currentScope();
|
1589 | if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
|
1590 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
1591 | this.maybeExportDefined(scope, name);
|
1592 | scope.declareFunctions.add(name);
|
1593 | return;
|
1594 | }
|
1595 | super.declareName(name, bindingType, loc);
|
1596 | }
|
1597 | isRedeclaredInScope(scope, name, bindingType) {
|
1598 | if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
|
1599 | if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
|
1600 | return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
|
1601 | }
|
1602 | return false;
|
1603 | }
|
1604 | checkLocalExport(id) {
|
1605 | if (!this.scopeStack[0].declareFunctions.has(id.name)) {
|
1606 | super.checkLocalExport(id);
|
1607 | }
|
1608 | }
|
1609 | }
|
1610 | class BaseParser {
|
1611 | constructor() {
|
1612 | this.sawUnambiguousESM = false;
|
1613 | this.ambiguousScriptDifferentAst = false;
|
1614 | }
|
1615 | hasPlugin(pluginConfig) {
|
1616 | if (typeof pluginConfig === "string") {
|
1617 | return this.plugins.has(pluginConfig);
|
1618 | } else {
|
1619 | const [pluginName, pluginOptions] = pluginConfig;
|
1620 | if (!this.hasPlugin(pluginName)) {
|
1621 | return false;
|
1622 | }
|
1623 | const actualOptions = this.plugins.get(pluginName);
|
1624 | for (const key of Object.keys(pluginOptions)) {
|
1625 | if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
|
1626 | return false;
|
1627 | }
|
1628 | }
|
1629 | return true;
|
1630 | }
|
1631 | }
|
1632 | getPluginOption(plugin, name) {
|
1633 | var _this$plugins$get;
|
1634 | return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
|
1635 | }
|
1636 | }
|
1637 | function setTrailingComments(node, comments) {
|
1638 | if (node.trailingComments === undefined) {
|
1639 | node.trailingComments = comments;
|
1640 | } else {
|
1641 | node.trailingComments.unshift(...comments);
|
1642 | }
|
1643 | }
|
1644 | function setLeadingComments(node, comments) {
|
1645 | if (node.leadingComments === undefined) {
|
1646 | node.leadingComments = comments;
|
1647 | } else {
|
1648 | node.leadingComments.unshift(...comments);
|
1649 | }
|
1650 | }
|
1651 | function setInnerComments(node, comments) {
|
1652 | if (node.innerComments === undefined) {
|
1653 | node.innerComments = comments;
|
1654 | } else {
|
1655 | node.innerComments.unshift(...comments);
|
1656 | }
|
1657 | }
|
1658 | function adjustInnerComments(node, elements, commentWS) {
|
1659 | let lastElement = null;
|
1660 | let i = elements.length;
|
1661 | while (lastElement === null && i > 0) {
|
1662 | lastElement = elements[--i];
|
1663 | }
|
1664 | if (lastElement === null || lastElement.start > commentWS.start) {
|
1665 | setInnerComments(node, commentWS.comments);
|
1666 | } else {
|
1667 | setTrailingComments(lastElement, commentWS.comments);
|
1668 | }
|
1669 | }
|
1670 | class CommentsParser extends BaseParser {
|
1671 | addComment(comment) {
|
1672 | if (this.filename) comment.loc.filename = this.filename;
|
1673 | this.state.comments.push(comment);
|
1674 | }
|
1675 | processComment(node) {
|
1676 | const {
|
1677 | commentStack
|
1678 | } = this.state;
|
1679 | const commentStackLength = commentStack.length;
|
1680 | if (commentStackLength === 0) return;
|
1681 | let i = commentStackLength - 1;
|
1682 | const lastCommentWS = commentStack[i];
|
1683 | if (lastCommentWS.start === node.end) {
|
1684 | lastCommentWS.leadingNode = node;
|
1685 | i--;
|
1686 | }
|
1687 | const {
|
1688 | start: nodeStart
|
1689 | } = node;
|
1690 | for (; i >= 0; i--) {
|
1691 | const commentWS = commentStack[i];
|
1692 | const commentEnd = commentWS.end;
|
1693 | if (commentEnd > nodeStart) {
|
1694 | commentWS.containingNode = node;
|
1695 | this.finalizeComment(commentWS);
|
1696 | commentStack.splice(i, 1);
|
1697 | } else {
|
1698 | if (commentEnd === nodeStart) {
|
1699 | commentWS.trailingNode = node;
|
1700 | }
|
1701 | break;
|
1702 | }
|
1703 | }
|
1704 | }
|
1705 | finalizeComment(commentWS) {
|
1706 | const {
|
1707 | comments
|
1708 | } = commentWS;
|
1709 | if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
|
1710 | if (commentWS.leadingNode !== null) {
|
1711 | setTrailingComments(commentWS.leadingNode, comments);
|
1712 | }
|
1713 | if (commentWS.trailingNode !== null) {
|
1714 | setLeadingComments(commentWS.trailingNode, comments);
|
1715 | }
|
1716 | } else {
|
1717 | const {
|
1718 | containingNode: node,
|
1719 | start: commentStart
|
1720 | } = commentWS;
|
1721 | if (this.input.charCodeAt(commentStart - 1) === 44) {
|
1722 | switch (node.type) {
|
1723 | case "ObjectExpression":
|
1724 | case "ObjectPattern":
|
1725 | case "RecordExpression":
|
1726 | adjustInnerComments(node, node.properties, commentWS);
|
1727 | break;
|
1728 | case "CallExpression":
|
1729 | case "OptionalCallExpression":
|
1730 | adjustInnerComments(node, node.arguments, commentWS);
|
1731 | break;
|
1732 | case "FunctionDeclaration":
|
1733 | case "FunctionExpression":
|
1734 | case "ArrowFunctionExpression":
|
1735 | case "ObjectMethod":
|
1736 | case "ClassMethod":
|
1737 | case "ClassPrivateMethod":
|
1738 | adjustInnerComments(node, node.params, commentWS);
|
1739 | break;
|
1740 | case "ArrayExpression":
|
1741 | case "ArrayPattern":
|
1742 | case "TupleExpression":
|
1743 | adjustInnerComments(node, node.elements, commentWS);
|
1744 | break;
|
1745 | case "ExportNamedDeclaration":
|
1746 | case "ImportDeclaration":
|
1747 | adjustInnerComments(node, node.specifiers, commentWS);
|
1748 | break;
|
1749 | default:
|
1750 | {
|
1751 | setInnerComments(node, comments);
|
1752 | }
|
1753 | }
|
1754 | } else {
|
1755 | setInnerComments(node, comments);
|
1756 | }
|
1757 | }
|
1758 | }
|
1759 | finalizeRemainingComments() {
|
1760 | const {
|
1761 | commentStack
|
1762 | } = this.state;
|
1763 | for (let i = commentStack.length - 1; i >= 0; i--) {
|
1764 | this.finalizeComment(commentStack[i]);
|
1765 | }
|
1766 | this.state.commentStack = [];
|
1767 | }
|
1768 | resetPreviousNodeTrailingComments(node) {
|
1769 | const {
|
1770 | commentStack
|
1771 | } = this.state;
|
1772 | const {
|
1773 | length
|
1774 | } = commentStack;
|
1775 | if (length === 0) return;
|
1776 | const commentWS = commentStack[length - 1];
|
1777 | if (commentWS.leadingNode === node) {
|
1778 | commentWS.leadingNode = null;
|
1779 | }
|
1780 | }
|
1781 | resetPreviousIdentifierLeadingComments(node) {
|
1782 | const {
|
1783 | commentStack
|
1784 | } = this.state;
|
1785 | const {
|
1786 | length
|
1787 | } = commentStack;
|
1788 | if (length === 0) return;
|
1789 | if (commentStack[length - 1].trailingNode === node) {
|
1790 | commentStack[length - 1].trailingNode = null;
|
1791 | } else if (length >= 2 && commentStack[length - 2].trailingNode === node) {
|
1792 | commentStack[length - 2].trailingNode = null;
|
1793 | }
|
1794 | }
|
1795 | takeSurroundingComments(node, start, end) {
|
1796 | const {
|
1797 | commentStack
|
1798 | } = this.state;
|
1799 | const commentStackLength = commentStack.length;
|
1800 | if (commentStackLength === 0) return;
|
1801 | let i = commentStackLength - 1;
|
1802 | for (; i >= 0; i--) {
|
1803 | const commentWS = commentStack[i];
|
1804 | const commentEnd = commentWS.end;
|
1805 | const commentStart = commentWS.start;
|
1806 | if (commentStart === end) {
|
1807 | commentWS.leadingNode = node;
|
1808 | } else if (commentEnd === start) {
|
1809 | commentWS.trailingNode = node;
|
1810 | } else if (commentEnd < start) {
|
1811 | break;
|
1812 | }
|
1813 | }
|
1814 | }
|
1815 | }
|
1816 | const lineBreak = /\r\n?|[\n\u2028\u2029]/;
|
1817 | const lineBreakG = new RegExp(lineBreak.source, "g");
|
1818 | function isNewLine(code) {
|
1819 | switch (code) {
|
1820 | case 10:
|
1821 | case 13:
|
1822 | case 8232:
|
1823 | case 8233:
|
1824 | return true;
|
1825 | default:
|
1826 | return false;
|
1827 | }
|
1828 | }
|
1829 | const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
1830 | const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
|
1831 | const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
|
1832 | function isWhitespace(code) {
|
1833 | switch (code) {
|
1834 | case 0x0009:
|
1835 | case 0x000b:
|
1836 | case 0x000c:
|
1837 | case 32:
|
1838 | case 160:
|
1839 | case 5760:
|
1840 | case 0x2000:
|
1841 | case 0x2001:
|
1842 | case 0x2002:
|
1843 | case 0x2003:
|
1844 | case 0x2004:
|
1845 | case 0x2005:
|
1846 | case 0x2006:
|
1847 | case 0x2007:
|
1848 | case 0x2008:
|
1849 | case 0x2009:
|
1850 | case 0x200a:
|
1851 | case 0x202f:
|
1852 | case 0x205f:
|
1853 | case 0x3000:
|
1854 | case 0xfeff:
|
1855 | return true;
|
1856 | default:
|
1857 | return false;
|
1858 | }
|
1859 | }
|
1860 | class State {
|
1861 | constructor() {
|
1862 | this.strict = void 0;
|
1863 | this.curLine = void 0;
|
1864 | this.lineStart = void 0;
|
1865 | this.startLoc = void 0;
|
1866 | this.endLoc = void 0;
|
1867 | this.errors = [];
|
1868 | this.potentialArrowAt = -1;
|
1869 | this.noArrowAt = [];
|
1870 | this.noArrowParamsConversionAt = [];
|
1871 | this.maybeInArrowParameters = false;
|
1872 | this.inType = false;
|
1873 | this.noAnonFunctionType = false;
|
1874 | this.hasFlowComment = false;
|
1875 | this.isAmbientContext = false;
|
1876 | this.inAbstractClass = false;
|
1877 | this.inDisallowConditionalTypesContext = false;
|
1878 | this.topicContext = {
|
1879 | maxNumOfResolvableTopics: 0,
|
1880 | maxTopicIndex: null
|
1881 | };
|
1882 | this.soloAwait = false;
|
1883 | this.inFSharpPipelineDirectBody = false;
|
1884 | this.labels = [];
|
1885 | this.comments = [];
|
1886 | this.commentStack = [];
|
1887 | this.pos = 0;
|
1888 | this.type = 137;
|
1889 | this.value = null;
|
1890 | this.start = 0;
|
1891 | this.end = 0;
|
1892 | this.lastTokEndLoc = null;
|
1893 | this.lastTokStartLoc = null;
|
1894 | this.lastTokStart = 0;
|
1895 | this.context = [types.brace];
|
1896 | this.canStartJSXElement = true;
|
1897 | this.containsEsc = false;
|
1898 | this.firstInvalidTemplateEscapePos = null;
|
1899 | this.strictErrors = new Map();
|
1900 | this.tokensLength = 0;
|
1901 | }
|
1902 | init({
|
1903 | strictMode,
|
1904 | sourceType,
|
1905 | startLine,
|
1906 | startColumn
|
1907 | }) {
|
1908 | this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
|
1909 | this.curLine = startLine;
|
1910 | this.lineStart = -startColumn;
|
1911 | this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
|
1912 | }
|
1913 | curPosition() {
|
1914 | return new Position(this.curLine, this.pos - this.lineStart, this.pos);
|
1915 | }
|
1916 | clone(skipArrays) {
|
1917 | const state = new State();
|
1918 | const keys = Object.keys(this);
|
1919 | for (let i = 0, length = keys.length; i < length; i++) {
|
1920 | const key = keys[i];
|
1921 | let val = this[key];
|
1922 | if (!skipArrays && Array.isArray(val)) {
|
1923 | val = val.slice();
|
1924 | }
|
1925 | state[key] = val;
|
1926 | }
|
1927 | return state;
|
1928 | }
|
1929 | }
|
1930 | var _isDigit = function isDigit(code) {
|
1931 | return code >= 48 && code <= 57;
|
1932 | };
|
1933 | const forbiddenNumericSeparatorSiblings = {
|
1934 | decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
|
1935 | hex: new Set([46, 88, 95, 120])
|
1936 | };
|
1937 | const isAllowedNumericSeparatorSibling = {
|
1938 | bin: ch => ch === 48 || ch === 49,
|
1939 | oct: ch => ch >= 48 && ch <= 55,
|
1940 | dec: ch => ch >= 48 && ch <= 57,
|
1941 | hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
|
1942 | };
|
1943 | function readStringContents(type, input, pos, lineStart, curLine, errors) {
|
1944 | const initialPos = pos;
|
1945 | const initialLineStart = lineStart;
|
1946 | const initialCurLine = curLine;
|
1947 | let out = "";
|
1948 | let firstInvalidLoc = null;
|
1949 | let chunkStart = pos;
|
1950 | const {
|
1951 | length
|
1952 | } = input;
|
1953 | for (;;) {
|
1954 | if (pos >= length) {
|
1955 | errors.unterminated(initialPos, initialLineStart, initialCurLine);
|
1956 | out += input.slice(chunkStart, pos);
|
1957 | break;
|
1958 | }
|
1959 | const ch = input.charCodeAt(pos);
|
1960 | if (isStringEnd(type, ch, input, pos)) {
|
1961 | out += input.slice(chunkStart, pos);
|
1962 | break;
|
1963 | }
|
1964 | if (ch === 92) {
|
1965 | out += input.slice(chunkStart, pos);
|
1966 | const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
|
1967 | if (res.ch === null && !firstInvalidLoc) {
|
1968 | firstInvalidLoc = {
|
1969 | pos,
|
1970 | lineStart,
|
1971 | curLine
|
1972 | };
|
1973 | } else {
|
1974 | out += res.ch;
|
1975 | }
|
1976 | ({
|
1977 | pos,
|
1978 | lineStart,
|
1979 | curLine
|
1980 | } = res);
|
1981 | chunkStart = pos;
|
1982 | } else if (ch === 8232 || ch === 8233) {
|
1983 | ++pos;
|
1984 | ++curLine;
|
1985 | lineStart = pos;
|
1986 | } else if (ch === 10 || ch === 13) {
|
1987 | if (type === "template") {
|
1988 | out += input.slice(chunkStart, pos) + "\n";
|
1989 | ++pos;
|
1990 | if (ch === 13 && input.charCodeAt(pos) === 10) {
|
1991 | ++pos;
|
1992 | }
|
1993 | ++curLine;
|
1994 | chunkStart = lineStart = pos;
|
1995 | } else {
|
1996 | errors.unterminated(initialPos, initialLineStart, initialCurLine);
|
1997 | }
|
1998 | } else {
|
1999 | ++pos;
|
2000 | }
|
2001 | }
|
2002 | return {
|
2003 | pos,
|
2004 | str: out,
|
2005 | firstInvalidLoc,
|
2006 | lineStart,
|
2007 | curLine,
|
2008 | containsInvalid: !!firstInvalidLoc
|
2009 | };
|
2010 | }
|
2011 | function isStringEnd(type, ch, input, pos) {
|
2012 | if (type === "template") {
|
2013 | return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
|
2014 | }
|
2015 | return ch === (type === "double" ? 34 : 39);
|
2016 | }
|
2017 | function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
|
2018 | const throwOnInvalid = !inTemplate;
|
2019 | pos++;
|
2020 | const res = ch => ({
|
2021 | pos,
|
2022 | ch,
|
2023 | lineStart,
|
2024 | curLine
|
2025 | });
|
2026 | const ch = input.charCodeAt(pos++);
|
2027 | switch (ch) {
|
2028 | case 110:
|
2029 | return res("\n");
|
2030 | case 114:
|
2031 | return res("\r");
|
2032 | case 120:
|
2033 | {
|
2034 | let code;
|
2035 | ({
|
2036 | code,
|
2037 | pos
|
2038 | } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
|
2039 | return res(code === null ? null : String.fromCharCode(code));
|
2040 | }
|
2041 | case 117:
|
2042 | {
|
2043 | let code;
|
2044 | ({
|
2045 | code,
|
2046 | pos
|
2047 | } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
|
2048 | return res(code === null ? null : String.fromCodePoint(code));
|
2049 | }
|
2050 | case 116:
|
2051 | return res("\t");
|
2052 | case 98:
|
2053 | return res("\b");
|
2054 | case 118:
|
2055 | return res("\u000b");
|
2056 | case 102:
|
2057 | return res("\f");
|
2058 | case 13:
|
2059 | if (input.charCodeAt(pos) === 10) {
|
2060 | ++pos;
|
2061 | }
|
2062 | case 10:
|
2063 | lineStart = pos;
|
2064 | ++curLine;
|
2065 | case 8232:
|
2066 | case 8233:
|
2067 | return res("");
|
2068 | case 56:
|
2069 | case 57:
|
2070 | if (inTemplate) {
|
2071 | return res(null);
|
2072 | } else {
|
2073 | errors.strictNumericEscape(pos - 1, lineStart, curLine);
|
2074 | }
|
2075 | default:
|
2076 | if (ch >= 48 && ch <= 55) {
|
2077 | const startPos = pos - 1;
|
2078 | const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
|
2079 | let octalStr = match[0];
|
2080 | let octal = parseInt(octalStr, 8);
|
2081 | if (octal > 255) {
|
2082 | octalStr = octalStr.slice(0, -1);
|
2083 | octal = parseInt(octalStr, 8);
|
2084 | }
|
2085 | pos += octalStr.length - 1;
|
2086 | const next = input.charCodeAt(pos);
|
2087 | if (octalStr !== "0" || next === 56 || next === 57) {
|
2088 | if (inTemplate) {
|
2089 | return res(null);
|
2090 | } else {
|
2091 | errors.strictNumericEscape(startPos, lineStart, curLine);
|
2092 | }
|
2093 | }
|
2094 | return res(String.fromCharCode(octal));
|
2095 | }
|
2096 | return res(String.fromCharCode(ch));
|
2097 | }
|
2098 | }
|
2099 | function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
|
2100 | const initialPos = pos;
|
2101 | let n;
|
2102 | ({
|
2103 | n,
|
2104 | pos
|
2105 | } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
|
2106 | if (n === null) {
|
2107 | if (throwOnInvalid) {
|
2108 | errors.invalidEscapeSequence(initialPos, lineStart, curLine);
|
2109 | } else {
|
2110 | pos = initialPos - 1;
|
2111 | }
|
2112 | }
|
2113 | return {
|
2114 | code: n,
|
2115 | pos
|
2116 | };
|
2117 | }
|
2118 | function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
|
2119 | const start = pos;
|
2120 | const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
|
2121 | const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
|
2122 | let invalid = false;
|
2123 | let total = 0;
|
2124 | for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
|
2125 | const code = input.charCodeAt(pos);
|
2126 | let val;
|
2127 | if (code === 95 && allowNumSeparator !== "bail") {
|
2128 | const prev = input.charCodeAt(pos - 1);
|
2129 | const next = input.charCodeAt(pos + 1);
|
2130 | if (!allowNumSeparator) {
|
2131 | if (bailOnError) return {
|
2132 | n: null,
|
2133 | pos
|
2134 | };
|
2135 | errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
|
2136 | } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
|
2137 | if (bailOnError) return {
|
2138 | n: null,
|
2139 | pos
|
2140 | };
|
2141 | errors.unexpectedNumericSeparator(pos, lineStart, curLine);
|
2142 | }
|
2143 | ++pos;
|
2144 | continue;
|
2145 | }
|
2146 | if (code >= 97) {
|
2147 | val = code - 97 + 10;
|
2148 | } else if (code >= 65) {
|
2149 | val = code - 65 + 10;
|
2150 | } else if (_isDigit(code)) {
|
2151 | val = code - 48;
|
2152 | } else {
|
2153 | val = Infinity;
|
2154 | }
|
2155 | if (val >= radix) {
|
2156 | if (val <= 9 && bailOnError) {
|
2157 | return {
|
2158 | n: null,
|
2159 | pos
|
2160 | };
|
2161 | } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
|
2162 | val = 0;
|
2163 | } else if (forceLen) {
|
2164 | val = 0;
|
2165 | invalid = true;
|
2166 | } else {
|
2167 | break;
|
2168 | }
|
2169 | }
|
2170 | ++pos;
|
2171 | total = total * radix + val;
|
2172 | }
|
2173 | if (pos === start || len != null && pos - start !== len || invalid) {
|
2174 | return {
|
2175 | n: null,
|
2176 | pos
|
2177 | };
|
2178 | }
|
2179 | return {
|
2180 | n: total,
|
2181 | pos
|
2182 | };
|
2183 | }
|
2184 | function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
|
2185 | const ch = input.charCodeAt(pos);
|
2186 | let code;
|
2187 | if (ch === 123) {
|
2188 | ++pos;
|
2189 | ({
|
2190 | code,
|
2191 | pos
|
2192 | } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
|
2193 | ++pos;
|
2194 | if (code !== null && code > 0x10ffff) {
|
2195 | if (throwOnInvalid) {
|
2196 | errors.invalidCodePoint(pos, lineStart, curLine);
|
2197 | } else {
|
2198 | return {
|
2199 | code: null,
|
2200 | pos
|
2201 | };
|
2202 | }
|
2203 | }
|
2204 | } else {
|
2205 | ({
|
2206 | code,
|
2207 | pos
|
2208 | } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
|
2209 | }
|
2210 | return {
|
2211 | code,
|
2212 | pos
|
2213 | };
|
2214 | }
|
2215 | const _excluded = ["at"],
|
2216 | _excluded2 = ["at"];
|
2217 | function buildPosition(pos, lineStart, curLine) {
|
2218 | return new Position(curLine, pos - lineStart, pos);
|
2219 | }
|
2220 | const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
|
2221 | class Token {
|
2222 | constructor(state) {
|
2223 | this.type = state.type;
|
2224 | this.value = state.value;
|
2225 | this.start = state.start;
|
2226 | this.end = state.end;
|
2227 | this.loc = new SourceLocation(state.startLoc, state.endLoc);
|
2228 | }
|
2229 | }
|
2230 | class Tokenizer extends CommentsParser {
|
2231 | constructor(options, input) {
|
2232 | super();
|
2233 | this.isLookahead = void 0;
|
2234 | this.tokens = [];
|
2235 | this.errorHandlers_readInt = {
|
2236 | invalidDigit: (pos, lineStart, curLine, radix) => {
|
2237 | if (!this.options.errorRecovery) return false;
|
2238 | this.raise(Errors.InvalidDigit, {
|
2239 | at: buildPosition(pos, lineStart, curLine),
|
2240 | radix
|
2241 | });
|
2242 | return true;
|
2243 | },
|
2244 | numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
|
2245 | unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
|
2246 | };
|
2247 | this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
|
2248 | invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
|
2249 | invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
|
2250 | });
|
2251 | this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
|
2252 | strictNumericEscape: (pos, lineStart, curLine) => {
|
2253 | this.recordStrictModeErrors(Errors.StrictNumericEscape, {
|
2254 | at: buildPosition(pos, lineStart, curLine)
|
2255 | });
|
2256 | },
|
2257 | unterminated: (pos, lineStart, curLine) => {
|
2258 | throw this.raise(Errors.UnterminatedString, {
|
2259 | at: buildPosition(pos - 1, lineStart, curLine)
|
2260 | });
|
2261 | }
|
2262 | });
|
2263 | this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
|
2264 | strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
|
2265 | unterminated: (pos, lineStart, curLine) => {
|
2266 | throw this.raise(Errors.UnterminatedTemplate, {
|
2267 | at: buildPosition(pos, lineStart, curLine)
|
2268 | });
|
2269 | }
|
2270 | });
|
2271 | this.state = new State();
|
2272 | this.state.init(options);
|
2273 | this.input = input;
|
2274 | this.length = input.length;
|
2275 | this.isLookahead = false;
|
2276 | }
|
2277 | pushToken(token) {
|
2278 | this.tokens.length = this.state.tokensLength;
|
2279 | this.tokens.push(token);
|
2280 | ++this.state.tokensLength;
|
2281 | }
|
2282 | next() {
|
2283 | this.checkKeywordEscapes();
|
2284 | if (this.options.tokens) {
|
2285 | this.pushToken(new Token(this.state));
|
2286 | }
|
2287 | this.state.lastTokStart = this.state.start;
|
2288 | this.state.lastTokEndLoc = this.state.endLoc;
|
2289 | this.state.lastTokStartLoc = this.state.startLoc;
|
2290 | this.nextToken();
|
2291 | }
|
2292 | eat(type) {
|
2293 | if (this.match(type)) {
|
2294 | this.next();
|
2295 | return true;
|
2296 | } else {
|
2297 | return false;
|
2298 | }
|
2299 | }
|
2300 | match(type) {
|
2301 | return this.state.type === type;
|
2302 | }
|
2303 | createLookaheadState(state) {
|
2304 | return {
|
2305 | pos: state.pos,
|
2306 | value: null,
|
2307 | type: state.type,
|
2308 | start: state.start,
|
2309 | end: state.end,
|
2310 | context: [this.curContext()],
|
2311 | inType: state.inType,
|
2312 | startLoc: state.startLoc,
|
2313 | lastTokEndLoc: state.lastTokEndLoc,
|
2314 | curLine: state.curLine,
|
2315 | lineStart: state.lineStart,
|
2316 | curPosition: state.curPosition
|
2317 | };
|
2318 | }
|
2319 | lookahead() {
|
2320 | const old = this.state;
|
2321 | this.state = this.createLookaheadState(old);
|
2322 | this.isLookahead = true;
|
2323 | this.nextToken();
|
2324 | this.isLookahead = false;
|
2325 | const curr = this.state;
|
2326 | this.state = old;
|
2327 | return curr;
|
2328 | }
|
2329 | nextTokenStart() {
|
2330 | return this.nextTokenStartSince(this.state.pos);
|
2331 | }
|
2332 | nextTokenStartSince(pos) {
|
2333 | skipWhiteSpace.lastIndex = pos;
|
2334 | return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
|
2335 | }
|
2336 | lookaheadCharCode() {
|
2337 | return this.input.charCodeAt(this.nextTokenStart());
|
2338 | }
|
2339 | nextTokenInLineStart() {
|
2340 | return this.nextTokenInLineStartSince(this.state.pos);
|
2341 | }
|
2342 | nextTokenInLineStartSince(pos) {
|
2343 | skipWhiteSpaceInLine.lastIndex = pos;
|
2344 | return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
|
2345 | }
|
2346 | lookaheadInLineCharCode() {
|
2347 | return this.input.charCodeAt(this.nextTokenInLineStart());
|
2348 | }
|
2349 | codePointAtPos(pos) {
|
2350 | let cp = this.input.charCodeAt(pos);
|
2351 | if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
|
2352 | const trail = this.input.charCodeAt(pos);
|
2353 | if ((trail & 0xfc00) === 0xdc00) {
|
2354 | cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
|
2355 | }
|
2356 | }
|
2357 | return cp;
|
2358 | }
|
2359 | setStrict(strict) {
|
2360 | this.state.strict = strict;
|
2361 | if (strict) {
|
2362 | this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
|
2363 | at
|
2364 | }));
|
2365 | this.state.strictErrors.clear();
|
2366 | }
|
2367 | }
|
2368 | curContext() {
|
2369 | return this.state.context[this.state.context.length - 1];
|
2370 | }
|
2371 | nextToken() {
|
2372 | this.skipSpace();
|
2373 | this.state.start = this.state.pos;
|
2374 | if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
|
2375 | if (this.state.pos >= this.length) {
|
2376 | this.finishToken(137);
|
2377 | return;
|
2378 | }
|
2379 | this.getTokenFromCode(this.codePointAtPos(this.state.pos));
|
2380 | }
|
2381 | skipBlockComment(commentEnd) {
|
2382 | let startLoc;
|
2383 | if (!this.isLookahead) startLoc = this.state.curPosition();
|
2384 | const start = this.state.pos;
|
2385 | const end = this.input.indexOf(commentEnd, start + 2);
|
2386 | if (end === -1) {
|
2387 | throw this.raise(Errors.UnterminatedComment, {
|
2388 | at: this.state.curPosition()
|
2389 | });
|
2390 | }
|
2391 | this.state.pos = end + commentEnd.length;
|
2392 | lineBreakG.lastIndex = start + 2;
|
2393 | while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
|
2394 | ++this.state.curLine;
|
2395 | this.state.lineStart = lineBreakG.lastIndex;
|
2396 | }
|
2397 | if (this.isLookahead) return;
|
2398 | const comment = {
|
2399 | type: "CommentBlock",
|
2400 | value: this.input.slice(start + 2, end),
|
2401 | start,
|
2402 | end: end + commentEnd.length,
|
2403 | loc: new SourceLocation(startLoc, this.state.curPosition())
|
2404 | };
|
2405 | if (this.options.tokens) this.pushToken(comment);
|
2406 | return comment;
|
2407 | }
|
2408 | skipLineComment(startSkip) {
|
2409 | const start = this.state.pos;
|
2410 | let startLoc;
|
2411 | if (!this.isLookahead) startLoc = this.state.curPosition();
|
2412 | let ch = this.input.charCodeAt(this.state.pos += startSkip);
|
2413 | if (this.state.pos < this.length) {
|
2414 | while (!isNewLine(ch) && ++this.state.pos < this.length) {
|
2415 | ch = this.input.charCodeAt(this.state.pos);
|
2416 | }
|
2417 | }
|
2418 | if (this.isLookahead) return;
|
2419 | const end = this.state.pos;
|
2420 | const value = this.input.slice(start + startSkip, end);
|
2421 | const comment = {
|
2422 | type: "CommentLine",
|
2423 | value,
|
2424 | start,
|
2425 | end,
|
2426 | loc: new SourceLocation(startLoc, this.state.curPosition())
|
2427 | };
|
2428 | if (this.options.tokens) this.pushToken(comment);
|
2429 | return comment;
|
2430 | }
|
2431 | skipSpace() {
|
2432 | const spaceStart = this.state.pos;
|
2433 | const comments = [];
|
2434 | loop: while (this.state.pos < this.length) {
|
2435 | const ch = this.input.charCodeAt(this.state.pos);
|
2436 | switch (ch) {
|
2437 | case 32:
|
2438 | case 160:
|
2439 | case 9:
|
2440 | ++this.state.pos;
|
2441 | break;
|
2442 | case 13:
|
2443 | if (this.input.charCodeAt(this.state.pos + 1) === 10) {
|
2444 | ++this.state.pos;
|
2445 | }
|
2446 | case 10:
|
2447 | case 8232:
|
2448 | case 8233:
|
2449 | ++this.state.pos;
|
2450 | ++this.state.curLine;
|
2451 | this.state.lineStart = this.state.pos;
|
2452 | break;
|
2453 | case 47:
|
2454 | switch (this.input.charCodeAt(this.state.pos + 1)) {
|
2455 | case 42:
|
2456 | {
|
2457 | const comment = this.skipBlockComment("*/");
|
2458 | if (comment !== undefined) {
|
2459 | this.addComment(comment);
|
2460 | if (this.options.attachComment) comments.push(comment);
|
2461 | }
|
2462 | break;
|
2463 | }
|
2464 | case 47:
|
2465 | {
|
2466 | const comment = this.skipLineComment(2);
|
2467 | if (comment !== undefined) {
|
2468 | this.addComment(comment);
|
2469 | if (this.options.attachComment) comments.push(comment);
|
2470 | }
|
2471 | break;
|
2472 | }
|
2473 | default:
|
2474 | break loop;
|
2475 | }
|
2476 | break;
|
2477 | default:
|
2478 | if (isWhitespace(ch)) {
|
2479 | ++this.state.pos;
|
2480 | } else if (ch === 45 && !this.inModule && this.options.annexB) {
|
2481 | const pos = this.state.pos;
|
2482 | if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
|
2483 | const comment = this.skipLineComment(3);
|
2484 | if (comment !== undefined) {
|
2485 | this.addComment(comment);
|
2486 | if (this.options.attachComment) comments.push(comment);
|
2487 | }
|
2488 | } else {
|
2489 | break loop;
|
2490 | }
|
2491 | } else if (ch === 60 && !this.inModule && this.options.annexB) {
|
2492 | const pos = this.state.pos;
|
2493 | if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
|
2494 | const comment = this.skipLineComment(4);
|
2495 | if (comment !== undefined) {
|
2496 | this.addComment(comment);
|
2497 | if (this.options.attachComment) comments.push(comment);
|
2498 | }
|
2499 | } else {
|
2500 | break loop;
|
2501 | }
|
2502 | } else {
|
2503 | break loop;
|
2504 | }
|
2505 | }
|
2506 | }
|
2507 | if (comments.length > 0) {
|
2508 | const end = this.state.pos;
|
2509 | const commentWhitespace = {
|
2510 | start: spaceStart,
|
2511 | end,
|
2512 | comments,
|
2513 | leadingNode: null,
|
2514 | trailingNode: null,
|
2515 | containingNode: null
|
2516 | };
|
2517 | this.state.commentStack.push(commentWhitespace);
|
2518 | }
|
2519 | }
|
2520 | finishToken(type, val) {
|
2521 | this.state.end = this.state.pos;
|
2522 | this.state.endLoc = this.state.curPosition();
|
2523 | const prevType = this.state.type;
|
2524 | this.state.type = type;
|
2525 | this.state.value = val;
|
2526 | if (!this.isLookahead) {
|
2527 | this.updateContext(prevType);
|
2528 | }
|
2529 | }
|
2530 | replaceToken(type) {
|
2531 | this.state.type = type;
|
2532 | this.updateContext();
|
2533 | }
|
2534 | readToken_numberSign() {
|
2535 | if (this.state.pos === 0 && this.readToken_interpreter()) {
|
2536 | return;
|
2537 | }
|
2538 | const nextPos = this.state.pos + 1;
|
2539 | const next = this.codePointAtPos(nextPos);
|
2540 | if (next >= 48 && next <= 57) {
|
2541 | throw this.raise(Errors.UnexpectedDigitAfterHash, {
|
2542 | at: this.state.curPosition()
|
2543 | });
|
2544 | }
|
2545 | if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
|
2546 | this.expectPlugin("recordAndTuple");
|
2547 | if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
|
2548 | throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
|
2549 | at: this.state.curPosition()
|
2550 | });
|
2551 | }
|
2552 | this.state.pos += 2;
|
2553 | if (next === 123) {
|
2554 | this.finishToken(7);
|
2555 | } else {
|
2556 | this.finishToken(1);
|
2557 | }
|
2558 | } else if (isIdentifierStart(next)) {
|
2559 | ++this.state.pos;
|
2560 | this.finishToken(136, this.readWord1(next));
|
2561 | } else if (next === 92) {
|
2562 | ++this.state.pos;
|
2563 | this.finishToken(136, this.readWord1());
|
2564 | } else {
|
2565 | this.finishOp(27, 1);
|
2566 | }
|
2567 | }
|
2568 | readToken_dot() {
|
2569 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2570 | if (next >= 48 && next <= 57) {
|
2571 | this.readNumber(true);
|
2572 | return;
|
2573 | }
|
2574 | if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
|
2575 | this.state.pos += 3;
|
2576 | this.finishToken(21);
|
2577 | } else {
|
2578 | ++this.state.pos;
|
2579 | this.finishToken(16);
|
2580 | }
|
2581 | }
|
2582 | readToken_slash() {
|
2583 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2584 | if (next === 61) {
|
2585 | this.finishOp(31, 2);
|
2586 | } else {
|
2587 | this.finishOp(56, 1);
|
2588 | }
|
2589 | }
|
2590 | readToken_interpreter() {
|
2591 | if (this.state.pos !== 0 || this.length < 2) return false;
|
2592 | let ch = this.input.charCodeAt(this.state.pos + 1);
|
2593 | if (ch !== 33) return false;
|
2594 | const start = this.state.pos;
|
2595 | this.state.pos += 1;
|
2596 | while (!isNewLine(ch) && ++this.state.pos < this.length) {
|
2597 | ch = this.input.charCodeAt(this.state.pos);
|
2598 | }
|
2599 | const value = this.input.slice(start + 2, this.state.pos);
|
2600 | this.finishToken(28, value);
|
2601 | return true;
|
2602 | }
|
2603 | readToken_mult_modulo(code) {
|
2604 | let type = code === 42 ? 55 : 54;
|
2605 | let width = 1;
|
2606 | let next = this.input.charCodeAt(this.state.pos + 1);
|
2607 | if (code === 42 && next === 42) {
|
2608 | width++;
|
2609 | next = this.input.charCodeAt(this.state.pos + 2);
|
2610 | type = 57;
|
2611 | }
|
2612 | if (next === 61 && !this.state.inType) {
|
2613 | width++;
|
2614 | type = code === 37 ? 33 : 30;
|
2615 | }
|
2616 | this.finishOp(type, width);
|
2617 | }
|
2618 | readToken_pipe_amp(code) {
|
2619 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2620 | if (next === code) {
|
2621 | if (this.input.charCodeAt(this.state.pos + 2) === 61) {
|
2622 | this.finishOp(30, 3);
|
2623 | } else {
|
2624 | this.finishOp(code === 124 ? 41 : 42, 2);
|
2625 | }
|
2626 | return;
|
2627 | }
|
2628 | if (code === 124) {
|
2629 | if (next === 62) {
|
2630 | this.finishOp(39, 2);
|
2631 | return;
|
2632 | }
|
2633 | if (this.hasPlugin("recordAndTuple") && next === 125) {
|
2634 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2635 | throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
|
2636 | at: this.state.curPosition()
|
2637 | });
|
2638 | }
|
2639 | this.state.pos += 2;
|
2640 | this.finishToken(9);
|
2641 | return;
|
2642 | }
|
2643 | if (this.hasPlugin("recordAndTuple") && next === 93) {
|
2644 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2645 | throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
|
2646 | at: this.state.curPosition()
|
2647 | });
|
2648 | }
|
2649 | this.state.pos += 2;
|
2650 | this.finishToken(4);
|
2651 | return;
|
2652 | }
|
2653 | }
|
2654 | if (next === 61) {
|
2655 | this.finishOp(30, 2);
|
2656 | return;
|
2657 | }
|
2658 | this.finishOp(code === 124 ? 43 : 45, 1);
|
2659 | }
|
2660 | readToken_caret() {
|
2661 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2662 | if (next === 61 && !this.state.inType) {
|
2663 | this.finishOp(32, 2);
|
2664 | } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
|
2665 | proposal: "hack",
|
2666 | topicToken: "^^"
|
2667 | }])) {
|
2668 | this.finishOp(37, 2);
|
2669 | const lookaheadCh = this.input.codePointAt(this.state.pos);
|
2670 | if (lookaheadCh === 94) {
|
2671 | this.unexpected();
|
2672 | }
|
2673 | } else {
|
2674 | this.finishOp(44, 1);
|
2675 | }
|
2676 | }
|
2677 | readToken_atSign() {
|
2678 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2679 | if (next === 64 && this.hasPlugin(["pipelineOperator", {
|
2680 | proposal: "hack",
|
2681 | topicToken: "@@"
|
2682 | }])) {
|
2683 | this.finishOp(38, 2);
|
2684 | } else {
|
2685 | this.finishOp(26, 1);
|
2686 | }
|
2687 | }
|
2688 | readToken_plus_min(code) {
|
2689 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2690 | if (next === code) {
|
2691 | this.finishOp(34, 2);
|
2692 | return;
|
2693 | }
|
2694 | if (next === 61) {
|
2695 | this.finishOp(30, 2);
|
2696 | } else {
|
2697 | this.finishOp(53, 1);
|
2698 | }
|
2699 | }
|
2700 | readToken_lt() {
|
2701 | const {
|
2702 | pos
|
2703 | } = this.state;
|
2704 | const next = this.input.charCodeAt(pos + 1);
|
2705 | if (next === 60) {
|
2706 | if (this.input.charCodeAt(pos + 2) === 61) {
|
2707 | this.finishOp(30, 3);
|
2708 | return;
|
2709 | }
|
2710 | this.finishOp(51, 2);
|
2711 | return;
|
2712 | }
|
2713 | if (next === 61) {
|
2714 | this.finishOp(49, 2);
|
2715 | return;
|
2716 | }
|
2717 | this.finishOp(47, 1);
|
2718 | }
|
2719 | readToken_gt() {
|
2720 | const {
|
2721 | pos
|
2722 | } = this.state;
|
2723 | const next = this.input.charCodeAt(pos + 1);
|
2724 | if (next === 62) {
|
2725 | const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
|
2726 | if (this.input.charCodeAt(pos + size) === 61) {
|
2727 | this.finishOp(30, size + 1);
|
2728 | return;
|
2729 | }
|
2730 | this.finishOp(52, size);
|
2731 | return;
|
2732 | }
|
2733 | if (next === 61) {
|
2734 | this.finishOp(49, 2);
|
2735 | return;
|
2736 | }
|
2737 | this.finishOp(48, 1);
|
2738 | }
|
2739 | readToken_eq_excl(code) {
|
2740 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2741 | if (next === 61) {
|
2742 | this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
|
2743 | return;
|
2744 | }
|
2745 | if (code === 61 && next === 62) {
|
2746 | this.state.pos += 2;
|
2747 | this.finishToken(19);
|
2748 | return;
|
2749 | }
|
2750 | this.finishOp(code === 61 ? 29 : 35, 1);
|
2751 | }
|
2752 | readToken_question() {
|
2753 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2754 | const next2 = this.input.charCodeAt(this.state.pos + 2);
|
2755 | if (next === 63) {
|
2756 | if (next2 === 61) {
|
2757 | this.finishOp(30, 3);
|
2758 | } else {
|
2759 | this.finishOp(40, 2);
|
2760 | }
|
2761 | } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
|
2762 | this.state.pos += 2;
|
2763 | this.finishToken(18);
|
2764 | } else {
|
2765 | ++this.state.pos;
|
2766 | this.finishToken(17);
|
2767 | }
|
2768 | }
|
2769 | getTokenFromCode(code) {
|
2770 | switch (code) {
|
2771 | case 46:
|
2772 | this.readToken_dot();
|
2773 | return;
|
2774 | case 40:
|
2775 | ++this.state.pos;
|
2776 | this.finishToken(10);
|
2777 | return;
|
2778 | case 41:
|
2779 | ++this.state.pos;
|
2780 | this.finishToken(11);
|
2781 | return;
|
2782 | case 59:
|
2783 | ++this.state.pos;
|
2784 | this.finishToken(13);
|
2785 | return;
|
2786 | case 44:
|
2787 | ++this.state.pos;
|
2788 | this.finishToken(12);
|
2789 | return;
|
2790 | case 91:
|
2791 | if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
|
2792 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2793 | throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
|
2794 | at: this.state.curPosition()
|
2795 | });
|
2796 | }
|
2797 | this.state.pos += 2;
|
2798 | this.finishToken(2);
|
2799 | } else {
|
2800 | ++this.state.pos;
|
2801 | this.finishToken(0);
|
2802 | }
|
2803 | return;
|
2804 | case 93:
|
2805 | ++this.state.pos;
|
2806 | this.finishToken(3);
|
2807 | return;
|
2808 | case 123:
|
2809 | if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
|
2810 | if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
|
2811 | throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
|
2812 | at: this.state.curPosition()
|
2813 | });
|
2814 | }
|
2815 | this.state.pos += 2;
|
2816 | this.finishToken(6);
|
2817 | } else {
|
2818 | ++this.state.pos;
|
2819 | this.finishToken(5);
|
2820 | }
|
2821 | return;
|
2822 | case 125:
|
2823 | ++this.state.pos;
|
2824 | this.finishToken(8);
|
2825 | return;
|
2826 | case 58:
|
2827 | if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
|
2828 | this.finishOp(15, 2);
|
2829 | } else {
|
2830 | ++this.state.pos;
|
2831 | this.finishToken(14);
|
2832 | }
|
2833 | return;
|
2834 | case 63:
|
2835 | this.readToken_question();
|
2836 | return;
|
2837 | case 96:
|
2838 | this.readTemplateToken();
|
2839 | return;
|
2840 | case 48:
|
2841 | {
|
2842 | const next = this.input.charCodeAt(this.state.pos + 1);
|
2843 | if (next === 120 || next === 88) {
|
2844 | this.readRadixNumber(16);
|
2845 | return;
|
2846 | }
|
2847 | if (next === 111 || next === 79) {
|
2848 | this.readRadixNumber(8);
|
2849 | return;
|
2850 | }
|
2851 | if (next === 98 || next === 66) {
|
2852 | this.readRadixNumber(2);
|
2853 | return;
|
2854 | }
|
2855 | }
|
2856 | case 49:
|
2857 | case 50:
|
2858 | case 51:
|
2859 | case 52:
|
2860 | case 53:
|
2861 | case 54:
|
2862 | case 55:
|
2863 | case 56:
|
2864 | case 57:
|
2865 | this.readNumber(false);
|
2866 | return;
|
2867 | case 34:
|
2868 | case 39:
|
2869 | this.readString(code);
|
2870 | return;
|
2871 | case 47:
|
2872 | this.readToken_slash();
|
2873 | return;
|
2874 | case 37:
|
2875 | case 42:
|
2876 | this.readToken_mult_modulo(code);
|
2877 | return;
|
2878 | case 124:
|
2879 | case 38:
|
2880 | this.readToken_pipe_amp(code);
|
2881 | return;
|
2882 | case 94:
|
2883 | this.readToken_caret();
|
2884 | return;
|
2885 | case 43:
|
2886 | case 45:
|
2887 | this.readToken_plus_min(code);
|
2888 | return;
|
2889 | case 60:
|
2890 | this.readToken_lt();
|
2891 | return;
|
2892 | case 62:
|
2893 | this.readToken_gt();
|
2894 | return;
|
2895 | case 61:
|
2896 | case 33:
|
2897 | this.readToken_eq_excl(code);
|
2898 | return;
|
2899 | case 126:
|
2900 | this.finishOp(36, 1);
|
2901 | return;
|
2902 | case 64:
|
2903 | this.readToken_atSign();
|
2904 | return;
|
2905 | case 35:
|
2906 | this.readToken_numberSign();
|
2907 | return;
|
2908 | case 92:
|
2909 | this.readWord();
|
2910 | return;
|
2911 | default:
|
2912 | if (isIdentifierStart(code)) {
|
2913 | this.readWord(code);
|
2914 | return;
|
2915 | }
|
2916 | }
|
2917 | throw this.raise(Errors.InvalidOrUnexpectedToken, {
|
2918 | at: this.state.curPosition(),
|
2919 | unexpected: String.fromCodePoint(code)
|
2920 | });
|
2921 | }
|
2922 | finishOp(type, size) {
|
2923 | const str = this.input.slice(this.state.pos, this.state.pos + size);
|
2924 | this.state.pos += size;
|
2925 | this.finishToken(type, str);
|
2926 | }
|
2927 | readRegexp() {
|
2928 | const startLoc = this.state.startLoc;
|
2929 | const start = this.state.start + 1;
|
2930 | let escaped, inClass;
|
2931 | let {
|
2932 | pos
|
2933 | } = this.state;
|
2934 | for (;; ++pos) {
|
2935 | if (pos >= this.length) {
|
2936 | throw this.raise(Errors.UnterminatedRegExp, {
|
2937 | at: createPositionWithColumnOffset(startLoc, 1)
|
2938 | });
|
2939 | }
|
2940 | const ch = this.input.charCodeAt(pos);
|
2941 | if (isNewLine(ch)) {
|
2942 | throw this.raise(Errors.UnterminatedRegExp, {
|
2943 | at: createPositionWithColumnOffset(startLoc, 1)
|
2944 | });
|
2945 | }
|
2946 | if (escaped) {
|
2947 | escaped = false;
|
2948 | } else {
|
2949 | if (ch === 91) {
|
2950 | inClass = true;
|
2951 | } else if (ch === 93 && inClass) {
|
2952 | inClass = false;
|
2953 | } else if (ch === 47 && !inClass) {
|
2954 | break;
|
2955 | }
|
2956 | escaped = ch === 92;
|
2957 | }
|
2958 | }
|
2959 | const content = this.input.slice(start, pos);
|
2960 | ++pos;
|
2961 | let mods = "";
|
2962 | const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
|
2963 | while (pos < this.length) {
|
2964 | const cp = this.codePointAtPos(pos);
|
2965 | const char = String.fromCharCode(cp);
|
2966 | if (VALID_REGEX_FLAGS.has(cp)) {
|
2967 | if (cp === 118) {
|
2968 | if (mods.includes("u")) {
|
2969 | this.raise(Errors.IncompatibleRegExpUVFlags, {
|
2970 | at: nextPos()
|
2971 | });
|
2972 | }
|
2973 | } else if (cp === 117) {
|
2974 | if (mods.includes("v")) {
|
2975 | this.raise(Errors.IncompatibleRegExpUVFlags, {
|
2976 | at: nextPos()
|
2977 | });
|
2978 | }
|
2979 | }
|
2980 | if (mods.includes(char)) {
|
2981 | this.raise(Errors.DuplicateRegExpFlags, {
|
2982 | at: nextPos()
|
2983 | });
|
2984 | }
|
2985 | } else if (isIdentifierChar(cp) || cp === 92) {
|
2986 | this.raise(Errors.MalformedRegExpFlags, {
|
2987 | at: nextPos()
|
2988 | });
|
2989 | } else {
|
2990 | break;
|
2991 | }
|
2992 | ++pos;
|
2993 | mods += char;
|
2994 | }
|
2995 | this.state.pos = pos;
|
2996 | this.finishToken(135, {
|
2997 | pattern: content,
|
2998 | flags: mods
|
2999 | });
|
3000 | }
|
3001 | readInt(radix, len, forceLen = false, allowNumSeparator = true) {
|
3002 | const {
|
3003 | n,
|
3004 | pos
|
3005 | } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
|
3006 | this.state.pos = pos;
|
3007 | return n;
|
3008 | }
|
3009 | readRadixNumber(radix) {
|
3010 | const startLoc = this.state.curPosition();
|
3011 | let isBigInt = false;
|
3012 | this.state.pos += 2;
|
3013 | const val = this.readInt(radix);
|
3014 | if (val == null) {
|
3015 | this.raise(Errors.InvalidDigit, {
|
3016 | at: createPositionWithColumnOffset(startLoc, 2),
|
3017 | radix
|
3018 | });
|
3019 | }
|
3020 | const next = this.input.charCodeAt(this.state.pos);
|
3021 | if (next === 110) {
|
3022 | ++this.state.pos;
|
3023 | isBigInt = true;
|
3024 | } else if (next === 109) {
|
3025 | throw this.raise(Errors.InvalidDecimal, {
|
3026 | at: startLoc
|
3027 | });
|
3028 | }
|
3029 | if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
|
3030 | throw this.raise(Errors.NumberIdentifier, {
|
3031 | at: this.state.curPosition()
|
3032 | });
|
3033 | }
|
3034 | if (isBigInt) {
|
3035 | const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
|
3036 | this.finishToken(133, str);
|
3037 | return;
|
3038 | }
|
3039 | this.finishToken(132, val);
|
3040 | }
|
3041 | readNumber(startsWithDot) {
|
3042 | const start = this.state.pos;
|
3043 | const startLoc = this.state.curPosition();
|
3044 | let isFloat = false;
|
3045 | let isBigInt = false;
|
3046 | let isDecimal = false;
|
3047 | let hasExponent = false;
|
3048 | let isOctal = false;
|
3049 | if (!startsWithDot && this.readInt(10) === null) {
|
3050 | this.raise(Errors.InvalidNumber, {
|
3051 | at: this.state.curPosition()
|
3052 | });
|
3053 | }
|
3054 | const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
|
3055 | if (hasLeadingZero) {
|
3056 | const integer = this.input.slice(start, this.state.pos);
|
3057 | this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
|
3058 | at: startLoc
|
3059 | });
|
3060 | if (!this.state.strict) {
|
3061 | const underscorePos = integer.indexOf("_");
|
3062 | if (underscorePos > 0) {
|
3063 | this.raise(Errors.ZeroDigitNumericSeparator, {
|
3064 | at: createPositionWithColumnOffset(startLoc, underscorePos)
|
3065 | });
|
3066 | }
|
3067 | }
|
3068 | isOctal = hasLeadingZero && !/[89]/.test(integer);
|
3069 | }
|
3070 | let next = this.input.charCodeAt(this.state.pos);
|
3071 | if (next === 46 && !isOctal) {
|
3072 | ++this.state.pos;
|
3073 | this.readInt(10);
|
3074 | isFloat = true;
|
3075 | next = this.input.charCodeAt(this.state.pos);
|
3076 | }
|
3077 | if ((next === 69 || next === 101) && !isOctal) {
|
3078 | next = this.input.charCodeAt(++this.state.pos);
|
3079 | if (next === 43 || next === 45) {
|
3080 | ++this.state.pos;
|
3081 | }
|
3082 | if (this.readInt(10) === null) {
|
3083 | this.raise(Errors.InvalidOrMissingExponent, {
|
3084 | at: startLoc
|
3085 | });
|
3086 | }
|
3087 | isFloat = true;
|
3088 | hasExponent = true;
|
3089 | next = this.input.charCodeAt(this.state.pos);
|
3090 | }
|
3091 | if (next === 110) {
|
3092 | if (isFloat || hasLeadingZero) {
|
3093 | this.raise(Errors.InvalidBigIntLiteral, {
|
3094 | at: startLoc
|
3095 | });
|
3096 | }
|
3097 | ++this.state.pos;
|
3098 | isBigInt = true;
|
3099 | }
|
3100 | if (next === 109) {
|
3101 | this.expectPlugin("decimal", this.state.curPosition());
|
3102 | if (hasExponent || hasLeadingZero) {
|
3103 | this.raise(Errors.InvalidDecimal, {
|
3104 | at: startLoc
|
3105 | });
|
3106 | }
|
3107 | ++this.state.pos;
|
3108 | isDecimal = true;
|
3109 | }
|
3110 | if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
|
3111 | throw this.raise(Errors.NumberIdentifier, {
|
3112 | at: this.state.curPosition()
|
3113 | });
|
3114 | }
|
3115 | const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
|
3116 | if (isBigInt) {
|
3117 | this.finishToken(133, str);
|
3118 | return;
|
3119 | }
|
3120 | if (isDecimal) {
|
3121 | this.finishToken(134, str);
|
3122 | return;
|
3123 | }
|
3124 | const val = isOctal ? parseInt(str, 8) : parseFloat(str);
|
3125 | this.finishToken(132, val);
|
3126 | }
|
3127 | readCodePoint(throwOnInvalid) {
|
3128 | const {
|
3129 | code,
|
3130 | pos
|
3131 | } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
|
3132 | this.state.pos = pos;
|
3133 | return code;
|
3134 | }
|
3135 | readString(quote) {
|
3136 | const {
|
3137 | str,
|
3138 | pos,
|
3139 | curLine,
|
3140 | lineStart
|
3141 | } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
|
3142 | this.state.pos = pos + 1;
|
3143 | this.state.lineStart = lineStart;
|
3144 | this.state.curLine = curLine;
|
3145 | this.finishToken(131, str);
|
3146 | }
|
3147 | readTemplateContinuation() {
|
3148 | if (!this.match(8)) {
|
3149 | this.unexpected(null, 8);
|
3150 | }
|
3151 | this.state.pos--;
|
3152 | this.readTemplateToken();
|
3153 | }
|
3154 | readTemplateToken() {
|
3155 | const opening = this.input[this.state.pos];
|
3156 | const {
|
3157 | str,
|
3158 | firstInvalidLoc,
|
3159 | pos,
|
3160 | curLine,
|
3161 | lineStart
|
3162 | } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
|
3163 | this.state.pos = pos + 1;
|
3164 | this.state.lineStart = lineStart;
|
3165 | this.state.curLine = curLine;
|
3166 | if (firstInvalidLoc) {
|
3167 | this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
|
3168 | }
|
3169 | if (this.input.codePointAt(pos) === 96) {
|
3170 | this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
|
3171 | } else {
|
3172 | this.state.pos++;
|
3173 | this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
|
3174 | }
|
3175 | }
|
3176 | recordStrictModeErrors(toParseError, {
|
3177 | at
|
3178 | }) {
|
3179 | const index = at.index;
|
3180 | if (this.state.strict && !this.state.strictErrors.has(index)) {
|
3181 | this.raise(toParseError, {
|
3182 | at
|
3183 | });
|
3184 | } else {
|
3185 | this.state.strictErrors.set(index, [toParseError, at]);
|
3186 | }
|
3187 | }
|
3188 | readWord1(firstCode) {
|
3189 | this.state.containsEsc = false;
|
3190 | let word = "";
|
3191 | const start = this.state.pos;
|
3192 | let chunkStart = this.state.pos;
|
3193 | if (firstCode !== undefined) {
|
3194 | this.state.pos += firstCode <= 0xffff ? 1 : 2;
|
3195 | }
|
3196 | while (this.state.pos < this.length) {
|
3197 | const ch = this.codePointAtPos(this.state.pos);
|
3198 | if (isIdentifierChar(ch)) {
|
3199 | this.state.pos += ch <= 0xffff ? 1 : 2;
|
3200 | } else if (ch === 92) {
|
3201 | this.state.containsEsc = true;
|
3202 | word += this.input.slice(chunkStart, this.state.pos);
|
3203 | const escStart = this.state.curPosition();
|
3204 | const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
|
3205 | if (this.input.charCodeAt(++this.state.pos) !== 117) {
|
3206 | this.raise(Errors.MissingUnicodeEscape, {
|
3207 | at: this.state.curPosition()
|
3208 | });
|
3209 | chunkStart = this.state.pos - 1;
|
3210 | continue;
|
3211 | }
|
3212 | ++this.state.pos;
|
3213 | const esc = this.readCodePoint(true);
|
3214 | if (esc !== null) {
|
3215 | if (!identifierCheck(esc)) {
|
3216 | this.raise(Errors.EscapedCharNotAnIdentifier, {
|
3217 | at: escStart
|
3218 | });
|
3219 | }
|
3220 | word += String.fromCodePoint(esc);
|
3221 | }
|
3222 | chunkStart = this.state.pos;
|
3223 | } else {
|
3224 | break;
|
3225 | }
|
3226 | }
|
3227 | return word + this.input.slice(chunkStart, this.state.pos);
|
3228 | }
|
3229 | readWord(firstCode) {
|
3230 | const word = this.readWord1(firstCode);
|
3231 | const type = keywords$1.get(word);
|
3232 | if (type !== undefined) {
|
3233 | this.finishToken(type, tokenLabelName(type));
|
3234 | } else {
|
3235 | this.finishToken(130, word);
|
3236 | }
|
3237 | }
|
3238 | checkKeywordEscapes() {
|
3239 | const {
|
3240 | type
|
3241 | } = this.state;
|
3242 | if (tokenIsKeyword(type) && this.state.containsEsc) {
|
3243 | this.raise(Errors.InvalidEscapedReservedWord, {
|
3244 | at: this.state.startLoc,
|
3245 | reservedWord: tokenLabelName(type)
|
3246 | });
|
3247 | }
|
3248 | }
|
3249 | raise(toParseError, raiseProperties) {
|
3250 | const {
|
3251 | at
|
3252 | } = raiseProperties,
|
3253 | details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
|
3254 | const loc = at instanceof Position ? at : at.loc.start;
|
3255 | const error = toParseError({
|
3256 | loc,
|
3257 | details
|
3258 | });
|
3259 | if (!this.options.errorRecovery) throw error;
|
3260 | if (!this.isLookahead) this.state.errors.push(error);
|
3261 | return error;
|
3262 | }
|
3263 | raiseOverwrite(toParseError, raiseProperties) {
|
3264 | const {
|
3265 | at
|
3266 | } = raiseProperties,
|
3267 | details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
|
3268 | const loc = at instanceof Position ? at : at.loc.start;
|
3269 | const pos = loc.index;
|
3270 | const errors = this.state.errors;
|
3271 | for (let i = errors.length - 1; i >= 0; i--) {
|
3272 | const error = errors[i];
|
3273 | if (error.loc.index === pos) {
|
3274 | return errors[i] = toParseError({
|
3275 | loc,
|
3276 | details
|
3277 | });
|
3278 | }
|
3279 | if (error.loc.index < pos) break;
|
3280 | }
|
3281 | return this.raise(toParseError, raiseProperties);
|
3282 | }
|
3283 | updateContext(prevType) {}
|
3284 | unexpected(loc, type) {
|
3285 | throw this.raise(Errors.UnexpectedToken, {
|
3286 | expected: type ? tokenLabelName(type) : null,
|
3287 | at: loc != null ? loc : this.state.startLoc
|
3288 | });
|
3289 | }
|
3290 | expectPlugin(pluginName, loc) {
|
3291 | if (this.hasPlugin(pluginName)) {
|
3292 | return true;
|
3293 | }
|
3294 | throw this.raise(Errors.MissingPlugin, {
|
3295 | at: loc != null ? loc : this.state.startLoc,
|
3296 | missingPlugin: [pluginName]
|
3297 | });
|
3298 | }
|
3299 | expectOnePlugin(pluginNames) {
|
3300 | if (!pluginNames.some(name => this.hasPlugin(name))) {
|
3301 | throw this.raise(Errors.MissingOneOfPlugins, {
|
3302 | at: this.state.startLoc,
|
3303 | missingPlugin: pluginNames
|
3304 | });
|
3305 | }
|
3306 | }
|
3307 | errorBuilder(error) {
|
3308 | return (pos, lineStart, curLine) => {
|
3309 | this.raise(error, {
|
3310 | at: buildPosition(pos, lineStart, curLine)
|
3311 | });
|
3312 | };
|
3313 | }
|
3314 | }
|
3315 | class ClassScope {
|
3316 | constructor() {
|
3317 | this.privateNames = new Set();
|
3318 | this.loneAccessors = new Map();
|
3319 | this.undefinedPrivateNames = new Map();
|
3320 | }
|
3321 | }
|
3322 | class ClassScopeHandler {
|
3323 | constructor(parser) {
|
3324 | this.parser = void 0;
|
3325 | this.stack = [];
|
3326 | this.undefinedPrivateNames = new Map();
|
3327 | this.parser = parser;
|
3328 | }
|
3329 | current() {
|
3330 | return this.stack[this.stack.length - 1];
|
3331 | }
|
3332 | enter() {
|
3333 | this.stack.push(new ClassScope());
|
3334 | }
|
3335 | exit() {
|
3336 | const oldClassScope = this.stack.pop();
|
3337 | const current = this.current();
|
3338 | for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
|
3339 | if (current) {
|
3340 | if (!current.undefinedPrivateNames.has(name)) {
|
3341 | current.undefinedPrivateNames.set(name, loc);
|
3342 | }
|
3343 | } else {
|
3344 | this.parser.raise(Errors.InvalidPrivateFieldResolution, {
|
3345 | at: loc,
|
3346 | identifierName: name
|
3347 | });
|
3348 | }
|
3349 | }
|
3350 | }
|
3351 | declarePrivateName(name, elementType, loc) {
|
3352 | const {
|
3353 | privateNames,
|
3354 | loneAccessors,
|
3355 | undefinedPrivateNames
|
3356 | } = this.current();
|
3357 | let redefined = privateNames.has(name);
|
3358 | if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
|
3359 | const accessor = redefined && loneAccessors.get(name);
|
3360 | if (accessor) {
|
3361 | const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
|
3362 | const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
|
3363 | const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
|
3364 | const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
|
3365 | redefined = oldKind === newKind || oldStatic !== newStatic;
|
3366 | if (!redefined) loneAccessors.delete(name);
|
3367 | } else if (!redefined) {
|
3368 | loneAccessors.set(name, elementType);
|
3369 | }
|
3370 | }
|
3371 | if (redefined) {
|
3372 | this.parser.raise(Errors.PrivateNameRedeclaration, {
|
3373 | at: loc,
|
3374 | identifierName: name
|
3375 | });
|
3376 | }
|
3377 | privateNames.add(name);
|
3378 | undefinedPrivateNames.delete(name);
|
3379 | }
|
3380 | usePrivateName(name, loc) {
|
3381 | let classScope;
|
3382 | for (classScope of this.stack) {
|
3383 | if (classScope.privateNames.has(name)) return;
|
3384 | }
|
3385 | if (classScope) {
|
3386 | classScope.undefinedPrivateNames.set(name, loc);
|
3387 | } else {
|
3388 | this.parser.raise(Errors.InvalidPrivateFieldResolution, {
|
3389 | at: loc,
|
3390 | identifierName: name
|
3391 | });
|
3392 | }
|
3393 | }
|
3394 | }
|
3395 | const kExpression = 0,
|
3396 | kMaybeArrowParameterDeclaration = 1,
|
3397 | kMaybeAsyncArrowParameterDeclaration = 2,
|
3398 | kParameterDeclaration = 3;
|
3399 | class ExpressionScope {
|
3400 | constructor(type = kExpression) {
|
3401 | this.type = void 0;
|
3402 | this.type = type;
|
3403 | }
|
3404 | canBeArrowParameterDeclaration() {
|
3405 | return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
|
3406 | }
|
3407 | isCertainlyParameterDeclaration() {
|
3408 | return this.type === kParameterDeclaration;
|
3409 | }
|
3410 | }
|
3411 | class ArrowHeadParsingScope extends ExpressionScope {
|
3412 | constructor(type) {
|
3413 | super(type);
|
3414 | this.declarationErrors = new Map();
|
3415 | }
|
3416 | recordDeclarationError(ParsingErrorClass, {
|
3417 | at
|
3418 | }) {
|
3419 | const index = at.index;
|
3420 | this.declarationErrors.set(index, [ParsingErrorClass, at]);
|
3421 | }
|
3422 | clearDeclarationError(index) {
|
3423 | this.declarationErrors.delete(index);
|
3424 | }
|
3425 | iterateErrors(iterator) {
|
3426 | this.declarationErrors.forEach(iterator);
|
3427 | }
|
3428 | }
|
3429 | class ExpressionScopeHandler {
|
3430 | constructor(parser) {
|
3431 | this.parser = void 0;
|
3432 | this.stack = [new ExpressionScope()];
|
3433 | this.parser = parser;
|
3434 | }
|
3435 | enter(scope) {
|
3436 | this.stack.push(scope);
|
3437 | }
|
3438 | exit() {
|
3439 | this.stack.pop();
|
3440 | }
|
3441 | recordParameterInitializerError(toParseError, {
|
3442 | at: node
|
3443 | }) {
|
3444 | const origin = {
|
3445 | at: node.loc.start
|
3446 | };
|
3447 | const {
|
3448 | stack
|
3449 | } = this;
|
3450 | let i = stack.length - 1;
|
3451 | let scope = stack[i];
|
3452 | while (!scope.isCertainlyParameterDeclaration()) {
|
3453 | if (scope.canBeArrowParameterDeclaration()) {
|
3454 | scope.recordDeclarationError(toParseError, origin);
|
3455 | } else {
|
3456 | return;
|
3457 | }
|
3458 | scope = stack[--i];
|
3459 | }
|
3460 | this.parser.raise(toParseError, origin);
|
3461 | }
|
3462 | recordArrowParameterBindingError(error, {
|
3463 | at: node
|
3464 | }) {
|
3465 | const {
|
3466 | stack
|
3467 | } = this;
|
3468 | const scope = stack[stack.length - 1];
|
3469 | const origin = {
|
3470 | at: node.loc.start
|
3471 | };
|
3472 | if (scope.isCertainlyParameterDeclaration()) {
|
3473 | this.parser.raise(error, origin);
|
3474 | } else if (scope.canBeArrowParameterDeclaration()) {
|
3475 | scope.recordDeclarationError(error, origin);
|
3476 | } else {
|
3477 | return;
|
3478 | }
|
3479 | }
|
3480 | recordAsyncArrowParametersError({
|
3481 | at
|
3482 | }) {
|
3483 | const {
|
3484 | stack
|
3485 | } = this;
|
3486 | let i = stack.length - 1;
|
3487 | let scope = stack[i];
|
3488 | while (scope.canBeArrowParameterDeclaration()) {
|
3489 | if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
|
3490 | scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
|
3491 | at
|
3492 | });
|
3493 | }
|
3494 | scope = stack[--i];
|
3495 | }
|
3496 | }
|
3497 | validateAsPattern() {
|
3498 | const {
|
3499 | stack
|
3500 | } = this;
|
3501 | const currentScope = stack[stack.length - 1];
|
3502 | if (!currentScope.canBeArrowParameterDeclaration()) return;
|
3503 | currentScope.iterateErrors(([toParseError, loc]) => {
|
3504 | this.parser.raise(toParseError, {
|
3505 | at: loc
|
3506 | });
|
3507 | let i = stack.length - 2;
|
3508 | let scope = stack[i];
|
3509 | while (scope.canBeArrowParameterDeclaration()) {
|
3510 | scope.clearDeclarationError(loc.index);
|
3511 | scope = stack[--i];
|
3512 | }
|
3513 | });
|
3514 | }
|
3515 | }
|
3516 | function newParameterDeclarationScope() {
|
3517 | return new ExpressionScope(kParameterDeclaration);
|
3518 | }
|
3519 | function newArrowHeadScope() {
|
3520 | return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
|
3521 | }
|
3522 | function newAsyncArrowScope() {
|
3523 | return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
|
3524 | }
|
3525 | function newExpressionScope() {
|
3526 | return new ExpressionScope();
|
3527 | }
|
3528 | const PARAM = 0b0000,
|
3529 | PARAM_YIELD = 0b0001,
|
3530 | PARAM_AWAIT = 0b0010,
|
3531 | PARAM_RETURN = 0b0100,
|
3532 | PARAM_IN = 0b1000;
|
3533 | class ProductionParameterHandler {
|
3534 | constructor() {
|
3535 | this.stacks = [];
|
3536 | }
|
3537 | enter(flags) {
|
3538 | this.stacks.push(flags);
|
3539 | }
|
3540 | exit() {
|
3541 | this.stacks.pop();
|
3542 | }
|
3543 | currentFlags() {
|
3544 | return this.stacks[this.stacks.length - 1];
|
3545 | }
|
3546 | get hasAwait() {
|
3547 | return (this.currentFlags() & PARAM_AWAIT) > 0;
|
3548 | }
|
3549 | get hasYield() {
|
3550 | return (this.currentFlags() & PARAM_YIELD) > 0;
|
3551 | }
|
3552 | get hasReturn() {
|
3553 | return (this.currentFlags() & PARAM_RETURN) > 0;
|
3554 | }
|
3555 | get hasIn() {
|
3556 | return (this.currentFlags() & PARAM_IN) > 0;
|
3557 | }
|
3558 | }
|
3559 | function functionFlags(isAsync, isGenerator) {
|
3560 | return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
|
3561 | }
|
3562 | class UtilParser extends Tokenizer {
|
3563 | addExtra(node, key, value, enumerable = true) {
|
3564 | if (!node) return;
|
3565 | const extra = node.extra = node.extra || {};
|
3566 | if (enumerable) {
|
3567 | extra[key] = value;
|
3568 | } else {
|
3569 | Object.defineProperty(extra, key, {
|
3570 | enumerable,
|
3571 | value
|
3572 | });
|
3573 | }
|
3574 | }
|
3575 | isContextual(token) {
|
3576 | return this.state.type === token && !this.state.containsEsc;
|
3577 | }
|
3578 | isUnparsedContextual(nameStart, name) {
|
3579 | const nameEnd = nameStart + name.length;
|
3580 | if (this.input.slice(nameStart, nameEnd) === name) {
|
3581 | const nextCh = this.input.charCodeAt(nameEnd);
|
3582 | return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
|
3583 | }
|
3584 | return false;
|
3585 | }
|
3586 | isLookaheadContextual(name) {
|
3587 | const next = this.nextTokenStart();
|
3588 | return this.isUnparsedContextual(next, name);
|
3589 | }
|
3590 | eatContextual(token) {
|
3591 | if (this.isContextual(token)) {
|
3592 | this.next();
|
3593 | return true;
|
3594 | }
|
3595 | return false;
|
3596 | }
|
3597 | expectContextual(token, toParseError) {
|
3598 | if (!this.eatContextual(token)) {
|
3599 | if (toParseError != null) {
|
3600 | throw this.raise(toParseError, {
|
3601 | at: this.state.startLoc
|
3602 | });
|
3603 | }
|
3604 | this.unexpected(null, token);
|
3605 | }
|
3606 | }
|
3607 | canInsertSemicolon() {
|
3608 | return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
|
3609 | }
|
3610 | hasPrecedingLineBreak() {
|
3611 | return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
|
3612 | }
|
3613 | hasFollowingLineBreak() {
|
3614 | skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
|
3615 | return skipWhiteSpaceToLineBreak.test(this.input);
|
3616 | }
|
3617 | isLineTerminator() {
|
3618 | return this.eat(13) || this.canInsertSemicolon();
|
3619 | }
|
3620 | semicolon(allowAsi = true) {
|
3621 | if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
|
3622 | this.raise(Errors.MissingSemicolon, {
|
3623 | at: this.state.lastTokEndLoc
|
3624 | });
|
3625 | }
|
3626 | expect(type, loc) {
|
3627 | this.eat(type) || this.unexpected(loc, type);
|
3628 | }
|
3629 | tryParse(fn, oldState = this.state.clone()) {
|
3630 | const abortSignal = {
|
3631 | node: null
|
3632 | };
|
3633 | try {
|
3634 | const node = fn((node = null) => {
|
3635 | abortSignal.node = node;
|
3636 | throw abortSignal;
|
3637 | });
|
3638 | if (this.state.errors.length > oldState.errors.length) {
|
3639 | const failState = this.state;
|
3640 | this.state = oldState;
|
3641 | this.state.tokensLength = failState.tokensLength;
|
3642 | return {
|
3643 | node,
|
3644 | error: failState.errors[oldState.errors.length],
|
3645 | thrown: false,
|
3646 | aborted: false,
|
3647 | failState
|
3648 | };
|
3649 | }
|
3650 | return {
|
3651 | node,
|
3652 | error: null,
|
3653 | thrown: false,
|
3654 | aborted: false,
|
3655 | failState: null
|
3656 | };
|
3657 | } catch (error) {
|
3658 | const failState = this.state;
|
3659 | this.state = oldState;
|
3660 | if (error instanceof SyntaxError) {
|
3661 | return {
|
3662 | node: null,
|
3663 | error,
|
3664 | thrown: true,
|
3665 | aborted: false,
|
3666 | failState
|
3667 | };
|
3668 | }
|
3669 | if (error === abortSignal) {
|
3670 | return {
|
3671 | node: abortSignal.node,
|
3672 | error: null,
|
3673 | thrown: false,
|
3674 | aborted: true,
|
3675 | failState
|
3676 | };
|
3677 | }
|
3678 | throw error;
|
3679 | }
|
3680 | }
|
3681 | checkExpressionErrors(refExpressionErrors, andThrow) {
|
3682 | if (!refExpressionErrors) return false;
|
3683 | const {
|
3684 | shorthandAssignLoc,
|
3685 | doubleProtoLoc,
|
3686 | privateKeyLoc,
|
3687 | optionalParametersLoc
|
3688 | } = refExpressionErrors;
|
3689 | const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
|
3690 | if (!andThrow) {
|
3691 | return hasErrors;
|
3692 | }
|
3693 | if (shorthandAssignLoc != null) {
|
3694 | this.raise(Errors.InvalidCoverInitializedName, {
|
3695 | at: shorthandAssignLoc
|
3696 | });
|
3697 | }
|
3698 | if (doubleProtoLoc != null) {
|
3699 | this.raise(Errors.DuplicateProto, {
|
3700 | at: doubleProtoLoc
|
3701 | });
|
3702 | }
|
3703 | if (privateKeyLoc != null) {
|
3704 | this.raise(Errors.UnexpectedPrivateField, {
|
3705 | at: privateKeyLoc
|
3706 | });
|
3707 | }
|
3708 | if (optionalParametersLoc != null) {
|
3709 | this.unexpected(optionalParametersLoc);
|
3710 | }
|
3711 | }
|
3712 | isLiteralPropertyName() {
|
3713 | return tokenIsLiteralPropertyName(this.state.type);
|
3714 | }
|
3715 | isPrivateName(node) {
|
3716 | return node.type === "PrivateName";
|
3717 | }
|
3718 | getPrivateNameSV(node) {
|
3719 | return node.id.name;
|
3720 | }
|
3721 | hasPropertyAsPrivateName(node) {
|
3722 | return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
|
3723 | }
|
3724 | isObjectProperty(node) {
|
3725 | return node.type === "ObjectProperty";
|
3726 | }
|
3727 | isObjectMethod(node) {
|
3728 | return node.type === "ObjectMethod";
|
3729 | }
|
3730 | initializeScopes(inModule = this.options.sourceType === "module") {
|
3731 | const oldLabels = this.state.labels;
|
3732 | this.state.labels = [];
|
3733 | const oldExportedIdentifiers = this.exportedIdentifiers;
|
3734 | this.exportedIdentifiers = new Set();
|
3735 | const oldInModule = this.inModule;
|
3736 | this.inModule = inModule;
|
3737 | const oldScope = this.scope;
|
3738 | const ScopeHandler = this.getScopeHandler();
|
3739 | this.scope = new ScopeHandler(this, inModule);
|
3740 | const oldProdParam = this.prodParam;
|
3741 | this.prodParam = new ProductionParameterHandler();
|
3742 | const oldClassScope = this.classScope;
|
3743 | this.classScope = new ClassScopeHandler(this);
|
3744 | const oldExpressionScope = this.expressionScope;
|
3745 | this.expressionScope = new ExpressionScopeHandler(this);
|
3746 | return () => {
|
3747 | this.state.labels = oldLabels;
|
3748 | this.exportedIdentifiers = oldExportedIdentifiers;
|
3749 | this.inModule = oldInModule;
|
3750 | this.scope = oldScope;
|
3751 | this.prodParam = oldProdParam;
|
3752 | this.classScope = oldClassScope;
|
3753 | this.expressionScope = oldExpressionScope;
|
3754 | };
|
3755 | }
|
3756 | enterInitialScopes() {
|
3757 | let paramFlags = PARAM;
|
3758 | if (this.inModule) {
|
3759 | paramFlags |= PARAM_AWAIT;
|
3760 | }
|
3761 | this.scope.enter(SCOPE_PROGRAM);
|
3762 | this.prodParam.enter(paramFlags);
|
3763 | }
|
3764 | checkDestructuringPrivate(refExpressionErrors) {
|
3765 | const {
|
3766 | privateKeyLoc
|
3767 | } = refExpressionErrors;
|
3768 | if (privateKeyLoc !== null) {
|
3769 | this.expectPlugin("destructuringPrivate", privateKeyLoc);
|
3770 | }
|
3771 | }
|
3772 | }
|
3773 | class ExpressionErrors {
|
3774 | constructor() {
|
3775 | this.shorthandAssignLoc = null;
|
3776 | this.doubleProtoLoc = null;
|
3777 | this.privateKeyLoc = null;
|
3778 | this.optionalParametersLoc = null;
|
3779 | }
|
3780 | }
|
3781 | class Node {
|
3782 | constructor(parser, pos, loc) {
|
3783 | this.type = "";
|
3784 | this.start = pos;
|
3785 | this.end = 0;
|
3786 | this.loc = new SourceLocation(loc);
|
3787 | if (parser != null && parser.options.ranges) this.range = [pos, 0];
|
3788 | if (parser != null && parser.filename) this.loc.filename = parser.filename;
|
3789 | }
|
3790 | }
|
3791 | const NodePrototype = Node.prototype;
|
3792 | {
|
3793 | NodePrototype.__clone = function () {
|
3794 | const newNode = new Node(undefined, this.start, this.loc.start);
|
3795 | const keys = Object.keys(this);
|
3796 | for (let i = 0, length = keys.length; i < length; i++) {
|
3797 | const key = keys[i];
|
3798 | if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
|
3799 | newNode[key] = this[key];
|
3800 | }
|
3801 | }
|
3802 | return newNode;
|
3803 | };
|
3804 | }
|
3805 | function clonePlaceholder(node) {
|
3806 | return cloneIdentifier(node);
|
3807 | }
|
3808 | function cloneIdentifier(node) {
|
3809 | const {
|
3810 | type,
|
3811 | start,
|
3812 | end,
|
3813 | loc,
|
3814 | range,
|
3815 | extra,
|
3816 | name
|
3817 | } = node;
|
3818 | const cloned = Object.create(NodePrototype);
|
3819 | cloned.type = type;
|
3820 | cloned.start = start;
|
3821 | cloned.end = end;
|
3822 | cloned.loc = loc;
|
3823 | cloned.range = range;
|
3824 | cloned.extra = extra;
|
3825 | cloned.name = name;
|
3826 | if (type === "Placeholder") {
|
3827 | cloned.expectedNode = node.expectedNode;
|
3828 | }
|
3829 | return cloned;
|
3830 | }
|
3831 | function cloneStringLiteral(node) {
|
3832 | const {
|
3833 | type,
|
3834 | start,
|
3835 | end,
|
3836 | loc,
|
3837 | range,
|
3838 | extra
|
3839 | } = node;
|
3840 | if (type === "Placeholder") {
|
3841 | return clonePlaceholder(node);
|
3842 | }
|
3843 | const cloned = Object.create(NodePrototype);
|
3844 | cloned.type = type;
|
3845 | cloned.start = start;
|
3846 | cloned.end = end;
|
3847 | cloned.loc = loc;
|
3848 | cloned.range = range;
|
3849 | if (node.raw !== undefined) {
|
3850 | cloned.raw = node.raw;
|
3851 | } else {
|
3852 | cloned.extra = extra;
|
3853 | }
|
3854 | cloned.value = node.value;
|
3855 | return cloned;
|
3856 | }
|
3857 | class NodeUtils extends UtilParser {
|
3858 | startNode() {
|
3859 | return new Node(this, this.state.start, this.state.startLoc);
|
3860 | }
|
3861 | startNodeAt(loc) {
|
3862 | return new Node(this, loc.index, loc);
|
3863 | }
|
3864 | startNodeAtNode(type) {
|
3865 | return this.startNodeAt(type.loc.start);
|
3866 | }
|
3867 | finishNode(node, type) {
|
3868 | return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
|
3869 | }
|
3870 | finishNodeAt(node, type, endLoc) {
|
3871 | node.type = type;
|
3872 | node.end = endLoc.index;
|
3873 | node.loc.end = endLoc;
|
3874 | if (this.options.ranges) node.range[1] = endLoc.index;
|
3875 | if (this.options.attachComment) this.processComment(node);
|
3876 | return node;
|
3877 | }
|
3878 | resetStartLocation(node, startLoc) {
|
3879 | node.start = startLoc.index;
|
3880 | node.loc.start = startLoc;
|
3881 | if (this.options.ranges) node.range[0] = startLoc.index;
|
3882 | }
|
3883 | resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
|
3884 | node.end = endLoc.index;
|
3885 | node.loc.end = endLoc;
|
3886 | if (this.options.ranges) node.range[1] = endLoc.index;
|
3887 | }
|
3888 | resetStartLocationFromNode(node, locationNode) {
|
3889 | this.resetStartLocation(node, locationNode.loc.start);
|
3890 | }
|
3891 | }
|
3892 | const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
|
3893 | const FlowErrors = ParseErrorEnum`flow`({
|
3894 | AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
|
3895 | 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.",
|
3896 | AssignReservedType: ({
|
3897 | reservedType
|
3898 | }) => `Cannot overwrite reserved type ${reservedType}.`,
|
3899 | DeclareClassElement: "The `declare` modifier can only appear on class fields.",
|
3900 | DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
|
3901 | DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
|
3902 | EnumBooleanMemberNotInitialized: ({
|
3903 | memberName,
|
3904 | enumName
|
3905 | }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
|
3906 | EnumDuplicateMemberName: ({
|
3907 | memberName,
|
3908 | enumName
|
3909 | }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
|
3910 | EnumInconsistentMemberValues: ({
|
3911 | enumName
|
3912 | }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
|
3913 | EnumInvalidExplicitType: ({
|
3914 | invalidEnumType,
|
3915 | enumName
|
3916 | }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
|
3917 | EnumInvalidExplicitTypeUnknownSupplied: ({
|
3918 | enumName
|
3919 | }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
|
3920 | EnumInvalidMemberInitializerPrimaryType: ({
|
3921 | enumName,
|
3922 | memberName,
|
3923 | explicitType
|
3924 | }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
|
3925 | EnumInvalidMemberInitializerSymbolType: ({
|
3926 | enumName,
|
3927 | memberName
|
3928 | }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
|
3929 | EnumInvalidMemberInitializerUnknownType: ({
|
3930 | enumName,
|
3931 | memberName
|
3932 | }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
|
3933 | EnumInvalidMemberName: ({
|
3934 | enumName,
|
3935 | memberName,
|
3936 | suggestion
|
3937 | }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
|
3938 | EnumNumberMemberNotInitialized: ({
|
3939 | enumName,
|
3940 | memberName
|
3941 | }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
|
3942 | EnumStringMemberInconsistentlyInitailized: ({
|
3943 | enumName
|
3944 | }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
|
3945 | GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
|
3946 | ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
|
3947 | 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.",
|
3948 | InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
|
3949 | InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
|
3950 | InexactVariance: "Explicit inexact syntax cannot have variance.",
|
3951 | InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
|
3952 | MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
|
3953 | NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
|
3954 | NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
|
3955 | PatternIsOptional: Object.assign({
|
3956 | message: "A binding pattern parameter cannot be optional in an implementation signature."
|
3957 | }, {
|
3958 | reasonCode: "OptionalBindingPattern"
|
3959 | }),
|
3960 | SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
|
3961 | SpreadVariance: "Spread properties cannot have variance.",
|
3962 | ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
|
3963 | ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
|
3964 | ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
|
3965 | ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
|
3966 | ThisParamNoDefault: "The `this` parameter may not have a default value.",
|
3967 | TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
|
3968 | TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
|
3969 | UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
|
3970 | UnexpectedReservedType: ({
|
3971 | reservedType
|
3972 | }) => `Unexpected reserved type ${reservedType}.`,
|
3973 | UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
|
3974 | UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
|
3975 | UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
|
3976 | UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
|
3977 | UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
|
3978 | UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
|
3979 | UnsupportedDeclareExportKind: ({
|
3980 | unsupportedExportKind,
|
3981 | suggestion
|
3982 | }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
|
3983 | UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
|
3984 | UnterminatedFlowComment: "Unterminated flow-comment."
|
3985 | });
|
3986 | function isEsModuleType(bodyElement) {
|
3987 | return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
|
3988 | }
|
3989 | function hasTypeImportKind(node) {
|
3990 | return node.importKind === "type" || node.importKind === "typeof";
|
3991 | }
|
3992 | const exportSuggestions = {
|
3993 | const: "declare export var",
|
3994 | let: "declare export var",
|
3995 | type: "export type",
|
3996 | interface: "export interface"
|
3997 | };
|
3998 | function partition(list, test) {
|
3999 | const list1 = [];
|
4000 | const list2 = [];
|
4001 | for (let i = 0; i < list.length; i++) {
|
4002 | (test(list[i], i, list) ? list1 : list2).push(list[i]);
|
4003 | }
|
4004 | return [list1, list2];
|
4005 | }
|
4006 | const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
|
4007 | var flow = superClass => class FlowParserMixin extends superClass {
|
4008 | constructor(...args) {
|
4009 | super(...args);
|
4010 | this.flowPragma = undefined;
|
4011 | }
|
4012 | getScopeHandler() {
|
4013 | return FlowScopeHandler;
|
4014 | }
|
4015 | shouldParseTypes() {
|
4016 | return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
|
4017 | }
|
4018 | shouldParseEnums() {
|
4019 | return !!this.getPluginOption("flow", "enums");
|
4020 | }
|
4021 | finishToken(type, val) {
|
4022 | if (type !== 131 && type !== 13 && type !== 28) {
|
4023 | if (this.flowPragma === undefined) {
|
4024 | this.flowPragma = null;
|
4025 | }
|
4026 | }
|
4027 | super.finishToken(type, val);
|
4028 | }
|
4029 | addComment(comment) {
|
4030 | if (this.flowPragma === undefined) {
|
4031 | const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
|
4032 | if (!matches) ;else if (matches[1] === "flow") {
|
4033 | this.flowPragma = "flow";
|
4034 | } else if (matches[1] === "noflow") {
|
4035 | this.flowPragma = "noflow";
|
4036 | } else {
|
4037 | throw new Error("Unexpected flow pragma");
|
4038 | }
|
4039 | }
|
4040 | super.addComment(comment);
|
4041 | }
|
4042 | flowParseTypeInitialiser(tok) {
|
4043 | const oldInType = this.state.inType;
|
4044 | this.state.inType = true;
|
4045 | this.expect(tok || 14);
|
4046 | const type = this.flowParseType();
|
4047 | this.state.inType = oldInType;
|
4048 | return type;
|
4049 | }
|
4050 | flowParsePredicate() {
|
4051 | const node = this.startNode();
|
4052 | const moduloLoc = this.state.startLoc;
|
4053 | this.next();
|
4054 | this.expectContextual(108);
|
4055 | if (this.state.lastTokStart > moduloLoc.index + 1) {
|
4056 | this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
|
4057 | at: moduloLoc
|
4058 | });
|
4059 | }
|
4060 | if (this.eat(10)) {
|
4061 | node.value = super.parseExpression();
|
4062 | this.expect(11);
|
4063 | return this.finishNode(node, "DeclaredPredicate");
|
4064 | } else {
|
4065 | return this.finishNode(node, "InferredPredicate");
|
4066 | }
|
4067 | }
|
4068 | flowParseTypeAndPredicateInitialiser() {
|
4069 | const oldInType = this.state.inType;
|
4070 | this.state.inType = true;
|
4071 | this.expect(14);
|
4072 | let type = null;
|
4073 | let predicate = null;
|
4074 | if (this.match(54)) {
|
4075 | this.state.inType = oldInType;
|
4076 | predicate = this.flowParsePredicate();
|
4077 | } else {
|
4078 | type = this.flowParseType();
|
4079 | this.state.inType = oldInType;
|
4080 | if (this.match(54)) {
|
4081 | predicate = this.flowParsePredicate();
|
4082 | }
|
4083 | }
|
4084 | return [type, predicate];
|
4085 | }
|
4086 | flowParseDeclareClass(node) {
|
4087 | this.next();
|
4088 | this.flowParseInterfaceish(node, true);
|
4089 | return this.finishNode(node, "DeclareClass");
|
4090 | }
|
4091 | flowParseDeclareFunction(node) {
|
4092 | this.next();
|
4093 | const id = node.id = this.parseIdentifier();
|
4094 | const typeNode = this.startNode();
|
4095 | const typeContainer = this.startNode();
|
4096 | if (this.match(47)) {
|
4097 | typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
|
4098 | } else {
|
4099 | typeNode.typeParameters = null;
|
4100 | }
|
4101 | this.expect(10);
|
4102 | const tmp = this.flowParseFunctionTypeParams();
|
4103 | typeNode.params = tmp.params;
|
4104 | typeNode.rest = tmp.rest;
|
4105 | typeNode.this = tmp._this;
|
4106 | this.expect(11);
|
4107 | [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
4108 | typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
|
4109 | id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
|
4110 | this.resetEndLocation(id);
|
4111 | this.semicolon();
|
4112 | this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
|
4113 | return this.finishNode(node, "DeclareFunction");
|
4114 | }
|
4115 | flowParseDeclare(node, insideModule) {
|
4116 | if (this.match(80)) {
|
4117 | return this.flowParseDeclareClass(node);
|
4118 | } else if (this.match(68)) {
|
4119 | return this.flowParseDeclareFunction(node);
|
4120 | } else if (this.match(74)) {
|
4121 | return this.flowParseDeclareVariable(node);
|
4122 | } else if (this.eatContextual(125)) {
|
4123 | if (this.match(16)) {
|
4124 | return this.flowParseDeclareModuleExports(node);
|
4125 | } else {
|
4126 | if (insideModule) {
|
4127 | this.raise(FlowErrors.NestedDeclareModule, {
|
4128 | at: this.state.lastTokStartLoc
|
4129 | });
|
4130 | }
|
4131 | return this.flowParseDeclareModule(node);
|
4132 | }
|
4133 | } else if (this.isContextual(128)) {
|
4134 | return this.flowParseDeclareTypeAlias(node);
|
4135 | } else if (this.isContextual(129)) {
|
4136 | return this.flowParseDeclareOpaqueType(node);
|
4137 | } else if (this.isContextual(127)) {
|
4138 | return this.flowParseDeclareInterface(node);
|
4139 | } else if (this.match(82)) {
|
4140 | return this.flowParseDeclareExportDeclaration(node, insideModule);
|
4141 | } else {
|
4142 | this.unexpected();
|
4143 | }
|
4144 | }
|
4145 | flowParseDeclareVariable(node) {
|
4146 | this.next();
|
4147 | node.id = this.flowParseTypeAnnotatableIdentifier(true);
|
4148 | this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
|
4149 | this.semicolon();
|
4150 | return this.finishNode(node, "DeclareVariable");
|
4151 | }
|
4152 | flowParseDeclareModule(node) {
|
4153 | this.scope.enter(SCOPE_OTHER);
|
4154 | if (this.match(131)) {
|
4155 | node.id = super.parseExprAtom();
|
4156 | } else {
|
4157 | node.id = this.parseIdentifier();
|
4158 | }
|
4159 | const bodyNode = node.body = this.startNode();
|
4160 | const body = bodyNode.body = [];
|
4161 | this.expect(5);
|
4162 | while (!this.match(8)) {
|
4163 | let bodyNode = this.startNode();
|
4164 | if (this.match(83)) {
|
4165 | this.next();
|
4166 | if (!this.isContextual(128) && !this.match(87)) {
|
4167 | this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
|
4168 | at: this.state.lastTokStartLoc
|
4169 | });
|
4170 | }
|
4171 | super.parseImport(bodyNode);
|
4172 | } else {
|
4173 | this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
|
4174 | bodyNode = this.flowParseDeclare(bodyNode, true);
|
4175 | }
|
4176 | body.push(bodyNode);
|
4177 | }
|
4178 | this.scope.exit();
|
4179 | this.expect(8);
|
4180 | this.finishNode(bodyNode, "BlockStatement");
|
4181 | let kind = null;
|
4182 | let hasModuleExport = false;
|
4183 | body.forEach(bodyElement => {
|
4184 | if (isEsModuleType(bodyElement)) {
|
4185 | if (kind === "CommonJS") {
|
4186 | this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
|
4187 | at: bodyElement
|
4188 | });
|
4189 | }
|
4190 | kind = "ES";
|
4191 | } else if (bodyElement.type === "DeclareModuleExports") {
|
4192 | if (hasModuleExport) {
|
4193 | this.raise(FlowErrors.DuplicateDeclareModuleExports, {
|
4194 | at: bodyElement
|
4195 | });
|
4196 | }
|
4197 | if (kind === "ES") {
|
4198 | this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
|
4199 | at: bodyElement
|
4200 | });
|
4201 | }
|
4202 | kind = "CommonJS";
|
4203 | hasModuleExport = true;
|
4204 | }
|
4205 | });
|
4206 | node.kind = kind || "CommonJS";
|
4207 | return this.finishNode(node, "DeclareModule");
|
4208 | }
|
4209 | flowParseDeclareExportDeclaration(node, insideModule) {
|
4210 | this.expect(82);
|
4211 | if (this.eat(65)) {
|
4212 | if (this.match(68) || this.match(80)) {
|
4213 | node.declaration = this.flowParseDeclare(this.startNode());
|
4214 | } else {
|
4215 | node.declaration = this.flowParseType();
|
4216 | this.semicolon();
|
4217 | }
|
4218 | node.default = true;
|
4219 | return this.finishNode(node, "DeclareExportDeclaration");
|
4220 | } else {
|
4221 | if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
|
4222 | const label = this.state.value;
|
4223 | throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
|
4224 | at: this.state.startLoc,
|
4225 | unsupportedExportKind: label,
|
4226 | suggestion: exportSuggestions[label]
|
4227 | });
|
4228 | }
|
4229 | if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
|
4230 | node.declaration = this.flowParseDeclare(this.startNode());
|
4231 | node.default = false;
|
4232 | return this.finishNode(node, "DeclareExportDeclaration");
|
4233 | } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
|
4234 | node = this.parseExport(node, null);
|
4235 | if (node.type === "ExportNamedDeclaration") {
|
4236 | node.type = "ExportDeclaration";
|
4237 | node.default = false;
|
4238 | delete node.exportKind;
|
4239 | }
|
4240 | node.type = "Declare" + node.type;
|
4241 | return node;
|
4242 | }
|
4243 | }
|
4244 | this.unexpected();
|
4245 | }
|
4246 | flowParseDeclareModuleExports(node) {
|
4247 | this.next();
|
4248 | this.expectContextual(109);
|
4249 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
4250 | this.semicolon();
|
4251 | return this.finishNode(node, "DeclareModuleExports");
|
4252 | }
|
4253 | flowParseDeclareTypeAlias(node) {
|
4254 | this.next();
|
4255 | const finished = this.flowParseTypeAlias(node);
|
4256 | finished.type = "DeclareTypeAlias";
|
4257 | return finished;
|
4258 | }
|
4259 | flowParseDeclareOpaqueType(node) {
|
4260 | this.next();
|
4261 | const finished = this.flowParseOpaqueType(node, true);
|
4262 | finished.type = "DeclareOpaqueType";
|
4263 | return finished;
|
4264 | }
|
4265 | flowParseDeclareInterface(node) {
|
4266 | this.next();
|
4267 | this.flowParseInterfaceish(node, false);
|
4268 | return this.finishNode(node, "DeclareInterface");
|
4269 | }
|
4270 | flowParseInterfaceish(node, isClass) {
|
4271 | node.id = this.flowParseRestrictedIdentifier(!isClass, true);
|
4272 | this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
|
4273 | if (this.match(47)) {
|
4274 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4275 | } else {
|
4276 | node.typeParameters = null;
|
4277 | }
|
4278 | node.extends = [];
|
4279 | if (this.eat(81)) {
|
4280 | do {
|
4281 | node.extends.push(this.flowParseInterfaceExtends());
|
4282 | } while (!isClass && this.eat(12));
|
4283 | }
|
4284 | if (isClass) {
|
4285 | node.implements = [];
|
4286 | node.mixins = [];
|
4287 | if (this.eatContextual(115)) {
|
4288 | do {
|
4289 | node.mixins.push(this.flowParseInterfaceExtends());
|
4290 | } while (this.eat(12));
|
4291 | }
|
4292 | if (this.eatContextual(111)) {
|
4293 | do {
|
4294 | node.implements.push(this.flowParseInterfaceExtends());
|
4295 | } while (this.eat(12));
|
4296 | }
|
4297 | }
|
4298 | node.body = this.flowParseObjectType({
|
4299 | allowStatic: isClass,
|
4300 | allowExact: false,
|
4301 | allowSpread: false,
|
4302 | allowProto: isClass,
|
4303 | allowInexact: false
|
4304 | });
|
4305 | }
|
4306 | flowParseInterfaceExtends() {
|
4307 | const node = this.startNode();
|
4308 | node.id = this.flowParseQualifiedTypeIdentifier();
|
4309 | if (this.match(47)) {
|
4310 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
4311 | } else {
|
4312 | node.typeParameters = null;
|
4313 | }
|
4314 | return this.finishNode(node, "InterfaceExtends");
|
4315 | }
|
4316 | flowParseInterface(node) {
|
4317 | this.flowParseInterfaceish(node, false);
|
4318 | return this.finishNode(node, "InterfaceDeclaration");
|
4319 | }
|
4320 | checkNotUnderscore(word) {
|
4321 | if (word === "_") {
|
4322 | this.raise(FlowErrors.UnexpectedReservedUnderscore, {
|
4323 | at: this.state.startLoc
|
4324 | });
|
4325 | }
|
4326 | }
|
4327 | checkReservedType(word, startLoc, declaration) {
|
4328 | if (!reservedTypes.has(word)) return;
|
4329 | this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
|
4330 | at: startLoc,
|
4331 | reservedType: word
|
4332 | });
|
4333 | }
|
4334 | flowParseRestrictedIdentifier(liberal, declaration) {
|
4335 | this.checkReservedType(this.state.value, this.state.startLoc, declaration);
|
4336 | return this.parseIdentifier(liberal);
|
4337 | }
|
4338 | flowParseTypeAlias(node) {
|
4339 | node.id = this.flowParseRestrictedIdentifier(false, true);
|
4340 | this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
|
4341 | if (this.match(47)) {
|
4342 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4343 | } else {
|
4344 | node.typeParameters = null;
|
4345 | }
|
4346 | node.right = this.flowParseTypeInitialiser(29);
|
4347 | this.semicolon();
|
4348 | return this.finishNode(node, "TypeAlias");
|
4349 | }
|
4350 | flowParseOpaqueType(node, declare) {
|
4351 | this.expectContextual(128);
|
4352 | node.id = this.flowParseRestrictedIdentifier(true, true);
|
4353 | this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
|
4354 | if (this.match(47)) {
|
4355 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4356 | } else {
|
4357 | node.typeParameters = null;
|
4358 | }
|
4359 | node.supertype = null;
|
4360 | if (this.match(14)) {
|
4361 | node.supertype = this.flowParseTypeInitialiser(14);
|
4362 | }
|
4363 | node.impltype = null;
|
4364 | if (!declare) {
|
4365 | node.impltype = this.flowParseTypeInitialiser(29);
|
4366 | }
|
4367 | this.semicolon();
|
4368 | return this.finishNode(node, "OpaqueType");
|
4369 | }
|
4370 | flowParseTypeParameter(requireDefault = false) {
|
4371 | const nodeStartLoc = this.state.startLoc;
|
4372 | const node = this.startNode();
|
4373 | const variance = this.flowParseVariance();
|
4374 | const ident = this.flowParseTypeAnnotatableIdentifier();
|
4375 | node.name = ident.name;
|
4376 | node.variance = variance;
|
4377 | node.bound = ident.typeAnnotation;
|
4378 | if (this.match(29)) {
|
4379 | this.eat(29);
|
4380 | node.default = this.flowParseType();
|
4381 | } else {
|
4382 | if (requireDefault) {
|
4383 | this.raise(FlowErrors.MissingTypeParamDefault, {
|
4384 | at: nodeStartLoc
|
4385 | });
|
4386 | }
|
4387 | }
|
4388 | return this.finishNode(node, "TypeParameter");
|
4389 | }
|
4390 | flowParseTypeParameterDeclaration() {
|
4391 | const oldInType = this.state.inType;
|
4392 | const node = this.startNode();
|
4393 | node.params = [];
|
4394 | this.state.inType = true;
|
4395 | if (this.match(47) || this.match(140)) {
|
4396 | this.next();
|
4397 | } else {
|
4398 | this.unexpected();
|
4399 | }
|
4400 | let defaultRequired = false;
|
4401 | do {
|
4402 | const typeParameter = this.flowParseTypeParameter(defaultRequired);
|
4403 | node.params.push(typeParameter);
|
4404 | if (typeParameter.default) {
|
4405 | defaultRequired = true;
|
4406 | }
|
4407 | if (!this.match(48)) {
|
4408 | this.expect(12);
|
4409 | }
|
4410 | } while (!this.match(48));
|
4411 | this.expect(48);
|
4412 | this.state.inType = oldInType;
|
4413 | return this.finishNode(node, "TypeParameterDeclaration");
|
4414 | }
|
4415 | flowParseTypeParameterInstantiation() {
|
4416 | const node = this.startNode();
|
4417 | const oldInType = this.state.inType;
|
4418 | node.params = [];
|
4419 | this.state.inType = true;
|
4420 | this.expect(47);
|
4421 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
4422 | this.state.noAnonFunctionType = false;
|
4423 | while (!this.match(48)) {
|
4424 | node.params.push(this.flowParseType());
|
4425 | if (!this.match(48)) {
|
4426 | this.expect(12);
|
4427 | }
|
4428 | }
|
4429 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
4430 | this.expect(48);
|
4431 | this.state.inType = oldInType;
|
4432 | return this.finishNode(node, "TypeParameterInstantiation");
|
4433 | }
|
4434 | flowParseTypeParameterInstantiationCallOrNew() {
|
4435 | const node = this.startNode();
|
4436 | const oldInType = this.state.inType;
|
4437 | node.params = [];
|
4438 | this.state.inType = true;
|
4439 | this.expect(47);
|
4440 | while (!this.match(48)) {
|
4441 | node.params.push(this.flowParseTypeOrImplicitInstantiation());
|
4442 | if (!this.match(48)) {
|
4443 | this.expect(12);
|
4444 | }
|
4445 | }
|
4446 | this.expect(48);
|
4447 | this.state.inType = oldInType;
|
4448 | return this.finishNode(node, "TypeParameterInstantiation");
|
4449 | }
|
4450 | flowParseInterfaceType() {
|
4451 | const node = this.startNode();
|
4452 | this.expectContextual(127);
|
4453 | node.extends = [];
|
4454 | if (this.eat(81)) {
|
4455 | do {
|
4456 | node.extends.push(this.flowParseInterfaceExtends());
|
4457 | } while (this.eat(12));
|
4458 | }
|
4459 | node.body = this.flowParseObjectType({
|
4460 | allowStatic: false,
|
4461 | allowExact: false,
|
4462 | allowSpread: false,
|
4463 | allowProto: false,
|
4464 | allowInexact: false
|
4465 | });
|
4466 | return this.finishNode(node, "InterfaceTypeAnnotation");
|
4467 | }
|
4468 | flowParseObjectPropertyKey() {
|
4469 | return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
|
4470 | }
|
4471 | flowParseObjectTypeIndexer(node, isStatic, variance) {
|
4472 | node.static = isStatic;
|
4473 | if (this.lookahead().type === 14) {
|
4474 | node.id = this.flowParseObjectPropertyKey();
|
4475 | node.key = this.flowParseTypeInitialiser();
|
4476 | } else {
|
4477 | node.id = null;
|
4478 | node.key = this.flowParseType();
|
4479 | }
|
4480 | this.expect(3);
|
4481 | node.value = this.flowParseTypeInitialiser();
|
4482 | node.variance = variance;
|
4483 | return this.finishNode(node, "ObjectTypeIndexer");
|
4484 | }
|
4485 | flowParseObjectTypeInternalSlot(node, isStatic) {
|
4486 | node.static = isStatic;
|
4487 | node.id = this.flowParseObjectPropertyKey();
|
4488 | this.expect(3);
|
4489 | this.expect(3);
|
4490 | if (this.match(47) || this.match(10)) {
|
4491 | node.method = true;
|
4492 | node.optional = false;
|
4493 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
|
4494 | } else {
|
4495 | node.method = false;
|
4496 | if (this.eat(17)) {
|
4497 | node.optional = true;
|
4498 | }
|
4499 | node.value = this.flowParseTypeInitialiser();
|
4500 | }
|
4501 | return this.finishNode(node, "ObjectTypeInternalSlot");
|
4502 | }
|
4503 | flowParseObjectTypeMethodish(node) {
|
4504 | node.params = [];
|
4505 | node.rest = null;
|
4506 | node.typeParameters = null;
|
4507 | node.this = null;
|
4508 | if (this.match(47)) {
|
4509 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4510 | }
|
4511 | this.expect(10);
|
4512 | if (this.match(78)) {
|
4513 | node.this = this.flowParseFunctionTypeParam(true);
|
4514 | node.this.name = null;
|
4515 | if (!this.match(11)) {
|
4516 | this.expect(12);
|
4517 | }
|
4518 | }
|
4519 | while (!this.match(11) && !this.match(21)) {
|
4520 | node.params.push(this.flowParseFunctionTypeParam(false));
|
4521 | if (!this.match(11)) {
|
4522 | this.expect(12);
|
4523 | }
|
4524 | }
|
4525 | if (this.eat(21)) {
|
4526 | node.rest = this.flowParseFunctionTypeParam(false);
|
4527 | }
|
4528 | this.expect(11);
|
4529 | node.returnType = this.flowParseTypeInitialiser();
|
4530 | return this.finishNode(node, "FunctionTypeAnnotation");
|
4531 | }
|
4532 | flowParseObjectTypeCallProperty(node, isStatic) {
|
4533 | const valueNode = this.startNode();
|
4534 | node.static = isStatic;
|
4535 | node.value = this.flowParseObjectTypeMethodish(valueNode);
|
4536 | return this.finishNode(node, "ObjectTypeCallProperty");
|
4537 | }
|
4538 | flowParseObjectType({
|
4539 | allowStatic,
|
4540 | allowExact,
|
4541 | allowSpread,
|
4542 | allowProto,
|
4543 | allowInexact
|
4544 | }) {
|
4545 | const oldInType = this.state.inType;
|
4546 | this.state.inType = true;
|
4547 | const nodeStart = this.startNode();
|
4548 | nodeStart.callProperties = [];
|
4549 | nodeStart.properties = [];
|
4550 | nodeStart.indexers = [];
|
4551 | nodeStart.internalSlots = [];
|
4552 | let endDelim;
|
4553 | let exact;
|
4554 | let inexact = false;
|
4555 | if (allowExact && this.match(6)) {
|
4556 | this.expect(6);
|
4557 | endDelim = 9;
|
4558 | exact = true;
|
4559 | } else {
|
4560 | this.expect(5);
|
4561 | endDelim = 8;
|
4562 | exact = false;
|
4563 | }
|
4564 | nodeStart.exact = exact;
|
4565 | while (!this.match(endDelim)) {
|
4566 | let isStatic = false;
|
4567 | let protoStartLoc = null;
|
4568 | let inexactStartLoc = null;
|
4569 | const node = this.startNode();
|
4570 | if (allowProto && this.isContextual(116)) {
|
4571 | const lookahead = this.lookahead();
|
4572 | if (lookahead.type !== 14 && lookahead.type !== 17) {
|
4573 | this.next();
|
4574 | protoStartLoc = this.state.startLoc;
|
4575 | allowStatic = false;
|
4576 | }
|
4577 | }
|
4578 | if (allowStatic && this.isContextual(104)) {
|
4579 | const lookahead = this.lookahead();
|
4580 | if (lookahead.type !== 14 && lookahead.type !== 17) {
|
4581 | this.next();
|
4582 | isStatic = true;
|
4583 | }
|
4584 | }
|
4585 | const variance = this.flowParseVariance();
|
4586 | if (this.eat(0)) {
|
4587 | if (protoStartLoc != null) {
|
4588 | this.unexpected(protoStartLoc);
|
4589 | }
|
4590 | if (this.eat(0)) {
|
4591 | if (variance) {
|
4592 | this.unexpected(variance.loc.start);
|
4593 | }
|
4594 | nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
|
4595 | } else {
|
4596 | nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
|
4597 | }
|
4598 | } else if (this.match(10) || this.match(47)) {
|
4599 | if (protoStartLoc != null) {
|
4600 | this.unexpected(protoStartLoc);
|
4601 | }
|
4602 | if (variance) {
|
4603 | this.unexpected(variance.loc.start);
|
4604 | }
|
4605 | nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
|
4606 | } else {
|
4607 | let kind = "init";
|
4608 | if (this.isContextual(98) || this.isContextual(103)) {
|
4609 | const lookahead = this.lookahead();
|
4610 | if (tokenIsLiteralPropertyName(lookahead.type)) {
|
4611 | kind = this.state.value;
|
4612 | this.next();
|
4613 | }
|
4614 | }
|
4615 | const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
|
4616 | if (propOrInexact === null) {
|
4617 | inexact = true;
|
4618 | inexactStartLoc = this.state.lastTokStartLoc;
|
4619 | } else {
|
4620 | nodeStart.properties.push(propOrInexact);
|
4621 | }
|
4622 | }
|
4623 | this.flowObjectTypeSemicolon();
|
4624 | if (inexactStartLoc && !this.match(8) && !this.match(9)) {
|
4625 | this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
|
4626 | at: inexactStartLoc
|
4627 | });
|
4628 | }
|
4629 | }
|
4630 | this.expect(endDelim);
|
4631 | if (allowSpread) {
|
4632 | nodeStart.inexact = inexact;
|
4633 | }
|
4634 | const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
|
4635 | this.state.inType = oldInType;
|
4636 | return out;
|
4637 | }
|
4638 | flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
|
4639 | if (this.eat(21)) {
|
4640 | const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
|
4641 | if (isInexactToken) {
|
4642 | if (!allowSpread) {
|
4643 | this.raise(FlowErrors.InexactInsideNonObject, {
|
4644 | at: this.state.lastTokStartLoc
|
4645 | });
|
4646 | } else if (!allowInexact) {
|
4647 | this.raise(FlowErrors.InexactInsideExact, {
|
4648 | at: this.state.lastTokStartLoc
|
4649 | });
|
4650 | }
|
4651 | if (variance) {
|
4652 | this.raise(FlowErrors.InexactVariance, {
|
4653 | at: variance
|
4654 | });
|
4655 | }
|
4656 | return null;
|
4657 | }
|
4658 | if (!allowSpread) {
|
4659 | this.raise(FlowErrors.UnexpectedSpreadType, {
|
4660 | at: this.state.lastTokStartLoc
|
4661 | });
|
4662 | }
|
4663 | if (protoStartLoc != null) {
|
4664 | this.unexpected(protoStartLoc);
|
4665 | }
|
4666 | if (variance) {
|
4667 | this.raise(FlowErrors.SpreadVariance, {
|
4668 | at: variance
|
4669 | });
|
4670 | }
|
4671 | node.argument = this.flowParseType();
|
4672 | return this.finishNode(node, "ObjectTypeSpreadProperty");
|
4673 | } else {
|
4674 | node.key = this.flowParseObjectPropertyKey();
|
4675 | node.static = isStatic;
|
4676 | node.proto = protoStartLoc != null;
|
4677 | node.kind = kind;
|
4678 | let optional = false;
|
4679 | if (this.match(47) || this.match(10)) {
|
4680 | node.method = true;
|
4681 | if (protoStartLoc != null) {
|
4682 | this.unexpected(protoStartLoc);
|
4683 | }
|
4684 | if (variance) {
|
4685 | this.unexpected(variance.loc.start);
|
4686 | }
|
4687 | node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
|
4688 | if (kind === "get" || kind === "set") {
|
4689 | this.flowCheckGetterSetterParams(node);
|
4690 | }
|
4691 | if (!allowSpread && node.key.name === "constructor" && node.value.this) {
|
4692 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
4693 | at: node.value.this
|
4694 | });
|
4695 | }
|
4696 | } else {
|
4697 | if (kind !== "init") this.unexpected();
|
4698 | node.method = false;
|
4699 | if (this.eat(17)) {
|
4700 | optional = true;
|
4701 | }
|
4702 | node.value = this.flowParseTypeInitialiser();
|
4703 | node.variance = variance;
|
4704 | }
|
4705 | node.optional = optional;
|
4706 | return this.finishNode(node, "ObjectTypeProperty");
|
4707 | }
|
4708 | }
|
4709 | flowCheckGetterSetterParams(property) {
|
4710 | const paramCount = property.kind === "get" ? 0 : 1;
|
4711 | const length = property.value.params.length + (property.value.rest ? 1 : 0);
|
4712 | if (property.value.this) {
|
4713 | this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
|
4714 | at: property.value.this
|
4715 | });
|
4716 | }
|
4717 | if (length !== paramCount) {
|
4718 | this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
|
4719 | at: property
|
4720 | });
|
4721 | }
|
4722 | if (property.kind === "set" && property.value.rest) {
|
4723 | this.raise(Errors.BadSetterRestParameter, {
|
4724 | at: property
|
4725 | });
|
4726 | }
|
4727 | }
|
4728 | flowObjectTypeSemicolon() {
|
4729 | if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
|
4730 | this.unexpected();
|
4731 | }
|
4732 | }
|
4733 | flowParseQualifiedTypeIdentifier(startLoc, id) {
|
4734 | var _startLoc;
|
4735 | (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
|
4736 | let node = id || this.flowParseRestrictedIdentifier(true);
|
4737 | while (this.eat(16)) {
|
4738 | const node2 = this.startNodeAt(startLoc);
|
4739 | node2.qualification = node;
|
4740 | node2.id = this.flowParseRestrictedIdentifier(true);
|
4741 | node = this.finishNode(node2, "QualifiedTypeIdentifier");
|
4742 | }
|
4743 | return node;
|
4744 | }
|
4745 | flowParseGenericType(startLoc, id) {
|
4746 | const node = this.startNodeAt(startLoc);
|
4747 | node.typeParameters = null;
|
4748 | node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
|
4749 | if (this.match(47)) {
|
4750 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
4751 | }
|
4752 | return this.finishNode(node, "GenericTypeAnnotation");
|
4753 | }
|
4754 | flowParseTypeofType() {
|
4755 | const node = this.startNode();
|
4756 | this.expect(87);
|
4757 | node.argument = this.flowParsePrimaryType();
|
4758 | return this.finishNode(node, "TypeofTypeAnnotation");
|
4759 | }
|
4760 | flowParseTupleType() {
|
4761 | const node = this.startNode();
|
4762 | node.types = [];
|
4763 | this.expect(0);
|
4764 | while (this.state.pos < this.length && !this.match(3)) {
|
4765 | node.types.push(this.flowParseType());
|
4766 | if (this.match(3)) break;
|
4767 | this.expect(12);
|
4768 | }
|
4769 | this.expect(3);
|
4770 | return this.finishNode(node, "TupleTypeAnnotation");
|
4771 | }
|
4772 | flowParseFunctionTypeParam(first) {
|
4773 | let name = null;
|
4774 | let optional = false;
|
4775 | let typeAnnotation = null;
|
4776 | const node = this.startNode();
|
4777 | const lh = this.lookahead();
|
4778 | const isThis = this.state.type === 78;
|
4779 | if (lh.type === 14 || lh.type === 17) {
|
4780 | if (isThis && !first) {
|
4781 | this.raise(FlowErrors.ThisParamMustBeFirst, {
|
4782 | at: node
|
4783 | });
|
4784 | }
|
4785 | name = this.parseIdentifier(isThis);
|
4786 | if (this.eat(17)) {
|
4787 | optional = true;
|
4788 | if (isThis) {
|
4789 | this.raise(FlowErrors.ThisParamMayNotBeOptional, {
|
4790 | at: node
|
4791 | });
|
4792 | }
|
4793 | }
|
4794 | typeAnnotation = this.flowParseTypeInitialiser();
|
4795 | } else {
|
4796 | typeAnnotation = this.flowParseType();
|
4797 | }
|
4798 | node.name = name;
|
4799 | node.optional = optional;
|
4800 | node.typeAnnotation = typeAnnotation;
|
4801 | return this.finishNode(node, "FunctionTypeParam");
|
4802 | }
|
4803 | reinterpretTypeAsFunctionTypeParam(type) {
|
4804 | const node = this.startNodeAt(type.loc.start);
|
4805 | node.name = null;
|
4806 | node.optional = false;
|
4807 | node.typeAnnotation = type;
|
4808 | return this.finishNode(node, "FunctionTypeParam");
|
4809 | }
|
4810 | flowParseFunctionTypeParams(params = []) {
|
4811 | let rest = null;
|
4812 | let _this = null;
|
4813 | if (this.match(78)) {
|
4814 | _this = this.flowParseFunctionTypeParam(true);
|
4815 | _this.name = null;
|
4816 | if (!this.match(11)) {
|
4817 | this.expect(12);
|
4818 | }
|
4819 | }
|
4820 | while (!this.match(11) && !this.match(21)) {
|
4821 | params.push(this.flowParseFunctionTypeParam(false));
|
4822 | if (!this.match(11)) {
|
4823 | this.expect(12);
|
4824 | }
|
4825 | }
|
4826 | if (this.eat(21)) {
|
4827 | rest = this.flowParseFunctionTypeParam(false);
|
4828 | }
|
4829 | return {
|
4830 | params,
|
4831 | rest,
|
4832 | _this
|
4833 | };
|
4834 | }
|
4835 | flowIdentToTypeAnnotation(startLoc, node, id) {
|
4836 | switch (id.name) {
|
4837 | case "any":
|
4838 | return this.finishNode(node, "AnyTypeAnnotation");
|
4839 | case "bool":
|
4840 | case "boolean":
|
4841 | return this.finishNode(node, "BooleanTypeAnnotation");
|
4842 | case "mixed":
|
4843 | return this.finishNode(node, "MixedTypeAnnotation");
|
4844 | case "empty":
|
4845 | return this.finishNode(node, "EmptyTypeAnnotation");
|
4846 | case "number":
|
4847 | return this.finishNode(node, "NumberTypeAnnotation");
|
4848 | case "string":
|
4849 | return this.finishNode(node, "StringTypeAnnotation");
|
4850 | case "symbol":
|
4851 | return this.finishNode(node, "SymbolTypeAnnotation");
|
4852 | default:
|
4853 | this.checkNotUnderscore(id.name);
|
4854 | return this.flowParseGenericType(startLoc, id);
|
4855 | }
|
4856 | }
|
4857 | flowParsePrimaryType() {
|
4858 | const startLoc = this.state.startLoc;
|
4859 | const node = this.startNode();
|
4860 | let tmp;
|
4861 | let type;
|
4862 | let isGroupedType = false;
|
4863 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
4864 | switch (this.state.type) {
|
4865 | case 5:
|
4866 | return this.flowParseObjectType({
|
4867 | allowStatic: false,
|
4868 | allowExact: false,
|
4869 | allowSpread: true,
|
4870 | allowProto: false,
|
4871 | allowInexact: true
|
4872 | });
|
4873 | case 6:
|
4874 | return this.flowParseObjectType({
|
4875 | allowStatic: false,
|
4876 | allowExact: true,
|
4877 | allowSpread: true,
|
4878 | allowProto: false,
|
4879 | allowInexact: false
|
4880 | });
|
4881 | case 0:
|
4882 | this.state.noAnonFunctionType = false;
|
4883 | type = this.flowParseTupleType();
|
4884 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
4885 | return type;
|
4886 | case 47:
|
4887 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
4888 | this.expect(10);
|
4889 | tmp = this.flowParseFunctionTypeParams();
|
4890 | node.params = tmp.params;
|
4891 | node.rest = tmp.rest;
|
4892 | node.this = tmp._this;
|
4893 | this.expect(11);
|
4894 | this.expect(19);
|
4895 | node.returnType = this.flowParseType();
|
4896 | return this.finishNode(node, "FunctionTypeAnnotation");
|
4897 | case 10:
|
4898 | this.next();
|
4899 | if (!this.match(11) && !this.match(21)) {
|
4900 | if (tokenIsIdentifier(this.state.type) || this.match(78)) {
|
4901 | const token = this.lookahead().type;
|
4902 | isGroupedType = token !== 17 && token !== 14;
|
4903 | } else {
|
4904 | isGroupedType = true;
|
4905 | }
|
4906 | }
|
4907 | if (isGroupedType) {
|
4908 | this.state.noAnonFunctionType = false;
|
4909 | type = this.flowParseType();
|
4910 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
4911 | if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
|
4912 | this.expect(11);
|
4913 | return type;
|
4914 | } else {
|
4915 | this.eat(12);
|
4916 | }
|
4917 | }
|
4918 | if (type) {
|
4919 | tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
|
4920 | } else {
|
4921 | tmp = this.flowParseFunctionTypeParams();
|
4922 | }
|
4923 | node.params = tmp.params;
|
4924 | node.rest = tmp.rest;
|
4925 | node.this = tmp._this;
|
4926 | this.expect(11);
|
4927 | this.expect(19);
|
4928 | node.returnType = this.flowParseType();
|
4929 | node.typeParameters = null;
|
4930 | return this.finishNode(node, "FunctionTypeAnnotation");
|
4931 | case 131:
|
4932 | return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
|
4933 | case 85:
|
4934 | case 86:
|
4935 | node.value = this.match(85);
|
4936 | this.next();
|
4937 | return this.finishNode(node, "BooleanLiteralTypeAnnotation");
|
4938 | case 53:
|
4939 | if (this.state.value === "-") {
|
4940 | this.next();
|
4941 | if (this.match(132)) {
|
4942 | return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
|
4943 | }
|
4944 | if (this.match(133)) {
|
4945 | return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
|
4946 | }
|
4947 | throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
|
4948 | at: this.state.startLoc
|
4949 | });
|
4950 | }
|
4951 | this.unexpected();
|
4952 | return;
|
4953 | case 132:
|
4954 | return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
|
4955 | case 133:
|
4956 | return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
|
4957 | case 88:
|
4958 | this.next();
|
4959 | return this.finishNode(node, "VoidTypeAnnotation");
|
4960 | case 84:
|
4961 | this.next();
|
4962 | return this.finishNode(node, "NullLiteralTypeAnnotation");
|
4963 | case 78:
|
4964 | this.next();
|
4965 | return this.finishNode(node, "ThisTypeAnnotation");
|
4966 | case 55:
|
4967 | this.next();
|
4968 | return this.finishNode(node, "ExistsTypeAnnotation");
|
4969 | case 87:
|
4970 | return this.flowParseTypeofType();
|
4971 | default:
|
4972 | if (tokenIsKeyword(this.state.type)) {
|
4973 | const label = tokenLabelName(this.state.type);
|
4974 | this.next();
|
4975 | return super.createIdentifier(node, label);
|
4976 | } else if (tokenIsIdentifier(this.state.type)) {
|
4977 | if (this.isContextual(127)) {
|
4978 | return this.flowParseInterfaceType();
|
4979 | }
|
4980 | return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
|
4981 | }
|
4982 | }
|
4983 | this.unexpected();
|
4984 | }
|
4985 | flowParsePostfixType() {
|
4986 | const startLoc = this.state.startLoc;
|
4987 | let type = this.flowParsePrimaryType();
|
4988 | let seenOptionalIndexedAccess = false;
|
4989 | while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
|
4990 | const node = this.startNodeAt(startLoc);
|
4991 | const optional = this.eat(18);
|
4992 | seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
|
4993 | this.expect(0);
|
4994 | if (!optional && this.match(3)) {
|
4995 | node.elementType = type;
|
4996 | this.next();
|
4997 | type = this.finishNode(node, "ArrayTypeAnnotation");
|
4998 | } else {
|
4999 | node.objectType = type;
|
5000 | node.indexType = this.flowParseType();
|
5001 | this.expect(3);
|
5002 | if (seenOptionalIndexedAccess) {
|
5003 | node.optional = optional;
|
5004 | type = this.finishNode(node, "OptionalIndexedAccessType");
|
5005 | } else {
|
5006 | type = this.finishNode(node, "IndexedAccessType");
|
5007 | }
|
5008 | }
|
5009 | }
|
5010 | return type;
|
5011 | }
|
5012 | flowParsePrefixType() {
|
5013 | const node = this.startNode();
|
5014 | if (this.eat(17)) {
|
5015 | node.typeAnnotation = this.flowParsePrefixType();
|
5016 | return this.finishNode(node, "NullableTypeAnnotation");
|
5017 | } else {
|
5018 | return this.flowParsePostfixType();
|
5019 | }
|
5020 | }
|
5021 | flowParseAnonFunctionWithoutParens() {
|
5022 | const param = this.flowParsePrefixType();
|
5023 | if (!this.state.noAnonFunctionType && this.eat(19)) {
|
5024 | const node = this.startNodeAt(param.loc.start);
|
5025 | node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
|
5026 | node.rest = null;
|
5027 | node.this = null;
|
5028 | node.returnType = this.flowParseType();
|
5029 | node.typeParameters = null;
|
5030 | return this.finishNode(node, "FunctionTypeAnnotation");
|
5031 | }
|
5032 | return param;
|
5033 | }
|
5034 | flowParseIntersectionType() {
|
5035 | const node = this.startNode();
|
5036 | this.eat(45);
|
5037 | const type = this.flowParseAnonFunctionWithoutParens();
|
5038 | node.types = [type];
|
5039 | while (this.eat(45)) {
|
5040 | node.types.push(this.flowParseAnonFunctionWithoutParens());
|
5041 | }
|
5042 | return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
|
5043 | }
|
5044 | flowParseUnionType() {
|
5045 | const node = this.startNode();
|
5046 | this.eat(43);
|
5047 | const type = this.flowParseIntersectionType();
|
5048 | node.types = [type];
|
5049 | while (this.eat(43)) {
|
5050 | node.types.push(this.flowParseIntersectionType());
|
5051 | }
|
5052 | return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
|
5053 | }
|
5054 | flowParseType() {
|
5055 | const oldInType = this.state.inType;
|
5056 | this.state.inType = true;
|
5057 | const type = this.flowParseUnionType();
|
5058 | this.state.inType = oldInType;
|
5059 | return type;
|
5060 | }
|
5061 | flowParseTypeOrImplicitInstantiation() {
|
5062 | if (this.state.type === 130 && this.state.value === "_") {
|
5063 | const startLoc = this.state.startLoc;
|
5064 | const node = this.parseIdentifier();
|
5065 | return this.flowParseGenericType(startLoc, node);
|
5066 | } else {
|
5067 | return this.flowParseType();
|
5068 | }
|
5069 | }
|
5070 | flowParseTypeAnnotation() {
|
5071 | const node = this.startNode();
|
5072 | node.typeAnnotation = this.flowParseTypeInitialiser();
|
5073 | return this.finishNode(node, "TypeAnnotation");
|
5074 | }
|
5075 | flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
|
5076 | const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
|
5077 | if (this.match(14)) {
|
5078 | ident.typeAnnotation = this.flowParseTypeAnnotation();
|
5079 | this.resetEndLocation(ident);
|
5080 | }
|
5081 | return ident;
|
5082 | }
|
5083 | typeCastToParameter(node) {
|
5084 | node.expression.typeAnnotation = node.typeAnnotation;
|
5085 | this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
|
5086 | return node.expression;
|
5087 | }
|
5088 | flowParseVariance() {
|
5089 | let variance = null;
|
5090 | if (this.match(53)) {
|
5091 | variance = this.startNode();
|
5092 | if (this.state.value === "+") {
|
5093 | variance.kind = "plus";
|
5094 | } else {
|
5095 | variance.kind = "minus";
|
5096 | }
|
5097 | this.next();
|
5098 | return this.finishNode(variance, "Variance");
|
5099 | }
|
5100 | return variance;
|
5101 | }
|
5102 | parseFunctionBody(node, allowExpressionBody, isMethod = false) {
|
5103 | if (allowExpressionBody) {
|
5104 | this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
|
5105 | return;
|
5106 | }
|
5107 | super.parseFunctionBody(node, false, isMethod);
|
5108 | }
|
5109 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
5110 | if (this.match(14)) {
|
5111 | const typeNode = this.startNode();
|
5112 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
5113 | node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
|
5114 | }
|
5115 | return super.parseFunctionBodyAndFinish(node, type, isMethod);
|
5116 | }
|
5117 | parseStatementLike(flags) {
|
5118 | if (this.state.strict && this.isContextual(127)) {
|
5119 | const lookahead = this.lookahead();
|
5120 | if (tokenIsKeywordOrIdentifier(lookahead.type)) {
|
5121 | const node = this.startNode();
|
5122 | this.next();
|
5123 | return this.flowParseInterface(node);
|
5124 | }
|
5125 | } else if (this.shouldParseEnums() && this.isContextual(124)) {
|
5126 | const node = this.startNode();
|
5127 | this.next();
|
5128 | return this.flowParseEnumDeclaration(node);
|
5129 | }
|
5130 | const stmt = super.parseStatementLike(flags);
|
5131 | if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
|
5132 | this.flowPragma = null;
|
5133 | }
|
5134 | return stmt;
|
5135 | }
|
5136 | parseExpressionStatement(node, expr, decorators) {
|
5137 | if (expr.type === "Identifier") {
|
5138 | if (expr.name === "declare") {
|
5139 | if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
|
5140 | return this.flowParseDeclare(node);
|
5141 | }
|
5142 | } else if (tokenIsIdentifier(this.state.type)) {
|
5143 | if (expr.name === "interface") {
|
5144 | return this.flowParseInterface(node);
|
5145 | } else if (expr.name === "type") {
|
5146 | return this.flowParseTypeAlias(node);
|
5147 | } else if (expr.name === "opaque") {
|
5148 | return this.flowParseOpaqueType(node, false);
|
5149 | }
|
5150 | }
|
5151 | }
|
5152 | return super.parseExpressionStatement(node, expr, decorators);
|
5153 | }
|
5154 | shouldParseExportDeclaration() {
|
5155 | const {
|
5156 | type
|
5157 | } = this.state;
|
5158 | if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
|
5159 | return !this.state.containsEsc;
|
5160 | }
|
5161 | return super.shouldParseExportDeclaration();
|
5162 | }
|
5163 | isExportDefaultSpecifier() {
|
5164 | const {
|
5165 | type
|
5166 | } = this.state;
|
5167 | if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
|
5168 | return this.state.containsEsc;
|
5169 | }
|
5170 | return super.isExportDefaultSpecifier();
|
5171 | }
|
5172 | parseExportDefaultExpression() {
|
5173 | if (this.shouldParseEnums() && this.isContextual(124)) {
|
5174 | const node = this.startNode();
|
5175 | this.next();
|
5176 | return this.flowParseEnumDeclaration(node);
|
5177 | }
|
5178 | return super.parseExportDefaultExpression();
|
5179 | }
|
5180 | parseConditional(expr, startLoc, refExpressionErrors) {
|
5181 | if (!this.match(17)) return expr;
|
5182 | if (this.state.maybeInArrowParameters) {
|
5183 | const nextCh = this.lookaheadCharCode();
|
5184 | if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
|
5185 | this.setOptionalParametersError(refExpressionErrors);
|
5186 | return expr;
|
5187 | }
|
5188 | }
|
5189 | this.expect(17);
|
5190 | const state = this.state.clone();
|
5191 | const originalNoArrowAt = this.state.noArrowAt;
|
5192 | const node = this.startNodeAt(startLoc);
|
5193 | let {
|
5194 | consequent,
|
5195 | failed
|
5196 | } = this.tryParseConditionalConsequent();
|
5197 | let [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
5198 | if (failed || invalid.length > 0) {
|
5199 | const noArrowAt = [...originalNoArrowAt];
|
5200 | if (invalid.length > 0) {
|
5201 | this.state = state;
|
5202 | this.state.noArrowAt = noArrowAt;
|
5203 | for (let i = 0; i < invalid.length; i++) {
|
5204 | noArrowAt.push(invalid[i].start);
|
5205 | }
|
5206 | ({
|
5207 | consequent,
|
5208 | failed
|
5209 | } = this.tryParseConditionalConsequent());
|
5210 | [valid, invalid] = this.getArrowLikeExpressions(consequent);
|
5211 | }
|
5212 | if (failed && valid.length > 1) {
|
5213 | this.raise(FlowErrors.AmbiguousConditionalArrow, {
|
5214 | at: state.startLoc
|
5215 | });
|
5216 | }
|
5217 | if (failed && valid.length === 1) {
|
5218 | this.state = state;
|
5219 | noArrowAt.push(valid[0].start);
|
5220 | this.state.noArrowAt = noArrowAt;
|
5221 | ({
|
5222 | consequent,
|
5223 | failed
|
5224 | } = this.tryParseConditionalConsequent());
|
5225 | }
|
5226 | }
|
5227 | this.getArrowLikeExpressions(consequent, true);
|
5228 | this.state.noArrowAt = originalNoArrowAt;
|
5229 | this.expect(14);
|
5230 | node.test = expr;
|
5231 | node.consequent = consequent;
|
5232 | node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
|
5233 | return this.finishNode(node, "ConditionalExpression");
|
5234 | }
|
5235 | tryParseConditionalConsequent() {
|
5236 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
5237 | const consequent = this.parseMaybeAssignAllowIn();
|
5238 | const failed = !this.match(14);
|
5239 | this.state.noArrowParamsConversionAt.pop();
|
5240 | return {
|
5241 | consequent,
|
5242 | failed
|
5243 | };
|
5244 | }
|
5245 | getArrowLikeExpressions(node, disallowInvalid) {
|
5246 | const stack = [node];
|
5247 | const arrows = [];
|
5248 | while (stack.length !== 0) {
|
5249 | const node = stack.pop();
|
5250 | if (node.type === "ArrowFunctionExpression") {
|
5251 | if (node.typeParameters || !node.returnType) {
|
5252 | this.finishArrowValidation(node);
|
5253 | } else {
|
5254 | arrows.push(node);
|
5255 | }
|
5256 | stack.push(node.body);
|
5257 | } else if (node.type === "ConditionalExpression") {
|
5258 | stack.push(node.consequent);
|
5259 | stack.push(node.alternate);
|
5260 | }
|
5261 | }
|
5262 | if (disallowInvalid) {
|
5263 | arrows.forEach(node => this.finishArrowValidation(node));
|
5264 | return [arrows, []];
|
5265 | }
|
5266 | return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
|
5267 | }
|
5268 | finishArrowValidation(node) {
|
5269 | var _node$extra;
|
5270 | this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
|
5271 | this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
|
5272 | super.checkParams(node, false, true);
|
5273 | this.scope.exit();
|
5274 | }
|
5275 | forwardNoArrowParamsConversionAt(node, parse) {
|
5276 | let result;
|
5277 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
5278 | this.state.noArrowParamsConversionAt.push(this.state.start);
|
5279 | result = parse();
|
5280 | this.state.noArrowParamsConversionAt.pop();
|
5281 | } else {
|
5282 | result = parse();
|
5283 | }
|
5284 | return result;
|
5285 | }
|
5286 | parseParenItem(node, startLoc) {
|
5287 | node = super.parseParenItem(node, startLoc);
|
5288 | if (this.eat(17)) {
|
5289 | node.optional = true;
|
5290 | this.resetEndLocation(node);
|
5291 | }
|
5292 | if (this.match(14)) {
|
5293 | const typeCastNode = this.startNodeAt(startLoc);
|
5294 | typeCastNode.expression = node;
|
5295 | typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
|
5296 | return this.finishNode(typeCastNode, "TypeCastExpression");
|
5297 | }
|
5298 | return node;
|
5299 | }
|
5300 | assertModuleNodeAllowed(node) {
|
5301 | if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
|
5302 | return;
|
5303 | }
|
5304 | super.assertModuleNodeAllowed(node);
|
5305 | }
|
5306 | parseExportDeclaration(node) {
|
5307 | if (this.isContextual(128)) {
|
5308 | node.exportKind = "type";
|
5309 | const declarationNode = this.startNode();
|
5310 | this.next();
|
5311 | if (this.match(5)) {
|
5312 | node.specifiers = this.parseExportSpecifiers(true);
|
5313 | super.parseExportFrom(node);
|
5314 | return null;
|
5315 | } else {
|
5316 | return this.flowParseTypeAlias(declarationNode);
|
5317 | }
|
5318 | } else if (this.isContextual(129)) {
|
5319 | node.exportKind = "type";
|
5320 | const declarationNode = this.startNode();
|
5321 | this.next();
|
5322 | return this.flowParseOpaqueType(declarationNode, false);
|
5323 | } else if (this.isContextual(127)) {
|
5324 | node.exportKind = "type";
|
5325 | const declarationNode = this.startNode();
|
5326 | this.next();
|
5327 | return this.flowParseInterface(declarationNode);
|
5328 | } else if (this.shouldParseEnums() && this.isContextual(124)) {
|
5329 | node.exportKind = "value";
|
5330 | const declarationNode = this.startNode();
|
5331 | this.next();
|
5332 | return this.flowParseEnumDeclaration(declarationNode);
|
5333 | } else {
|
5334 | return super.parseExportDeclaration(node);
|
5335 | }
|
5336 | }
|
5337 | eatExportStar(node) {
|
5338 | if (super.eatExportStar(node)) return true;
|
5339 | if (this.isContextual(128) && this.lookahead().type === 55) {
|
5340 | node.exportKind = "type";
|
5341 | this.next();
|
5342 | this.next();
|
5343 | return true;
|
5344 | }
|
5345 | return false;
|
5346 | }
|
5347 | maybeParseExportNamespaceSpecifier(node) {
|
5348 | const {
|
5349 | startLoc
|
5350 | } = this.state;
|
5351 | const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
|
5352 | if (hasNamespace && node.exportKind === "type") {
|
5353 | this.unexpected(startLoc);
|
5354 | }
|
5355 | return hasNamespace;
|
5356 | }
|
5357 | parseClassId(node, isStatement, optionalId) {
|
5358 | super.parseClassId(node, isStatement, optionalId);
|
5359 | if (this.match(47)) {
|
5360 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
5361 | }
|
5362 | }
|
5363 | parseClassMember(classBody, member, state) {
|
5364 | const {
|
5365 | startLoc
|
5366 | } = this.state;
|
5367 | if (this.isContextual(123)) {
|
5368 | if (super.parseClassMemberFromModifier(classBody, member)) {
|
5369 | return;
|
5370 | }
|
5371 | member.declare = true;
|
5372 | }
|
5373 | super.parseClassMember(classBody, member, state);
|
5374 | if (member.declare) {
|
5375 | if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
|
5376 | this.raise(FlowErrors.DeclareClassElement, {
|
5377 | at: startLoc
|
5378 | });
|
5379 | } else if (member.value) {
|
5380 | this.raise(FlowErrors.DeclareClassFieldInitializer, {
|
5381 | at: member.value
|
5382 | });
|
5383 | }
|
5384 | }
|
5385 | }
|
5386 | isIterator(word) {
|
5387 | return word === "iterator" || word === "asyncIterator";
|
5388 | }
|
5389 | readIterator() {
|
5390 | const word = super.readWord1();
|
5391 | const fullWord = "@@" + word;
|
5392 | if (!this.isIterator(word) || !this.state.inType) {
|
5393 | this.raise(Errors.InvalidIdentifier, {
|
5394 | at: this.state.curPosition(),
|
5395 | identifierName: fullWord
|
5396 | });
|
5397 | }
|
5398 | this.finishToken(130, fullWord);
|
5399 | }
|
5400 | getTokenFromCode(code) {
|
5401 | const next = this.input.charCodeAt(this.state.pos + 1);
|
5402 | if (code === 123 && next === 124) {
|
5403 | this.finishOp(6, 2);
|
5404 | } else if (this.state.inType && (code === 62 || code === 60)) {
|
5405 | this.finishOp(code === 62 ? 48 : 47, 1);
|
5406 | } else if (this.state.inType && code === 63) {
|
5407 | if (next === 46) {
|
5408 | this.finishOp(18, 2);
|
5409 | } else {
|
5410 | this.finishOp(17, 1);
|
5411 | }
|
5412 | } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
|
5413 | this.state.pos += 2;
|
5414 | this.readIterator();
|
5415 | } else {
|
5416 | super.getTokenFromCode(code);
|
5417 | }
|
5418 | }
|
5419 | isAssignable(node, isBinding) {
|
5420 | if (node.type === "TypeCastExpression") {
|
5421 | return this.isAssignable(node.expression, isBinding);
|
5422 | } else {
|
5423 | return super.isAssignable(node, isBinding);
|
5424 | }
|
5425 | }
|
5426 | toAssignable(node, isLHS = false) {
|
5427 | if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
|
5428 | node.left = this.typeCastToParameter(node.left);
|
5429 | }
|
5430 | super.toAssignable(node, isLHS);
|
5431 | }
|
5432 | toAssignableList(exprList, trailingCommaLoc, isLHS) {
|
5433 | for (let i = 0; i < exprList.length; i++) {
|
5434 | const expr = exprList[i];
|
5435 | if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
|
5436 | exprList[i] = this.typeCastToParameter(expr);
|
5437 | }
|
5438 | }
|
5439 | super.toAssignableList(exprList, trailingCommaLoc, isLHS);
|
5440 | }
|
5441 | toReferencedList(exprList, isParenthesizedExpr) {
|
5442 | for (let i = 0; i < exprList.length; i++) {
|
5443 | var _expr$extra;
|
5444 | const expr = exprList[i];
|
5445 | if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
|
5446 | this.raise(FlowErrors.TypeCastInPattern, {
|
5447 | at: expr.typeAnnotation
|
5448 | });
|
5449 | }
|
5450 | }
|
5451 | return exprList;
|
5452 | }
|
5453 | parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
|
5454 | const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
|
5455 | if (canBePattern && !this.state.maybeInArrowParameters) {
|
5456 | this.toReferencedList(node.elements);
|
5457 | }
|
5458 | return node;
|
5459 | }
|
5460 | isValidLVal(type, isParenthesized, binding) {
|
5461 | return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
|
5462 | }
|
5463 | parseClassProperty(node) {
|
5464 | if (this.match(14)) {
|
5465 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
5466 | }
|
5467 | return super.parseClassProperty(node);
|
5468 | }
|
5469 | parseClassPrivateProperty(node) {
|
5470 | if (this.match(14)) {
|
5471 | node.typeAnnotation = this.flowParseTypeAnnotation();
|
5472 | }
|
5473 | return super.parseClassPrivateProperty(node);
|
5474 | }
|
5475 | isClassMethod() {
|
5476 | return this.match(47) || super.isClassMethod();
|
5477 | }
|
5478 | isClassProperty() {
|
5479 | return this.match(14) || super.isClassProperty();
|
5480 | }
|
5481 | isNonstaticConstructor(method) {
|
5482 | return !this.match(14) && super.isNonstaticConstructor(method);
|
5483 | }
|
5484 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
5485 | if (method.variance) {
|
5486 | this.unexpected(method.variance.loc.start);
|
5487 | }
|
5488 | delete method.variance;
|
5489 | if (this.match(47)) {
|
5490 | method.typeParameters = this.flowParseTypeParameterDeclaration();
|
5491 | }
|
5492 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
|
5493 | if (method.params && isConstructor) {
|
5494 | const params = method.params;
|
5495 | if (params.length > 0 && this.isThisParam(params[0])) {
|
5496 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
5497 | at: method
|
5498 | });
|
5499 | }
|
5500 | } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
|
5501 | const params = method.value.params;
|
5502 | if (params.length > 0 && this.isThisParam(params[0])) {
|
5503 | this.raise(FlowErrors.ThisParamBannedInConstructor, {
|
5504 | at: method
|
5505 | });
|
5506 | }
|
5507 | }
|
5508 | }
|
5509 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
5510 | if (method.variance) {
|
5511 | this.unexpected(method.variance.loc.start);
|
5512 | }
|
5513 | delete method.variance;
|
5514 | if (this.match(47)) {
|
5515 | method.typeParameters = this.flowParseTypeParameterDeclaration();
|
5516 | }
|
5517 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
5518 | }
|
5519 | parseClassSuper(node) {
|
5520 | super.parseClassSuper(node);
|
5521 | if (node.superClass && this.match(47)) {
|
5522 | node.superTypeParameters = this.flowParseTypeParameterInstantiation();
|
5523 | }
|
5524 | if (this.isContextual(111)) {
|
5525 | this.next();
|
5526 | const implemented = node.implements = [];
|
5527 | do {
|
5528 | const node = this.startNode();
|
5529 | node.id = this.flowParseRestrictedIdentifier(true);
|
5530 | if (this.match(47)) {
|
5531 | node.typeParameters = this.flowParseTypeParameterInstantiation();
|
5532 | } else {
|
5533 | node.typeParameters = null;
|
5534 | }
|
5535 | implemented.push(this.finishNode(node, "ClassImplements"));
|
5536 | } while (this.eat(12));
|
5537 | }
|
5538 | }
|
5539 | checkGetterSetterParams(method) {
|
5540 | super.checkGetterSetterParams(method);
|
5541 | const params = this.getObjectOrClassMethodParams(method);
|
5542 | if (params.length > 0) {
|
5543 | const param = params[0];
|
5544 | if (this.isThisParam(param) && method.kind === "get") {
|
5545 | this.raise(FlowErrors.GetterMayNotHaveThisParam, {
|
5546 | at: param
|
5547 | });
|
5548 | } else if (this.isThisParam(param)) {
|
5549 | this.raise(FlowErrors.SetterMayNotHaveThisParam, {
|
5550 | at: param
|
5551 | });
|
5552 | }
|
5553 | }
|
5554 | }
|
5555 | parsePropertyNamePrefixOperator(node) {
|
5556 | node.variance = this.flowParseVariance();
|
5557 | }
|
5558 | parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
|
5559 | if (prop.variance) {
|
5560 | this.unexpected(prop.variance.loc.start);
|
5561 | }
|
5562 | delete prop.variance;
|
5563 | let typeParameters;
|
5564 | if (this.match(47) && !isAccessor) {
|
5565 | typeParameters = this.flowParseTypeParameterDeclaration();
|
5566 | if (!this.match(10)) this.unexpected();
|
5567 | }
|
5568 | const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
|
5569 | if (typeParameters) {
|
5570 | (result.value || result).typeParameters = typeParameters;
|
5571 | }
|
5572 | return result;
|
5573 | }
|
5574 | parseAssignableListItemTypes(param) {
|
5575 | if (this.eat(17)) {
|
5576 | if (param.type !== "Identifier") {
|
5577 | this.raise(FlowErrors.PatternIsOptional, {
|
5578 | at: param
|
5579 | });
|
5580 | }
|
5581 | if (this.isThisParam(param)) {
|
5582 | this.raise(FlowErrors.ThisParamMayNotBeOptional, {
|
5583 | at: param
|
5584 | });
|
5585 | }
|
5586 | param.optional = true;
|
5587 | }
|
5588 | if (this.match(14)) {
|
5589 | param.typeAnnotation = this.flowParseTypeAnnotation();
|
5590 | } else if (this.isThisParam(param)) {
|
5591 | this.raise(FlowErrors.ThisParamAnnotationRequired, {
|
5592 | at: param
|
5593 | });
|
5594 | }
|
5595 | if (this.match(29) && this.isThisParam(param)) {
|
5596 | this.raise(FlowErrors.ThisParamNoDefault, {
|
5597 | at: param
|
5598 | });
|
5599 | }
|
5600 | this.resetEndLocation(param);
|
5601 | return param;
|
5602 | }
|
5603 | parseMaybeDefault(startLoc, left) {
|
5604 | const node = super.parseMaybeDefault(startLoc, left);
|
5605 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
5606 | this.raise(FlowErrors.TypeBeforeInitializer, {
|
5607 | at: node.typeAnnotation
|
5608 | });
|
5609 | }
|
5610 | return node;
|
5611 | }
|
5612 | checkImportReflection(node) {
|
5613 | super.checkImportReflection(node);
|
5614 | if (node.module && node.importKind !== "value") {
|
5615 | this.raise(FlowErrors.ImportReflectionHasImportType, {
|
5616 | at: node.specifiers[0].loc.start
|
5617 | });
|
5618 | }
|
5619 | }
|
5620 | parseImportSpecifierLocal(node, specifier, type) {
|
5621 | specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
|
5622 | node.specifiers.push(this.finishImportSpecifier(specifier, type));
|
5623 | }
|
5624 | isPotentialImportPhase(isExport) {
|
5625 | if (super.isPotentialImportPhase(isExport)) return true;
|
5626 | if (this.isContextual(128)) {
|
5627 | if (!isExport) return true;
|
5628 | const ch = this.lookaheadCharCode();
|
5629 | return ch === 123 || ch === 42;
|
5630 | }
|
5631 | return !isExport && this.isContextual(87);
|
5632 | }
|
5633 | applyImportPhase(node, isExport, phase, loc) {
|
5634 | super.applyImportPhase(node, isExport, phase, loc);
|
5635 | if (isExport) {
|
5636 | if (!phase && this.match(65)) {
|
5637 | return;
|
5638 | }
|
5639 | node.exportKind = phase === "type" ? phase : "value";
|
5640 | } else {
|
5641 | if (phase === "type" && this.match(55)) this.unexpected();
|
5642 | node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
|
5643 | }
|
5644 | }
|
5645 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
|
5646 | const firstIdent = specifier.imported;
|
5647 | let specifierTypeKind = null;
|
5648 | if (firstIdent.type === "Identifier") {
|
5649 | if (firstIdent.name === "type") {
|
5650 | specifierTypeKind = "type";
|
5651 | } else if (firstIdent.name === "typeof") {
|
5652 | specifierTypeKind = "typeof";
|
5653 | }
|
5654 | }
|
5655 | let isBinding = false;
|
5656 | if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
|
5657 | const as_ident = this.parseIdentifier(true);
|
5658 | if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
|
5659 | specifier.imported = as_ident;
|
5660 | specifier.importKind = specifierTypeKind;
|
5661 | specifier.local = cloneIdentifier(as_ident);
|
5662 | } else {
|
5663 | specifier.imported = firstIdent;
|
5664 | specifier.importKind = null;
|
5665 | specifier.local = this.parseIdentifier();
|
5666 | }
|
5667 | } else {
|
5668 | if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
|
5669 | specifier.imported = this.parseIdentifier(true);
|
5670 | specifier.importKind = specifierTypeKind;
|
5671 | } else {
|
5672 | if (importedIsString) {
|
5673 | throw this.raise(Errors.ImportBindingIsString, {
|
5674 | at: specifier,
|
5675 | importName: firstIdent.value
|
5676 | });
|
5677 | }
|
5678 | specifier.imported = firstIdent;
|
5679 | specifier.importKind = null;
|
5680 | }
|
5681 | if (this.eatContextual(93)) {
|
5682 | specifier.local = this.parseIdentifier();
|
5683 | } else {
|
5684 | isBinding = true;
|
5685 | specifier.local = cloneIdentifier(specifier.imported);
|
5686 | }
|
5687 | }
|
5688 | const specifierIsTypeImport = hasTypeImportKind(specifier);
|
5689 | if (isInTypeOnlyImport && specifierIsTypeImport) {
|
5690 | this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
|
5691 | at: specifier
|
5692 | });
|
5693 | }
|
5694 | if (isInTypeOnlyImport || specifierIsTypeImport) {
|
5695 | this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
|
5696 | }
|
5697 | if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
|
5698 | this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
|
5699 | }
|
5700 | return this.finishImportSpecifier(specifier, "ImportSpecifier");
|
5701 | }
|
5702 | parseBindingAtom() {
|
5703 | switch (this.state.type) {
|
5704 | case 78:
|
5705 | return this.parseIdentifier(true);
|
5706 | default:
|
5707 | return super.parseBindingAtom();
|
5708 | }
|
5709 | }
|
5710 | parseFunctionParams(node, isConstructor) {
|
5711 | const kind = node.kind;
|
5712 | if (kind !== "get" && kind !== "set" && this.match(47)) {
|
5713 | node.typeParameters = this.flowParseTypeParameterDeclaration();
|
5714 | }
|
5715 | super.parseFunctionParams(node, isConstructor);
|
5716 | }
|
5717 | parseVarId(decl, kind) {
|
5718 | super.parseVarId(decl, kind);
|
5719 | if (this.match(14)) {
|
5720 | decl.id.typeAnnotation = this.flowParseTypeAnnotation();
|
5721 | this.resetEndLocation(decl.id);
|
5722 | }
|
5723 | }
|
5724 | parseAsyncArrowFromCallExpression(node, call) {
|
5725 | if (this.match(14)) {
|
5726 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
5727 | this.state.noAnonFunctionType = true;
|
5728 | node.returnType = this.flowParseTypeAnnotation();
|
5729 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
5730 | }
|
5731 | return super.parseAsyncArrowFromCallExpression(node, call);
|
5732 | }
|
5733 | shouldParseAsyncArrow() {
|
5734 | return this.match(14) || super.shouldParseAsyncArrow();
|
5735 | }
|
5736 | parseMaybeAssign(refExpressionErrors, afterLeftParse) {
|
5737 | var _jsx;
|
5738 | let state = null;
|
5739 | let jsx;
|
5740 | if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
|
5741 | state = this.state.clone();
|
5742 | jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
|
5743 | if (!jsx.error) return jsx.node;
|
5744 | const {
|
5745 | context
|
5746 | } = this.state;
|
5747 | const currentContext = context[context.length - 1];
|
5748 | if (currentContext === types.j_oTag || currentContext === types.j_expr) {
|
5749 | context.pop();
|
5750 | }
|
5751 | }
|
5752 | if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
|
5753 | var _jsx2, _jsx3;
|
5754 | state = state || this.state.clone();
|
5755 | let typeParameters;
|
5756 | const arrow = this.tryParse(abort => {
|
5757 | var _arrowExpression$extr;
|
5758 | typeParameters = this.flowParseTypeParameterDeclaration();
|
5759 | const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
|
5760 | const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
5761 | this.resetStartLocationFromNode(result, typeParameters);
|
5762 | return result;
|
5763 | });
|
5764 | if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
|
5765 | const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
|
5766 | if (expr.type !== "ArrowFunctionExpression") abort();
|
5767 | expr.typeParameters = typeParameters;
|
5768 | this.resetStartLocationFromNode(expr, typeParameters);
|
5769 | return arrowExpression;
|
5770 | }, state);
|
5771 | let arrowExpression = null;
|
5772 | if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
|
5773 | if (!arrow.error && !arrow.aborted) {
|
5774 | if (arrow.node.async) {
|
5775 | this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
|
5776 | at: typeParameters
|
5777 | });
|
5778 | }
|
5779 | return arrow.node;
|
5780 | }
|
5781 | arrowExpression = arrow.node;
|
5782 | }
|
5783 | if ((_jsx2 = jsx) != null && _jsx2.node) {
|
5784 | this.state = jsx.failState;
|
5785 | return jsx.node;
|
5786 | }
|
5787 | if (arrowExpression) {
|
5788 | this.state = arrow.failState;
|
5789 | return arrowExpression;
|
5790 | }
|
5791 | if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
|
5792 | if (arrow.thrown) throw arrow.error;
|
5793 | throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
|
5794 | at: typeParameters
|
5795 | });
|
5796 | }
|
5797 | return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
5798 | }
|
5799 | parseArrow(node) {
|
5800 | if (this.match(14)) {
|
5801 | const result = this.tryParse(() => {
|
5802 | const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
5803 | this.state.noAnonFunctionType = true;
|
5804 | const typeNode = this.startNode();
|
5805 | [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
|
5806 | this.state.noAnonFunctionType = oldNoAnonFunctionType;
|
5807 | if (this.canInsertSemicolon()) this.unexpected();
|
5808 | if (!this.match(19)) this.unexpected();
|
5809 | return typeNode;
|
5810 | });
|
5811 | if (result.thrown) return null;
|
5812 | if (result.error) this.state = result.failState;
|
5813 | node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
|
5814 | }
|
5815 | return super.parseArrow(node);
|
5816 | }
|
5817 | shouldParseArrow(params) {
|
5818 | return this.match(14) || super.shouldParseArrow(params);
|
5819 | }
|
5820 | setArrowFunctionParameters(node, params) {
|
5821 | if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
5822 | node.params = params;
|
5823 | } else {
|
5824 | super.setArrowFunctionParameters(node, params);
|
5825 | }
|
5826 | }
|
5827 | checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
|
5828 | if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
|
5829 | return;
|
5830 | }
|
5831 | for (let i = 0; i < node.params.length; i++) {
|
5832 | if (this.isThisParam(node.params[i]) && i > 0) {
|
5833 | this.raise(FlowErrors.ThisParamMustBeFirst, {
|
5834 | at: node.params[i]
|
5835 | });
|
5836 | }
|
5837 | }
|
5838 | super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
|
5839 | }
|
5840 | parseParenAndDistinguishExpression(canBeArrow) {
|
5841 | return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
|
5842 | }
|
5843 | parseSubscripts(base, startLoc, noCalls) {
|
5844 | if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
|
5845 | this.next();
|
5846 | const node = this.startNodeAt(startLoc);
|
5847 | node.callee = base;
|
5848 | node.arguments = super.parseCallExpressionArguments(11, false);
|
5849 | base = this.finishNode(node, "CallExpression");
|
5850 | } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
|
5851 | const state = this.state.clone();
|
5852 | const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
|
5853 | if (!arrow.error && !arrow.aborted) return arrow.node;
|
5854 | const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
|
5855 | if (result.node && !result.error) return result.node;
|
5856 | if (arrow.node) {
|
5857 | this.state = arrow.failState;
|
5858 | return arrow.node;
|
5859 | }
|
5860 | if (result.node) {
|
5861 | this.state = result.failState;
|
5862 | return result.node;
|
5863 | }
|
5864 | throw arrow.error || result.error;
|
5865 | }
|
5866 | return super.parseSubscripts(base, startLoc, noCalls);
|
5867 | }
|
5868 | parseSubscript(base, startLoc, noCalls, subscriptState) {
|
5869 | if (this.match(18) && this.isLookaheadToken_lt()) {
|
5870 | subscriptState.optionalChainMember = true;
|
5871 | if (noCalls) {
|
5872 | subscriptState.stop = true;
|
5873 | return base;
|
5874 | }
|
5875 | this.next();
|
5876 | const node = this.startNodeAt(startLoc);
|
5877 | node.callee = base;
|
5878 | node.typeArguments = this.flowParseTypeParameterInstantiation();
|
5879 | this.expect(10);
|
5880 | node.arguments = this.parseCallExpressionArguments(11, false);
|
5881 | node.optional = true;
|
5882 | return this.finishCallExpression(node, true);
|
5883 | } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
|
5884 | const node = this.startNodeAt(startLoc);
|
5885 | node.callee = base;
|
5886 | const result = this.tryParse(() => {
|
5887 | node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
|
5888 | this.expect(10);
|
5889 | node.arguments = super.parseCallExpressionArguments(11, false);
|
5890 | if (subscriptState.optionalChainMember) {
|
5891 | node.optional = false;
|
5892 | }
|
5893 | return this.finishCallExpression(node, subscriptState.optionalChainMember);
|
5894 | });
|
5895 | if (result.node) {
|
5896 | if (result.error) this.state = result.failState;
|
5897 | return result.node;
|
5898 | }
|
5899 | }
|
5900 | return super.parseSubscript(base, startLoc, noCalls, subscriptState);
|
5901 | }
|
5902 | parseNewCallee(node) {
|
5903 | super.parseNewCallee(node);
|
5904 | let targs = null;
|
5905 | if (this.shouldParseTypes() && this.match(47)) {
|
5906 | targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
|
5907 | }
|
5908 | node.typeArguments = targs;
|
5909 | }
|
5910 | parseAsyncArrowWithTypeParameters(startLoc) {
|
5911 | const node = this.startNodeAt(startLoc);
|
5912 | this.parseFunctionParams(node, false);
|
5913 | if (!this.parseArrow(node)) return;
|
5914 | return super.parseArrowExpression(node, undefined, true);
|
5915 | }
|
5916 | readToken_mult_modulo(code) {
|
5917 | const next = this.input.charCodeAt(this.state.pos + 1);
|
5918 | if (code === 42 && next === 47 && this.state.hasFlowComment) {
|
5919 | this.state.hasFlowComment = false;
|
5920 | this.state.pos += 2;
|
5921 | this.nextToken();
|
5922 | return;
|
5923 | }
|
5924 | super.readToken_mult_modulo(code);
|
5925 | }
|
5926 | readToken_pipe_amp(code) {
|
5927 | const next = this.input.charCodeAt(this.state.pos + 1);
|
5928 | if (code === 124 && next === 125) {
|
5929 | this.finishOp(9, 2);
|
5930 | return;
|
5931 | }
|
5932 | super.readToken_pipe_amp(code);
|
5933 | }
|
5934 | parseTopLevel(file, program) {
|
5935 | const fileNode = super.parseTopLevel(file, program);
|
5936 | if (this.state.hasFlowComment) {
|
5937 | this.raise(FlowErrors.UnterminatedFlowComment, {
|
5938 | at: this.state.curPosition()
|
5939 | });
|
5940 | }
|
5941 | return fileNode;
|
5942 | }
|
5943 | skipBlockComment() {
|
5944 | if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
|
5945 | if (this.state.hasFlowComment) {
|
5946 | throw this.raise(FlowErrors.NestedFlowComment, {
|
5947 | at: this.state.startLoc
|
5948 | });
|
5949 | }
|
5950 | this.hasFlowCommentCompletion();
|
5951 | const commentSkip = this.skipFlowComment();
|
5952 | if (commentSkip) {
|
5953 | this.state.pos += commentSkip;
|
5954 | this.state.hasFlowComment = true;
|
5955 | }
|
5956 | return;
|
5957 | }
|
5958 | return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
|
5959 | }
|
5960 | skipFlowComment() {
|
5961 | const {
|
5962 | pos
|
5963 | } = this.state;
|
5964 | let shiftToFirstNonWhiteSpace = 2;
|
5965 | while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
|
5966 | shiftToFirstNonWhiteSpace++;
|
5967 | }
|
5968 | const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
|
5969 | const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
|
5970 | if (ch2 === 58 && ch3 === 58) {
|
5971 | return shiftToFirstNonWhiteSpace + 2;
|
5972 | }
|
5973 | if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
|
5974 | return shiftToFirstNonWhiteSpace + 12;
|
5975 | }
|
5976 | if (ch2 === 58 && ch3 !== 58) {
|
5977 | return shiftToFirstNonWhiteSpace;
|
5978 | }
|
5979 | return false;
|
5980 | }
|
5981 | hasFlowCommentCompletion() {
|
5982 | const end = this.input.indexOf("*/", this.state.pos);
|
5983 | if (end === -1) {
|
5984 | throw this.raise(Errors.UnterminatedComment, {
|
5985 | at: this.state.curPosition()
|
5986 | });
|
5987 | }
|
5988 | }
|
5989 | flowEnumErrorBooleanMemberNotInitialized(loc, {
|
5990 | enumName,
|
5991 | memberName
|
5992 | }) {
|
5993 | this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
|
5994 | at: loc,
|
5995 | memberName,
|
5996 | enumName
|
5997 | });
|
5998 | }
|
5999 | flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
|
6000 | return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
|
6001 | at: loc
|
6002 | }, enumContext));
|
6003 | }
|
6004 | flowEnumErrorNumberMemberNotInitialized(loc, {
|
6005 | enumName,
|
6006 | memberName
|
6007 | }) {
|
6008 | this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
|
6009 | at: loc,
|
6010 | enumName,
|
6011 | memberName
|
6012 | });
|
6013 | }
|
6014 | flowEnumErrorStringMemberInconsistentlyInitailized(node, {
|
6015 | enumName
|
6016 | }) {
|
6017 | this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
|
6018 | at: node,
|
6019 | enumName
|
6020 | });
|
6021 | }
|
6022 | flowEnumMemberInit() {
|
6023 | const startLoc = this.state.startLoc;
|
6024 | const endOfInit = () => this.match(12) || this.match(8);
|
6025 | switch (this.state.type) {
|
6026 | case 132:
|
6027 | {
|
6028 | const literal = this.parseNumericLiteral(this.state.value);
|
6029 | if (endOfInit()) {
|
6030 | return {
|
6031 | type: "number",
|
6032 | loc: literal.loc.start,
|
6033 | value: literal
|
6034 | };
|
6035 | }
|
6036 | return {
|
6037 | type: "invalid",
|
6038 | loc: startLoc
|
6039 | };
|
6040 | }
|
6041 | case 131:
|
6042 | {
|
6043 | const literal = this.parseStringLiteral(this.state.value);
|
6044 | if (endOfInit()) {
|
6045 | return {
|
6046 | type: "string",
|
6047 | loc: literal.loc.start,
|
6048 | value: literal
|
6049 | };
|
6050 | }
|
6051 | return {
|
6052 | type: "invalid",
|
6053 | loc: startLoc
|
6054 | };
|
6055 | }
|
6056 | case 85:
|
6057 | case 86:
|
6058 | {
|
6059 | const literal = this.parseBooleanLiteral(this.match(85));
|
6060 | if (endOfInit()) {
|
6061 | return {
|
6062 | type: "boolean",
|
6063 | loc: literal.loc.start,
|
6064 | value: literal
|
6065 | };
|
6066 | }
|
6067 | return {
|
6068 | type: "invalid",
|
6069 | loc: startLoc
|
6070 | };
|
6071 | }
|
6072 | default:
|
6073 | return {
|
6074 | type: "invalid",
|
6075 | loc: startLoc
|
6076 | };
|
6077 | }
|
6078 | }
|
6079 | flowEnumMemberRaw() {
|
6080 | const loc = this.state.startLoc;
|
6081 | const id = this.parseIdentifier(true);
|
6082 | const init = this.eat(29) ? this.flowEnumMemberInit() : {
|
6083 | type: "none",
|
6084 | loc
|
6085 | };
|
6086 | return {
|
6087 | id,
|
6088 | init
|
6089 | };
|
6090 | }
|
6091 | flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
|
6092 | const {
|
6093 | explicitType
|
6094 | } = context;
|
6095 | if (explicitType === null) {
|
6096 | return;
|
6097 | }
|
6098 | if (explicitType !== expectedType) {
|
6099 | this.flowEnumErrorInvalidMemberInitializer(loc, context);
|
6100 | }
|
6101 | }
|
6102 | flowEnumMembers({
|
6103 | enumName,
|
6104 | explicitType
|
6105 | }) {
|
6106 | const seenNames = new Set();
|
6107 | const members = {
|
6108 | booleanMembers: [],
|
6109 | numberMembers: [],
|
6110 | stringMembers: [],
|
6111 | defaultedMembers: []
|
6112 | };
|
6113 | let hasUnknownMembers = false;
|
6114 | while (!this.match(8)) {
|
6115 | if (this.eat(21)) {
|
6116 | hasUnknownMembers = true;
|
6117 | break;
|
6118 | }
|
6119 | const memberNode = this.startNode();
|
6120 | const {
|
6121 | id,
|
6122 | init
|
6123 | } = this.flowEnumMemberRaw();
|
6124 | const memberName = id.name;
|
6125 | if (memberName === "") {
|
6126 | continue;
|
6127 | }
|
6128 | if (/^[a-z]/.test(memberName)) {
|
6129 | this.raise(FlowErrors.EnumInvalidMemberName, {
|
6130 | at: id,
|
6131 | memberName,
|
6132 | suggestion: memberName[0].toUpperCase() + memberName.slice(1),
|
6133 | enumName
|
6134 | });
|
6135 | }
|
6136 | if (seenNames.has(memberName)) {
|
6137 | this.raise(FlowErrors.EnumDuplicateMemberName, {
|
6138 | at: id,
|
6139 | memberName,
|
6140 | enumName
|
6141 | });
|
6142 | }
|
6143 | seenNames.add(memberName);
|
6144 | const context = {
|
6145 | enumName,
|
6146 | explicitType,
|
6147 | memberName
|
6148 | };
|
6149 | memberNode.id = id;
|
6150 | switch (init.type) {
|
6151 | case "boolean":
|
6152 | {
|
6153 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
|
6154 | memberNode.init = init.value;
|
6155 | members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
|
6156 | break;
|
6157 | }
|
6158 | case "number":
|
6159 | {
|
6160 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
|
6161 | memberNode.init = init.value;
|
6162 | members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
|
6163 | break;
|
6164 | }
|
6165 | case "string":
|
6166 | {
|
6167 | this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
|
6168 | memberNode.init = init.value;
|
6169 | members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
|
6170 | break;
|
6171 | }
|
6172 | case "invalid":
|
6173 | {
|
6174 | throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
|
6175 | }
|
6176 | case "none":
|
6177 | {
|
6178 | switch (explicitType) {
|
6179 | case "boolean":
|
6180 | this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
|
6181 | break;
|
6182 | case "number":
|
6183 | this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
|
6184 | break;
|
6185 | default:
|
6186 | members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
|
6187 | }
|
6188 | }
|
6189 | }
|
6190 | if (!this.match(8)) {
|
6191 | this.expect(12);
|
6192 | }
|
6193 | }
|
6194 | return {
|
6195 | members,
|
6196 | hasUnknownMembers
|
6197 | };
|
6198 | }
|
6199 | flowEnumStringMembers(initializedMembers, defaultedMembers, {
|
6200 | enumName
|
6201 | }) {
|
6202 | if (initializedMembers.length === 0) {
|
6203 | return defaultedMembers;
|
6204 | } else if (defaultedMembers.length === 0) {
|
6205 | return initializedMembers;
|
6206 | } else if (defaultedMembers.length > initializedMembers.length) {
|
6207 | for (const member of initializedMembers) {
|
6208 | this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
|
6209 | enumName
|
6210 | });
|
6211 | }
|
6212 | return defaultedMembers;
|
6213 | } else {
|
6214 | for (const member of defaultedMembers) {
|
6215 | this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
|
6216 | enumName
|
6217 | });
|
6218 | }
|
6219 | return initializedMembers;
|
6220 | }
|
6221 | }
|
6222 | flowEnumParseExplicitType({
|
6223 | enumName
|
6224 | }) {
|
6225 | if (!this.eatContextual(101)) return null;
|
6226 | if (!tokenIsIdentifier(this.state.type)) {
|
6227 | throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
|
6228 | at: this.state.startLoc,
|
6229 | enumName
|
6230 | });
|
6231 | }
|
6232 | const {
|
6233 | value
|
6234 | } = this.state;
|
6235 | this.next();
|
6236 | if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
|
6237 | this.raise(FlowErrors.EnumInvalidExplicitType, {
|
6238 | at: this.state.startLoc,
|
6239 | enumName,
|
6240 | invalidEnumType: value
|
6241 | });
|
6242 | }
|
6243 | return value;
|
6244 | }
|
6245 | flowEnumBody(node, id) {
|
6246 | const enumName = id.name;
|
6247 | const nameLoc = id.loc.start;
|
6248 | const explicitType = this.flowEnumParseExplicitType({
|
6249 | enumName
|
6250 | });
|
6251 | this.expect(5);
|
6252 | const {
|
6253 | members,
|
6254 | hasUnknownMembers
|
6255 | } = this.flowEnumMembers({
|
6256 | enumName,
|
6257 | explicitType
|
6258 | });
|
6259 | node.hasUnknownMembers = hasUnknownMembers;
|
6260 | switch (explicitType) {
|
6261 | case "boolean":
|
6262 | node.explicitType = true;
|
6263 | node.members = members.booleanMembers;
|
6264 | this.expect(8);
|
6265 | return this.finishNode(node, "EnumBooleanBody");
|
6266 | case "number":
|
6267 | node.explicitType = true;
|
6268 | node.members = members.numberMembers;
|
6269 | this.expect(8);
|
6270 | return this.finishNode(node, "EnumNumberBody");
|
6271 | case "string":
|
6272 | node.explicitType = true;
|
6273 | node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
|
6274 | enumName
|
6275 | });
|
6276 | this.expect(8);
|
6277 | return this.finishNode(node, "EnumStringBody");
|
6278 | case "symbol":
|
6279 | node.members = members.defaultedMembers;
|
6280 | this.expect(8);
|
6281 | return this.finishNode(node, "EnumSymbolBody");
|
6282 | default:
|
6283 | {
|
6284 | const empty = () => {
|
6285 | node.members = [];
|
6286 | this.expect(8);
|
6287 | return this.finishNode(node, "EnumStringBody");
|
6288 | };
|
6289 | node.explicitType = false;
|
6290 | const boolsLen = members.booleanMembers.length;
|
6291 | const numsLen = members.numberMembers.length;
|
6292 | const strsLen = members.stringMembers.length;
|
6293 | const defaultedLen = members.defaultedMembers.length;
|
6294 | if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
|
6295 | return empty();
|
6296 | } else if (!boolsLen && !numsLen) {
|
6297 | node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
|
6298 | enumName
|
6299 | });
|
6300 | this.expect(8);
|
6301 | return this.finishNode(node, "EnumStringBody");
|
6302 | } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
|
6303 | for (const member of members.defaultedMembers) {
|
6304 | this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
|
6305 | enumName,
|
6306 | memberName: member.id.name
|
6307 | });
|
6308 | }
|
6309 | node.members = members.booleanMembers;
|
6310 | this.expect(8);
|
6311 | return this.finishNode(node, "EnumBooleanBody");
|
6312 | } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
|
6313 | for (const member of members.defaultedMembers) {
|
6314 | this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
|
6315 | enumName,
|
6316 | memberName: member.id.name
|
6317 | });
|
6318 | }
|
6319 | node.members = members.numberMembers;
|
6320 | this.expect(8);
|
6321 | return this.finishNode(node, "EnumNumberBody");
|
6322 | } else {
|
6323 | this.raise(FlowErrors.EnumInconsistentMemberValues, {
|
6324 | at: nameLoc,
|
6325 | enumName
|
6326 | });
|
6327 | return empty();
|
6328 | }
|
6329 | }
|
6330 | }
|
6331 | }
|
6332 | flowParseEnumDeclaration(node) {
|
6333 | const id = this.parseIdentifier();
|
6334 | node.id = id;
|
6335 | node.body = this.flowEnumBody(this.startNode(), id);
|
6336 | return this.finishNode(node, "EnumDeclaration");
|
6337 | }
|
6338 | isLookaheadToken_lt() {
|
6339 | const next = this.nextTokenStart();
|
6340 | if (this.input.charCodeAt(next) === 60) {
|
6341 | const afterNext = this.input.charCodeAt(next + 1);
|
6342 | return afterNext !== 60 && afterNext !== 61;
|
6343 | }
|
6344 | return false;
|
6345 | }
|
6346 | maybeUnwrapTypeCastExpression(node) {
|
6347 | return node.type === "TypeCastExpression" ? node.expression : node;
|
6348 | }
|
6349 | };
|
6350 | const entities = {
|
6351 | __proto__: null,
|
6352 | quot: "\u0022",
|
6353 | amp: "&",
|
6354 | apos: "\u0027",
|
6355 | lt: "<",
|
6356 | gt: ">",
|
6357 | nbsp: "\u00A0",
|
6358 | iexcl: "\u00A1",
|
6359 | cent: "\u00A2",
|
6360 | pound: "\u00A3",
|
6361 | curren: "\u00A4",
|
6362 | yen: "\u00A5",
|
6363 | brvbar: "\u00A6",
|
6364 | sect: "\u00A7",
|
6365 | uml: "\u00A8",
|
6366 | copy: "\u00A9",
|
6367 | ordf: "\u00AA",
|
6368 | laquo: "\u00AB",
|
6369 | not: "\u00AC",
|
6370 | shy: "\u00AD",
|
6371 | reg: "\u00AE",
|
6372 | macr: "\u00AF",
|
6373 | deg: "\u00B0",
|
6374 | plusmn: "\u00B1",
|
6375 | sup2: "\u00B2",
|
6376 | sup3: "\u00B3",
|
6377 | acute: "\u00B4",
|
6378 | micro: "\u00B5",
|
6379 | para: "\u00B6",
|
6380 | middot: "\u00B7",
|
6381 | cedil: "\u00B8",
|
6382 | sup1: "\u00B9",
|
6383 | ordm: "\u00BA",
|
6384 | raquo: "\u00BB",
|
6385 | frac14: "\u00BC",
|
6386 | frac12: "\u00BD",
|
6387 | frac34: "\u00BE",
|
6388 | iquest: "\u00BF",
|
6389 | Agrave: "\u00C0",
|
6390 | Aacute: "\u00C1",
|
6391 | Acirc: "\u00C2",
|
6392 | Atilde: "\u00C3",
|
6393 | Auml: "\u00C4",
|
6394 | Aring: "\u00C5",
|
6395 | AElig: "\u00C6",
|
6396 | Ccedil: "\u00C7",
|
6397 | Egrave: "\u00C8",
|
6398 | Eacute: "\u00C9",
|
6399 | Ecirc: "\u00CA",
|
6400 | Euml: "\u00CB",
|
6401 | Igrave: "\u00CC",
|
6402 | Iacute: "\u00CD",
|
6403 | Icirc: "\u00CE",
|
6404 | Iuml: "\u00CF",
|
6405 | ETH: "\u00D0",
|
6406 | Ntilde: "\u00D1",
|
6407 | Ograve: "\u00D2",
|
6408 | Oacute: "\u00D3",
|
6409 | Ocirc: "\u00D4",
|
6410 | Otilde: "\u00D5",
|
6411 | Ouml: "\u00D6",
|
6412 | times: "\u00D7",
|
6413 | Oslash: "\u00D8",
|
6414 | Ugrave: "\u00D9",
|
6415 | Uacute: "\u00DA",
|
6416 | Ucirc: "\u00DB",
|
6417 | Uuml: "\u00DC",
|
6418 | Yacute: "\u00DD",
|
6419 | THORN: "\u00DE",
|
6420 | szlig: "\u00DF",
|
6421 | agrave: "\u00E0",
|
6422 | aacute: "\u00E1",
|
6423 | acirc: "\u00E2",
|
6424 | atilde: "\u00E3",
|
6425 | auml: "\u00E4",
|
6426 | aring: "\u00E5",
|
6427 | aelig: "\u00E6",
|
6428 | ccedil: "\u00E7",
|
6429 | egrave: "\u00E8",
|
6430 | eacute: "\u00E9",
|
6431 | ecirc: "\u00EA",
|
6432 | euml: "\u00EB",
|
6433 | igrave: "\u00EC",
|
6434 | iacute: "\u00ED",
|
6435 | icirc: "\u00EE",
|
6436 | iuml: "\u00EF",
|
6437 | eth: "\u00F0",
|
6438 | ntilde: "\u00F1",
|
6439 | ograve: "\u00F2",
|
6440 | oacute: "\u00F3",
|
6441 | ocirc: "\u00F4",
|
6442 | otilde: "\u00F5",
|
6443 | ouml: "\u00F6",
|
6444 | divide: "\u00F7",
|
6445 | oslash: "\u00F8",
|
6446 | ugrave: "\u00F9",
|
6447 | uacute: "\u00FA",
|
6448 | ucirc: "\u00FB",
|
6449 | uuml: "\u00FC",
|
6450 | yacute: "\u00FD",
|
6451 | thorn: "\u00FE",
|
6452 | yuml: "\u00FF",
|
6453 | OElig: "\u0152",
|
6454 | oelig: "\u0153",
|
6455 | Scaron: "\u0160",
|
6456 | scaron: "\u0161",
|
6457 | Yuml: "\u0178",
|
6458 | fnof: "\u0192",
|
6459 | circ: "\u02C6",
|
6460 | tilde: "\u02DC",
|
6461 | Alpha: "\u0391",
|
6462 | Beta: "\u0392",
|
6463 | Gamma: "\u0393",
|
6464 | Delta: "\u0394",
|
6465 | Epsilon: "\u0395",
|
6466 | Zeta: "\u0396",
|
6467 | Eta: "\u0397",
|
6468 | Theta: "\u0398",
|
6469 | Iota: "\u0399",
|
6470 | Kappa: "\u039A",
|
6471 | Lambda: "\u039B",
|
6472 | Mu: "\u039C",
|
6473 | Nu: "\u039D",
|
6474 | Xi: "\u039E",
|
6475 | Omicron: "\u039F",
|
6476 | Pi: "\u03A0",
|
6477 | Rho: "\u03A1",
|
6478 | Sigma: "\u03A3",
|
6479 | Tau: "\u03A4",
|
6480 | Upsilon: "\u03A5",
|
6481 | Phi: "\u03A6",
|
6482 | Chi: "\u03A7",
|
6483 | Psi: "\u03A8",
|
6484 | Omega: "\u03A9",
|
6485 | alpha: "\u03B1",
|
6486 | beta: "\u03B2",
|
6487 | gamma: "\u03B3",
|
6488 | delta: "\u03B4",
|
6489 | epsilon: "\u03B5",
|
6490 | zeta: "\u03B6",
|
6491 | eta: "\u03B7",
|
6492 | theta: "\u03B8",
|
6493 | iota: "\u03B9",
|
6494 | kappa: "\u03BA",
|
6495 | lambda: "\u03BB",
|
6496 | mu: "\u03BC",
|
6497 | nu: "\u03BD",
|
6498 | xi: "\u03BE",
|
6499 | omicron: "\u03BF",
|
6500 | pi: "\u03C0",
|
6501 | rho: "\u03C1",
|
6502 | sigmaf: "\u03C2",
|
6503 | sigma: "\u03C3",
|
6504 | tau: "\u03C4",
|
6505 | upsilon: "\u03C5",
|
6506 | phi: "\u03C6",
|
6507 | chi: "\u03C7",
|
6508 | psi: "\u03C8",
|
6509 | omega: "\u03C9",
|
6510 | thetasym: "\u03D1",
|
6511 | upsih: "\u03D2",
|
6512 | piv: "\u03D6",
|
6513 | ensp: "\u2002",
|
6514 | emsp: "\u2003",
|
6515 | thinsp: "\u2009",
|
6516 | zwnj: "\u200C",
|
6517 | zwj: "\u200D",
|
6518 | lrm: "\u200E",
|
6519 | rlm: "\u200F",
|
6520 | ndash: "\u2013",
|
6521 | mdash: "\u2014",
|
6522 | lsquo: "\u2018",
|
6523 | rsquo: "\u2019",
|
6524 | sbquo: "\u201A",
|
6525 | ldquo: "\u201C",
|
6526 | rdquo: "\u201D",
|
6527 | bdquo: "\u201E",
|
6528 | dagger: "\u2020",
|
6529 | Dagger: "\u2021",
|
6530 | bull: "\u2022",
|
6531 | hellip: "\u2026",
|
6532 | permil: "\u2030",
|
6533 | prime: "\u2032",
|
6534 | Prime: "\u2033",
|
6535 | lsaquo: "\u2039",
|
6536 | rsaquo: "\u203A",
|
6537 | oline: "\u203E",
|
6538 | frasl: "\u2044",
|
6539 | euro: "\u20AC",
|
6540 | image: "\u2111",
|
6541 | weierp: "\u2118",
|
6542 | real: "\u211C",
|
6543 | trade: "\u2122",
|
6544 | alefsym: "\u2135",
|
6545 | larr: "\u2190",
|
6546 | uarr: "\u2191",
|
6547 | rarr: "\u2192",
|
6548 | darr: "\u2193",
|
6549 | harr: "\u2194",
|
6550 | crarr: "\u21B5",
|
6551 | lArr: "\u21D0",
|
6552 | uArr: "\u21D1",
|
6553 | rArr: "\u21D2",
|
6554 | dArr: "\u21D3",
|
6555 | hArr: "\u21D4",
|
6556 | forall: "\u2200",
|
6557 | part: "\u2202",
|
6558 | exist: "\u2203",
|
6559 | empty: "\u2205",
|
6560 | nabla: "\u2207",
|
6561 | isin: "\u2208",
|
6562 | notin: "\u2209",
|
6563 | ni: "\u220B",
|
6564 | prod: "\u220F",
|
6565 | sum: "\u2211",
|
6566 | minus: "\u2212",
|
6567 | lowast: "\u2217",
|
6568 | radic: "\u221A",
|
6569 | prop: "\u221D",
|
6570 | infin: "\u221E",
|
6571 | ang: "\u2220",
|
6572 | and: "\u2227",
|
6573 | or: "\u2228",
|
6574 | cap: "\u2229",
|
6575 | cup: "\u222A",
|
6576 | int: "\u222B",
|
6577 | there4: "\u2234",
|
6578 | sim: "\u223C",
|
6579 | cong: "\u2245",
|
6580 | asymp: "\u2248",
|
6581 | ne: "\u2260",
|
6582 | equiv: "\u2261",
|
6583 | le: "\u2264",
|
6584 | ge: "\u2265",
|
6585 | sub: "\u2282",
|
6586 | sup: "\u2283",
|
6587 | nsub: "\u2284",
|
6588 | sube: "\u2286",
|
6589 | supe: "\u2287",
|
6590 | oplus: "\u2295",
|
6591 | otimes: "\u2297",
|
6592 | perp: "\u22A5",
|
6593 | sdot: "\u22C5",
|
6594 | lceil: "\u2308",
|
6595 | rceil: "\u2309",
|
6596 | lfloor: "\u230A",
|
6597 | rfloor: "\u230B",
|
6598 | lang: "\u2329",
|
6599 | rang: "\u232A",
|
6600 | loz: "\u25CA",
|
6601 | spades: "\u2660",
|
6602 | clubs: "\u2663",
|
6603 | hearts: "\u2665",
|
6604 | diams: "\u2666"
|
6605 | };
|
6606 | const JsxErrors = ParseErrorEnum`jsx`({
|
6607 | AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
|
6608 | MissingClosingTagElement: ({
|
6609 | openingTagName
|
6610 | }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
|
6611 | MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
|
6612 | UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
|
6613 | UnexpectedToken: ({
|
6614 | unexpected,
|
6615 | HTMLEntity
|
6616 | }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
|
6617 | UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
|
6618 | UnterminatedJsxContent: "Unterminated JSX contents.",
|
6619 | UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
|
6620 | });
|
6621 | function isFragment(object) {
|
6622 | return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
|
6623 | }
|
6624 | function getQualifiedJSXName(object) {
|
6625 | if (object.type === "JSXIdentifier") {
|
6626 | return object.name;
|
6627 | }
|
6628 | if (object.type === "JSXNamespacedName") {
|
6629 | return object.namespace.name + ":" + object.name.name;
|
6630 | }
|
6631 | if (object.type === "JSXMemberExpression") {
|
6632 | return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
|
6633 | }
|
6634 | throw new Error("Node had unexpected type: " + object.type);
|
6635 | }
|
6636 | var jsx = superClass => class JSXParserMixin extends superClass {
|
6637 | jsxReadToken() {
|
6638 | let out = "";
|
6639 | let chunkStart = this.state.pos;
|
6640 | for (;;) {
|
6641 | if (this.state.pos >= this.length) {
|
6642 | throw this.raise(JsxErrors.UnterminatedJsxContent, {
|
6643 | at: this.state.startLoc
|
6644 | });
|
6645 | }
|
6646 | const ch = this.input.charCodeAt(this.state.pos);
|
6647 | switch (ch) {
|
6648 | case 60:
|
6649 | case 123:
|
6650 | if (this.state.pos === this.state.start) {
|
6651 | if (ch === 60 && this.state.canStartJSXElement) {
|
6652 | ++this.state.pos;
|
6653 | this.finishToken(140);
|
6654 | } else {
|
6655 | super.getTokenFromCode(ch);
|
6656 | }
|
6657 | return;
|
6658 | }
|
6659 | out += this.input.slice(chunkStart, this.state.pos);
|
6660 | this.finishToken(139, out);
|
6661 | return;
|
6662 | case 38:
|
6663 | out += this.input.slice(chunkStart, this.state.pos);
|
6664 | out += this.jsxReadEntity();
|
6665 | chunkStart = this.state.pos;
|
6666 | break;
|
6667 | case 62:
|
6668 | case 125:
|
6669 | default:
|
6670 | if (isNewLine(ch)) {
|
6671 | out += this.input.slice(chunkStart, this.state.pos);
|
6672 | out += this.jsxReadNewLine(true);
|
6673 | chunkStart = this.state.pos;
|
6674 | } else {
|
6675 | ++this.state.pos;
|
6676 | }
|
6677 | }
|
6678 | }
|
6679 | }
|
6680 | jsxReadNewLine(normalizeCRLF) {
|
6681 | const ch = this.input.charCodeAt(this.state.pos);
|
6682 | let out;
|
6683 | ++this.state.pos;
|
6684 | if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
|
6685 | ++this.state.pos;
|
6686 | out = normalizeCRLF ? "\n" : "\r\n";
|
6687 | } else {
|
6688 | out = String.fromCharCode(ch);
|
6689 | }
|
6690 | ++this.state.curLine;
|
6691 | this.state.lineStart = this.state.pos;
|
6692 | return out;
|
6693 | }
|
6694 | jsxReadString(quote) {
|
6695 | let out = "";
|
6696 | let chunkStart = ++this.state.pos;
|
6697 | for (;;) {
|
6698 | if (this.state.pos >= this.length) {
|
6699 | throw this.raise(Errors.UnterminatedString, {
|
6700 | at: this.state.startLoc
|
6701 | });
|
6702 | }
|
6703 | const ch = this.input.charCodeAt(this.state.pos);
|
6704 | if (ch === quote) break;
|
6705 | if (ch === 38) {
|
6706 | out += this.input.slice(chunkStart, this.state.pos);
|
6707 | out += this.jsxReadEntity();
|
6708 | chunkStart = this.state.pos;
|
6709 | } else if (isNewLine(ch)) {
|
6710 | out += this.input.slice(chunkStart, this.state.pos);
|
6711 | out += this.jsxReadNewLine(false);
|
6712 | chunkStart = this.state.pos;
|
6713 | } else {
|
6714 | ++this.state.pos;
|
6715 | }
|
6716 | }
|
6717 | out += this.input.slice(chunkStart, this.state.pos++);
|
6718 | this.finishToken(131, out);
|
6719 | }
|
6720 | jsxReadEntity() {
|
6721 | const startPos = ++this.state.pos;
|
6722 | if (this.codePointAtPos(this.state.pos) === 35) {
|
6723 | ++this.state.pos;
|
6724 | let radix = 10;
|
6725 | if (this.codePointAtPos(this.state.pos) === 120) {
|
6726 | radix = 16;
|
6727 | ++this.state.pos;
|
6728 | }
|
6729 | const codePoint = this.readInt(radix, undefined, false, "bail");
|
6730 | if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
|
6731 | ++this.state.pos;
|
6732 | return String.fromCodePoint(codePoint);
|
6733 | }
|
6734 | } else {
|
6735 | let count = 0;
|
6736 | let semi = false;
|
6737 | while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
|
6738 | ++this.state.pos;
|
6739 | }
|
6740 | if (semi) {
|
6741 | const desc = this.input.slice(startPos, this.state.pos);
|
6742 | const entity = entities[desc];
|
6743 | ++this.state.pos;
|
6744 | if (entity) {
|
6745 | return entity;
|
6746 | }
|
6747 | }
|
6748 | }
|
6749 | this.state.pos = startPos;
|
6750 | return "&";
|
6751 | }
|
6752 | jsxReadWord() {
|
6753 | let ch;
|
6754 | const start = this.state.pos;
|
6755 | do {
|
6756 | ch = this.input.charCodeAt(++this.state.pos);
|
6757 | } while (isIdentifierChar(ch) || ch === 45);
|
6758 | this.finishToken(138, this.input.slice(start, this.state.pos));
|
6759 | }
|
6760 | jsxParseIdentifier() {
|
6761 | const node = this.startNode();
|
6762 | if (this.match(138)) {
|
6763 | node.name = this.state.value;
|
6764 | } else if (tokenIsKeyword(this.state.type)) {
|
6765 | node.name = tokenLabelName(this.state.type);
|
6766 | } else {
|
6767 | this.unexpected();
|
6768 | }
|
6769 | this.next();
|
6770 | return this.finishNode(node, "JSXIdentifier");
|
6771 | }
|
6772 | jsxParseNamespacedName() {
|
6773 | const startLoc = this.state.startLoc;
|
6774 | const name = this.jsxParseIdentifier();
|
6775 | if (!this.eat(14)) return name;
|
6776 | const node = this.startNodeAt(startLoc);
|
6777 | node.namespace = name;
|
6778 | node.name = this.jsxParseIdentifier();
|
6779 | return this.finishNode(node, "JSXNamespacedName");
|
6780 | }
|
6781 | jsxParseElementName() {
|
6782 | const startLoc = this.state.startLoc;
|
6783 | let node = this.jsxParseNamespacedName();
|
6784 | if (node.type === "JSXNamespacedName") {
|
6785 | return node;
|
6786 | }
|
6787 | while (this.eat(16)) {
|
6788 | const newNode = this.startNodeAt(startLoc);
|
6789 | newNode.object = node;
|
6790 | newNode.property = this.jsxParseIdentifier();
|
6791 | node = this.finishNode(newNode, "JSXMemberExpression");
|
6792 | }
|
6793 | return node;
|
6794 | }
|
6795 | jsxParseAttributeValue() {
|
6796 | let node;
|
6797 | switch (this.state.type) {
|
6798 | case 5:
|
6799 | node = this.startNode();
|
6800 | this.setContext(types.brace);
|
6801 | this.next();
|
6802 | node = this.jsxParseExpressionContainer(node, types.j_oTag);
|
6803 | if (node.expression.type === "JSXEmptyExpression") {
|
6804 | this.raise(JsxErrors.AttributeIsEmpty, {
|
6805 | at: node
|
6806 | });
|
6807 | }
|
6808 | return node;
|
6809 | case 140:
|
6810 | case 131:
|
6811 | return this.parseExprAtom();
|
6812 | default:
|
6813 | throw this.raise(JsxErrors.UnsupportedJsxValue, {
|
6814 | at: this.state.startLoc
|
6815 | });
|
6816 | }
|
6817 | }
|
6818 | jsxParseEmptyExpression() {
|
6819 | const node = this.startNodeAt(this.state.lastTokEndLoc);
|
6820 | return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
|
6821 | }
|
6822 | jsxParseSpreadChild(node) {
|
6823 | this.next();
|
6824 | node.expression = this.parseExpression();
|
6825 | this.setContext(types.j_expr);
|
6826 | this.state.canStartJSXElement = true;
|
6827 | this.expect(8);
|
6828 | return this.finishNode(node, "JSXSpreadChild");
|
6829 | }
|
6830 | jsxParseExpressionContainer(node, previousContext) {
|
6831 | if (this.match(8)) {
|
6832 | node.expression = this.jsxParseEmptyExpression();
|
6833 | } else {
|
6834 | const expression = this.parseExpression();
|
6835 | node.expression = expression;
|
6836 | }
|
6837 | this.setContext(previousContext);
|
6838 | this.state.canStartJSXElement = true;
|
6839 | this.expect(8);
|
6840 | return this.finishNode(node, "JSXExpressionContainer");
|
6841 | }
|
6842 | jsxParseAttribute() {
|
6843 | const node = this.startNode();
|
6844 | if (this.match(5)) {
|
6845 | this.setContext(types.brace);
|
6846 | this.next();
|
6847 | this.expect(21);
|
6848 | node.argument = this.parseMaybeAssignAllowIn();
|
6849 | this.setContext(types.j_oTag);
|
6850 | this.state.canStartJSXElement = true;
|
6851 | this.expect(8);
|
6852 | return this.finishNode(node, "JSXSpreadAttribute");
|
6853 | }
|
6854 | node.name = this.jsxParseNamespacedName();
|
6855 | node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
|
6856 | return this.finishNode(node, "JSXAttribute");
|
6857 | }
|
6858 | jsxParseOpeningElementAt(startLoc) {
|
6859 | const node = this.startNodeAt(startLoc);
|
6860 | if (this.eat(141)) {
|
6861 | return this.finishNode(node, "JSXOpeningFragment");
|
6862 | }
|
6863 | node.name = this.jsxParseElementName();
|
6864 | return this.jsxParseOpeningElementAfterName(node);
|
6865 | }
|
6866 | jsxParseOpeningElementAfterName(node) {
|
6867 | const attributes = [];
|
6868 | while (!this.match(56) && !this.match(141)) {
|
6869 | attributes.push(this.jsxParseAttribute());
|
6870 | }
|
6871 | node.attributes = attributes;
|
6872 | node.selfClosing = this.eat(56);
|
6873 | this.expect(141);
|
6874 | return this.finishNode(node, "JSXOpeningElement");
|
6875 | }
|
6876 | jsxParseClosingElementAt(startLoc) {
|
6877 | const node = this.startNodeAt(startLoc);
|
6878 | if (this.eat(141)) {
|
6879 | return this.finishNode(node, "JSXClosingFragment");
|
6880 | }
|
6881 | node.name = this.jsxParseElementName();
|
6882 | this.expect(141);
|
6883 | return this.finishNode(node, "JSXClosingElement");
|
6884 | }
|
6885 | jsxParseElementAt(startLoc) {
|
6886 | const node = this.startNodeAt(startLoc);
|
6887 | const children = [];
|
6888 | const openingElement = this.jsxParseOpeningElementAt(startLoc);
|
6889 | let closingElement = null;
|
6890 | if (!openingElement.selfClosing) {
|
6891 | contents: for (;;) {
|
6892 | switch (this.state.type) {
|
6893 | case 140:
|
6894 | startLoc = this.state.startLoc;
|
6895 | this.next();
|
6896 | if (this.eat(56)) {
|
6897 | closingElement = this.jsxParseClosingElementAt(startLoc);
|
6898 | break contents;
|
6899 | }
|
6900 | children.push(this.jsxParseElementAt(startLoc));
|
6901 | break;
|
6902 | case 139:
|
6903 | children.push(this.parseExprAtom());
|
6904 | break;
|
6905 | case 5:
|
6906 | {
|
6907 | const node = this.startNode();
|
6908 | this.setContext(types.brace);
|
6909 | this.next();
|
6910 | if (this.match(21)) {
|
6911 | children.push(this.jsxParseSpreadChild(node));
|
6912 | } else {
|
6913 | children.push(this.jsxParseExpressionContainer(node, types.j_expr));
|
6914 | }
|
6915 | break;
|
6916 | }
|
6917 | default:
|
6918 | this.unexpected();
|
6919 | }
|
6920 | }
|
6921 | if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
|
6922 | this.raise(JsxErrors.MissingClosingTagFragment, {
|
6923 | at: closingElement
|
6924 | });
|
6925 | } else if (!isFragment(openingElement) && isFragment(closingElement)) {
|
6926 | this.raise(JsxErrors.MissingClosingTagElement, {
|
6927 | at: closingElement,
|
6928 | openingTagName: getQualifiedJSXName(openingElement.name)
|
6929 | });
|
6930 | } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
|
6931 | if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
|
6932 | this.raise(JsxErrors.MissingClosingTagElement, {
|
6933 | at: closingElement,
|
6934 | openingTagName: getQualifiedJSXName(openingElement.name)
|
6935 | });
|
6936 | }
|
6937 | }
|
6938 | }
|
6939 | if (isFragment(openingElement)) {
|
6940 | node.openingFragment = openingElement;
|
6941 | node.closingFragment = closingElement;
|
6942 | } else {
|
6943 | node.openingElement = openingElement;
|
6944 | node.closingElement = closingElement;
|
6945 | }
|
6946 | node.children = children;
|
6947 | if (this.match(47)) {
|
6948 | throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
|
6949 | at: this.state.startLoc
|
6950 | });
|
6951 | }
|
6952 | return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
|
6953 | }
|
6954 | jsxParseElement() {
|
6955 | const startLoc = this.state.startLoc;
|
6956 | this.next();
|
6957 | return this.jsxParseElementAt(startLoc);
|
6958 | }
|
6959 | setContext(newContext) {
|
6960 | const {
|
6961 | context
|
6962 | } = this.state;
|
6963 | context[context.length - 1] = newContext;
|
6964 | }
|
6965 | parseExprAtom(refExpressionErrors) {
|
6966 | if (this.match(139)) {
|
6967 | return this.parseLiteral(this.state.value, "JSXText");
|
6968 | } else if (this.match(140)) {
|
6969 | return this.jsxParseElement();
|
6970 | } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
|
6971 | this.replaceToken(140);
|
6972 | return this.jsxParseElement();
|
6973 | } else {
|
6974 | return super.parseExprAtom(refExpressionErrors);
|
6975 | }
|
6976 | }
|
6977 | skipSpace() {
|
6978 | const curContext = this.curContext();
|
6979 | if (!curContext.preserveSpace) super.skipSpace();
|
6980 | }
|
6981 | getTokenFromCode(code) {
|
6982 | const context = this.curContext();
|
6983 | if (context === types.j_expr) {
|
6984 | this.jsxReadToken();
|
6985 | return;
|
6986 | }
|
6987 | if (context === types.j_oTag || context === types.j_cTag) {
|
6988 | if (isIdentifierStart(code)) {
|
6989 | this.jsxReadWord();
|
6990 | return;
|
6991 | }
|
6992 | if (code === 62) {
|
6993 | ++this.state.pos;
|
6994 | this.finishToken(141);
|
6995 | return;
|
6996 | }
|
6997 | if ((code === 34 || code === 39) && context === types.j_oTag) {
|
6998 | this.jsxReadString(code);
|
6999 | return;
|
7000 | }
|
7001 | }
|
7002 | if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
|
7003 | ++this.state.pos;
|
7004 | this.finishToken(140);
|
7005 | return;
|
7006 | }
|
7007 | super.getTokenFromCode(code);
|
7008 | }
|
7009 | updateContext(prevType) {
|
7010 | const {
|
7011 | context,
|
7012 | type
|
7013 | } = this.state;
|
7014 | if (type === 56 && prevType === 140) {
|
7015 | context.splice(-2, 2, types.j_cTag);
|
7016 | this.state.canStartJSXElement = false;
|
7017 | } else if (type === 140) {
|
7018 | context.push(types.j_oTag);
|
7019 | } else if (type === 141) {
|
7020 | const out = context[context.length - 1];
|
7021 | if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
|
7022 | context.pop();
|
7023 | this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
|
7024 | } else {
|
7025 | this.setContext(types.j_expr);
|
7026 | this.state.canStartJSXElement = true;
|
7027 | }
|
7028 | } else {
|
7029 | this.state.canStartJSXElement = tokenComesBeforeExpression(type);
|
7030 | }
|
7031 | }
|
7032 | };
|
7033 | class TypeScriptScope extends Scope {
|
7034 | constructor(...args) {
|
7035 | super(...args);
|
7036 | this.types = new Set();
|
7037 | this.enums = new Set();
|
7038 | this.constEnums = new Set();
|
7039 | this.classes = new Set();
|
7040 | this.exportOnlyBindings = new Set();
|
7041 | }
|
7042 | }
|
7043 | class TypeScriptScopeHandler extends ScopeHandler {
|
7044 | constructor(...args) {
|
7045 | super(...args);
|
7046 | this.importsStack = [];
|
7047 | }
|
7048 | createScope(flags) {
|
7049 | this.importsStack.push(new Set());
|
7050 | return new TypeScriptScope(flags);
|
7051 | }
|
7052 | enter(flags) {
|
7053 | if (flags == SCOPE_TS_MODULE) {
|
7054 | this.importsStack.push(new Set());
|
7055 | }
|
7056 | super.enter(flags);
|
7057 | }
|
7058 | exit() {
|
7059 | const flags = super.exit();
|
7060 | if (flags == SCOPE_TS_MODULE) {
|
7061 | this.importsStack.pop();
|
7062 | }
|
7063 | return flags;
|
7064 | }
|
7065 | hasImport(name, allowShadow) {
|
7066 | const len = this.importsStack.length;
|
7067 | if (this.importsStack[len - 1].has(name)) {
|
7068 | return true;
|
7069 | }
|
7070 | if (!allowShadow && len > 1) {
|
7071 | for (let i = 0; i < len - 1; i++) {
|
7072 | if (this.importsStack[i].has(name)) return true;
|
7073 | }
|
7074 | }
|
7075 | return false;
|
7076 | }
|
7077 | declareName(name, bindingType, loc) {
|
7078 | if (bindingType & BIND_FLAGS_TS_IMPORT) {
|
7079 | if (this.hasImport(name, true)) {
|
7080 | this.parser.raise(Errors.VarRedeclaration, {
|
7081 | at: loc,
|
7082 | identifierName: name
|
7083 | });
|
7084 | }
|
7085 | this.importsStack[this.importsStack.length - 1].add(name);
|
7086 | return;
|
7087 | }
|
7088 | const scope = this.currentScope();
|
7089 | if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
|
7090 | this.maybeExportDefined(scope, name);
|
7091 | scope.exportOnlyBindings.add(name);
|
7092 | return;
|
7093 | }
|
7094 | super.declareName(name, bindingType, loc);
|
7095 | if (bindingType & BIND_KIND_TYPE) {
|
7096 | if (!(bindingType & BIND_KIND_VALUE)) {
|
7097 | this.checkRedeclarationInScope(scope, name, bindingType, loc);
|
7098 | this.maybeExportDefined(scope, name);
|
7099 | }
|
7100 | scope.types.add(name);
|
7101 | }
|
7102 | if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
|
7103 | if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
|
7104 | if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
|
7105 | }
|
7106 | isRedeclaredInScope(scope, name, bindingType) {
|
7107 | if (scope.enums.has(name)) {
|
7108 | if (bindingType & BIND_FLAGS_TS_ENUM) {
|
7109 | const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
|
7110 | const wasConst = scope.constEnums.has(name);
|
7111 | return isConst !== wasConst;
|
7112 | }
|
7113 | return true;
|
7114 | }
|
7115 | if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
|
7116 | if (scope.lexical.has(name)) {
|
7117 | return !!(bindingType & BIND_KIND_VALUE);
|
7118 | } else {
|
7119 | return false;
|
7120 | }
|
7121 | }
|
7122 | if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
|
7123 | return true;
|
7124 | }
|
7125 | return super.isRedeclaredInScope(scope, name, bindingType);
|
7126 | }
|
7127 | checkLocalExport(id) {
|
7128 | const {
|
7129 | name
|
7130 | } = id;
|
7131 | if (this.hasImport(name)) return;
|
7132 | const len = this.scopeStack.length;
|
7133 | for (let i = len - 1; i >= 0; i--) {
|
7134 | const scope = this.scopeStack[i];
|
7135 | if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
|
7136 | }
|
7137 | super.checkLocalExport(id);
|
7138 | }
|
7139 | }
|
7140 | const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
|
7141 | const unwrapParenthesizedExpression = node => {
|
7142 | return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
|
7143 | };
|
7144 | class LValParser extends NodeUtils {
|
7145 | toAssignable(node, isLHS = false) {
|
7146 | var _node$extra, _node$extra3;
|
7147 | let parenthesized = undefined;
|
7148 | if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
|
7149 | parenthesized = unwrapParenthesizedExpression(node);
|
7150 | if (isLHS) {
|
7151 | if (parenthesized.type === "Identifier") {
|
7152 | this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
|
7153 | at: node
|
7154 | });
|
7155 | } else if (parenthesized.type !== "MemberExpression") {
|
7156 | this.raise(Errors.InvalidParenthesizedAssignment, {
|
7157 | at: node
|
7158 | });
|
7159 | }
|
7160 | } else {
|
7161 | this.raise(Errors.InvalidParenthesizedAssignment, {
|
7162 | at: node
|
7163 | });
|
7164 | }
|
7165 | }
|
7166 | switch (node.type) {
|
7167 | case "Identifier":
|
7168 | case "ObjectPattern":
|
7169 | case "ArrayPattern":
|
7170 | case "AssignmentPattern":
|
7171 | case "RestElement":
|
7172 | break;
|
7173 | case "ObjectExpression":
|
7174 | node.type = "ObjectPattern";
|
7175 | for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
|
7176 | var _node$extra2;
|
7177 | const prop = node.properties[i];
|
7178 | const isLast = i === last;
|
7179 | this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
|
7180 | if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
|
7181 | this.raise(Errors.RestTrailingComma, {
|
7182 | at: node.extra.trailingCommaLoc
|
7183 | });
|
7184 | }
|
7185 | }
|
7186 | break;
|
7187 | case "ObjectProperty":
|
7188 | {
|
7189 | const {
|
7190 | key,
|
7191 | value
|
7192 | } = node;
|
7193 | if (this.isPrivateName(key)) {
|
7194 | this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
|
7195 | }
|
7196 | this.toAssignable(value, isLHS);
|
7197 | break;
|
7198 | }
|
7199 | case "SpreadElement":
|
7200 | {
|
7201 | throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
|
7202 | }
|
7203 | case "ArrayExpression":
|
7204 | node.type = "ArrayPattern";
|
7205 | this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
|
7206 | break;
|
7207 | case "AssignmentExpression":
|
7208 | if (node.operator !== "=") {
|
7209 | this.raise(Errors.MissingEqInAssignment, {
|
7210 | at: node.left.loc.end
|
7211 | });
|
7212 | }
|
7213 | node.type = "AssignmentPattern";
|
7214 | delete node.operator;
|
7215 | this.toAssignable(node.left, isLHS);
|
7216 | break;
|
7217 | case "ParenthesizedExpression":
|
7218 | this.toAssignable(parenthesized, isLHS);
|
7219 | break;
|
7220 | }
|
7221 | }
|
7222 | toAssignableObjectExpressionProp(prop, isLast, isLHS) {
|
7223 | if (prop.type === "ObjectMethod") {
|
7224 | this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
|
7225 | at: prop.key
|
7226 | });
|
7227 | } else if (prop.type === "SpreadElement") {
|
7228 | prop.type = "RestElement";
|
7229 | const arg = prop.argument;
|
7230 | this.checkToRestConversion(arg, false);
|
7231 | this.toAssignable(arg, isLHS);
|
7232 | if (!isLast) {
|
7233 | this.raise(Errors.RestTrailingComma, {
|
7234 | at: prop
|
7235 | });
|
7236 | }
|
7237 | } else {
|
7238 | this.toAssignable(prop, isLHS);
|
7239 | }
|
7240 | }
|
7241 | toAssignableList(exprList, trailingCommaLoc, isLHS) {
|
7242 | const end = exprList.length - 1;
|
7243 | for (let i = 0; i <= end; i++) {
|
7244 | const elt = exprList[i];
|
7245 | if (!elt) continue;
|
7246 | if (elt.type === "SpreadElement") {
|
7247 | elt.type = "RestElement";
|
7248 | const arg = elt.argument;
|
7249 | this.checkToRestConversion(arg, true);
|
7250 | this.toAssignable(arg, isLHS);
|
7251 | } else {
|
7252 | this.toAssignable(elt, isLHS);
|
7253 | }
|
7254 | if (elt.type === "RestElement") {
|
7255 | if (i < end) {
|
7256 | this.raise(Errors.RestTrailingComma, {
|
7257 | at: elt
|
7258 | });
|
7259 | } else if (trailingCommaLoc) {
|
7260 | this.raise(Errors.RestTrailingComma, {
|
7261 | at: trailingCommaLoc
|
7262 | });
|
7263 | }
|
7264 | }
|
7265 | }
|
7266 | }
|
7267 | isAssignable(node, isBinding) {
|
7268 | switch (node.type) {
|
7269 | case "Identifier":
|
7270 | case "ObjectPattern":
|
7271 | case "ArrayPattern":
|
7272 | case "AssignmentPattern":
|
7273 | case "RestElement":
|
7274 | return true;
|
7275 | case "ObjectExpression":
|
7276 | {
|
7277 | const last = node.properties.length - 1;
|
7278 | return node.properties.every((prop, i) => {
|
7279 | return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
|
7280 | });
|
7281 | }
|
7282 | case "ObjectProperty":
|
7283 | return this.isAssignable(node.value);
|
7284 | case "SpreadElement":
|
7285 | return this.isAssignable(node.argument);
|
7286 | case "ArrayExpression":
|
7287 | return node.elements.every(element => element === null || this.isAssignable(element));
|
7288 | case "AssignmentExpression":
|
7289 | return node.operator === "=";
|
7290 | case "ParenthesizedExpression":
|
7291 | return this.isAssignable(node.expression);
|
7292 | case "MemberExpression":
|
7293 | case "OptionalMemberExpression":
|
7294 | return !isBinding;
|
7295 | default:
|
7296 | return false;
|
7297 | }
|
7298 | }
|
7299 | toReferencedList(exprList, isParenthesizedExpr) {
|
7300 | return exprList;
|
7301 | }
|
7302 | toReferencedListDeep(exprList, isParenthesizedExpr) {
|
7303 | this.toReferencedList(exprList, isParenthesizedExpr);
|
7304 | for (const expr of exprList) {
|
7305 | if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
|
7306 | this.toReferencedListDeep(expr.elements);
|
7307 | }
|
7308 | }
|
7309 | }
|
7310 | parseSpread(refExpressionErrors) {
|
7311 | const node = this.startNode();
|
7312 | this.next();
|
7313 | node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
|
7314 | return this.finishNode(node, "SpreadElement");
|
7315 | }
|
7316 | parseRestBinding() {
|
7317 | const node = this.startNode();
|
7318 | this.next();
|
7319 | node.argument = this.parseBindingAtom();
|
7320 | return this.finishNode(node, "RestElement");
|
7321 | }
|
7322 | parseBindingAtom() {
|
7323 | switch (this.state.type) {
|
7324 | case 0:
|
7325 | {
|
7326 | const node = this.startNode();
|
7327 | this.next();
|
7328 | node.elements = this.parseBindingList(3, 93, 1);
|
7329 | return this.finishNode(node, "ArrayPattern");
|
7330 | }
|
7331 | case 5:
|
7332 | return this.parseObjectLike(8, true);
|
7333 | }
|
7334 | return this.parseIdentifier();
|
7335 | }
|
7336 | parseBindingList(close, closeCharCode, flags) {
|
7337 | const allowEmpty = flags & 1;
|
7338 | const elts = [];
|
7339 | let first = true;
|
7340 | while (!this.eat(close)) {
|
7341 | if (first) {
|
7342 | first = false;
|
7343 | } else {
|
7344 | this.expect(12);
|
7345 | }
|
7346 | if (allowEmpty && this.match(12)) {
|
7347 | elts.push(null);
|
7348 | } else if (this.eat(close)) {
|
7349 | break;
|
7350 | } else if (this.match(21)) {
|
7351 | elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
|
7352 | if (!this.checkCommaAfterRest(closeCharCode)) {
|
7353 | this.expect(close);
|
7354 | break;
|
7355 | }
|
7356 | } else {
|
7357 | const decorators = [];
|
7358 | if (this.match(26) && this.hasPlugin("decorators")) {
|
7359 | this.raise(Errors.UnsupportedParameterDecorator, {
|
7360 | at: this.state.startLoc
|
7361 | });
|
7362 | }
|
7363 | while (this.match(26)) {
|
7364 | decorators.push(this.parseDecorator());
|
7365 | }
|
7366 | elts.push(this.parseAssignableListItem(flags, decorators));
|
7367 | }
|
7368 | }
|
7369 | return elts;
|
7370 | }
|
7371 | parseBindingRestProperty(prop) {
|
7372 | this.next();
|
7373 | prop.argument = this.parseIdentifier();
|
7374 | this.checkCommaAfterRest(125);
|
7375 | return this.finishNode(prop, "RestElement");
|
7376 | }
|
7377 | parseBindingProperty() {
|
7378 | const prop = this.startNode();
|
7379 | const {
|
7380 | type,
|
7381 | startLoc
|
7382 | } = this.state;
|
7383 | if (type === 21) {
|
7384 | return this.parseBindingRestProperty(prop);
|
7385 | } else if (type === 136) {
|
7386 | this.expectPlugin("destructuringPrivate", startLoc);
|
7387 | this.classScope.usePrivateName(this.state.value, startLoc);
|
7388 | prop.key = this.parsePrivateName();
|
7389 | } else {
|
7390 | this.parsePropertyName(prop);
|
7391 | }
|
7392 | prop.method = false;
|
7393 | return this.parseObjPropValue(prop, startLoc, false, false, true, false);
|
7394 | }
|
7395 | parseAssignableListItem(flags, decorators) {
|
7396 | const left = this.parseMaybeDefault();
|
7397 | this.parseAssignableListItemTypes(left, flags);
|
7398 | const elt = this.parseMaybeDefault(left.loc.start, left);
|
7399 | if (decorators.length) {
|
7400 | left.decorators = decorators;
|
7401 | }
|
7402 | return elt;
|
7403 | }
|
7404 | parseAssignableListItemTypes(param, flags) {
|
7405 | return param;
|
7406 | }
|
7407 | parseMaybeDefault(startLoc, left) {
|
7408 | var _startLoc, _left;
|
7409 | (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
|
7410 | left = (_left = left) != null ? _left : this.parseBindingAtom();
|
7411 | if (!this.eat(29)) return left;
|
7412 | const node = this.startNodeAt(startLoc);
|
7413 | node.left = left;
|
7414 | node.right = this.parseMaybeAssignAllowIn();
|
7415 | return this.finishNode(node, "AssignmentPattern");
|
7416 | }
|
7417 | isValidLVal(type, isUnparenthesizedInAssign, binding) {
|
7418 | return getOwn$1({
|
7419 | AssignmentPattern: "left",
|
7420 | RestElement: "argument",
|
7421 | ObjectProperty: "value",
|
7422 | ParenthesizedExpression: "expression",
|
7423 | ArrayPattern: "elements",
|
7424 | ObjectPattern: "properties"
|
7425 | }, type);
|
7426 | }
|
7427 | checkLVal(expression, {
|
7428 | in: ancestor,
|
7429 | binding = BIND_NONE,
|
7430 | checkClashes = false,
|
7431 | strictModeChanged = false,
|
7432 | hasParenthesizedAncestor = false
|
7433 | }) {
|
7434 | var _expression$extra;
|
7435 | const type = expression.type;
|
7436 | if (this.isObjectMethod(expression)) return;
|
7437 | if (type === "MemberExpression") {
|
7438 | if (binding !== BIND_NONE) {
|
7439 | this.raise(Errors.InvalidPropertyBindingPattern, {
|
7440 | at: expression
|
7441 | });
|
7442 | }
|
7443 | return;
|
7444 | }
|
7445 | if (type === "Identifier") {
|
7446 | this.checkIdentifier(expression, binding, strictModeChanged);
|
7447 | const {
|
7448 | name
|
7449 | } = expression;
|
7450 | if (checkClashes) {
|
7451 | if (checkClashes.has(name)) {
|
7452 | this.raise(Errors.ParamDupe, {
|
7453 | at: expression
|
7454 | });
|
7455 | } else {
|
7456 | checkClashes.add(name);
|
7457 | }
|
7458 | }
|
7459 | return;
|
7460 | }
|
7461 | const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
|
7462 | if (validity === true) return;
|
7463 | if (validity === false) {
|
7464 | const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
|
7465 | this.raise(ParseErrorClass, {
|
7466 | at: expression,
|
7467 | ancestor
|
7468 | });
|
7469 | return;
|
7470 | }
|
7471 | const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
|
7472 | const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
|
7473 | type
|
7474 | } : ancestor;
|
7475 | for (const child of [].concat(expression[key])) {
|
7476 | if (child) {
|
7477 | this.checkLVal(child, {
|
7478 | in: nextAncestor,
|
7479 | binding,
|
7480 | checkClashes,
|
7481 | strictModeChanged,
|
7482 | hasParenthesizedAncestor: isParenthesizedExpression
|
7483 | });
|
7484 | }
|
7485 | }
|
7486 | }
|
7487 | checkIdentifier(at, bindingType, strictModeChanged = false) {
|
7488 | if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
|
7489 | if (bindingType === BIND_NONE) {
|
7490 | this.raise(Errors.StrictEvalArguments, {
|
7491 | at,
|
7492 | referenceName: at.name
|
7493 | });
|
7494 | } else {
|
7495 | this.raise(Errors.StrictEvalArgumentsBinding, {
|
7496 | at,
|
7497 | bindingName: at.name
|
7498 | });
|
7499 | }
|
7500 | }
|
7501 | if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
|
7502 | this.raise(Errors.LetInLexicalBinding, {
|
7503 | at
|
7504 | });
|
7505 | }
|
7506 | if (!(bindingType & BIND_NONE)) {
|
7507 | this.declareNameFromIdentifier(at, bindingType);
|
7508 | }
|
7509 | }
|
7510 | declareNameFromIdentifier(identifier, binding) {
|
7511 | this.scope.declareName(identifier.name, binding, identifier.loc.start);
|
7512 | }
|
7513 | checkToRestConversion(node, allowPattern) {
|
7514 | switch (node.type) {
|
7515 | case "ParenthesizedExpression":
|
7516 | this.checkToRestConversion(node.expression, allowPattern);
|
7517 | break;
|
7518 | case "Identifier":
|
7519 | case "MemberExpression":
|
7520 | break;
|
7521 | case "ArrayExpression":
|
7522 | case "ObjectExpression":
|
7523 | if (allowPattern) break;
|
7524 | default:
|
7525 | this.raise(Errors.InvalidRestAssignmentPattern, {
|
7526 | at: node
|
7527 | });
|
7528 | }
|
7529 | }
|
7530 | checkCommaAfterRest(close) {
|
7531 | if (!this.match(12)) {
|
7532 | return false;
|
7533 | }
|
7534 | this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
|
7535 | at: this.state.startLoc
|
7536 | });
|
7537 | return true;
|
7538 | }
|
7539 | }
|
7540 | const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
|
7541 | function nonNull(x) {
|
7542 | if (x == null) {
|
7543 | throw new Error(`Unexpected ${x} value.`);
|
7544 | }
|
7545 | return x;
|
7546 | }
|
7547 | function assert(x) {
|
7548 | if (!x) {
|
7549 | throw new Error("Assert fail");
|
7550 | }
|
7551 | }
|
7552 | const TSErrors = ParseErrorEnum`typescript`({
|
7553 | AbstractMethodHasImplementation: ({
|
7554 | methodName
|
7555 | }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
|
7556 | AbstractPropertyHasInitializer: ({
|
7557 | propertyName
|
7558 | }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
|
7559 | AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
|
7560 | AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
|
7561 | AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
|
7562 | ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
|
7563 | ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
|
7564 | ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
|
7565 | ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
|
7566 | DeclareAccessor: ({
|
7567 | kind
|
7568 | }) => `'declare' is not allowed in ${kind}ters.`,
|
7569 | DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
|
7570 | DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
|
7571 | DuplicateAccessibilityModifier: ({
|
7572 | modifier
|
7573 | }) => `Accessibility modifier already seen.`,
|
7574 | DuplicateModifier: ({
|
7575 | modifier
|
7576 | }) => `Duplicate modifier: '${modifier}'.`,
|
7577 | EmptyHeritageClauseType: ({
|
7578 | token
|
7579 | }) => `'${token}' list cannot be empty.`,
|
7580 | EmptyTypeArguments: "Type argument list cannot be empty.",
|
7581 | EmptyTypeParameters: "Type parameter list cannot be empty.",
|
7582 | ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
|
7583 | ImportAliasHasImportType: "An import alias can not use 'import type'.",
|
7584 | ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
|
7585 | IncompatibleModifiers: ({
|
7586 | modifiers
|
7587 | }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
|
7588 | IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
|
7589 | IndexSignatureHasAccessibility: ({
|
7590 | modifier
|
7591 | }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
|
7592 | IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
|
7593 | IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
|
7594 | IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
|
7595 | InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
|
7596 | InvalidModifierOnTypeMember: ({
|
7597 | modifier
|
7598 | }) => `'${modifier}' modifier cannot appear on a type member.`,
|
7599 | InvalidModifierOnTypeParameter: ({
|
7600 | modifier
|
7601 | }) => `'${modifier}' modifier cannot appear on a type parameter.`,
|
7602 | InvalidModifierOnTypeParameterPositions: ({
|
7603 | modifier
|
7604 | }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
|
7605 | InvalidModifiersOrder: ({
|
7606 | orderedModifiers
|
7607 | }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
|
7608 | InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
|
7609 | InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
|
7610 | MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
|
7611 | MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
|
7612 | NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
|
7613 | NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
|
7614 | OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
|
7615 | OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
|
7616 | PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
|
7617 | PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
|
7618 | PrivateElementHasAccessibility: ({
|
7619 | modifier
|
7620 | }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
|
7621 | ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
|
7622 | ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
|
7623 | ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
|
7624 | SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
|
7625 | SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
|
7626 | SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
|
7627 | SingleTypeParameterWithoutTrailingComma: ({
|
7628 | typeParameterName
|
7629 | }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
|
7630 | StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
|
7631 | TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
|
7632 | TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
|
7633 | TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
|
7634 | TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
|
7635 | TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
|
7636 | UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
|
7637 | UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
|
7638 | UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
|
7639 | UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
|
7640 | UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
|
7641 | UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
|
7642 | UnsupportedSignatureParameterKind: ({
|
7643 | type
|
7644 | }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
|
7645 | });
|
7646 | function keywordTypeFromName(value) {
|
7647 | switch (value) {
|
7648 | case "any":
|
7649 | return "TSAnyKeyword";
|
7650 | case "boolean":
|
7651 | return "TSBooleanKeyword";
|
7652 | case "bigint":
|
7653 | return "TSBigIntKeyword";
|
7654 | case "never":
|
7655 | return "TSNeverKeyword";
|
7656 | case "number":
|
7657 | return "TSNumberKeyword";
|
7658 | case "object":
|
7659 | return "TSObjectKeyword";
|
7660 | case "string":
|
7661 | return "TSStringKeyword";
|
7662 | case "symbol":
|
7663 | return "TSSymbolKeyword";
|
7664 | case "undefined":
|
7665 | return "TSUndefinedKeyword";
|
7666 | case "unknown":
|
7667 | return "TSUnknownKeyword";
|
7668 | default:
|
7669 | return undefined;
|
7670 | }
|
7671 | }
|
7672 | function tsIsAccessModifier(modifier) {
|
7673 | return modifier === "private" || modifier === "public" || modifier === "protected";
|
7674 | }
|
7675 | function tsIsVarianceAnnotations(modifier) {
|
7676 | return modifier === "in" || modifier === "out";
|
7677 | }
|
7678 | var typescript = superClass => class TypeScriptParserMixin extends superClass {
|
7679 | constructor(...args) {
|
7680 | super(...args);
|
7681 | this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
|
7682 | allowedModifiers: ["in", "out"],
|
7683 | disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
|
7684 | errorTemplate: TSErrors.InvalidModifierOnTypeParameter
|
7685 | });
|
7686 | this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
|
7687 | allowedModifiers: ["const"],
|
7688 | disallowedModifiers: ["in", "out"],
|
7689 | errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
|
7690 | });
|
7691 | this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
|
7692 | allowedModifiers: ["in", "out", "const"],
|
7693 | disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
|
7694 | errorTemplate: TSErrors.InvalidModifierOnTypeParameter
|
7695 | });
|
7696 | }
|
7697 | getScopeHandler() {
|
7698 | return TypeScriptScopeHandler;
|
7699 | }
|
7700 | tsIsIdentifier() {
|
7701 | return tokenIsIdentifier(this.state.type);
|
7702 | }
|
7703 | tsTokenCanFollowModifier() {
|
7704 | return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
|
7705 | }
|
7706 | tsNextTokenCanFollowModifier() {
|
7707 | this.next();
|
7708 | return this.tsTokenCanFollowModifier();
|
7709 | }
|
7710 | tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
|
7711 | if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
|
7712 | return undefined;
|
7713 | }
|
7714 | const modifier = this.state.value;
|
7715 | if (allowedModifiers.indexOf(modifier) !== -1) {
|
7716 | if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
|
7717 | return undefined;
|
7718 | }
|
7719 | if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
|
7720 | return modifier;
|
7721 | }
|
7722 | }
|
7723 | return undefined;
|
7724 | }
|
7725 | tsParseModifiers({
|
7726 | allowedModifiers,
|
7727 | disallowedModifiers,
|
7728 | stopOnStartOfClassStaticBlock,
|
7729 | errorTemplate = TSErrors.InvalidModifierOnTypeMember
|
7730 | }, modified) {
|
7731 | const enforceOrder = (loc, modifier, before, after) => {
|
7732 | if (modifier === before && modified[after]) {
|
7733 | this.raise(TSErrors.InvalidModifiersOrder, {
|
7734 | at: loc,
|
7735 | orderedModifiers: [before, after]
|
7736 | });
|
7737 | }
|
7738 | };
|
7739 | const incompatible = (loc, modifier, mod1, mod2) => {
|
7740 | if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
|
7741 | this.raise(TSErrors.IncompatibleModifiers, {
|
7742 | at: loc,
|
7743 | modifiers: [mod1, mod2]
|
7744 | });
|
7745 | }
|
7746 | };
|
7747 | for (;;) {
|
7748 | const {
|
7749 | startLoc
|
7750 | } = this.state;
|
7751 | const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
|
7752 | if (!modifier) break;
|
7753 | if (tsIsAccessModifier(modifier)) {
|
7754 | if (modified.accessibility) {
|
7755 | this.raise(TSErrors.DuplicateAccessibilityModifier, {
|
7756 | at: startLoc,
|
7757 | modifier
|
7758 | });
|
7759 | } else {
|
7760 | enforceOrder(startLoc, modifier, modifier, "override");
|
7761 | enforceOrder(startLoc, modifier, modifier, "static");
|
7762 | enforceOrder(startLoc, modifier, modifier, "readonly");
|
7763 | modified.accessibility = modifier;
|
7764 | }
|
7765 | } else if (tsIsVarianceAnnotations(modifier)) {
|
7766 | if (modified[modifier]) {
|
7767 | this.raise(TSErrors.DuplicateModifier, {
|
7768 | at: startLoc,
|
7769 | modifier
|
7770 | });
|
7771 | }
|
7772 | modified[modifier] = true;
|
7773 | enforceOrder(startLoc, modifier, "in", "out");
|
7774 | } else {
|
7775 | if (Object.hasOwnProperty.call(modified, modifier)) {
|
7776 | this.raise(TSErrors.DuplicateModifier, {
|
7777 | at: startLoc,
|
7778 | modifier
|
7779 | });
|
7780 | } else {
|
7781 | enforceOrder(startLoc, modifier, "static", "readonly");
|
7782 | enforceOrder(startLoc, modifier, "static", "override");
|
7783 | enforceOrder(startLoc, modifier, "override", "readonly");
|
7784 | enforceOrder(startLoc, modifier, "abstract", "override");
|
7785 | incompatible(startLoc, modifier, "declare", "override");
|
7786 | incompatible(startLoc, modifier, "static", "abstract");
|
7787 | }
|
7788 | modified[modifier] = true;
|
7789 | }
|
7790 | if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
|
7791 | this.raise(errorTemplate, {
|
7792 | at: startLoc,
|
7793 | modifier
|
7794 | });
|
7795 | }
|
7796 | }
|
7797 | }
|
7798 | tsIsListTerminator(kind) {
|
7799 | switch (kind) {
|
7800 | case "EnumMembers":
|
7801 | case "TypeMembers":
|
7802 | return this.match(8);
|
7803 | case "HeritageClauseElement":
|
7804 | return this.match(5);
|
7805 | case "TupleElementTypes":
|
7806 | return this.match(3);
|
7807 | case "TypeParametersOrArguments":
|
7808 | return this.match(48);
|
7809 | }
|
7810 | }
|
7811 | tsParseList(kind, parseElement) {
|
7812 | const result = [];
|
7813 | while (!this.tsIsListTerminator(kind)) {
|
7814 | result.push(parseElement());
|
7815 | }
|
7816 | return result;
|
7817 | }
|
7818 | tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
|
7819 | return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
|
7820 | }
|
7821 | tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
|
7822 | const result = [];
|
7823 | let trailingCommaPos = -1;
|
7824 | for (;;) {
|
7825 | if (this.tsIsListTerminator(kind)) {
|
7826 | break;
|
7827 | }
|
7828 | trailingCommaPos = -1;
|
7829 | const element = parseElement();
|
7830 | if (element == null) {
|
7831 | return undefined;
|
7832 | }
|
7833 | result.push(element);
|
7834 | if (this.eat(12)) {
|
7835 | trailingCommaPos = this.state.lastTokStart;
|
7836 | continue;
|
7837 | }
|
7838 | if (this.tsIsListTerminator(kind)) {
|
7839 | break;
|
7840 | }
|
7841 | if (expectSuccess) {
|
7842 | this.expect(12);
|
7843 | }
|
7844 | return undefined;
|
7845 | }
|
7846 | if (refTrailingCommaPos) {
|
7847 | refTrailingCommaPos.value = trailingCommaPos;
|
7848 | }
|
7849 | return result;
|
7850 | }
|
7851 | tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
|
7852 | if (!skipFirstToken) {
|
7853 | if (bracket) {
|
7854 | this.expect(0);
|
7855 | } else {
|
7856 | this.expect(47);
|
7857 | }
|
7858 | }
|
7859 | const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
|
7860 | if (bracket) {
|
7861 | this.expect(3);
|
7862 | } else {
|
7863 | this.expect(48);
|
7864 | }
|
7865 | return result;
|
7866 | }
|
7867 | tsParseImportType() {
|
7868 | const node = this.startNode();
|
7869 | this.expect(83);
|
7870 | this.expect(10);
|
7871 | if (!this.match(131)) {
|
7872 | this.raise(TSErrors.UnsupportedImportTypeArgument, {
|
7873 | at: this.state.startLoc
|
7874 | });
|
7875 | }
|
7876 | node.argument = super.parseExprAtom();
|
7877 | this.expect(11);
|
7878 | if (this.eat(16)) {
|
7879 | node.qualifier = this.tsParseEntityName();
|
7880 | }
|
7881 | if (this.match(47)) {
|
7882 | node.typeParameters = this.tsParseTypeArguments();
|
7883 | }
|
7884 | return this.finishNode(node, "TSImportType");
|
7885 | }
|
7886 | tsParseEntityName(allowReservedWords = true) {
|
7887 | let entity = this.parseIdentifier(allowReservedWords);
|
7888 | while (this.eat(16)) {
|
7889 | const node = this.startNodeAtNode(entity);
|
7890 | node.left = entity;
|
7891 | node.right = this.parseIdentifier(allowReservedWords);
|
7892 | entity = this.finishNode(node, "TSQualifiedName");
|
7893 | }
|
7894 | return entity;
|
7895 | }
|
7896 | tsParseTypeReference() {
|
7897 | const node = this.startNode();
|
7898 | node.typeName = this.tsParseEntityName();
|
7899 | if (!this.hasPrecedingLineBreak() && this.match(47)) {
|
7900 | node.typeParameters = this.tsParseTypeArguments();
|
7901 | }
|
7902 | return this.finishNode(node, "TSTypeReference");
|
7903 | }
|
7904 | tsParseThisTypePredicate(lhs) {
|
7905 | this.next();
|
7906 | const node = this.startNodeAtNode(lhs);
|
7907 | node.parameterName = lhs;
|
7908 | node.typeAnnotation = this.tsParseTypeAnnotation(false);
|
7909 | node.asserts = false;
|
7910 | return this.finishNode(node, "TSTypePredicate");
|
7911 | }
|
7912 | tsParseThisTypeNode() {
|
7913 | const node = this.startNode();
|
7914 | this.next();
|
7915 | return this.finishNode(node, "TSThisType");
|
7916 | }
|
7917 | tsParseTypeQuery() {
|
7918 | const node = this.startNode();
|
7919 | this.expect(87);
|
7920 | if (this.match(83)) {
|
7921 | node.exprName = this.tsParseImportType();
|
7922 | } else {
|
7923 | node.exprName = this.tsParseEntityName();
|
7924 | }
|
7925 | if (!this.hasPrecedingLineBreak() && this.match(47)) {
|
7926 | node.typeParameters = this.tsParseTypeArguments();
|
7927 | }
|
7928 | return this.finishNode(node, "TSTypeQuery");
|
7929 | }
|
7930 | tsParseTypeParameter(parseModifiers) {
|
7931 | const node = this.startNode();
|
7932 | parseModifiers(node);
|
7933 | node.name = this.tsParseTypeParameterName();
|
7934 | node.constraint = this.tsEatThenParseType(81);
|
7935 | node.default = this.tsEatThenParseType(29);
|
7936 | return this.finishNode(node, "TSTypeParameter");
|
7937 | }
|
7938 | tsTryParseTypeParameters(parseModifiers) {
|
7939 | if (this.match(47)) {
|
7940 | return this.tsParseTypeParameters(parseModifiers);
|
7941 | }
|
7942 | }
|
7943 | tsParseTypeParameters(parseModifiers) {
|
7944 | const node = this.startNode();
|
7945 | if (this.match(47) || this.match(140)) {
|
7946 | this.next();
|
7947 | } else {
|
7948 | this.unexpected();
|
7949 | }
|
7950 | const refTrailingCommaPos = {
|
7951 | value: -1
|
7952 | };
|
7953 | node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
|
7954 | if (node.params.length === 0) {
|
7955 | this.raise(TSErrors.EmptyTypeParameters, {
|
7956 | at: node
|
7957 | });
|
7958 | }
|
7959 | if (refTrailingCommaPos.value !== -1) {
|
7960 | this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
|
7961 | }
|
7962 | return this.finishNode(node, "TSTypeParameterDeclaration");
|
7963 | }
|
7964 | tsFillSignature(returnToken, signature) {
|
7965 | const returnTokenRequired = returnToken === 19;
|
7966 | const paramsKey = "parameters";
|
7967 | const returnTypeKey = "typeAnnotation";
|
7968 | signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
|
7969 | this.expect(10);
|
7970 | signature[paramsKey] = this.tsParseBindingListForSignature();
|
7971 | if (returnTokenRequired) {
|
7972 | signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
7973 | } else if (this.match(returnToken)) {
|
7974 | signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
|
7975 | }
|
7976 | }
|
7977 | tsParseBindingListForSignature() {
|
7978 | const list = super.parseBindingList(11, 41, 2);
|
7979 | for (const pattern of list) {
|
7980 | const {
|
7981 | type
|
7982 | } = pattern;
|
7983 | if (type === "AssignmentPattern" || type === "TSParameterProperty") {
|
7984 | this.raise(TSErrors.UnsupportedSignatureParameterKind, {
|
7985 | at: pattern,
|
7986 | type
|
7987 | });
|
7988 | }
|
7989 | }
|
7990 | return list;
|
7991 | }
|
7992 | tsParseTypeMemberSemicolon() {
|
7993 | if (!this.eat(12) && !this.isLineTerminator()) {
|
7994 | this.expect(13);
|
7995 | }
|
7996 | }
|
7997 | tsParseSignatureMember(kind, node) {
|
7998 | this.tsFillSignature(14, node);
|
7999 | this.tsParseTypeMemberSemicolon();
|
8000 | return this.finishNode(node, kind);
|
8001 | }
|
8002 | tsIsUnambiguouslyIndexSignature() {
|
8003 | this.next();
|
8004 | if (tokenIsIdentifier(this.state.type)) {
|
8005 | this.next();
|
8006 | return this.match(14);
|
8007 | }
|
8008 | return false;
|
8009 | }
|
8010 | tsTryParseIndexSignature(node) {
|
8011 | if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
|
8012 | return;
|
8013 | }
|
8014 | this.expect(0);
|
8015 | const id = this.parseIdentifier();
|
8016 | id.typeAnnotation = this.tsParseTypeAnnotation();
|
8017 | this.resetEndLocation(id);
|
8018 | this.expect(3);
|
8019 | node.parameters = [id];
|
8020 | const type = this.tsTryParseTypeAnnotation();
|
8021 | if (type) node.typeAnnotation = type;
|
8022 | this.tsParseTypeMemberSemicolon();
|
8023 | return this.finishNode(node, "TSIndexSignature");
|
8024 | }
|
8025 | tsParsePropertyOrMethodSignature(node, readonly) {
|
8026 | if (this.eat(17)) node.optional = true;
|
8027 | const nodeAny = node;
|
8028 | if (this.match(10) || this.match(47)) {
|
8029 | if (readonly) {
|
8030 | this.raise(TSErrors.ReadonlyForMethodSignature, {
|
8031 | at: node
|
8032 | });
|
8033 | }
|
8034 | const method = nodeAny;
|
8035 | if (method.kind && this.match(47)) {
|
8036 | this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
|
8037 | at: this.state.curPosition()
|
8038 | });
|
8039 | }
|
8040 | this.tsFillSignature(14, method);
|
8041 | this.tsParseTypeMemberSemicolon();
|
8042 | const paramsKey = "parameters";
|
8043 | const returnTypeKey = "typeAnnotation";
|
8044 | if (method.kind === "get") {
|
8045 | if (method[paramsKey].length > 0) {
|
8046 | this.raise(Errors.BadGetterArity, {
|
8047 | at: this.state.curPosition()
|
8048 | });
|
8049 | if (this.isThisParam(method[paramsKey][0])) {
|
8050 | this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
|
8051 | at: this.state.curPosition()
|
8052 | });
|
8053 | }
|
8054 | }
|
8055 | } else if (method.kind === "set") {
|
8056 | if (method[paramsKey].length !== 1) {
|
8057 | this.raise(Errors.BadSetterArity, {
|
8058 | at: this.state.curPosition()
|
8059 | });
|
8060 | } else {
|
8061 | const firstParameter = method[paramsKey][0];
|
8062 | if (this.isThisParam(firstParameter)) {
|
8063 | this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
|
8064 | at: this.state.curPosition()
|
8065 | });
|
8066 | }
|
8067 | if (firstParameter.type === "Identifier" && firstParameter.optional) {
|
8068 | this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
|
8069 | at: this.state.curPosition()
|
8070 | });
|
8071 | }
|
8072 | if (firstParameter.type === "RestElement") {
|
8073 | this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
|
8074 | at: this.state.curPosition()
|
8075 | });
|
8076 | }
|
8077 | }
|
8078 | if (method[returnTypeKey]) {
|
8079 | this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
|
8080 | at: method[returnTypeKey]
|
8081 | });
|
8082 | }
|
8083 | } else {
|
8084 | method.kind = "method";
|
8085 | }
|
8086 | return this.finishNode(method, "TSMethodSignature");
|
8087 | } else {
|
8088 | const property = nodeAny;
|
8089 | if (readonly) property.readonly = true;
|
8090 | const type = this.tsTryParseTypeAnnotation();
|
8091 | if (type) property.typeAnnotation = type;
|
8092 | this.tsParseTypeMemberSemicolon();
|
8093 | return this.finishNode(property, "TSPropertySignature");
|
8094 | }
|
8095 | }
|
8096 | tsParseTypeMember() {
|
8097 | const node = this.startNode();
|
8098 | if (this.match(10) || this.match(47)) {
|
8099 | return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
|
8100 | }
|
8101 | if (this.match(77)) {
|
8102 | const id = this.startNode();
|
8103 | this.next();
|
8104 | if (this.match(10) || this.match(47)) {
|
8105 | return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
|
8106 | } else {
|
8107 | node.key = this.createIdentifier(id, "new");
|
8108 | return this.tsParsePropertyOrMethodSignature(node, false);
|
8109 | }
|
8110 | }
|
8111 | this.tsParseModifiers({
|
8112 | allowedModifiers: ["readonly"],
|
8113 | disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
|
8114 | }, node);
|
8115 | const idx = this.tsTryParseIndexSignature(node);
|
8116 | if (idx) {
|
8117 | return idx;
|
8118 | }
|
8119 | super.parsePropertyName(node);
|
8120 | if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
|
8121 | node.kind = node.key.name;
|
8122 | super.parsePropertyName(node);
|
8123 | }
|
8124 | return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
|
8125 | }
|
8126 | tsParseTypeLiteral() {
|
8127 | const node = this.startNode();
|
8128 | node.members = this.tsParseObjectTypeMembers();
|
8129 | return this.finishNode(node, "TSTypeLiteral");
|
8130 | }
|
8131 | tsParseObjectTypeMembers() {
|
8132 | this.expect(5);
|
8133 | const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
|
8134 | this.expect(8);
|
8135 | return members;
|
8136 | }
|
8137 | tsIsStartOfMappedType() {
|
8138 | this.next();
|
8139 | if (this.eat(53)) {
|
8140 | return this.isContextual(120);
|
8141 | }
|
8142 | if (this.isContextual(120)) {
|
8143 | this.next();
|
8144 | }
|
8145 | if (!this.match(0)) {
|
8146 | return false;
|
8147 | }
|
8148 | this.next();
|
8149 | if (!this.tsIsIdentifier()) {
|
8150 | return false;
|
8151 | }
|
8152 | this.next();
|
8153 | return this.match(58);
|
8154 | }
|
8155 | tsParseMappedTypeParameter() {
|
8156 | const node = this.startNode();
|
8157 | node.name = this.tsParseTypeParameterName();
|
8158 | node.constraint = this.tsExpectThenParseType(58);
|
8159 | return this.finishNode(node, "TSTypeParameter");
|
8160 | }
|
8161 | tsParseMappedType() {
|
8162 | const node = this.startNode();
|
8163 | this.expect(5);
|
8164 | if (this.match(53)) {
|
8165 | node.readonly = this.state.value;
|
8166 | this.next();
|
8167 | this.expectContextual(120);
|
8168 | } else if (this.eatContextual(120)) {
|
8169 | node.readonly = true;
|
8170 | }
|
8171 | this.expect(0);
|
8172 | node.typeParameter = this.tsParseMappedTypeParameter();
|
8173 | node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
|
8174 | this.expect(3);
|
8175 | if (this.match(53)) {
|
8176 | node.optional = this.state.value;
|
8177 | this.next();
|
8178 | this.expect(17);
|
8179 | } else if (this.eat(17)) {
|
8180 | node.optional = true;
|
8181 | }
|
8182 | node.typeAnnotation = this.tsTryParseType();
|
8183 | this.semicolon();
|
8184 | this.expect(8);
|
8185 | return this.finishNode(node, "TSMappedType");
|
8186 | }
|
8187 | tsParseTupleType() {
|
8188 | const node = this.startNode();
|
8189 | node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
|
8190 | let seenOptionalElement = false;
|
8191 | let labeledElements = null;
|
8192 | node.elementTypes.forEach(elementNode => {
|
8193 | var _labeledElements;
|
8194 | const {
|
8195 | type
|
8196 | } = elementNode;
|
8197 | if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
|
8198 | this.raise(TSErrors.OptionalTypeBeforeRequired, {
|
8199 | at: elementNode
|
8200 | });
|
8201 | }
|
8202 | seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
|
8203 | let checkType = type;
|
8204 | if (type === "TSRestType") {
|
8205 | elementNode = elementNode.typeAnnotation;
|
8206 | checkType = elementNode.type;
|
8207 | }
|
8208 | const isLabeled = checkType === "TSNamedTupleMember";
|
8209 | (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
|
8210 | if (labeledElements !== isLabeled) {
|
8211 | this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
|
8212 | at: elementNode
|
8213 | });
|
8214 | }
|
8215 | });
|
8216 | return this.finishNode(node, "TSTupleType");
|
8217 | }
|
8218 | tsParseTupleElementType() {
|
8219 | const {
|
8220 | startLoc
|
8221 | } = this.state;
|
8222 | const rest = this.eat(21);
|
8223 | let labeled;
|
8224 | let label;
|
8225 | let optional;
|
8226 | let type;
|
8227 | const isWord = tokenIsKeywordOrIdentifier(this.state.type);
|
8228 | const chAfterWord = isWord ? this.lookaheadCharCode() : null;
|
8229 | if (chAfterWord === 58) {
|
8230 | labeled = true;
|
8231 | optional = false;
|
8232 | label = this.parseIdentifier(true);
|
8233 | this.expect(14);
|
8234 | type = this.tsParseType();
|
8235 | } else if (chAfterWord === 63) {
|
8236 | optional = true;
|
8237 | const startLoc = this.state.startLoc;
|
8238 | const wordName = this.state.value;
|
8239 | const typeOrLabel = this.tsParseNonArrayType();
|
8240 | if (this.lookaheadCharCode() === 58) {
|
8241 | labeled = true;
|
8242 | label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
|
8243 | this.expect(17);
|
8244 | this.expect(14);
|
8245 | type = this.tsParseType();
|
8246 | } else {
|
8247 | labeled = false;
|
8248 | type = typeOrLabel;
|
8249 | this.expect(17);
|
8250 | }
|
8251 | } else {
|
8252 | type = this.tsParseType();
|
8253 | optional = this.eat(17);
|
8254 | labeled = this.eat(14);
|
8255 | }
|
8256 | if (labeled) {
|
8257 | let labeledNode;
|
8258 | if (label) {
|
8259 | labeledNode = this.startNodeAtNode(label);
|
8260 | labeledNode.optional = optional;
|
8261 | labeledNode.label = label;
|
8262 | labeledNode.elementType = type;
|
8263 | if (this.eat(17)) {
|
8264 | labeledNode.optional = true;
|
8265 | this.raise(TSErrors.TupleOptionalAfterType, {
|
8266 | at: this.state.lastTokStartLoc
|
8267 | });
|
8268 | }
|
8269 | } else {
|
8270 | labeledNode = this.startNodeAtNode(type);
|
8271 | labeledNode.optional = optional;
|
8272 | this.raise(TSErrors.InvalidTupleMemberLabel, {
|
8273 | at: type
|
8274 | });
|
8275 | labeledNode.label = type;
|
8276 | labeledNode.elementType = this.tsParseType();
|
8277 | }
|
8278 | type = this.finishNode(labeledNode, "TSNamedTupleMember");
|
8279 | } else if (optional) {
|
8280 | const optionalTypeNode = this.startNodeAtNode(type);
|
8281 | optionalTypeNode.typeAnnotation = type;
|
8282 | type = this.finishNode(optionalTypeNode, "TSOptionalType");
|
8283 | }
|
8284 | if (rest) {
|
8285 | const restNode = this.startNodeAt(startLoc);
|
8286 | restNode.typeAnnotation = type;
|
8287 | type = this.finishNode(restNode, "TSRestType");
|
8288 | }
|
8289 | return type;
|
8290 | }
|
8291 | tsParseParenthesizedType() {
|
8292 | const node = this.startNode();
|
8293 | this.expect(10);
|
8294 | node.typeAnnotation = this.tsParseType();
|
8295 | this.expect(11);
|
8296 | return this.finishNode(node, "TSParenthesizedType");
|
8297 | }
|
8298 | tsParseFunctionOrConstructorType(type, abstract) {
|
8299 | const node = this.startNode();
|
8300 | if (type === "TSConstructorType") {
|
8301 | node.abstract = !!abstract;
|
8302 | if (abstract) this.next();
|
8303 | this.next();
|
8304 | }
|
8305 | this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
|
8306 | return this.finishNode(node, type);
|
8307 | }
|
8308 | tsParseLiteralTypeNode() {
|
8309 | const node = this.startNode();
|
8310 | switch (this.state.type) {
|
8311 | case 132:
|
8312 | case 133:
|
8313 | case 131:
|
8314 | case 85:
|
8315 | case 86:
|
8316 | node.literal = super.parseExprAtom();
|
8317 | break;
|
8318 | default:
|
8319 | this.unexpected();
|
8320 | }
|
8321 | return this.finishNode(node, "TSLiteralType");
|
8322 | }
|
8323 | tsParseTemplateLiteralType() {
|
8324 | const node = this.startNode();
|
8325 | node.literal = super.parseTemplate(false);
|
8326 | return this.finishNode(node, "TSLiteralType");
|
8327 | }
|
8328 | parseTemplateSubstitution() {
|
8329 | if (this.state.inType) return this.tsParseType();
|
8330 | return super.parseTemplateSubstitution();
|
8331 | }
|
8332 | tsParseThisTypeOrThisTypePredicate() {
|
8333 | const thisKeyword = this.tsParseThisTypeNode();
|
8334 | if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
|
8335 | return this.tsParseThisTypePredicate(thisKeyword);
|
8336 | } else {
|
8337 | return thisKeyword;
|
8338 | }
|
8339 | }
|
8340 | tsParseNonArrayType() {
|
8341 | switch (this.state.type) {
|
8342 | case 131:
|
8343 | case 132:
|
8344 | case 133:
|
8345 | case 85:
|
8346 | case 86:
|
8347 | return this.tsParseLiteralTypeNode();
|
8348 | case 53:
|
8349 | if (this.state.value === "-") {
|
8350 | const node = this.startNode();
|
8351 | const nextToken = this.lookahead();
|
8352 | if (nextToken.type !== 132 && nextToken.type !== 133) {
|
8353 | this.unexpected();
|
8354 | }
|
8355 | node.literal = this.parseMaybeUnary();
|
8356 | return this.finishNode(node, "TSLiteralType");
|
8357 | }
|
8358 | break;
|
8359 | case 78:
|
8360 | return this.tsParseThisTypeOrThisTypePredicate();
|
8361 | case 87:
|
8362 | return this.tsParseTypeQuery();
|
8363 | case 83:
|
8364 | return this.tsParseImportType();
|
8365 | case 5:
|
8366 | return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
|
8367 | case 0:
|
8368 | return this.tsParseTupleType();
|
8369 | case 10:
|
8370 | return this.tsParseParenthesizedType();
|
8371 | case 25:
|
8372 | case 24:
|
8373 | return this.tsParseTemplateLiteralType();
|
8374 | default:
|
8375 | {
|
8376 | const {
|
8377 | type
|
8378 | } = this.state;
|
8379 | if (tokenIsIdentifier(type) || type === 88 || type === 84) {
|
8380 | const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
|
8381 | if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
|
8382 | const node = this.startNode();
|
8383 | this.next();
|
8384 | return this.finishNode(node, nodeType);
|
8385 | }
|
8386 | return this.tsParseTypeReference();
|
8387 | }
|
8388 | }
|
8389 | }
|
8390 | this.unexpected();
|
8391 | }
|
8392 | tsParseArrayTypeOrHigher() {
|
8393 | let type = this.tsParseNonArrayType();
|
8394 | while (!this.hasPrecedingLineBreak() && this.eat(0)) {
|
8395 | if (this.match(3)) {
|
8396 | const node = this.startNodeAtNode(type);
|
8397 | node.elementType = type;
|
8398 | this.expect(3);
|
8399 | type = this.finishNode(node, "TSArrayType");
|
8400 | } else {
|
8401 | const node = this.startNodeAtNode(type);
|
8402 | node.objectType = type;
|
8403 | node.indexType = this.tsParseType();
|
8404 | this.expect(3);
|
8405 | type = this.finishNode(node, "TSIndexedAccessType");
|
8406 | }
|
8407 | }
|
8408 | return type;
|
8409 | }
|
8410 | tsParseTypeOperator() {
|
8411 | const node = this.startNode();
|
8412 | const operator = this.state.value;
|
8413 | this.next();
|
8414 | node.operator = operator;
|
8415 | node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
|
8416 | if (operator === "readonly") {
|
8417 | this.tsCheckTypeAnnotationForReadOnly(node);
|
8418 | }
|
8419 | return this.finishNode(node, "TSTypeOperator");
|
8420 | }
|
8421 | tsCheckTypeAnnotationForReadOnly(node) {
|
8422 | switch (node.typeAnnotation.type) {
|
8423 | case "TSTupleType":
|
8424 | case "TSArrayType":
|
8425 | return;
|
8426 | default:
|
8427 | this.raise(TSErrors.UnexpectedReadonly, {
|
8428 | at: node
|
8429 | });
|
8430 | }
|
8431 | }
|
8432 | tsParseInferType() {
|
8433 | const node = this.startNode();
|
8434 | this.expectContextual(113);
|
8435 | const typeParameter = this.startNode();
|
8436 | typeParameter.name = this.tsParseTypeParameterName();
|
8437 | typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
|
8438 | node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
|
8439 | return this.finishNode(node, "TSInferType");
|
8440 | }
|
8441 | tsParseConstraintForInferType() {
|
8442 | if (this.eat(81)) {
|
8443 | const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
|
8444 | if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
|
8445 | return constraint;
|
8446 | }
|
8447 | }
|
8448 | }
|
8449 | tsParseTypeOperatorOrHigher() {
|
8450 | const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
|
8451 | return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
|
8452 | }
|
8453 | tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
|
8454 | const node = this.startNode();
|
8455 | const hasLeadingOperator = this.eat(operator);
|
8456 | const types = [];
|
8457 | do {
|
8458 | types.push(parseConstituentType());
|
8459 | } while (this.eat(operator));
|
8460 | if (types.length === 1 && !hasLeadingOperator) {
|
8461 | return types[0];
|
8462 | }
|
8463 | node.types = types;
|
8464 | return this.finishNode(node, kind);
|
8465 | }
|
8466 | tsParseIntersectionTypeOrHigher() {
|
8467 | return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
|
8468 | }
|
8469 | tsParseUnionTypeOrHigher() {
|
8470 | return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
|
8471 | }
|
8472 | tsIsStartOfFunctionType() {
|
8473 | if (this.match(47)) {
|
8474 | return true;
|
8475 | }
|
8476 | return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
|
8477 | }
|
8478 | tsSkipParameterStart() {
|
8479 | if (tokenIsIdentifier(this.state.type) || this.match(78)) {
|
8480 | this.next();
|
8481 | return true;
|
8482 | }
|
8483 | if (this.match(5)) {
|
8484 | const {
|
8485 | errors
|
8486 | } = this.state;
|
8487 | const previousErrorCount = errors.length;
|
8488 | try {
|
8489 | this.parseObjectLike(8, true);
|
8490 | return errors.length === previousErrorCount;
|
8491 | } catch (_unused) {
|
8492 | return false;
|
8493 | }
|
8494 | }
|
8495 | if (this.match(0)) {
|
8496 | this.next();
|
8497 | const {
|
8498 | errors
|
8499 | } = this.state;
|
8500 | const previousErrorCount = errors.length;
|
8501 | try {
|
8502 | super.parseBindingList(3, 93, 1);
|
8503 | return errors.length === previousErrorCount;
|
8504 | } catch (_unused2) {
|
8505 | return false;
|
8506 | }
|
8507 | }
|
8508 | return false;
|
8509 | }
|
8510 | tsIsUnambiguouslyStartOfFunctionType() {
|
8511 | this.next();
|
8512 | if (this.match(11) || this.match(21)) {
|
8513 | return true;
|
8514 | }
|
8515 | if (this.tsSkipParameterStart()) {
|
8516 | if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
|
8517 | return true;
|
8518 | }
|
8519 | if (this.match(11)) {
|
8520 | this.next();
|
8521 | if (this.match(19)) {
|
8522 | return true;
|
8523 | }
|
8524 | }
|
8525 | }
|
8526 | return false;
|
8527 | }
|
8528 | tsParseTypeOrTypePredicateAnnotation(returnToken) {
|
8529 | return this.tsInType(() => {
|
8530 | const t = this.startNode();
|
8531 | this.expect(returnToken);
|
8532 | const node = this.startNode();
|
8533 | const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
|
8534 | if (asserts && this.match(78)) {
|
8535 | let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
|
8536 | if (thisTypePredicate.type === "TSThisType") {
|
8537 | node.parameterName = thisTypePredicate;
|
8538 | node.asserts = true;
|
8539 | node.typeAnnotation = null;
|
8540 | thisTypePredicate = this.finishNode(node, "TSTypePredicate");
|
8541 | } else {
|
8542 | this.resetStartLocationFromNode(thisTypePredicate, node);
|
8543 | thisTypePredicate.asserts = true;
|
8544 | }
|
8545 | t.typeAnnotation = thisTypePredicate;
|
8546 | return this.finishNode(t, "TSTypeAnnotation");
|
8547 | }
|
8548 | const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
|
8549 | if (!typePredicateVariable) {
|
8550 | if (!asserts) {
|
8551 | return this.tsParseTypeAnnotation(false, t);
|
8552 | }
|
8553 | node.parameterName = this.parseIdentifier();
|
8554 | node.asserts = asserts;
|
8555 | node.typeAnnotation = null;
|
8556 | t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
|
8557 | return this.finishNode(t, "TSTypeAnnotation");
|
8558 | }
|
8559 | const type = this.tsParseTypeAnnotation(false);
|
8560 | node.parameterName = typePredicateVariable;
|
8561 | node.typeAnnotation = type;
|
8562 | node.asserts = asserts;
|
8563 | t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
|
8564 | return this.finishNode(t, "TSTypeAnnotation");
|
8565 | });
|
8566 | }
|
8567 | tsTryParseTypeOrTypePredicateAnnotation() {
|
8568 | if (this.match(14)) {
|
8569 | return this.tsParseTypeOrTypePredicateAnnotation(14);
|
8570 | }
|
8571 | }
|
8572 | tsTryParseTypeAnnotation() {
|
8573 | if (this.match(14)) {
|
8574 | return this.tsParseTypeAnnotation();
|
8575 | }
|
8576 | }
|
8577 | tsTryParseType() {
|
8578 | return this.tsEatThenParseType(14);
|
8579 | }
|
8580 | tsParseTypePredicatePrefix() {
|
8581 | const id = this.parseIdentifier();
|
8582 | if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
|
8583 | this.next();
|
8584 | return id;
|
8585 | }
|
8586 | }
|
8587 | tsParseTypePredicateAsserts() {
|
8588 | if (this.state.type !== 107) {
|
8589 | return false;
|
8590 | }
|
8591 | const containsEsc = this.state.containsEsc;
|
8592 | this.next();
|
8593 | if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
|
8594 | return false;
|
8595 | }
|
8596 | if (containsEsc) {
|
8597 | this.raise(Errors.InvalidEscapedReservedWord, {
|
8598 | at: this.state.lastTokStartLoc,
|
8599 | reservedWord: "asserts"
|
8600 | });
|
8601 | }
|
8602 | return true;
|
8603 | }
|
8604 | tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
|
8605 | this.tsInType(() => {
|
8606 | if (eatColon) this.expect(14);
|
8607 | t.typeAnnotation = this.tsParseType();
|
8608 | });
|
8609 | return this.finishNode(t, "TSTypeAnnotation");
|
8610 | }
|
8611 | tsParseType() {
|
8612 | assert(this.state.inType);
|
8613 | const type = this.tsParseNonConditionalType();
|
8614 | if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
|
8615 | return type;
|
8616 | }
|
8617 | const node = this.startNodeAtNode(type);
|
8618 | node.checkType = type;
|
8619 | node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
|
8620 | this.expect(17);
|
8621 | node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
|
8622 | this.expect(14);
|
8623 | node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
|
8624 | return this.finishNode(node, "TSConditionalType");
|
8625 | }
|
8626 | isAbstractConstructorSignature() {
|
8627 | return this.isContextual(122) && this.lookahead().type === 77;
|
8628 | }
|
8629 | tsParseNonConditionalType() {
|
8630 | if (this.tsIsStartOfFunctionType()) {
|
8631 | return this.tsParseFunctionOrConstructorType("TSFunctionType");
|
8632 | }
|
8633 | if (this.match(77)) {
|
8634 | return this.tsParseFunctionOrConstructorType("TSConstructorType");
|
8635 | } else if (this.isAbstractConstructorSignature()) {
|
8636 | return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
|
8637 | }
|
8638 | return this.tsParseUnionTypeOrHigher();
|
8639 | }
|
8640 | tsParseTypeAssertion() {
|
8641 | if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
|
8642 | this.raise(TSErrors.ReservedTypeAssertion, {
|
8643 | at: this.state.startLoc
|
8644 | });
|
8645 | }
|
8646 | const node = this.startNode();
|
8647 | node.typeAnnotation = this.tsInType(() => {
|
8648 | this.next();
|
8649 | return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
|
8650 | });
|
8651 | this.expect(48);
|
8652 | node.expression = this.parseMaybeUnary();
|
8653 | return this.finishNode(node, "TSTypeAssertion");
|
8654 | }
|
8655 | tsParseHeritageClause(token) {
|
8656 | const originalStartLoc = this.state.startLoc;
|
8657 | const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
|
8658 | const node = this.startNode();
|
8659 | node.expression = this.tsParseEntityName();
|
8660 | if (this.match(47)) {
|
8661 | node.typeParameters = this.tsParseTypeArguments();
|
8662 | }
|
8663 | return this.finishNode(node, "TSExpressionWithTypeArguments");
|
8664 | });
|
8665 | if (!delimitedList.length) {
|
8666 | this.raise(TSErrors.EmptyHeritageClauseType, {
|
8667 | at: originalStartLoc,
|
8668 | token
|
8669 | });
|
8670 | }
|
8671 | return delimitedList;
|
8672 | }
|
8673 | tsParseInterfaceDeclaration(node, properties = {}) {
|
8674 | if (this.hasFollowingLineBreak()) return null;
|
8675 | this.expectContextual(127);
|
8676 | if (properties.declare) node.declare = true;
|
8677 | if (tokenIsIdentifier(this.state.type)) {
|
8678 | node.id = this.parseIdentifier();
|
8679 | this.checkIdentifier(node.id, BIND_TS_INTERFACE);
|
8680 | } else {
|
8681 | node.id = null;
|
8682 | this.raise(TSErrors.MissingInterfaceName, {
|
8683 | at: this.state.startLoc
|
8684 | });
|
8685 | }
|
8686 | node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
|
8687 | if (this.eat(81)) {
|
8688 | node.extends = this.tsParseHeritageClause("extends");
|
8689 | }
|
8690 | const body = this.startNode();
|
8691 | body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
|
8692 | node.body = this.finishNode(body, "TSInterfaceBody");
|
8693 | return this.finishNode(node, "TSInterfaceDeclaration");
|
8694 | }
|
8695 | tsParseTypeAliasDeclaration(node) {
|
8696 | node.id = this.parseIdentifier();
|
8697 | this.checkIdentifier(node.id, BIND_TS_TYPE);
|
8698 | node.typeAnnotation = this.tsInType(() => {
|
8699 | node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
|
8700 | this.expect(29);
|
8701 | if (this.isContextual(112) && this.lookahead().type !== 16) {
|
8702 | const node = this.startNode();
|
8703 | this.next();
|
8704 | return this.finishNode(node, "TSIntrinsicKeyword");
|
8705 | }
|
8706 | return this.tsParseType();
|
8707 | });
|
8708 | this.semicolon();
|
8709 | return this.finishNode(node, "TSTypeAliasDeclaration");
|
8710 | }
|
8711 | tsInNoContext(cb) {
|
8712 | const oldContext = this.state.context;
|
8713 | this.state.context = [oldContext[0]];
|
8714 | try {
|
8715 | return cb();
|
8716 | } finally {
|
8717 | this.state.context = oldContext;
|
8718 | }
|
8719 | }
|
8720 | tsInType(cb) {
|
8721 | const oldInType = this.state.inType;
|
8722 | this.state.inType = true;
|
8723 | try {
|
8724 | return cb();
|
8725 | } finally {
|
8726 | this.state.inType = oldInType;
|
8727 | }
|
8728 | }
|
8729 | tsInDisallowConditionalTypesContext(cb) {
|
8730 | const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
|
8731 | this.state.inDisallowConditionalTypesContext = true;
|
8732 | try {
|
8733 | return cb();
|
8734 | } finally {
|
8735 | this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
|
8736 | }
|
8737 | }
|
8738 | tsInAllowConditionalTypesContext(cb) {
|
8739 | const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
|
8740 | this.state.inDisallowConditionalTypesContext = false;
|
8741 | try {
|
8742 | return cb();
|
8743 | } finally {
|
8744 | this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
|
8745 | }
|
8746 | }
|
8747 | tsEatThenParseType(token) {
|
8748 | if (this.match(token)) {
|
8749 | return this.tsNextThenParseType();
|
8750 | }
|
8751 | }
|
8752 | tsExpectThenParseType(token) {
|
8753 | return this.tsInType(() => {
|
8754 | this.expect(token);
|
8755 | return this.tsParseType();
|
8756 | });
|
8757 | }
|
8758 | tsNextThenParseType() {
|
8759 | return this.tsInType(() => {
|
8760 | this.next();
|
8761 | return this.tsParseType();
|
8762 | });
|
8763 | }
|
8764 | tsParseEnumMember() {
|
8765 | const node = this.startNode();
|
8766 | node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
|
8767 | if (this.eat(29)) {
|
8768 | node.initializer = super.parseMaybeAssignAllowIn();
|
8769 | }
|
8770 | return this.finishNode(node, "TSEnumMember");
|
8771 | }
|
8772 | tsParseEnumDeclaration(node, properties = {}) {
|
8773 | if (properties.const) node.const = true;
|
8774 | if (properties.declare) node.declare = true;
|
8775 | this.expectContextual(124);
|
8776 | node.id = this.parseIdentifier();
|
8777 | this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
|
8778 | this.expect(5);
|
8779 | node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
|
8780 | this.expect(8);
|
8781 | return this.finishNode(node, "TSEnumDeclaration");
|
8782 | }
|
8783 | tsParseModuleBlock() {
|
8784 | const node = this.startNode();
|
8785 | this.scope.enter(SCOPE_OTHER);
|
8786 | this.expect(5);
|
8787 | super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
|
8788 | this.scope.exit();
|
8789 | return this.finishNode(node, "TSModuleBlock");
|
8790 | }
|
8791 | tsParseModuleOrNamespaceDeclaration(node, nested = false) {
|
8792 | node.id = this.parseIdentifier();
|
8793 | if (!nested) {
|
8794 | this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
|
8795 | }
|
8796 | if (this.eat(16)) {
|
8797 | const inner = this.startNode();
|
8798 | this.tsParseModuleOrNamespaceDeclaration(inner, true);
|
8799 | node.body = inner;
|
8800 | } else {
|
8801 | this.scope.enter(SCOPE_TS_MODULE);
|
8802 | this.prodParam.enter(PARAM);
|
8803 | node.body = this.tsParseModuleBlock();
|
8804 | this.prodParam.exit();
|
8805 | this.scope.exit();
|
8806 | }
|
8807 | return this.finishNode(node, "TSModuleDeclaration");
|
8808 | }
|
8809 | tsParseAmbientExternalModuleDeclaration(node) {
|
8810 | if (this.isContextual(110)) {
|
8811 | node.global = true;
|
8812 | node.id = this.parseIdentifier();
|
8813 | } else if (this.match(131)) {
|
8814 | node.id = super.parseStringLiteral(this.state.value);
|
8815 | } else {
|
8816 | this.unexpected();
|
8817 | }
|
8818 | if (this.match(5)) {
|
8819 | this.scope.enter(SCOPE_TS_MODULE);
|
8820 | this.prodParam.enter(PARAM);
|
8821 | node.body = this.tsParseModuleBlock();
|
8822 | this.prodParam.exit();
|
8823 | this.scope.exit();
|
8824 | } else {
|
8825 | this.semicolon();
|
8826 | }
|
8827 | return this.finishNode(node, "TSModuleDeclaration");
|
8828 | }
|
8829 | tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
|
8830 | node.isExport = isExport || false;
|
8831 | node.id = maybeDefaultIdentifier || this.parseIdentifier();
|
8832 | this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
|
8833 | this.expect(29);
|
8834 | const moduleReference = this.tsParseModuleReference();
|
8835 | if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
|
8836 | this.raise(TSErrors.ImportAliasHasImportType, {
|
8837 | at: moduleReference
|
8838 | });
|
8839 | }
|
8840 | node.moduleReference = moduleReference;
|
8841 | this.semicolon();
|
8842 | return this.finishNode(node, "TSImportEqualsDeclaration");
|
8843 | }
|
8844 | tsIsExternalModuleReference() {
|
8845 | return this.isContextual(117) && this.lookaheadCharCode() === 40;
|
8846 | }
|
8847 | tsParseModuleReference() {
|
8848 | return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
|
8849 | }
|
8850 | tsParseExternalModuleReference() {
|
8851 | const node = this.startNode();
|
8852 | this.expectContextual(117);
|
8853 | this.expect(10);
|
8854 | if (!this.match(131)) {
|
8855 | this.unexpected();
|
8856 | }
|
8857 | node.expression = super.parseExprAtom();
|
8858 | this.expect(11);
|
8859 | this.sawUnambiguousESM = true;
|
8860 | return this.finishNode(node, "TSExternalModuleReference");
|
8861 | }
|
8862 | tsLookAhead(f) {
|
8863 | const state = this.state.clone();
|
8864 | const res = f();
|
8865 | this.state = state;
|
8866 | return res;
|
8867 | }
|
8868 | tsTryParseAndCatch(f) {
|
8869 | const result = this.tryParse(abort => f() || abort());
|
8870 | if (result.aborted || !result.node) return;
|
8871 | if (result.error) this.state = result.failState;
|
8872 | return result.node;
|
8873 | }
|
8874 | tsTryParse(f) {
|
8875 | const state = this.state.clone();
|
8876 | const result = f();
|
8877 | if (result !== undefined && result !== false) {
|
8878 | return result;
|
8879 | }
|
8880 | this.state = state;
|
8881 | }
|
8882 | tsTryParseDeclare(nany) {
|
8883 | if (this.isLineTerminator()) {
|
8884 | return;
|
8885 | }
|
8886 | let startType = this.state.type;
|
8887 | let kind;
|
8888 | if (this.isContextual(99)) {
|
8889 | startType = 74;
|
8890 | kind = "let";
|
8891 | }
|
8892 | return this.tsInAmbientContext(() => {
|
8893 | switch (startType) {
|
8894 | case 68:
|
8895 | nany.declare = true;
|
8896 | return super.parseFunctionStatement(nany, false, false);
|
8897 | case 80:
|
8898 | nany.declare = true;
|
8899 | return this.parseClass(nany, true, false);
|
8900 | case 124:
|
8901 | return this.tsParseEnumDeclaration(nany, {
|
8902 | declare: true
|
8903 | });
|
8904 | case 110:
|
8905 | return this.tsParseAmbientExternalModuleDeclaration(nany);
|
8906 | case 75:
|
8907 | case 74:
|
8908 | if (!this.match(75) || !this.isLookaheadContextual("enum")) {
|
8909 | nany.declare = true;
|
8910 | return this.parseVarStatement(nany, kind || this.state.value, true);
|
8911 | }
|
8912 | this.expect(75);
|
8913 | return this.tsParseEnumDeclaration(nany, {
|
8914 | const: true,
|
8915 | declare: true
|
8916 | });
|
8917 | case 127:
|
8918 | {
|
8919 | const result = this.tsParseInterfaceDeclaration(nany, {
|
8920 | declare: true
|
8921 | });
|
8922 | if (result) return result;
|
8923 | }
|
8924 | default:
|
8925 | if (tokenIsIdentifier(startType)) {
|
8926 | return this.tsParseDeclaration(nany, this.state.value, true, null);
|
8927 | }
|
8928 | }
|
8929 | });
|
8930 | }
|
8931 | tsTryParseExportDeclaration() {
|
8932 | return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
|
8933 | }
|
8934 | tsParseExpressionStatement(node, expr, decorators) {
|
8935 | switch (expr.name) {
|
8936 | case "declare":
|
8937 | {
|
8938 | const declaration = this.tsTryParseDeclare(node);
|
8939 | if (declaration) {
|
8940 | declaration.declare = true;
|
8941 | }
|
8942 | return declaration;
|
8943 | }
|
8944 | case "global":
|
8945 | if (this.match(5)) {
|
8946 | this.scope.enter(SCOPE_TS_MODULE);
|
8947 | this.prodParam.enter(PARAM);
|
8948 | const mod = node;
|
8949 | mod.global = true;
|
8950 | mod.id = expr;
|
8951 | mod.body = this.tsParseModuleBlock();
|
8952 | this.scope.exit();
|
8953 | this.prodParam.exit();
|
8954 | return this.finishNode(mod, "TSModuleDeclaration");
|
8955 | }
|
8956 | break;
|
8957 | default:
|
8958 | return this.tsParseDeclaration(node, expr.name, false, decorators);
|
8959 | }
|
8960 | }
|
8961 | tsParseDeclaration(node, value, next, decorators) {
|
8962 | switch (value) {
|
8963 | case "abstract":
|
8964 | if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
|
8965 | return this.tsParseAbstractDeclaration(node, decorators);
|
8966 | }
|
8967 | break;
|
8968 | case "module":
|
8969 | if (this.tsCheckLineTerminator(next)) {
|
8970 | if (this.match(131)) {
|
8971 | return this.tsParseAmbientExternalModuleDeclaration(node);
|
8972 | } else if (tokenIsIdentifier(this.state.type)) {
|
8973 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
8974 | }
|
8975 | }
|
8976 | break;
|
8977 | case "namespace":
|
8978 | if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
|
8979 | return this.tsParseModuleOrNamespaceDeclaration(node);
|
8980 | }
|
8981 | break;
|
8982 | case "type":
|
8983 | if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
|
8984 | return this.tsParseTypeAliasDeclaration(node);
|
8985 | }
|
8986 | break;
|
8987 | }
|
8988 | }
|
8989 | tsCheckLineTerminator(next) {
|
8990 | if (next) {
|
8991 | if (this.hasFollowingLineBreak()) return false;
|
8992 | this.next();
|
8993 | return true;
|
8994 | }
|
8995 | return !this.isLineTerminator();
|
8996 | }
|
8997 | tsTryParseGenericAsyncArrowFunction(startLoc) {
|
8998 | if (!this.match(47)) return;
|
8999 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
9000 | this.state.maybeInArrowParameters = true;
|
9001 | const res = this.tsTryParseAndCatch(() => {
|
9002 | const node = this.startNodeAt(startLoc);
|
9003 | node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
|
9004 | super.parseFunctionParams(node);
|
9005 | node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
|
9006 | this.expect(19);
|
9007 | return node;
|
9008 | });
|
9009 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
9010 | if (!res) return;
|
9011 | return super.parseArrowExpression(res, null, true);
|
9012 | }
|
9013 | tsParseTypeArgumentsInExpression() {
|
9014 | if (this.reScan_lt() !== 47) return;
|
9015 | return this.tsParseTypeArguments();
|
9016 | }
|
9017 | tsParseTypeArguments() {
|
9018 | const node = this.startNode();
|
9019 | node.params = this.tsInType(() => this.tsInNoContext(() => {
|
9020 | this.expect(47);
|
9021 | return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
|
9022 | }));
|
9023 | if (node.params.length === 0) {
|
9024 | this.raise(TSErrors.EmptyTypeArguments, {
|
9025 | at: node
|
9026 | });
|
9027 | } else if (!this.state.inType && this.curContext() === types.brace) {
|
9028 | this.reScan_lt_gt();
|
9029 | }
|
9030 | this.expect(48);
|
9031 | return this.finishNode(node, "TSTypeParameterInstantiation");
|
9032 | }
|
9033 | tsIsDeclarationStart() {
|
9034 | return tokenIsTSDeclarationStart(this.state.type);
|
9035 | }
|
9036 | isExportDefaultSpecifier() {
|
9037 | if (this.tsIsDeclarationStart()) return false;
|
9038 | return super.isExportDefaultSpecifier();
|
9039 | }
|
9040 | parseAssignableListItem(flags, decorators) {
|
9041 | const startLoc = this.state.startLoc;
|
9042 | const modified = {};
|
9043 | this.tsParseModifiers({
|
9044 | allowedModifiers: ["public", "private", "protected", "override", "readonly"]
|
9045 | }, modified);
|
9046 | const accessibility = modified.accessibility;
|
9047 | const override = modified.override;
|
9048 | const readonly = modified.readonly;
|
9049 | if (!(flags & 4) && (accessibility || readonly || override)) {
|
9050 | this.raise(TSErrors.UnexpectedParameterModifier, {
|
9051 | at: startLoc
|
9052 | });
|
9053 | }
|
9054 | const left = this.parseMaybeDefault();
|
9055 | this.parseAssignableListItemTypes(left, flags);
|
9056 | const elt = this.parseMaybeDefault(left.loc.start, left);
|
9057 | if (accessibility || readonly || override) {
|
9058 | const pp = this.startNodeAt(startLoc);
|
9059 | if (decorators.length) {
|
9060 | pp.decorators = decorators;
|
9061 | }
|
9062 | if (accessibility) pp.accessibility = accessibility;
|
9063 | if (readonly) pp.readonly = readonly;
|
9064 | if (override) pp.override = override;
|
9065 | if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
|
9066 | this.raise(TSErrors.UnsupportedParameterPropertyKind, {
|
9067 | at: pp
|
9068 | });
|
9069 | }
|
9070 | pp.parameter = elt;
|
9071 | return this.finishNode(pp, "TSParameterProperty");
|
9072 | }
|
9073 | if (decorators.length) {
|
9074 | left.decorators = decorators;
|
9075 | }
|
9076 | return elt;
|
9077 | }
|
9078 | isSimpleParameter(node) {
|
9079 | return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
|
9080 | }
|
9081 | tsDisallowOptionalPattern(node) {
|
9082 | for (const param of node.params) {
|
9083 | if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
|
9084 | this.raise(TSErrors.PatternIsOptional, {
|
9085 | at: param
|
9086 | });
|
9087 | }
|
9088 | }
|
9089 | }
|
9090 | setArrowFunctionParameters(node, params, trailingCommaLoc) {
|
9091 | super.setArrowFunctionParameters(node, params, trailingCommaLoc);
|
9092 | this.tsDisallowOptionalPattern(node);
|
9093 | }
|
9094 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
9095 | if (this.match(14)) {
|
9096 | node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
|
9097 | }
|
9098 | const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
|
9099 | if (bodilessType && !this.match(5) && this.isLineTerminator()) {
|
9100 | return this.finishNode(node, bodilessType);
|
9101 | }
|
9102 | if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
|
9103 | this.raise(TSErrors.DeclareFunctionHasImplementation, {
|
9104 | at: node
|
9105 | });
|
9106 | if (node.declare) {
|
9107 | return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
|
9108 | }
|
9109 | }
|
9110 | this.tsDisallowOptionalPattern(node);
|
9111 | return super.parseFunctionBodyAndFinish(node, type, isMethod);
|
9112 | }
|
9113 | registerFunctionStatementId(node) {
|
9114 | if (!node.body && node.id) {
|
9115 | this.checkIdentifier(node.id, BIND_TS_AMBIENT);
|
9116 | } else {
|
9117 | super.registerFunctionStatementId(node);
|
9118 | }
|
9119 | }
|
9120 | tsCheckForInvalidTypeCasts(items) {
|
9121 | items.forEach(node => {
|
9122 | if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
|
9123 | this.raise(TSErrors.UnexpectedTypeAnnotation, {
|
9124 | at: node.typeAnnotation
|
9125 | });
|
9126 | }
|
9127 | });
|
9128 | }
|
9129 | toReferencedList(exprList, isInParens) {
|
9130 | this.tsCheckForInvalidTypeCasts(exprList);
|
9131 | return exprList;
|
9132 | }
|
9133 | parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
|
9134 | const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
|
9135 | if (node.type === "ArrayExpression") {
|
9136 | this.tsCheckForInvalidTypeCasts(node.elements);
|
9137 | }
|
9138 | return node;
|
9139 | }
|
9140 | parseSubscript(base, startLoc, noCalls, state) {
|
9141 | if (!this.hasPrecedingLineBreak() && this.match(35)) {
|
9142 | this.state.canStartJSXElement = false;
|
9143 | this.next();
|
9144 | const nonNullExpression = this.startNodeAt(startLoc);
|
9145 | nonNullExpression.expression = base;
|
9146 | return this.finishNode(nonNullExpression, "TSNonNullExpression");
|
9147 | }
|
9148 | let isOptionalCall = false;
|
9149 | if (this.match(18) && this.lookaheadCharCode() === 60) {
|
9150 | if (noCalls) {
|
9151 | state.stop = true;
|
9152 | return base;
|
9153 | }
|
9154 | state.optionalChainMember = isOptionalCall = true;
|
9155 | this.next();
|
9156 | }
|
9157 | if (this.match(47) || this.match(51)) {
|
9158 | let missingParenErrorLoc;
|
9159 | const result = this.tsTryParseAndCatch(() => {
|
9160 | if (!noCalls && this.atPossibleAsyncArrow(base)) {
|
9161 | const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
|
9162 | if (asyncArrowFn) {
|
9163 | return asyncArrowFn;
|
9164 | }
|
9165 | }
|
9166 | const typeArguments = this.tsParseTypeArgumentsInExpression();
|
9167 | if (!typeArguments) return;
|
9168 | if (isOptionalCall && !this.match(10)) {
|
9169 | missingParenErrorLoc = this.state.curPosition();
|
9170 | return;
|
9171 | }
|
9172 | if (tokenIsTemplate(this.state.type)) {
|
9173 | const result = super.parseTaggedTemplateExpression(base, startLoc, state);
|
9174 | result.typeParameters = typeArguments;
|
9175 | return result;
|
9176 | }
|
9177 | if (!noCalls && this.eat(10)) {
|
9178 | const node = this.startNodeAt(startLoc);
|
9179 | node.callee = base;
|
9180 | node.arguments = this.parseCallExpressionArguments(11, false);
|
9181 | this.tsCheckForInvalidTypeCasts(node.arguments);
|
9182 | node.typeParameters = typeArguments;
|
9183 | if (state.optionalChainMember) {
|
9184 | node.optional = isOptionalCall;
|
9185 | }
|
9186 | return this.finishCallExpression(node, state.optionalChainMember);
|
9187 | }
|
9188 | const tokenType = this.state.type;
|
9189 | if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
|
9190 | return;
|
9191 | }
|
9192 | const node = this.startNodeAt(startLoc);
|
9193 | node.expression = base;
|
9194 | node.typeParameters = typeArguments;
|
9195 | return this.finishNode(node, "TSInstantiationExpression");
|
9196 | });
|
9197 | if (missingParenErrorLoc) {
|
9198 | this.unexpected(missingParenErrorLoc, 10);
|
9199 | }
|
9200 | if (result) {
|
9201 | if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
|
9202 | this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
|
9203 | at: this.state.startLoc
|
9204 | });
|
9205 | }
|
9206 | return result;
|
9207 | }
|
9208 | }
|
9209 | return super.parseSubscript(base, startLoc, noCalls, state);
|
9210 | }
|
9211 | parseNewCallee(node) {
|
9212 | var _callee$extra;
|
9213 | super.parseNewCallee(node);
|
9214 | const {
|
9215 | callee
|
9216 | } = node;
|
9217 | if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
|
9218 | node.typeParameters = callee.typeParameters;
|
9219 | node.callee = callee.expression;
|
9220 | }
|
9221 | }
|
9222 | parseExprOp(left, leftStartLoc, minPrec) {
|
9223 | let isSatisfies;
|
9224 | if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
|
9225 | const node = this.startNodeAt(leftStartLoc);
|
9226 | node.expression = left;
|
9227 | node.typeAnnotation = this.tsInType(() => {
|
9228 | this.next();
|
9229 | if (this.match(75)) {
|
9230 | if (isSatisfies) {
|
9231 | this.raise(Errors.UnexpectedKeyword, {
|
9232 | at: this.state.startLoc,
|
9233 | keyword: "const"
|
9234 | });
|
9235 | }
|
9236 | return this.tsParseTypeReference();
|
9237 | }
|
9238 | return this.tsParseType();
|
9239 | });
|
9240 | this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
|
9241 | this.reScan_lt_gt();
|
9242 | return this.parseExprOp(node, leftStartLoc, minPrec);
|
9243 | }
|
9244 | return super.parseExprOp(left, leftStartLoc, minPrec);
|
9245 | }
|
9246 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
9247 | if (!this.state.isAmbientContext) {
|
9248 | super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
|
9249 | }
|
9250 | }
|
9251 | checkImportReflection(node) {
|
9252 | super.checkImportReflection(node);
|
9253 | if (node.module && node.importKind !== "value") {
|
9254 | this.raise(TSErrors.ImportReflectionHasImportType, {
|
9255 | at: node.specifiers[0].loc.start
|
9256 | });
|
9257 | }
|
9258 | }
|
9259 | checkDuplicateExports() {}
|
9260 | isPotentialImportPhase(isExport) {
|
9261 | if (super.isPotentialImportPhase(isExport)) return true;
|
9262 | if (this.isContextual(128)) {
|
9263 | const ch = this.lookaheadCharCode();
|
9264 | return isExport ? ch === 123 || ch === 42 : ch !== 61;
|
9265 | }
|
9266 | return !isExport && this.isContextual(87);
|
9267 | }
|
9268 | applyImportPhase(node, isExport, phase, loc) {
|
9269 | super.applyImportPhase(node, isExport, phase, loc);
|
9270 | if (isExport) {
|
9271 | node.exportKind = phase === "type" ? "type" : "value";
|
9272 | } else {
|
9273 | node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
|
9274 | }
|
9275 | }
|
9276 | parseImport(node) {
|
9277 | if (this.match(131)) {
|
9278 | node.importKind = "value";
|
9279 | return super.parseImport(node);
|
9280 | }
|
9281 | let importNode;
|
9282 | if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
|
9283 | node.importKind = "value";
|
9284 | return this.tsParseImportEqualsDeclaration(node);
|
9285 | } else if (this.isContextual(128)) {
|
9286 | const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
|
9287 | if (this.lookaheadCharCode() === 61) {
|
9288 | return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
|
9289 | } else {
|
9290 | importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
|
9291 | }
|
9292 | } else {
|
9293 | importNode = super.parseImport(node);
|
9294 | }
|
9295 | if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
|
9296 | this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
|
9297 | at: importNode
|
9298 | });
|
9299 | }
|
9300 | return importNode;
|
9301 | }
|
9302 | parseExport(node, decorators) {
|
9303 | if (this.match(83)) {
|
9304 | this.next();
|
9305 | let maybeDefaultIdentifier = null;
|
9306 | if (this.isContextual(128) && this.isPotentialImportPhase(false)) {
|
9307 | maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
|
9308 | } else {
|
9309 | node.importKind = "value";
|
9310 | }
|
9311 | return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, true);
|
9312 | } else if (this.eat(29)) {
|
9313 | const assign = node;
|
9314 | assign.expression = super.parseExpression();
|
9315 | this.semicolon();
|
9316 | this.sawUnambiguousESM = true;
|
9317 | return this.finishNode(assign, "TSExportAssignment");
|
9318 | } else if (this.eatContextual(93)) {
|
9319 | const decl = node;
|
9320 | this.expectContextual(126);
|
9321 | decl.id = this.parseIdentifier();
|
9322 | this.semicolon();
|
9323 | return this.finishNode(decl, "TSNamespaceExportDeclaration");
|
9324 | } else {
|
9325 | return super.parseExport(node, decorators);
|
9326 | }
|
9327 | }
|
9328 | isAbstractClass() {
|
9329 | return this.isContextual(122) && this.lookahead().type === 80;
|
9330 | }
|
9331 | parseExportDefaultExpression() {
|
9332 | if (this.isAbstractClass()) {
|
9333 | const cls = this.startNode();
|
9334 | this.next();
|
9335 | cls.abstract = true;
|
9336 | return this.parseClass(cls, true, true);
|
9337 | }
|
9338 | if (this.match(127)) {
|
9339 | const result = this.tsParseInterfaceDeclaration(this.startNode());
|
9340 | if (result) return result;
|
9341 | }
|
9342 | return super.parseExportDefaultExpression();
|
9343 | }
|
9344 | parseVarStatement(node, kind, allowMissingInitializer = false) {
|
9345 | const {
|
9346 | isAmbientContext
|
9347 | } = this.state;
|
9348 | const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
|
9349 | if (!isAmbientContext) return declaration;
|
9350 | for (const {
|
9351 | id,
|
9352 | init
|
9353 | } of declaration.declarations) {
|
9354 | if (!init) continue;
|
9355 | if (kind !== "const" || !!id.typeAnnotation) {
|
9356 | this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
|
9357 | at: init
|
9358 | });
|
9359 | } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
|
9360 | this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
|
9361 | at: init
|
9362 | });
|
9363 | }
|
9364 | }
|
9365 | return declaration;
|
9366 | }
|
9367 | parseStatementContent(flags, decorators) {
|
9368 | if (this.match(75) && this.isLookaheadContextual("enum")) {
|
9369 | const node = this.startNode();
|
9370 | this.expect(75);
|
9371 | return this.tsParseEnumDeclaration(node, {
|
9372 | const: true
|
9373 | });
|
9374 | }
|
9375 | if (this.isContextual(124)) {
|
9376 | return this.tsParseEnumDeclaration(this.startNode());
|
9377 | }
|
9378 | if (this.isContextual(127)) {
|
9379 | const result = this.tsParseInterfaceDeclaration(this.startNode());
|
9380 | if (result) return result;
|
9381 | }
|
9382 | return super.parseStatementContent(flags, decorators);
|
9383 | }
|
9384 | parseAccessModifier() {
|
9385 | return this.tsParseModifier(["public", "protected", "private"]);
|
9386 | }
|
9387 | tsHasSomeModifiers(member, modifiers) {
|
9388 | return modifiers.some(modifier => {
|
9389 | if (tsIsAccessModifier(modifier)) {
|
9390 | return member.accessibility === modifier;
|
9391 | }
|
9392 | return !!member[modifier];
|
9393 | });
|
9394 | }
|
9395 | tsIsStartOfStaticBlocks() {
|
9396 | return this.isContextual(104) && this.lookaheadCharCode() === 123;
|
9397 | }
|
9398 | parseClassMember(classBody, member, state) {
|
9399 | const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
|
9400 | this.tsParseModifiers({
|
9401 | allowedModifiers: modifiers,
|
9402 | disallowedModifiers: ["in", "out"],
|
9403 | stopOnStartOfClassStaticBlock: true,
|
9404 | errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
|
9405 | }, member);
|
9406 | const callParseClassMemberWithIsStatic = () => {
|
9407 | if (this.tsIsStartOfStaticBlocks()) {
|
9408 | this.next();
|
9409 | this.next();
|
9410 | if (this.tsHasSomeModifiers(member, modifiers)) {
|
9411 | this.raise(TSErrors.StaticBlockCannotHaveModifier, {
|
9412 | at: this.state.curPosition()
|
9413 | });
|
9414 | }
|
9415 | super.parseClassStaticBlock(classBody, member);
|
9416 | } else {
|
9417 | this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
|
9418 | }
|
9419 | };
|
9420 | if (member.declare) {
|
9421 | this.tsInAmbientContext(callParseClassMemberWithIsStatic);
|
9422 | } else {
|
9423 | callParseClassMemberWithIsStatic();
|
9424 | }
|
9425 | }
|
9426 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
9427 | const idx = this.tsTryParseIndexSignature(member);
|
9428 | if (idx) {
|
9429 | classBody.body.push(idx);
|
9430 | if (member.abstract) {
|
9431 | this.raise(TSErrors.IndexSignatureHasAbstract, {
|
9432 | at: member
|
9433 | });
|
9434 | }
|
9435 | if (member.accessibility) {
|
9436 | this.raise(TSErrors.IndexSignatureHasAccessibility, {
|
9437 | at: member,
|
9438 | modifier: member.accessibility
|
9439 | });
|
9440 | }
|
9441 | if (member.declare) {
|
9442 | this.raise(TSErrors.IndexSignatureHasDeclare, {
|
9443 | at: member
|
9444 | });
|
9445 | }
|
9446 | if (member.override) {
|
9447 | this.raise(TSErrors.IndexSignatureHasOverride, {
|
9448 | at: member
|
9449 | });
|
9450 | }
|
9451 | return;
|
9452 | }
|
9453 | if (!this.state.inAbstractClass && member.abstract) {
|
9454 | this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
|
9455 | at: member
|
9456 | });
|
9457 | }
|
9458 | if (member.override) {
|
9459 | if (!state.hadSuperClass) {
|
9460 | this.raise(TSErrors.OverrideNotInSubClass, {
|
9461 | at: member
|
9462 | });
|
9463 | }
|
9464 | }
|
9465 | super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
9466 | }
|
9467 | parsePostMemberNameModifiers(methodOrProp) {
|
9468 | const optional = this.eat(17);
|
9469 | if (optional) methodOrProp.optional = true;
|
9470 | if (methodOrProp.readonly && this.match(10)) {
|
9471 | this.raise(TSErrors.ClassMethodHasReadonly, {
|
9472 | at: methodOrProp
|
9473 | });
|
9474 | }
|
9475 | if (methodOrProp.declare && this.match(10)) {
|
9476 | this.raise(TSErrors.ClassMethodHasDeclare, {
|
9477 | at: methodOrProp
|
9478 | });
|
9479 | }
|
9480 | }
|
9481 | parseExpressionStatement(node, expr, decorators) {
|
9482 | const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
|
9483 | return decl || super.parseExpressionStatement(node, expr, decorators);
|
9484 | }
|
9485 | shouldParseExportDeclaration() {
|
9486 | if (this.tsIsDeclarationStart()) return true;
|
9487 | return super.shouldParseExportDeclaration();
|
9488 | }
|
9489 | parseConditional(expr, startLoc, refExpressionErrors) {
|
9490 | if (!this.state.maybeInArrowParameters || !this.match(17)) {
|
9491 | return super.parseConditional(expr, startLoc, refExpressionErrors);
|
9492 | }
|
9493 | const result = this.tryParse(() => super.parseConditional(expr, startLoc));
|
9494 | if (!result.node) {
|
9495 | if (result.error) {
|
9496 | super.setOptionalParametersError(refExpressionErrors, result.error);
|
9497 | }
|
9498 | return expr;
|
9499 | }
|
9500 | if (result.error) this.state = result.failState;
|
9501 | return result.node;
|
9502 | }
|
9503 | parseParenItem(node, startLoc) {
|
9504 | node = super.parseParenItem(node, startLoc);
|
9505 | if (this.eat(17)) {
|
9506 | node.optional = true;
|
9507 | this.resetEndLocation(node);
|
9508 | }
|
9509 | if (this.match(14)) {
|
9510 | const typeCastNode = this.startNodeAt(startLoc);
|
9511 | typeCastNode.expression = node;
|
9512 | typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
|
9513 | return this.finishNode(typeCastNode, "TSTypeCastExpression");
|
9514 | }
|
9515 | return node;
|
9516 | }
|
9517 | parseExportDeclaration(node) {
|
9518 | if (!this.state.isAmbientContext && this.isContextual(123)) {
|
9519 | return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
|
9520 | }
|
9521 | const startLoc = this.state.startLoc;
|
9522 | const isDeclare = this.eatContextual(123);
|
9523 | if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
|
9524 | throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
|
9525 | at: this.state.startLoc
|
9526 | });
|
9527 | }
|
9528 | const isIdentifier = tokenIsIdentifier(this.state.type);
|
9529 | const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
|
9530 | if (!declaration) return null;
|
9531 | if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
|
9532 | node.exportKind = "type";
|
9533 | }
|
9534 | if (isDeclare) {
|
9535 | this.resetStartLocation(declaration, startLoc);
|
9536 | declaration.declare = true;
|
9537 | }
|
9538 | return declaration;
|
9539 | }
|
9540 | parseClassId(node, isStatement, optionalId, bindingType) {
|
9541 | if ((!isStatement || optionalId) && this.isContextual(111)) {
|
9542 | return;
|
9543 | }
|
9544 | super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
|
9545 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
|
9546 | if (typeParameters) node.typeParameters = typeParameters;
|
9547 | }
|
9548 | parseClassPropertyAnnotation(node) {
|
9549 | if (!node.optional) {
|
9550 | if (this.eat(35)) {
|
9551 | node.definite = true;
|
9552 | } else if (this.eat(17)) {
|
9553 | node.optional = true;
|
9554 | }
|
9555 | }
|
9556 | const type = this.tsTryParseTypeAnnotation();
|
9557 | if (type) node.typeAnnotation = type;
|
9558 | }
|
9559 | parseClassProperty(node) {
|
9560 | this.parseClassPropertyAnnotation(node);
|
9561 | if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
|
9562 | this.raise(TSErrors.DeclareClassFieldHasInitializer, {
|
9563 | at: this.state.startLoc
|
9564 | });
|
9565 | }
|
9566 | if (node.abstract && this.match(29)) {
|
9567 | const {
|
9568 | key
|
9569 | } = node;
|
9570 | this.raise(TSErrors.AbstractPropertyHasInitializer, {
|
9571 | at: this.state.startLoc,
|
9572 | propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
|
9573 | });
|
9574 | }
|
9575 | return super.parseClassProperty(node);
|
9576 | }
|
9577 | parseClassPrivateProperty(node) {
|
9578 | if (node.abstract) {
|
9579 | this.raise(TSErrors.PrivateElementHasAbstract, {
|
9580 | at: node
|
9581 | });
|
9582 | }
|
9583 | if (node.accessibility) {
|
9584 | this.raise(TSErrors.PrivateElementHasAccessibility, {
|
9585 | at: node,
|
9586 | modifier: node.accessibility
|
9587 | });
|
9588 | }
|
9589 | this.parseClassPropertyAnnotation(node);
|
9590 | return super.parseClassPrivateProperty(node);
|
9591 | }
|
9592 | parseClassAccessorProperty(node) {
|
9593 | this.parseClassPropertyAnnotation(node);
|
9594 | if (node.optional) {
|
9595 | this.raise(TSErrors.AccessorCannotBeOptional, {
|
9596 | at: node
|
9597 | });
|
9598 | }
|
9599 | return super.parseClassAccessorProperty(node);
|
9600 | }
|
9601 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
9602 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
|
9603 | if (typeParameters && isConstructor) {
|
9604 | this.raise(TSErrors.ConstructorHasTypeParameters, {
|
9605 | at: typeParameters
|
9606 | });
|
9607 | }
|
9608 | const {
|
9609 | declare = false,
|
9610 | kind
|
9611 | } = method;
|
9612 | if (declare && (kind === "get" || kind === "set")) {
|
9613 | this.raise(TSErrors.DeclareAccessor, {
|
9614 | at: method,
|
9615 | kind
|
9616 | });
|
9617 | }
|
9618 | if (typeParameters) method.typeParameters = typeParameters;
|
9619 | super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
|
9620 | }
|
9621 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
9622 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
|
9623 | if (typeParameters) method.typeParameters = typeParameters;
|
9624 | super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
|
9625 | }
|
9626 | declareClassPrivateMethodInScope(node, kind) {
|
9627 | if (node.type === "TSDeclareMethod") return;
|
9628 | if (node.type === "MethodDefinition" && !node.value.body) return;
|
9629 | super.declareClassPrivateMethodInScope(node, kind);
|
9630 | }
|
9631 | parseClassSuper(node) {
|
9632 | super.parseClassSuper(node);
|
9633 | if (node.superClass && (this.match(47) || this.match(51))) {
|
9634 | node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
|
9635 | }
|
9636 | if (this.eatContextual(111)) {
|
9637 | node.implements = this.tsParseHeritageClause("implements");
|
9638 | }
|
9639 | }
|
9640 | parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
|
9641 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
|
9642 | if (typeParameters) prop.typeParameters = typeParameters;
|
9643 | return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
|
9644 | }
|
9645 | parseFunctionParams(node, isConstructor) {
|
9646 | const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
|
9647 | if (typeParameters) node.typeParameters = typeParameters;
|
9648 | super.parseFunctionParams(node, isConstructor);
|
9649 | }
|
9650 | parseVarId(decl, kind) {
|
9651 | super.parseVarId(decl, kind);
|
9652 | if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
|
9653 | decl.definite = true;
|
9654 | }
|
9655 | const type = this.tsTryParseTypeAnnotation();
|
9656 | if (type) {
|
9657 | decl.id.typeAnnotation = type;
|
9658 | this.resetEndLocation(decl.id);
|
9659 | }
|
9660 | }
|
9661 | parseAsyncArrowFromCallExpression(node, call) {
|
9662 | if (this.match(14)) {
|
9663 | node.returnType = this.tsParseTypeAnnotation();
|
9664 | }
|
9665 | return super.parseAsyncArrowFromCallExpression(node, call);
|
9666 | }
|
9667 | parseMaybeAssign(refExpressionErrors, afterLeftParse) {
|
9668 | var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
|
9669 | let state;
|
9670 | let jsx;
|
9671 | let typeCast;
|
9672 | if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
|
9673 | state = this.state.clone();
|
9674 | jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
|
9675 | if (!jsx.error) return jsx.node;
|
9676 | const {
|
9677 | context
|
9678 | } = this.state;
|
9679 | const currentContext = context[context.length - 1];
|
9680 | if (currentContext === types.j_oTag || currentContext === types.j_expr) {
|
9681 | context.pop();
|
9682 | }
|
9683 | }
|
9684 | if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
|
9685 | return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
9686 | }
|
9687 | if (!state || state === this.state) state = this.state.clone();
|
9688 | let typeParameters;
|
9689 | const arrow = this.tryParse(abort => {
|
9690 | var _expr$extra, _typeParameters;
|
9691 | typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
|
9692 | const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
9693 | if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
|
9694 | abort();
|
9695 | }
|
9696 | if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
|
9697 | this.resetStartLocationFromNode(expr, typeParameters);
|
9698 | }
|
9699 | expr.typeParameters = typeParameters;
|
9700 | return expr;
|
9701 | }, state);
|
9702 | if (!arrow.error && !arrow.aborted) {
|
9703 | if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
|
9704 | return arrow.node;
|
9705 | }
|
9706 | if (!jsx) {
|
9707 | assert(!this.hasPlugin("jsx"));
|
9708 | typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
|
9709 | if (!typeCast.error) return typeCast.node;
|
9710 | }
|
9711 | if ((_jsx2 = jsx) != null && _jsx2.node) {
|
9712 | this.state = jsx.failState;
|
9713 | return jsx.node;
|
9714 | }
|
9715 | if (arrow.node) {
|
9716 | this.state = arrow.failState;
|
9717 | if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
|
9718 | return arrow.node;
|
9719 | }
|
9720 | if ((_typeCast = typeCast) != null && _typeCast.node) {
|
9721 | this.state = typeCast.failState;
|
9722 | return typeCast.node;
|
9723 | }
|
9724 | throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
|
9725 | }
|
9726 | reportReservedArrowTypeParam(node) {
|
9727 | var _node$extra;
|
9728 | if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
|
9729 | this.raise(TSErrors.ReservedArrowTypeParam, {
|
9730 | at: node
|
9731 | });
|
9732 | }
|
9733 | }
|
9734 | parseMaybeUnary(refExpressionErrors, sawUnary) {
|
9735 | if (!this.hasPlugin("jsx") && this.match(47)) {
|
9736 | return this.tsParseTypeAssertion();
|
9737 | }
|
9738 | return super.parseMaybeUnary(refExpressionErrors, sawUnary);
|
9739 | }
|
9740 | parseArrow(node) {
|
9741 | if (this.match(14)) {
|
9742 | const result = this.tryParse(abort => {
|
9743 | const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
|
9744 | if (this.canInsertSemicolon() || !this.match(19)) abort();
|
9745 | return returnType;
|
9746 | });
|
9747 | if (result.aborted) return;
|
9748 | if (!result.thrown) {
|
9749 | if (result.error) this.state = result.failState;
|
9750 | node.returnType = result.node;
|
9751 | }
|
9752 | }
|
9753 | return super.parseArrow(node);
|
9754 | }
|
9755 | parseAssignableListItemTypes(param, flags) {
|
9756 | if (!(flags & 2)) return param;
|
9757 | if (this.eat(17)) {
|
9758 | param.optional = true;
|
9759 | }
|
9760 | const type = this.tsTryParseTypeAnnotation();
|
9761 | if (type) param.typeAnnotation = type;
|
9762 | this.resetEndLocation(param);
|
9763 | return param;
|
9764 | }
|
9765 | isAssignable(node, isBinding) {
|
9766 | switch (node.type) {
|
9767 | case "TSTypeCastExpression":
|
9768 | return this.isAssignable(node.expression, isBinding);
|
9769 | case "TSParameterProperty":
|
9770 | return true;
|
9771 | default:
|
9772 | return super.isAssignable(node, isBinding);
|
9773 | }
|
9774 | }
|
9775 | toAssignable(node, isLHS = false) {
|
9776 | switch (node.type) {
|
9777 | case "ParenthesizedExpression":
|
9778 | this.toAssignableParenthesizedExpression(node, isLHS);
|
9779 | break;
|
9780 | case "TSAsExpression":
|
9781 | case "TSSatisfiesExpression":
|
9782 | case "TSNonNullExpression":
|
9783 | case "TSTypeAssertion":
|
9784 | if (isLHS) {
|
9785 | this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
|
9786 | at: node
|
9787 | });
|
9788 | } else {
|
9789 | this.raise(TSErrors.UnexpectedTypeCastInParameter, {
|
9790 | at: node
|
9791 | });
|
9792 | }
|
9793 | this.toAssignable(node.expression, isLHS);
|
9794 | break;
|
9795 | case "AssignmentExpression":
|
9796 | if (!isLHS && node.left.type === "TSTypeCastExpression") {
|
9797 | node.left = this.typeCastToParameter(node.left);
|
9798 | }
|
9799 | default:
|
9800 | super.toAssignable(node, isLHS);
|
9801 | }
|
9802 | }
|
9803 | toAssignableParenthesizedExpression(node, isLHS) {
|
9804 | switch (node.expression.type) {
|
9805 | case "TSAsExpression":
|
9806 | case "TSSatisfiesExpression":
|
9807 | case "TSNonNullExpression":
|
9808 | case "TSTypeAssertion":
|
9809 | case "ParenthesizedExpression":
|
9810 | this.toAssignable(node.expression, isLHS);
|
9811 | break;
|
9812 | default:
|
9813 | super.toAssignable(node, isLHS);
|
9814 | }
|
9815 | }
|
9816 | checkToRestConversion(node, allowPattern) {
|
9817 | switch (node.type) {
|
9818 | case "TSAsExpression":
|
9819 | case "TSSatisfiesExpression":
|
9820 | case "TSTypeAssertion":
|
9821 | case "TSNonNullExpression":
|
9822 | this.checkToRestConversion(node.expression, false);
|
9823 | break;
|
9824 | default:
|
9825 | super.checkToRestConversion(node, allowPattern);
|
9826 | }
|
9827 | }
|
9828 | isValidLVal(type, isUnparenthesizedInAssign, binding) {
|
9829 | return getOwn({
|
9830 | TSTypeCastExpression: true,
|
9831 | TSParameterProperty: "parameter",
|
9832 | TSNonNullExpression: "expression",
|
9833 | TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
|
9834 | TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
|
9835 | TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
|
9836 | }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
|
9837 | }
|
9838 | parseBindingAtom() {
|
9839 | if (this.state.type === 78) {
|
9840 | return this.parseIdentifier(true);
|
9841 | }
|
9842 | return super.parseBindingAtom();
|
9843 | }
|
9844 | parseMaybeDecoratorArguments(expr) {
|
9845 | if (this.match(47) || this.match(51)) {
|
9846 | const typeArguments = this.tsParseTypeArgumentsInExpression();
|
9847 | if (this.match(10)) {
|
9848 | const call = super.parseMaybeDecoratorArguments(expr);
|
9849 | call.typeParameters = typeArguments;
|
9850 | return call;
|
9851 | }
|
9852 | this.unexpected(null, 10);
|
9853 | }
|
9854 | return super.parseMaybeDecoratorArguments(expr);
|
9855 | }
|
9856 | checkCommaAfterRest(close) {
|
9857 | if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
|
9858 | this.next();
|
9859 | return false;
|
9860 | }
|
9861 | return super.checkCommaAfterRest(close);
|
9862 | }
|
9863 | isClassMethod() {
|
9864 | return this.match(47) || super.isClassMethod();
|
9865 | }
|
9866 | isClassProperty() {
|
9867 | return this.match(35) || this.match(14) || super.isClassProperty();
|
9868 | }
|
9869 | parseMaybeDefault(startLoc, left) {
|
9870 | const node = super.parseMaybeDefault(startLoc, left);
|
9871 | if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
|
9872 | this.raise(TSErrors.TypeAnnotationAfterAssign, {
|
9873 | at: node.typeAnnotation
|
9874 | });
|
9875 | }
|
9876 | return node;
|
9877 | }
|
9878 | getTokenFromCode(code) {
|
9879 | if (this.state.inType) {
|
9880 | if (code === 62) {
|
9881 | this.finishOp(48, 1);
|
9882 | return;
|
9883 | }
|
9884 | if (code === 60) {
|
9885 | this.finishOp(47, 1);
|
9886 | return;
|
9887 | }
|
9888 | }
|
9889 | super.getTokenFromCode(code);
|
9890 | }
|
9891 | reScan_lt_gt() {
|
9892 | const {
|
9893 | type
|
9894 | } = this.state;
|
9895 | if (type === 47) {
|
9896 | this.state.pos -= 1;
|
9897 | this.readToken_lt();
|
9898 | } else if (type === 48) {
|
9899 | this.state.pos -= 1;
|
9900 | this.readToken_gt();
|
9901 | }
|
9902 | }
|
9903 | reScan_lt() {
|
9904 | const {
|
9905 | type
|
9906 | } = this.state;
|
9907 | if (type === 51) {
|
9908 | this.state.pos -= 2;
|
9909 | this.finishOp(47, 1);
|
9910 | return 47;
|
9911 | }
|
9912 | return type;
|
9913 | }
|
9914 | toAssignableList(exprList, trailingCommaLoc, isLHS) {
|
9915 | for (let i = 0; i < exprList.length; i++) {
|
9916 | const expr = exprList[i];
|
9917 | if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
|
9918 | exprList[i] = this.typeCastToParameter(expr);
|
9919 | }
|
9920 | }
|
9921 | super.toAssignableList(exprList, trailingCommaLoc, isLHS);
|
9922 | }
|
9923 | typeCastToParameter(node) {
|
9924 | node.expression.typeAnnotation = node.typeAnnotation;
|
9925 | this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
|
9926 | return node.expression;
|
9927 | }
|
9928 | shouldParseArrow(params) {
|
9929 | if (this.match(14)) {
|
9930 | return params.every(expr => this.isAssignable(expr, true));
|
9931 | }
|
9932 | return super.shouldParseArrow(params);
|
9933 | }
|
9934 | shouldParseAsyncArrow() {
|
9935 | return this.match(14) || super.shouldParseAsyncArrow();
|
9936 | }
|
9937 | canHaveLeadingDecorator() {
|
9938 | return super.canHaveLeadingDecorator() || this.isAbstractClass();
|
9939 | }
|
9940 | jsxParseOpeningElementAfterName(node) {
|
9941 | if (this.match(47) || this.match(51)) {
|
9942 | const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
|
9943 | if (typeArguments) node.typeParameters = typeArguments;
|
9944 | }
|
9945 | return super.jsxParseOpeningElementAfterName(node);
|
9946 | }
|
9947 | getGetterSetterExpectedParamCount(method) {
|
9948 | const baseCount = super.getGetterSetterExpectedParamCount(method);
|
9949 | const params = this.getObjectOrClassMethodParams(method);
|
9950 | const firstParam = params[0];
|
9951 | const hasContextParam = firstParam && this.isThisParam(firstParam);
|
9952 | return hasContextParam ? baseCount + 1 : baseCount;
|
9953 | }
|
9954 | parseCatchClauseParam() {
|
9955 | const param = super.parseCatchClauseParam();
|
9956 | const type = this.tsTryParseTypeAnnotation();
|
9957 | if (type) {
|
9958 | param.typeAnnotation = type;
|
9959 | this.resetEndLocation(param);
|
9960 | }
|
9961 | return param;
|
9962 | }
|
9963 | tsInAmbientContext(cb) {
|
9964 | const oldIsAmbientContext = this.state.isAmbientContext;
|
9965 | this.state.isAmbientContext = true;
|
9966 | try {
|
9967 | return cb();
|
9968 | } finally {
|
9969 | this.state.isAmbientContext = oldIsAmbientContext;
|
9970 | }
|
9971 | }
|
9972 | parseClass(node, isStatement, optionalId) {
|
9973 | const oldInAbstractClass = this.state.inAbstractClass;
|
9974 | this.state.inAbstractClass = !!node.abstract;
|
9975 | try {
|
9976 | return super.parseClass(node, isStatement, optionalId);
|
9977 | } finally {
|
9978 | this.state.inAbstractClass = oldInAbstractClass;
|
9979 | }
|
9980 | }
|
9981 | tsParseAbstractDeclaration(node, decorators) {
|
9982 | if (this.match(80)) {
|
9983 | node.abstract = true;
|
9984 | return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
|
9985 | } else if (this.isContextual(127)) {
|
9986 | if (!this.hasFollowingLineBreak()) {
|
9987 | node.abstract = true;
|
9988 | this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
|
9989 | at: node
|
9990 | });
|
9991 | return this.tsParseInterfaceDeclaration(node);
|
9992 | }
|
9993 | } else {
|
9994 | this.unexpected(null, 80);
|
9995 | }
|
9996 | }
|
9997 | parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
|
9998 | const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
|
9999 | if (method.abstract) {
|
10000 | const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
|
10001 | if (hasBody) {
|
10002 | const {
|
10003 | key
|
10004 | } = method;
|
10005 | this.raise(TSErrors.AbstractMethodHasImplementation, {
|
10006 | at: method,
|
10007 | methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
|
10008 | });
|
10009 | }
|
10010 | }
|
10011 | return method;
|
10012 | }
|
10013 | tsParseTypeParameterName() {
|
10014 | const typeName = this.parseIdentifier();
|
10015 | return typeName.name;
|
10016 | }
|
10017 | shouldParseAsAmbientContext() {
|
10018 | return !!this.getPluginOption("typescript", "dts");
|
10019 | }
|
10020 | parse() {
|
10021 | if (this.shouldParseAsAmbientContext()) {
|
10022 | this.state.isAmbientContext = true;
|
10023 | }
|
10024 | return super.parse();
|
10025 | }
|
10026 | getExpression() {
|
10027 | if (this.shouldParseAsAmbientContext()) {
|
10028 | this.state.isAmbientContext = true;
|
10029 | }
|
10030 | return super.getExpression();
|
10031 | }
|
10032 | parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
|
10033 | if (!isString && isMaybeTypeOnly) {
|
10034 | this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
|
10035 | return this.finishNode(node, "ExportSpecifier");
|
10036 | }
|
10037 | node.exportKind = "value";
|
10038 | return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
|
10039 | }
|
10040 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
|
10041 | if (!importedIsString && isMaybeTypeOnly) {
|
10042 | this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
|
10043 | return this.finishNode(specifier, "ImportSpecifier");
|
10044 | }
|
10045 | specifier.importKind = "value";
|
10046 | return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
|
10047 | }
|
10048 | parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
|
10049 | const leftOfAsKey = isImport ? "imported" : "local";
|
10050 | const rightOfAsKey = isImport ? "local" : "exported";
|
10051 | let leftOfAs = node[leftOfAsKey];
|
10052 | let rightOfAs;
|
10053 | let hasTypeSpecifier = false;
|
10054 | let canParseAsKeyword = true;
|
10055 | const loc = leftOfAs.loc.start;
|
10056 | if (this.isContextual(93)) {
|
10057 | const firstAs = this.parseIdentifier();
|
10058 | if (this.isContextual(93)) {
|
10059 | const secondAs = this.parseIdentifier();
|
10060 | if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
10061 | hasTypeSpecifier = true;
|
10062 | leftOfAs = firstAs;
|
10063 | rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
10064 | canParseAsKeyword = false;
|
10065 | } else {
|
10066 | rightOfAs = secondAs;
|
10067 | canParseAsKeyword = false;
|
10068 | }
|
10069 | } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
10070 | canParseAsKeyword = false;
|
10071 | rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
10072 | } else {
|
10073 | hasTypeSpecifier = true;
|
10074 | leftOfAs = firstAs;
|
10075 | }
|
10076 | } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
10077 | hasTypeSpecifier = true;
|
10078 | if (isImport) {
|
10079 | leftOfAs = this.parseIdentifier(true);
|
10080 | if (!this.isContextual(93)) {
|
10081 | this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
|
10082 | }
|
10083 | } else {
|
10084 | leftOfAs = this.parseModuleExportName();
|
10085 | }
|
10086 | }
|
10087 | if (hasTypeSpecifier && isInTypeOnlyImportExport) {
|
10088 | this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
|
10089 | at: loc
|
10090 | });
|
10091 | }
|
10092 | node[leftOfAsKey] = leftOfAs;
|
10093 | node[rightOfAsKey] = rightOfAs;
|
10094 | const kindKey = isImport ? "importKind" : "exportKind";
|
10095 | node[kindKey] = hasTypeSpecifier ? "type" : "value";
|
10096 | if (canParseAsKeyword && this.eatContextual(93)) {
|
10097 | node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
|
10098 | }
|
10099 | if (!node[rightOfAsKey]) {
|
10100 | node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
|
10101 | }
|
10102 | if (isImport) {
|
10103 | this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
|
10104 | }
|
10105 | }
|
10106 | };
|
10107 | function isPossiblyLiteralEnum(expression) {
|
10108 | if (expression.type !== "MemberExpression") return false;
|
10109 | const {
|
10110 | computed,
|
10111 | property
|
10112 | } = expression;
|
10113 | if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
|
10114 | return false;
|
10115 | }
|
10116 | return isUncomputedMemberExpressionChain(expression.object);
|
10117 | }
|
10118 | function isValidAmbientConstInitializer(expression, estree) {
|
10119 | var _expression$extra;
|
10120 | const {
|
10121 | type
|
10122 | } = expression;
|
10123 | if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
|
10124 | return false;
|
10125 | }
|
10126 | if (estree) {
|
10127 | if (type === "Literal") {
|
10128 | const {
|
10129 | value
|
10130 | } = expression;
|
10131 | if (typeof value === "string" || typeof value === "boolean") {
|
10132 | return true;
|
10133 | }
|
10134 | }
|
10135 | } else {
|
10136 | if (type === "StringLiteral" || type === "BooleanLiteral") {
|
10137 | return true;
|
10138 | }
|
10139 | }
|
10140 | if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
|
10141 | return true;
|
10142 | }
|
10143 | if (type === "TemplateLiteral" && expression.expressions.length === 0) {
|
10144 | return true;
|
10145 | }
|
10146 | if (isPossiblyLiteralEnum(expression)) {
|
10147 | return true;
|
10148 | }
|
10149 | return false;
|
10150 | }
|
10151 | function isNumber(expression, estree) {
|
10152 | if (estree) {
|
10153 | return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
|
10154 | }
|
10155 | return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
|
10156 | }
|
10157 | function isNegativeNumber(expression, estree) {
|
10158 | if (expression.type === "UnaryExpression") {
|
10159 | const {
|
10160 | operator,
|
10161 | argument
|
10162 | } = expression;
|
10163 | if (operator === "-" && isNumber(argument, estree)) {
|
10164 | return true;
|
10165 | }
|
10166 | }
|
10167 | return false;
|
10168 | }
|
10169 | function isUncomputedMemberExpressionChain(expression) {
|
10170 | if (expression.type === "Identifier") return true;
|
10171 | if (expression.type !== "MemberExpression" || expression.computed) {
|
10172 | return false;
|
10173 | }
|
10174 | return isUncomputedMemberExpressionChain(expression.object);
|
10175 | }
|
10176 | const PlaceholderErrors = ParseErrorEnum`placeholders`({
|
10177 | ClassNameIsRequired: "A class name is required.",
|
10178 | UnexpectedSpace: "Unexpected space in placeholder."
|
10179 | });
|
10180 | var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
|
10181 | parsePlaceholder(expectedNode) {
|
10182 | if (this.match(142)) {
|
10183 | const node = this.startNode();
|
10184 | this.next();
|
10185 | this.assertNoSpace();
|
10186 | node.name = super.parseIdentifier(true);
|
10187 | this.assertNoSpace();
|
10188 | this.expect(142);
|
10189 | return this.finishPlaceholder(node, expectedNode);
|
10190 | }
|
10191 | }
|
10192 | finishPlaceholder(node, expectedNode) {
|
10193 | const isFinished = !!(node.expectedNode && node.type === "Placeholder");
|
10194 | node.expectedNode = expectedNode;
|
10195 | return isFinished ? node : this.finishNode(node, "Placeholder");
|
10196 | }
|
10197 | getTokenFromCode(code) {
|
10198 | if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
|
10199 | this.finishOp(142, 2);
|
10200 | } else {
|
10201 | super.getTokenFromCode(code);
|
10202 | }
|
10203 | }
|
10204 | parseExprAtom(refExpressionErrors) {
|
10205 | return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
|
10206 | }
|
10207 | parseIdentifier(liberal) {
|
10208 | return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
|
10209 | }
|
10210 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
10211 | if (word !== undefined) {
|
10212 | super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
|
10213 | }
|
10214 | }
|
10215 | parseBindingAtom() {
|
10216 | return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
|
10217 | }
|
10218 | isValidLVal(type, isParenthesized, binding) {
|
10219 | return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
|
10220 | }
|
10221 | toAssignable(node, isLHS) {
|
10222 | if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
|
10223 | node.expectedNode = "Pattern";
|
10224 | } else {
|
10225 | super.toAssignable(node, isLHS);
|
10226 | }
|
10227 | }
|
10228 | chStartsBindingIdentifier(ch, pos) {
|
10229 | if (super.chStartsBindingIdentifier(ch, pos)) {
|
10230 | return true;
|
10231 | }
|
10232 | const nextToken = this.lookahead();
|
10233 | if (nextToken.type === 142) {
|
10234 | return true;
|
10235 | }
|
10236 | return false;
|
10237 | }
|
10238 | verifyBreakContinue(node, isBreak) {
|
10239 | if (node.label && node.label.type === "Placeholder") return;
|
10240 | super.verifyBreakContinue(node, isBreak);
|
10241 | }
|
10242 | parseExpressionStatement(node, expr) {
|
10243 | if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
|
10244 | return super.parseExpressionStatement(node, expr);
|
10245 | }
|
10246 | if (this.match(14)) {
|
10247 | const stmt = node;
|
10248 | stmt.label = this.finishPlaceholder(expr, "Identifier");
|
10249 | this.next();
|
10250 | stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
|
10251 | return this.finishNode(stmt, "LabeledStatement");
|
10252 | }
|
10253 | this.semicolon();
|
10254 | node.name = expr.name;
|
10255 | return this.finishPlaceholder(node, "Statement");
|
10256 | }
|
10257 | parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
|
10258 | return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
|
10259 | }
|
10260 | parseFunctionId(requireId) {
|
10261 | return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
|
10262 | }
|
10263 | parseClass(node, isStatement, optionalId) {
|
10264 | const type = isStatement ? "ClassDeclaration" : "ClassExpression";
|
10265 | this.next();
|
10266 | const oldStrict = this.state.strict;
|
10267 | const placeholder = this.parsePlaceholder("Identifier");
|
10268 | if (placeholder) {
|
10269 | if (this.match(81) || this.match(142) || this.match(5)) {
|
10270 | node.id = placeholder;
|
10271 | } else if (optionalId || !isStatement) {
|
10272 | node.id = null;
|
10273 | node.body = this.finishPlaceholder(placeholder, "ClassBody");
|
10274 | return this.finishNode(node, type);
|
10275 | } else {
|
10276 | throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
|
10277 | at: this.state.startLoc
|
10278 | });
|
10279 | }
|
10280 | } else {
|
10281 | this.parseClassId(node, isStatement, optionalId);
|
10282 | }
|
10283 | super.parseClassSuper(node);
|
10284 | node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
|
10285 | return this.finishNode(node, type);
|
10286 | }
|
10287 | parseExport(node, decorators) {
|
10288 | const placeholder = this.parsePlaceholder("Identifier");
|
10289 | if (!placeholder) return super.parseExport(node, decorators);
|
10290 | if (!this.isContextual(97) && !this.match(12)) {
|
10291 | node.specifiers = [];
|
10292 | node.source = null;
|
10293 | node.declaration = this.finishPlaceholder(placeholder, "Declaration");
|
10294 | return this.finishNode(node, "ExportNamedDeclaration");
|
10295 | }
|
10296 | this.expectPlugin("exportDefaultFrom");
|
10297 | const specifier = this.startNode();
|
10298 | specifier.exported = placeholder;
|
10299 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
10300 | return super.parseExport(node, decorators);
|
10301 | }
|
10302 | isExportDefaultSpecifier() {
|
10303 | if (this.match(65)) {
|
10304 | const next = this.nextTokenStart();
|
10305 | if (this.isUnparsedContextual(next, "from")) {
|
10306 | if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
|
10307 | return true;
|
10308 | }
|
10309 | }
|
10310 | }
|
10311 | return super.isExportDefaultSpecifier();
|
10312 | }
|
10313 | maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
|
10314 | var _specifiers;
|
10315 | if ((_specifiers = node.specifiers) != null && _specifiers.length) {
|
10316 | return true;
|
10317 | }
|
10318 | return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
|
10319 | }
|
10320 | checkExport(node) {
|
10321 | const {
|
10322 | specifiers
|
10323 | } = node;
|
10324 | if (specifiers != null && specifiers.length) {
|
10325 | node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
|
10326 | }
|
10327 | super.checkExport(node);
|
10328 | node.specifiers = specifiers;
|
10329 | }
|
10330 | parseImport(node) {
|
10331 | const placeholder = this.parsePlaceholder("Identifier");
|
10332 | if (!placeholder) return super.parseImport(node);
|
10333 | node.specifiers = [];
|
10334 | if (!this.isContextual(97) && !this.match(12)) {
|
10335 | node.source = this.finishPlaceholder(placeholder, "StringLiteral");
|
10336 | this.semicolon();
|
10337 | return this.finishNode(node, "ImportDeclaration");
|
10338 | }
|
10339 | const specifier = this.startNodeAtNode(placeholder);
|
10340 | specifier.local = placeholder;
|
10341 | node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
|
10342 | if (this.eat(12)) {
|
10343 | const hasStarImport = this.maybeParseStarImportSpecifier(node);
|
10344 | if (!hasStarImport) this.parseNamedImportSpecifiers(node);
|
10345 | }
|
10346 | this.expectContextual(97);
|
10347 | node.source = this.parseImportSource();
|
10348 | this.semicolon();
|
10349 | return this.finishNode(node, "ImportDeclaration");
|
10350 | }
|
10351 | parseImportSource() {
|
10352 | return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
|
10353 | }
|
10354 | assertNoSpace() {
|
10355 | if (this.state.start > this.state.lastTokEndLoc.index) {
|
10356 | this.raise(PlaceholderErrors.UnexpectedSpace, {
|
10357 | at: this.state.lastTokEndLoc
|
10358 | });
|
10359 | }
|
10360 | }
|
10361 | };
|
10362 | var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
|
10363 | parseV8Intrinsic() {
|
10364 | if (this.match(54)) {
|
10365 | const v8IntrinsicStartLoc = this.state.startLoc;
|
10366 | const node = this.startNode();
|
10367 | this.next();
|
10368 | if (tokenIsIdentifier(this.state.type)) {
|
10369 | const name = this.parseIdentifierName();
|
10370 | const identifier = this.createIdentifier(node, name);
|
10371 | identifier.type = "V8IntrinsicIdentifier";
|
10372 | if (this.match(10)) {
|
10373 | return identifier;
|
10374 | }
|
10375 | }
|
10376 | this.unexpected(v8IntrinsicStartLoc);
|
10377 | }
|
10378 | }
|
10379 | parseExprAtom(refExpressionErrors) {
|
10380 | return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
|
10381 | }
|
10382 | };
|
10383 | function hasPlugin(plugins, expectedConfig) {
|
10384 | const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
|
10385 | const expectedKeys = Object.keys(expectedOptions);
|
10386 | const expectedOptionsIsEmpty = expectedKeys.length === 0;
|
10387 | return plugins.some(p => {
|
10388 | if (typeof p === "string") {
|
10389 | return expectedOptionsIsEmpty && p === expectedName;
|
10390 | } else {
|
10391 | const [pluginName, pluginOptions] = p;
|
10392 | if (pluginName !== expectedName) {
|
10393 | return false;
|
10394 | }
|
10395 | for (const key of expectedKeys) {
|
10396 | if (pluginOptions[key] !== expectedOptions[key]) {
|
10397 | return false;
|
10398 | }
|
10399 | }
|
10400 | return true;
|
10401 | }
|
10402 | });
|
10403 | }
|
10404 | function getPluginOption(plugins, name, option) {
|
10405 | const plugin = plugins.find(plugin => {
|
10406 | if (Array.isArray(plugin)) {
|
10407 | return plugin[0] === name;
|
10408 | } else {
|
10409 | return plugin === name;
|
10410 | }
|
10411 | });
|
10412 | if (plugin && Array.isArray(plugin) && plugin.length > 1) {
|
10413 | return plugin[1][option];
|
10414 | }
|
10415 | return null;
|
10416 | }
|
10417 | const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
|
10418 | const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
|
10419 | const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
|
10420 | function validatePlugins(plugins) {
|
10421 | if (hasPlugin(plugins, "decorators")) {
|
10422 | if (hasPlugin(plugins, "decorators-legacy")) {
|
10423 | throw new Error("Cannot use the decorators and decorators-legacy plugin together");
|
10424 | }
|
10425 | const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
|
10426 | if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
|
10427 | throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
|
10428 | }
|
10429 | const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
|
10430 | if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
|
10431 | throw new Error("'allowCallParenthesized' must be a boolean.");
|
10432 | }
|
10433 | }
|
10434 | if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
|
10435 | throw new Error("Cannot combine flow and typescript plugins.");
|
10436 | }
|
10437 | if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
|
10438 | throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
|
10439 | }
|
10440 | if (hasPlugin(plugins, "pipelineOperator")) {
|
10441 | const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
|
10442 | if (!PIPELINE_PROPOSALS.includes(proposal)) {
|
10443 | const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
|
10444 | throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
|
10445 | }
|
10446 | const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
|
10447 | syntaxType: "hash"
|
10448 | }]);
|
10449 | if (proposal === "hack") {
|
10450 | if (hasPlugin(plugins, "placeholders")) {
|
10451 | throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
|
10452 | }
|
10453 | if (hasPlugin(plugins, "v8intrinsic")) {
|
10454 | throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
|
10455 | }
|
10456 | const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
|
10457 | if (!TOPIC_TOKENS.includes(topicToken)) {
|
10458 | const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
|
10459 | throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
|
10460 | }
|
10461 | if (topicToken === "#" && tupleSyntaxIsHash) {
|
10462 | throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
|
10463 | }
|
10464 | } else if (proposal === "smart" && tupleSyntaxIsHash) {
|
10465 | throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
|
10466 | }
|
10467 | }
|
10468 | if (hasPlugin(plugins, "moduleAttributes")) {
|
10469 | {
|
10470 | if (hasPlugin(plugins, "importAssertions") || hasPlugin(plugins, "importAttributes")) {
|
10471 | throw new Error("Cannot combine importAssertions, importAttributes and moduleAttributes plugins.");
|
10472 | }
|
10473 | const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
|
10474 | if (moduleAttributesVersionPluginOption !== "may-2020") {
|
10475 | throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
|
10476 | }
|
10477 | }
|
10478 | }
|
10479 | if (hasPlugin(plugins, "importAssertions") && hasPlugin(plugins, "importAttributes")) {
|
10480 | throw new Error("Cannot combine importAssertions and importAttributes plugins.");
|
10481 | }
|
10482 | if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
|
10483 | throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
|
10484 | }
|
10485 | if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
|
10486 | const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
|
10487 | error.missingPlugins = "doExpressions";
|
10488 | throw error;
|
10489 | }
|
10490 | }
|
10491 | const mixinPlugins = {
|
10492 | estree,
|
10493 | jsx,
|
10494 | flow,
|
10495 | typescript,
|
10496 | v8intrinsic,
|
10497 | placeholders
|
10498 | };
|
10499 | const mixinPluginNames = Object.keys(mixinPlugins);
|
10500 | const defaultOptions = {
|
10501 | sourceType: "script",
|
10502 | sourceFilename: undefined,
|
10503 | startColumn: 0,
|
10504 | startLine: 1,
|
10505 | allowAwaitOutsideFunction: false,
|
10506 | allowReturnOutsideFunction: false,
|
10507 | allowNewTargetOutsideFunction: false,
|
10508 | allowImportExportEverywhere: false,
|
10509 | allowSuperOutsideMethod: false,
|
10510 | allowUndeclaredExports: false,
|
10511 | plugins: [],
|
10512 | strictMode: null,
|
10513 | ranges: false,
|
10514 | tokens: false,
|
10515 | createParenthesizedExpressions: false,
|
10516 | errorRecovery: false,
|
10517 | attachComment: true,
|
10518 | annexB: true
|
10519 | };
|
10520 | function getOptions(opts) {
|
10521 | if (opts && opts.annexB != null && opts.annexB !== false) {
|
10522 | throw new Error("The `annexB` option can only be set to `false`.");
|
10523 | }
|
10524 | const options = {};
|
10525 | for (const key of Object.keys(defaultOptions)) {
|
10526 | options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
|
10527 | }
|
10528 | return options;
|
10529 | }
|
10530 | class ExpressionParser extends LValParser {
|
10531 | checkProto(prop, isRecord, protoRef, refExpressionErrors) {
|
10532 | if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
|
10533 | return;
|
10534 | }
|
10535 | const key = prop.key;
|
10536 | const name = key.type === "Identifier" ? key.name : key.value;
|
10537 | if (name === "__proto__") {
|
10538 | if (isRecord) {
|
10539 | this.raise(Errors.RecordNoProto, {
|
10540 | at: key
|
10541 | });
|
10542 | return;
|
10543 | }
|
10544 | if (protoRef.used) {
|
10545 | if (refExpressionErrors) {
|
10546 | if (refExpressionErrors.doubleProtoLoc === null) {
|
10547 | refExpressionErrors.doubleProtoLoc = key.loc.start;
|
10548 | }
|
10549 | } else {
|
10550 | this.raise(Errors.DuplicateProto, {
|
10551 | at: key
|
10552 | });
|
10553 | }
|
10554 | }
|
10555 | protoRef.used = true;
|
10556 | }
|
10557 | }
|
10558 | shouldExitDescending(expr, potentialArrowAt) {
|
10559 | return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
|
10560 | }
|
10561 | getExpression() {
|
10562 | this.enterInitialScopes();
|
10563 | this.nextToken();
|
10564 | const expr = this.parseExpression();
|
10565 | if (!this.match(137)) {
|
10566 | this.unexpected();
|
10567 | }
|
10568 | this.finalizeRemainingComments();
|
10569 | expr.comments = this.state.comments;
|
10570 | expr.errors = this.state.errors;
|
10571 | if (this.options.tokens) {
|
10572 | expr.tokens = this.tokens;
|
10573 | }
|
10574 | return expr;
|
10575 | }
|
10576 | parseExpression(disallowIn, refExpressionErrors) {
|
10577 | if (disallowIn) {
|
10578 | return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
|
10579 | }
|
10580 | return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
|
10581 | }
|
10582 | parseExpressionBase(refExpressionErrors) {
|
10583 | const startLoc = this.state.startLoc;
|
10584 | const expr = this.parseMaybeAssign(refExpressionErrors);
|
10585 | if (this.match(12)) {
|
10586 | const node = this.startNodeAt(startLoc);
|
10587 | node.expressions = [expr];
|
10588 | while (this.eat(12)) {
|
10589 | node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
|
10590 | }
|
10591 | this.toReferencedList(node.expressions);
|
10592 | return this.finishNode(node, "SequenceExpression");
|
10593 | }
|
10594 | return expr;
|
10595 | }
|
10596 | parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
|
10597 | return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
|
10598 | }
|
10599 | parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
|
10600 | return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
|
10601 | }
|
10602 | setOptionalParametersError(refExpressionErrors, resultError) {
|
10603 | var _resultError$loc;
|
10604 | refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
|
10605 | }
|
10606 | parseMaybeAssign(refExpressionErrors, afterLeftParse) {
|
10607 | const startLoc = this.state.startLoc;
|
10608 | if (this.isContextual(106)) {
|
10609 | if (this.prodParam.hasYield) {
|
10610 | let left = this.parseYield();
|
10611 | if (afterLeftParse) {
|
10612 | left = afterLeftParse.call(this, left, startLoc);
|
10613 | }
|
10614 | return left;
|
10615 | }
|
10616 | }
|
10617 | let ownExpressionErrors;
|
10618 | if (refExpressionErrors) {
|
10619 | ownExpressionErrors = false;
|
10620 | } else {
|
10621 | refExpressionErrors = new ExpressionErrors();
|
10622 | ownExpressionErrors = true;
|
10623 | }
|
10624 | const {
|
10625 | type
|
10626 | } = this.state;
|
10627 | if (type === 10 || tokenIsIdentifier(type)) {
|
10628 | this.state.potentialArrowAt = this.state.start;
|
10629 | }
|
10630 | let left = this.parseMaybeConditional(refExpressionErrors);
|
10631 | if (afterLeftParse) {
|
10632 | left = afterLeftParse.call(this, left, startLoc);
|
10633 | }
|
10634 | if (tokenIsAssignment(this.state.type)) {
|
10635 | const node = this.startNodeAt(startLoc);
|
10636 | const operator = this.state.value;
|
10637 | node.operator = operator;
|
10638 | if (this.match(29)) {
|
10639 | this.toAssignable(left, true);
|
10640 | node.left = left;
|
10641 | const startIndex = startLoc.index;
|
10642 | if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
|
10643 | refExpressionErrors.doubleProtoLoc = null;
|
10644 | }
|
10645 | if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
|
10646 | refExpressionErrors.shorthandAssignLoc = null;
|
10647 | }
|
10648 | if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
|
10649 | this.checkDestructuringPrivate(refExpressionErrors);
|
10650 | refExpressionErrors.privateKeyLoc = null;
|
10651 | }
|
10652 | } else {
|
10653 | node.left = left;
|
10654 | }
|
10655 | this.next();
|
10656 | node.right = this.parseMaybeAssign();
|
10657 | this.checkLVal(left, {
|
10658 | in: this.finishNode(node, "AssignmentExpression")
|
10659 | });
|
10660 | return node;
|
10661 | } else if (ownExpressionErrors) {
|
10662 | this.checkExpressionErrors(refExpressionErrors, true);
|
10663 | }
|
10664 | return left;
|
10665 | }
|
10666 | parseMaybeConditional(refExpressionErrors) {
|
10667 | const startLoc = this.state.startLoc;
|
10668 | const potentialArrowAt = this.state.potentialArrowAt;
|
10669 | const expr = this.parseExprOps(refExpressionErrors);
|
10670 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
10671 | return expr;
|
10672 | }
|
10673 | return this.parseConditional(expr, startLoc, refExpressionErrors);
|
10674 | }
|
10675 | parseConditional(expr, startLoc, refExpressionErrors) {
|
10676 | if (this.eat(17)) {
|
10677 | const node = this.startNodeAt(startLoc);
|
10678 | node.test = expr;
|
10679 | node.consequent = this.parseMaybeAssignAllowIn();
|
10680 | this.expect(14);
|
10681 | node.alternate = this.parseMaybeAssign();
|
10682 | return this.finishNode(node, "ConditionalExpression");
|
10683 | }
|
10684 | return expr;
|
10685 | }
|
10686 | parseMaybeUnaryOrPrivate(refExpressionErrors) {
|
10687 | return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
|
10688 | }
|
10689 | parseExprOps(refExpressionErrors) {
|
10690 | const startLoc = this.state.startLoc;
|
10691 | const potentialArrowAt = this.state.potentialArrowAt;
|
10692 | const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
|
10693 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
10694 | return expr;
|
10695 | }
|
10696 | return this.parseExprOp(expr, startLoc, -1);
|
10697 | }
|
10698 | parseExprOp(left, leftStartLoc, minPrec) {
|
10699 | if (this.isPrivateName(left)) {
|
10700 | const value = this.getPrivateNameSV(left);
|
10701 | if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
|
10702 | this.raise(Errors.PrivateInExpectedIn, {
|
10703 | at: left,
|
10704 | identifierName: value
|
10705 | });
|
10706 | }
|
10707 | this.classScope.usePrivateName(value, left.loc.start);
|
10708 | }
|
10709 | const op = this.state.type;
|
10710 | if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
|
10711 | let prec = tokenOperatorPrecedence(op);
|
10712 | if (prec > minPrec) {
|
10713 | if (op === 39) {
|
10714 | this.expectPlugin("pipelineOperator");
|
10715 | if (this.state.inFSharpPipelineDirectBody) {
|
10716 | return left;
|
10717 | }
|
10718 | this.checkPipelineAtInfixOperator(left, leftStartLoc);
|
10719 | }
|
10720 | const node = this.startNodeAt(leftStartLoc);
|
10721 | node.left = left;
|
10722 | node.operator = this.state.value;
|
10723 | const logical = op === 41 || op === 42;
|
10724 | const coalesce = op === 40;
|
10725 | if (coalesce) {
|
10726 | prec = tokenOperatorPrecedence(42);
|
10727 | }
|
10728 | this.next();
|
10729 | if (op === 39 && this.hasPlugin(["pipelineOperator", {
|
10730 | proposal: "minimal"
|
10731 | }])) {
|
10732 | if (this.state.type === 96 && this.prodParam.hasAwait) {
|
10733 | throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
|
10734 | at: this.state.startLoc
|
10735 | });
|
10736 | }
|
10737 | }
|
10738 | node.right = this.parseExprOpRightExpr(op, prec);
|
10739 | const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
|
10740 | const nextOp = this.state.type;
|
10741 | if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
|
10742 | throw this.raise(Errors.MixingCoalesceWithLogical, {
|
10743 | at: this.state.startLoc
|
10744 | });
|
10745 | }
|
10746 | return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
|
10747 | }
|
10748 | }
|
10749 | return left;
|
10750 | }
|
10751 | parseExprOpRightExpr(op, prec) {
|
10752 | const startLoc = this.state.startLoc;
|
10753 | switch (op) {
|
10754 | case 39:
|
10755 | switch (this.getPluginOption("pipelineOperator", "proposal")) {
|
10756 | case "hack":
|
10757 | return this.withTopicBindingContext(() => {
|
10758 | return this.parseHackPipeBody();
|
10759 | });
|
10760 | case "smart":
|
10761 | return this.withTopicBindingContext(() => {
|
10762 | if (this.prodParam.hasYield && this.isContextual(106)) {
|
10763 | throw this.raise(Errors.PipeBodyIsTighter, {
|
10764 | at: this.state.startLoc
|
10765 | });
|
10766 | }
|
10767 | return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
|
10768 | });
|
10769 | case "fsharp":
|
10770 | return this.withSoloAwaitPermittingContext(() => {
|
10771 | return this.parseFSharpPipelineBody(prec);
|
10772 | });
|
10773 | }
|
10774 | default:
|
10775 | return this.parseExprOpBaseRightExpr(op, prec);
|
10776 | }
|
10777 | }
|
10778 | parseExprOpBaseRightExpr(op, prec) {
|
10779 | const startLoc = this.state.startLoc;
|
10780 | return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
|
10781 | }
|
10782 | parseHackPipeBody() {
|
10783 | var _body$extra;
|
10784 | const {
|
10785 | startLoc
|
10786 | } = this.state;
|
10787 | const body = this.parseMaybeAssign();
|
10788 | const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
|
10789 | if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
|
10790 | this.raise(Errors.PipeUnparenthesizedBody, {
|
10791 | at: startLoc,
|
10792 | type: body.type
|
10793 | });
|
10794 | }
|
10795 | if (!this.topicReferenceWasUsedInCurrentContext()) {
|
10796 | this.raise(Errors.PipeTopicUnused, {
|
10797 | at: startLoc
|
10798 | });
|
10799 | }
|
10800 | return body;
|
10801 | }
|
10802 | checkExponentialAfterUnary(node) {
|
10803 | if (this.match(57)) {
|
10804 | this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
|
10805 | at: node.argument
|
10806 | });
|
10807 | }
|
10808 | }
|
10809 | parseMaybeUnary(refExpressionErrors, sawUnary) {
|
10810 | const startLoc = this.state.startLoc;
|
10811 | const isAwait = this.isContextual(96);
|
10812 | if (isAwait && this.isAwaitAllowed()) {
|
10813 | this.next();
|
10814 | const expr = this.parseAwait(startLoc);
|
10815 | if (!sawUnary) this.checkExponentialAfterUnary(expr);
|
10816 | return expr;
|
10817 | }
|
10818 | const update = this.match(34);
|
10819 | const node = this.startNode();
|
10820 | if (tokenIsPrefix(this.state.type)) {
|
10821 | node.operator = this.state.value;
|
10822 | node.prefix = true;
|
10823 | if (this.match(72)) {
|
10824 | this.expectPlugin("throwExpressions");
|
10825 | }
|
10826 | const isDelete = this.match(89);
|
10827 | this.next();
|
10828 | node.argument = this.parseMaybeUnary(null, true);
|
10829 | this.checkExpressionErrors(refExpressionErrors, true);
|
10830 | if (this.state.strict && isDelete) {
|
10831 | const arg = node.argument;
|
10832 | if (arg.type === "Identifier") {
|
10833 | this.raise(Errors.StrictDelete, {
|
10834 | at: node
|
10835 | });
|
10836 | } else if (this.hasPropertyAsPrivateName(arg)) {
|
10837 | this.raise(Errors.DeletePrivateField, {
|
10838 | at: node
|
10839 | });
|
10840 | }
|
10841 | }
|
10842 | if (!update) {
|
10843 | if (!sawUnary) {
|
10844 | this.checkExponentialAfterUnary(node);
|
10845 | }
|
10846 | return this.finishNode(node, "UnaryExpression");
|
10847 | }
|
10848 | }
|
10849 | const expr = this.parseUpdate(node, update, refExpressionErrors);
|
10850 | if (isAwait) {
|
10851 | const {
|
10852 | type
|
10853 | } = this.state;
|
10854 | const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
|
10855 | if (startsExpr && !this.isAmbiguousAwait()) {
|
10856 | this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
|
10857 | at: startLoc
|
10858 | });
|
10859 | return this.parseAwait(startLoc);
|
10860 | }
|
10861 | }
|
10862 | return expr;
|
10863 | }
|
10864 | parseUpdate(node, update, refExpressionErrors) {
|
10865 | if (update) {
|
10866 | const updateExpressionNode = node;
|
10867 | this.checkLVal(updateExpressionNode.argument, {
|
10868 | in: this.finishNode(updateExpressionNode, "UpdateExpression")
|
10869 | });
|
10870 | return node;
|
10871 | }
|
10872 | const startLoc = this.state.startLoc;
|
10873 | let expr = this.parseExprSubscripts(refExpressionErrors);
|
10874 | if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
|
10875 | while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
|
10876 | const node = this.startNodeAt(startLoc);
|
10877 | node.operator = this.state.value;
|
10878 | node.prefix = false;
|
10879 | node.argument = expr;
|
10880 | this.next();
|
10881 | this.checkLVal(expr, {
|
10882 | in: expr = this.finishNode(node, "UpdateExpression")
|
10883 | });
|
10884 | }
|
10885 | return expr;
|
10886 | }
|
10887 | parseExprSubscripts(refExpressionErrors) {
|
10888 | const startLoc = this.state.startLoc;
|
10889 | const potentialArrowAt = this.state.potentialArrowAt;
|
10890 | const expr = this.parseExprAtom(refExpressionErrors);
|
10891 | if (this.shouldExitDescending(expr, potentialArrowAt)) {
|
10892 | return expr;
|
10893 | }
|
10894 | return this.parseSubscripts(expr, startLoc);
|
10895 | }
|
10896 | parseSubscripts(base, startLoc, noCalls) {
|
10897 | const state = {
|
10898 | optionalChainMember: false,
|
10899 | maybeAsyncArrow: this.atPossibleAsyncArrow(base),
|
10900 | stop: false
|
10901 | };
|
10902 | do {
|
10903 | base = this.parseSubscript(base, startLoc, noCalls, state);
|
10904 | state.maybeAsyncArrow = false;
|
10905 | } while (!state.stop);
|
10906 | return base;
|
10907 | }
|
10908 | parseSubscript(base, startLoc, noCalls, state) {
|
10909 | const {
|
10910 | type
|
10911 | } = this.state;
|
10912 | if (!noCalls && type === 15) {
|
10913 | return this.parseBind(base, startLoc, noCalls, state);
|
10914 | } else if (tokenIsTemplate(type)) {
|
10915 | return this.parseTaggedTemplateExpression(base, startLoc, state);
|
10916 | }
|
10917 | let optional = false;
|
10918 | if (type === 18) {
|
10919 | if (noCalls) {
|
10920 | this.raise(Errors.OptionalChainingNoNew, {
|
10921 | at: this.state.startLoc
|
10922 | });
|
10923 | if (this.lookaheadCharCode() === 40) {
|
10924 | state.stop = true;
|
10925 | return base;
|
10926 | }
|
10927 | }
|
10928 | state.optionalChainMember = optional = true;
|
10929 | this.next();
|
10930 | }
|
10931 | if (!noCalls && this.match(10)) {
|
10932 | return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
|
10933 | } else {
|
10934 | const computed = this.eat(0);
|
10935 | if (computed || optional || this.eat(16)) {
|
10936 | return this.parseMember(base, startLoc, state, computed, optional);
|
10937 | } else {
|
10938 | state.stop = true;
|
10939 | return base;
|
10940 | }
|
10941 | }
|
10942 | }
|
10943 | parseMember(base, startLoc, state, computed, optional) {
|
10944 | const node = this.startNodeAt(startLoc);
|
10945 | node.object = base;
|
10946 | node.computed = computed;
|
10947 | if (computed) {
|
10948 | node.property = this.parseExpression();
|
10949 | this.expect(3);
|
10950 | } else if (this.match(136)) {
|
10951 | if (base.type === "Super") {
|
10952 | this.raise(Errors.SuperPrivateField, {
|
10953 | at: startLoc
|
10954 | });
|
10955 | }
|
10956 | this.classScope.usePrivateName(this.state.value, this.state.startLoc);
|
10957 | node.property = this.parsePrivateName();
|
10958 | } else {
|
10959 | node.property = this.parseIdentifier(true);
|
10960 | }
|
10961 | if (state.optionalChainMember) {
|
10962 | node.optional = optional;
|
10963 | return this.finishNode(node, "OptionalMemberExpression");
|
10964 | } else {
|
10965 | return this.finishNode(node, "MemberExpression");
|
10966 | }
|
10967 | }
|
10968 | parseBind(base, startLoc, noCalls, state) {
|
10969 | const node = this.startNodeAt(startLoc);
|
10970 | node.object = base;
|
10971 | this.next();
|
10972 | node.callee = this.parseNoCallExpr();
|
10973 | state.stop = true;
|
10974 | return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
|
10975 | }
|
10976 | parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
|
10977 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
10978 | let refExpressionErrors = null;
|
10979 | this.state.maybeInArrowParameters = true;
|
10980 | this.next();
|
10981 | const node = this.startNodeAt(startLoc);
|
10982 | node.callee = base;
|
10983 | const {
|
10984 | maybeAsyncArrow,
|
10985 | optionalChainMember
|
10986 | } = state;
|
10987 | if (maybeAsyncArrow) {
|
10988 | this.expressionScope.enter(newAsyncArrowScope());
|
10989 | refExpressionErrors = new ExpressionErrors();
|
10990 | }
|
10991 | if (optionalChainMember) {
|
10992 | node.optional = optional;
|
10993 | }
|
10994 | if (optional) {
|
10995 | node.arguments = this.parseCallExpressionArguments(11);
|
10996 | } else {
|
10997 | node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
|
10998 | }
|
10999 | let finishedNode = this.finishCallExpression(node, optionalChainMember);
|
11000 | if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
|
11001 | state.stop = true;
|
11002 | this.checkDestructuringPrivate(refExpressionErrors);
|
11003 | this.expressionScope.validateAsPattern();
|
11004 | this.expressionScope.exit();
|
11005 | finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
|
11006 | } else {
|
11007 | if (maybeAsyncArrow) {
|
11008 | this.checkExpressionErrors(refExpressionErrors, true);
|
11009 | this.expressionScope.exit();
|
11010 | }
|
11011 | this.toReferencedArguments(finishedNode);
|
11012 | }
|
11013 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
11014 | return finishedNode;
|
11015 | }
|
11016 | toReferencedArguments(node, isParenthesizedExpr) {
|
11017 | this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
|
11018 | }
|
11019 | parseTaggedTemplateExpression(base, startLoc, state) {
|
11020 | const node = this.startNodeAt(startLoc);
|
11021 | node.tag = base;
|
11022 | node.quasi = this.parseTemplate(true);
|
11023 | if (state.optionalChainMember) {
|
11024 | this.raise(Errors.OptionalChainingNoTemplate, {
|
11025 | at: startLoc
|
11026 | });
|
11027 | }
|
11028 | return this.finishNode(node, "TaggedTemplateExpression");
|
11029 | }
|
11030 | atPossibleAsyncArrow(base) {
|
11031 | 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;
|
11032 | }
|
11033 | expectImportAttributesPlugin() {
|
11034 | if (!this.hasPlugin("importAssertions")) {
|
11035 | this.expectPlugin("importAttributes");
|
11036 | }
|
11037 | }
|
11038 | finishCallExpression(node, optional) {
|
11039 | if (node.callee.type === "Import") {
|
11040 | if (node.arguments.length === 2) {
|
11041 | {
|
11042 | if (!this.hasPlugin("moduleAttributes")) {
|
11043 | this.expectImportAttributesPlugin();
|
11044 | }
|
11045 | }
|
11046 | }
|
11047 | if (node.arguments.length === 0 || node.arguments.length > 2) {
|
11048 | this.raise(Errors.ImportCallArity, {
|
11049 | at: node,
|
11050 | maxArgumentCount: this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
|
11051 | });
|
11052 | } else {
|
11053 | for (const arg of node.arguments) {
|
11054 | if (arg.type === "SpreadElement") {
|
11055 | this.raise(Errors.ImportCallSpreadArgument, {
|
11056 | at: arg
|
11057 | });
|
11058 | }
|
11059 | }
|
11060 | }
|
11061 | }
|
11062 | return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
|
11063 | }
|
11064 | parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
|
11065 | const elts = [];
|
11066 | let first = true;
|
11067 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
11068 | this.state.inFSharpPipelineDirectBody = false;
|
11069 | while (!this.eat(close)) {
|
11070 | if (first) {
|
11071 | first = false;
|
11072 | } else {
|
11073 | this.expect(12);
|
11074 | if (this.match(close)) {
|
11075 | if (dynamicImport && !this.hasPlugin("importAttributes") && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
|
11076 | this.raise(Errors.ImportCallArgumentTrailingComma, {
|
11077 | at: this.state.lastTokStartLoc
|
11078 | });
|
11079 | }
|
11080 | if (nodeForExtra) {
|
11081 | this.addTrailingCommaExtraToNode(nodeForExtra);
|
11082 | }
|
11083 | this.next();
|
11084 | break;
|
11085 | }
|
11086 | }
|
11087 | elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
|
11088 | }
|
11089 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
11090 | return elts;
|
11091 | }
|
11092 | shouldParseAsyncArrow() {
|
11093 | return this.match(19) && !this.canInsertSemicolon();
|
11094 | }
|
11095 | parseAsyncArrowFromCallExpression(node, call) {
|
11096 | var _call$extra;
|
11097 | this.resetPreviousNodeTrailingComments(call);
|
11098 | this.expect(19);
|
11099 | this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
|
11100 | if (call.innerComments) {
|
11101 | setInnerComments(node, call.innerComments);
|
11102 | }
|
11103 | if (call.callee.trailingComments) {
|
11104 | setInnerComments(node, call.callee.trailingComments);
|
11105 | }
|
11106 | return node;
|
11107 | }
|
11108 | parseNoCallExpr() {
|
11109 | const startLoc = this.state.startLoc;
|
11110 | return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
|
11111 | }
|
11112 | parseExprAtom(refExpressionErrors) {
|
11113 | let node;
|
11114 | let decorators = null;
|
11115 | const {
|
11116 | type
|
11117 | } = this.state;
|
11118 | switch (type) {
|
11119 | case 79:
|
11120 | return this.parseSuper();
|
11121 | case 83:
|
11122 | node = this.startNode();
|
11123 | this.next();
|
11124 | if (this.match(16)) {
|
11125 | return this.parseImportMetaProperty(node);
|
11126 | }
|
11127 | if (!this.match(10)) {
|
11128 | this.raise(Errors.UnsupportedImport, {
|
11129 | at: this.state.lastTokStartLoc
|
11130 | });
|
11131 | }
|
11132 | return this.finishNode(node, "Import");
|
11133 | case 78:
|
11134 | node = this.startNode();
|
11135 | this.next();
|
11136 | return this.finishNode(node, "ThisExpression");
|
11137 | case 90:
|
11138 | {
|
11139 | return this.parseDo(this.startNode(), false);
|
11140 | }
|
11141 | case 56:
|
11142 | case 31:
|
11143 | {
|
11144 | this.readRegexp();
|
11145 | return this.parseRegExpLiteral(this.state.value);
|
11146 | }
|
11147 | case 132:
|
11148 | return this.parseNumericLiteral(this.state.value);
|
11149 | case 133:
|
11150 | return this.parseBigIntLiteral(this.state.value);
|
11151 | case 134:
|
11152 | return this.parseDecimalLiteral(this.state.value);
|
11153 | case 131:
|
11154 | return this.parseStringLiteral(this.state.value);
|
11155 | case 84:
|
11156 | return this.parseNullLiteral();
|
11157 | case 85:
|
11158 | return this.parseBooleanLiteral(true);
|
11159 | case 86:
|
11160 | return this.parseBooleanLiteral(false);
|
11161 | case 10:
|
11162 | {
|
11163 | const canBeArrow = this.state.potentialArrowAt === this.state.start;
|
11164 | return this.parseParenAndDistinguishExpression(canBeArrow);
|
11165 | }
|
11166 | case 2:
|
11167 | case 1:
|
11168 | {
|
11169 | return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
|
11170 | }
|
11171 | case 0:
|
11172 | {
|
11173 | return this.parseArrayLike(3, true, false, refExpressionErrors);
|
11174 | }
|
11175 | case 6:
|
11176 | case 7:
|
11177 | {
|
11178 | return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
|
11179 | }
|
11180 | case 5:
|
11181 | {
|
11182 | return this.parseObjectLike(8, false, false, refExpressionErrors);
|
11183 | }
|
11184 | case 68:
|
11185 | return this.parseFunctionOrFunctionSent();
|
11186 | case 26:
|
11187 | decorators = this.parseDecorators();
|
11188 | case 80:
|
11189 | return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
|
11190 | case 77:
|
11191 | return this.parseNewOrNewTarget();
|
11192 | case 25:
|
11193 | case 24:
|
11194 | return this.parseTemplate(false);
|
11195 | case 15:
|
11196 | {
|
11197 | node = this.startNode();
|
11198 | this.next();
|
11199 | node.object = null;
|
11200 | const callee = node.callee = this.parseNoCallExpr();
|
11201 | if (callee.type === "MemberExpression") {
|
11202 | return this.finishNode(node, "BindExpression");
|
11203 | } else {
|
11204 | throw this.raise(Errors.UnsupportedBind, {
|
11205 | at: callee
|
11206 | });
|
11207 | }
|
11208 | }
|
11209 | case 136:
|
11210 | {
|
11211 | this.raise(Errors.PrivateInExpectedIn, {
|
11212 | at: this.state.startLoc,
|
11213 | identifierName: this.state.value
|
11214 | });
|
11215 | return this.parsePrivateName();
|
11216 | }
|
11217 | case 33:
|
11218 | {
|
11219 | return this.parseTopicReferenceThenEqualsSign(54, "%");
|
11220 | }
|
11221 | case 32:
|
11222 | {
|
11223 | return this.parseTopicReferenceThenEqualsSign(44, "^");
|
11224 | }
|
11225 | case 37:
|
11226 | case 38:
|
11227 | {
|
11228 | return this.parseTopicReference("hack");
|
11229 | }
|
11230 | case 44:
|
11231 | case 54:
|
11232 | case 27:
|
11233 | {
|
11234 | const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
|
11235 | if (pipeProposal) {
|
11236 | return this.parseTopicReference(pipeProposal);
|
11237 | }
|
11238 | this.unexpected();
|
11239 | break;
|
11240 | }
|
11241 | case 47:
|
11242 | {
|
11243 | const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
|
11244 | if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
|
11245 | this.expectOnePlugin(["jsx", "flow", "typescript"]);
|
11246 | } else {
|
11247 | this.unexpected();
|
11248 | }
|
11249 | break;
|
11250 | }
|
11251 | default:
|
11252 | if (tokenIsIdentifier(type)) {
|
11253 | if (this.isContextual(125) && this.lookaheadInLineCharCode() === 123) {
|
11254 | return this.parseModuleExpression();
|
11255 | }
|
11256 | const canBeArrow = this.state.potentialArrowAt === this.state.start;
|
11257 | const containsEsc = this.state.containsEsc;
|
11258 | const id = this.parseIdentifier();
|
11259 | if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
|
11260 | const {
|
11261 | type
|
11262 | } = this.state;
|
11263 | if (type === 68) {
|
11264 | this.resetPreviousNodeTrailingComments(id);
|
11265 | this.next();
|
11266 | return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
|
11267 | } else if (tokenIsIdentifier(type)) {
|
11268 | if (this.lookaheadCharCode() === 61) {
|
11269 | return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
|
11270 | } else {
|
11271 | return id;
|
11272 | }
|
11273 | } else if (type === 90) {
|
11274 | this.resetPreviousNodeTrailingComments(id);
|
11275 | return this.parseDo(this.startNodeAtNode(id), true);
|
11276 | }
|
11277 | }
|
11278 | if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
|
11279 | this.next();
|
11280 | return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
|
11281 | }
|
11282 | return id;
|
11283 | } else {
|
11284 | this.unexpected();
|
11285 | }
|
11286 | }
|
11287 | }
|
11288 | parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
|
11289 | const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
|
11290 | if (pipeProposal) {
|
11291 | this.state.type = topicTokenType;
|
11292 | this.state.value = topicTokenValue;
|
11293 | this.state.pos--;
|
11294 | this.state.end--;
|
11295 | this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
|
11296 | return this.parseTopicReference(pipeProposal);
|
11297 | } else {
|
11298 | this.unexpected();
|
11299 | }
|
11300 | }
|
11301 | parseTopicReference(pipeProposal) {
|
11302 | const node = this.startNode();
|
11303 | const startLoc = this.state.startLoc;
|
11304 | const tokenType = this.state.type;
|
11305 | this.next();
|
11306 | return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
|
11307 | }
|
11308 | finishTopicReference(node, startLoc, pipeProposal, tokenType) {
|
11309 | if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
|
11310 | const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
|
11311 | if (!this.topicReferenceIsAllowedInCurrentContext()) {
|
11312 | this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
|
11313 | at: startLoc
|
11314 | });
|
11315 | }
|
11316 | this.registerTopicReference();
|
11317 | return this.finishNode(node, nodeType);
|
11318 | } else {
|
11319 | throw this.raise(Errors.PipeTopicUnconfiguredToken, {
|
11320 | at: startLoc,
|
11321 | token: tokenLabelName(tokenType)
|
11322 | });
|
11323 | }
|
11324 | }
|
11325 | testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
|
11326 | switch (pipeProposal) {
|
11327 | case "hack":
|
11328 | {
|
11329 | return this.hasPlugin(["pipelineOperator", {
|
11330 | topicToken: tokenLabelName(tokenType)
|
11331 | }]);
|
11332 | }
|
11333 | case "smart":
|
11334 | return tokenType === 27;
|
11335 | default:
|
11336 | throw this.raise(Errors.PipeTopicRequiresHackPipes, {
|
11337 | at: startLoc
|
11338 | });
|
11339 | }
|
11340 | }
|
11341 | parseAsyncArrowUnaryFunction(node) {
|
11342 | this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
|
11343 | const params = [this.parseIdentifier()];
|
11344 | this.prodParam.exit();
|
11345 | if (this.hasPrecedingLineBreak()) {
|
11346 | this.raise(Errors.LineTerminatorBeforeArrow, {
|
11347 | at: this.state.curPosition()
|
11348 | });
|
11349 | }
|
11350 | this.expect(19);
|
11351 | return this.parseArrowExpression(node, params, true);
|
11352 | }
|
11353 | parseDo(node, isAsync) {
|
11354 | this.expectPlugin("doExpressions");
|
11355 | if (isAsync) {
|
11356 | this.expectPlugin("asyncDoExpressions");
|
11357 | }
|
11358 | node.async = isAsync;
|
11359 | this.next();
|
11360 | const oldLabels = this.state.labels;
|
11361 | this.state.labels = [];
|
11362 | if (isAsync) {
|
11363 | this.prodParam.enter(PARAM_AWAIT);
|
11364 | node.body = this.parseBlock();
|
11365 | this.prodParam.exit();
|
11366 | } else {
|
11367 | node.body = this.parseBlock();
|
11368 | }
|
11369 | this.state.labels = oldLabels;
|
11370 | return this.finishNode(node, "DoExpression");
|
11371 | }
|
11372 | parseSuper() {
|
11373 | const node = this.startNode();
|
11374 | this.next();
|
11375 | if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
|
11376 | this.raise(Errors.SuperNotAllowed, {
|
11377 | at: node
|
11378 | });
|
11379 | } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
|
11380 | this.raise(Errors.UnexpectedSuper, {
|
11381 | at: node
|
11382 | });
|
11383 | }
|
11384 | if (!this.match(10) && !this.match(0) && !this.match(16)) {
|
11385 | this.raise(Errors.UnsupportedSuper, {
|
11386 | at: node
|
11387 | });
|
11388 | }
|
11389 | return this.finishNode(node, "Super");
|
11390 | }
|
11391 | parsePrivateName() {
|
11392 | const node = this.startNode();
|
11393 | const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
|
11394 | const name = this.state.value;
|
11395 | this.next();
|
11396 | node.id = this.createIdentifier(id, name);
|
11397 | return this.finishNode(node, "PrivateName");
|
11398 | }
|
11399 | parseFunctionOrFunctionSent() {
|
11400 | const node = this.startNode();
|
11401 | this.next();
|
11402 | if (this.prodParam.hasYield && this.match(16)) {
|
11403 | const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
|
11404 | this.next();
|
11405 | if (this.match(102)) {
|
11406 | this.expectPlugin("functionSent");
|
11407 | } else if (!this.hasPlugin("functionSent")) {
|
11408 | this.unexpected();
|
11409 | }
|
11410 | return this.parseMetaProperty(node, meta, "sent");
|
11411 | }
|
11412 | return this.parseFunction(node);
|
11413 | }
|
11414 | parseMetaProperty(node, meta, propertyName) {
|
11415 | node.meta = meta;
|
11416 | const containsEsc = this.state.containsEsc;
|
11417 | node.property = this.parseIdentifier(true);
|
11418 | if (node.property.name !== propertyName || containsEsc) {
|
11419 | this.raise(Errors.UnsupportedMetaProperty, {
|
11420 | at: node.property,
|
11421 | target: meta.name,
|
11422 | onlyValidPropertyName: propertyName
|
11423 | });
|
11424 | }
|
11425 | return this.finishNode(node, "MetaProperty");
|
11426 | }
|
11427 | parseImportMetaProperty(node) {
|
11428 | const id = this.createIdentifier(this.startNodeAtNode(node), "import");
|
11429 | this.next();
|
11430 | if (this.isContextual(100)) {
|
11431 | if (!this.inModule) {
|
11432 | this.raise(Errors.ImportMetaOutsideModule, {
|
11433 | at: id
|
11434 | });
|
11435 | }
|
11436 | this.sawUnambiguousESM = true;
|
11437 | }
|
11438 | return this.parseMetaProperty(node, id, "meta");
|
11439 | }
|
11440 | parseLiteralAtNode(value, type, node) {
|
11441 | this.addExtra(node, "rawValue", value);
|
11442 | this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
|
11443 | node.value = value;
|
11444 | this.next();
|
11445 | return this.finishNode(node, type);
|
11446 | }
|
11447 | parseLiteral(value, type) {
|
11448 | const node = this.startNode();
|
11449 | return this.parseLiteralAtNode(value, type, node);
|
11450 | }
|
11451 | parseStringLiteral(value) {
|
11452 | return this.parseLiteral(value, "StringLiteral");
|
11453 | }
|
11454 | parseNumericLiteral(value) {
|
11455 | return this.parseLiteral(value, "NumericLiteral");
|
11456 | }
|
11457 | parseBigIntLiteral(value) {
|
11458 | return this.parseLiteral(value, "BigIntLiteral");
|
11459 | }
|
11460 | parseDecimalLiteral(value) {
|
11461 | return this.parseLiteral(value, "DecimalLiteral");
|
11462 | }
|
11463 | parseRegExpLiteral(value) {
|
11464 | const node = this.parseLiteral(value.value, "RegExpLiteral");
|
11465 | node.pattern = value.pattern;
|
11466 | node.flags = value.flags;
|
11467 | return node;
|
11468 | }
|
11469 | parseBooleanLiteral(value) {
|
11470 | const node = this.startNode();
|
11471 | node.value = value;
|
11472 | this.next();
|
11473 | return this.finishNode(node, "BooleanLiteral");
|
11474 | }
|
11475 | parseNullLiteral() {
|
11476 | const node = this.startNode();
|
11477 | this.next();
|
11478 | return this.finishNode(node, "NullLiteral");
|
11479 | }
|
11480 | parseParenAndDistinguishExpression(canBeArrow) {
|
11481 | const startLoc = this.state.startLoc;
|
11482 | let val;
|
11483 | this.next();
|
11484 | this.expressionScope.enter(newArrowHeadScope());
|
11485 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
11486 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
11487 | this.state.maybeInArrowParameters = true;
|
11488 | this.state.inFSharpPipelineDirectBody = false;
|
11489 | const innerStartLoc = this.state.startLoc;
|
11490 | const exprList = [];
|
11491 | const refExpressionErrors = new ExpressionErrors();
|
11492 | let first = true;
|
11493 | let spreadStartLoc;
|
11494 | let optionalCommaStartLoc;
|
11495 | while (!this.match(11)) {
|
11496 | if (first) {
|
11497 | first = false;
|
11498 | } else {
|
11499 | this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
|
11500 | if (this.match(11)) {
|
11501 | optionalCommaStartLoc = this.state.startLoc;
|
11502 | break;
|
11503 | }
|
11504 | }
|
11505 | if (this.match(21)) {
|
11506 | const spreadNodeStartLoc = this.state.startLoc;
|
11507 | spreadStartLoc = this.state.startLoc;
|
11508 | exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
|
11509 | if (!this.checkCommaAfterRest(41)) {
|
11510 | break;
|
11511 | }
|
11512 | } else {
|
11513 | exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
|
11514 | }
|
11515 | }
|
11516 | const innerEndLoc = this.state.lastTokEndLoc;
|
11517 | this.expect(11);
|
11518 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
11519 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
11520 | let arrowNode = this.startNodeAt(startLoc);
|
11521 | if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
|
11522 | this.checkDestructuringPrivate(refExpressionErrors);
|
11523 | this.expressionScope.validateAsPattern();
|
11524 | this.expressionScope.exit();
|
11525 | this.parseArrowExpression(arrowNode, exprList, false);
|
11526 | return arrowNode;
|
11527 | }
|
11528 | this.expressionScope.exit();
|
11529 | if (!exprList.length) {
|
11530 | this.unexpected(this.state.lastTokStartLoc);
|
11531 | }
|
11532 | if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
|
11533 | if (spreadStartLoc) this.unexpected(spreadStartLoc);
|
11534 | this.checkExpressionErrors(refExpressionErrors, true);
|
11535 | this.toReferencedListDeep(exprList, true);
|
11536 | if (exprList.length > 1) {
|
11537 | val = this.startNodeAt(innerStartLoc);
|
11538 | val.expressions = exprList;
|
11539 | this.finishNode(val, "SequenceExpression");
|
11540 | this.resetEndLocation(val, innerEndLoc);
|
11541 | } else {
|
11542 | val = exprList[0];
|
11543 | }
|
11544 | return this.wrapParenthesis(startLoc, val);
|
11545 | }
|
11546 | wrapParenthesis(startLoc, expression) {
|
11547 | if (!this.options.createParenthesizedExpressions) {
|
11548 | this.addExtra(expression, "parenthesized", true);
|
11549 | this.addExtra(expression, "parenStart", startLoc.index);
|
11550 | this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
|
11551 | return expression;
|
11552 | }
|
11553 | const parenExpression = this.startNodeAt(startLoc);
|
11554 | parenExpression.expression = expression;
|
11555 | return this.finishNode(parenExpression, "ParenthesizedExpression");
|
11556 | }
|
11557 | shouldParseArrow(params) {
|
11558 | return !this.canInsertSemicolon();
|
11559 | }
|
11560 | parseArrow(node) {
|
11561 | if (this.eat(19)) {
|
11562 | return node;
|
11563 | }
|
11564 | }
|
11565 | parseParenItem(node, startLoc) {
|
11566 | return node;
|
11567 | }
|
11568 | parseNewOrNewTarget() {
|
11569 | const node = this.startNode();
|
11570 | this.next();
|
11571 | if (this.match(16)) {
|
11572 | const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
|
11573 | this.next();
|
11574 | const metaProp = this.parseMetaProperty(node, meta, "target");
|
11575 | if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
|
11576 | this.raise(Errors.UnexpectedNewTarget, {
|
11577 | at: metaProp
|
11578 | });
|
11579 | }
|
11580 | return metaProp;
|
11581 | }
|
11582 | return this.parseNew(node);
|
11583 | }
|
11584 | parseNew(node) {
|
11585 | this.parseNewCallee(node);
|
11586 | if (this.eat(10)) {
|
11587 | const args = this.parseExprList(11);
|
11588 | this.toReferencedList(args);
|
11589 | node.arguments = args;
|
11590 | } else {
|
11591 | node.arguments = [];
|
11592 | }
|
11593 | return this.finishNode(node, "NewExpression");
|
11594 | }
|
11595 | parseNewCallee(node) {
|
11596 | node.callee = this.parseNoCallExpr();
|
11597 | if (node.callee.type === "Import") {
|
11598 | this.raise(Errors.ImportCallNotNewExpression, {
|
11599 | at: node.callee
|
11600 | });
|
11601 | }
|
11602 | }
|
11603 | parseTemplateElement(isTagged) {
|
11604 | const {
|
11605 | start,
|
11606 | startLoc,
|
11607 | end,
|
11608 | value
|
11609 | } = this.state;
|
11610 | const elemStart = start + 1;
|
11611 | const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
|
11612 | if (value === null) {
|
11613 | if (!isTagged) {
|
11614 | this.raise(Errors.InvalidEscapeSequenceTemplate, {
|
11615 | at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
|
11616 | });
|
11617 | }
|
11618 | }
|
11619 | const isTail = this.match(24);
|
11620 | const endOffset = isTail ? -1 : -2;
|
11621 | const elemEnd = end + endOffset;
|
11622 | elem.value = {
|
11623 | raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
|
11624 | cooked: value === null ? null : value.slice(1, endOffset)
|
11625 | };
|
11626 | elem.tail = isTail;
|
11627 | this.next();
|
11628 | const finishedNode = this.finishNode(elem, "TemplateElement");
|
11629 | this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
|
11630 | return finishedNode;
|
11631 | }
|
11632 | parseTemplate(isTagged) {
|
11633 | const node = this.startNode();
|
11634 | node.expressions = [];
|
11635 | let curElt = this.parseTemplateElement(isTagged);
|
11636 | node.quasis = [curElt];
|
11637 | while (!curElt.tail) {
|
11638 | node.expressions.push(this.parseTemplateSubstitution());
|
11639 | this.readTemplateContinuation();
|
11640 | node.quasis.push(curElt = this.parseTemplateElement(isTagged));
|
11641 | }
|
11642 | return this.finishNode(node, "TemplateLiteral");
|
11643 | }
|
11644 | parseTemplateSubstitution() {
|
11645 | return this.parseExpression();
|
11646 | }
|
11647 | parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
|
11648 | if (isRecord) {
|
11649 | this.expectPlugin("recordAndTuple");
|
11650 | }
|
11651 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
11652 | this.state.inFSharpPipelineDirectBody = false;
|
11653 | const propHash = Object.create(null);
|
11654 | let first = true;
|
11655 | const node = this.startNode();
|
11656 | node.properties = [];
|
11657 | this.next();
|
11658 | while (!this.match(close)) {
|
11659 | if (first) {
|
11660 | first = false;
|
11661 | } else {
|
11662 | this.expect(12);
|
11663 | if (this.match(close)) {
|
11664 | this.addTrailingCommaExtraToNode(node);
|
11665 | break;
|
11666 | }
|
11667 | }
|
11668 | let prop;
|
11669 | if (isPattern) {
|
11670 | prop = this.parseBindingProperty();
|
11671 | } else {
|
11672 | prop = this.parsePropertyDefinition(refExpressionErrors);
|
11673 | this.checkProto(prop, isRecord, propHash, refExpressionErrors);
|
11674 | }
|
11675 | if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
|
11676 | this.raise(Errors.InvalidRecordProperty, {
|
11677 | at: prop
|
11678 | });
|
11679 | }
|
11680 | if (prop.shorthand) {
|
11681 | this.addExtra(prop, "shorthand", true);
|
11682 | }
|
11683 | node.properties.push(prop);
|
11684 | }
|
11685 | this.next();
|
11686 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
11687 | let type = "ObjectExpression";
|
11688 | if (isPattern) {
|
11689 | type = "ObjectPattern";
|
11690 | } else if (isRecord) {
|
11691 | type = "RecordExpression";
|
11692 | }
|
11693 | return this.finishNode(node, type);
|
11694 | }
|
11695 | addTrailingCommaExtraToNode(node) {
|
11696 | this.addExtra(node, "trailingComma", this.state.lastTokStart);
|
11697 | this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
|
11698 | }
|
11699 | maybeAsyncOrAccessorProp(prop) {
|
11700 | return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
|
11701 | }
|
11702 | parsePropertyDefinition(refExpressionErrors) {
|
11703 | let decorators = [];
|
11704 | if (this.match(26)) {
|
11705 | if (this.hasPlugin("decorators")) {
|
11706 | this.raise(Errors.UnsupportedPropertyDecorator, {
|
11707 | at: this.state.startLoc
|
11708 | });
|
11709 | }
|
11710 | while (this.match(26)) {
|
11711 | decorators.push(this.parseDecorator());
|
11712 | }
|
11713 | }
|
11714 | const prop = this.startNode();
|
11715 | let isAsync = false;
|
11716 | let isAccessor = false;
|
11717 | let startLoc;
|
11718 | if (this.match(21)) {
|
11719 | if (decorators.length) this.unexpected();
|
11720 | return this.parseSpread();
|
11721 | }
|
11722 | if (decorators.length) {
|
11723 | prop.decorators = decorators;
|
11724 | decorators = [];
|
11725 | }
|
11726 | prop.method = false;
|
11727 | if (refExpressionErrors) {
|
11728 | startLoc = this.state.startLoc;
|
11729 | }
|
11730 | let isGenerator = this.eat(55);
|
11731 | this.parsePropertyNamePrefixOperator(prop);
|
11732 | const containsEsc = this.state.containsEsc;
|
11733 | const key = this.parsePropertyName(prop, refExpressionErrors);
|
11734 | if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
|
11735 | const keyName = key.name;
|
11736 | if (keyName === "async" && !this.hasPrecedingLineBreak()) {
|
11737 | isAsync = true;
|
11738 | this.resetPreviousNodeTrailingComments(key);
|
11739 | isGenerator = this.eat(55);
|
11740 | this.parsePropertyName(prop);
|
11741 | }
|
11742 | if (keyName === "get" || keyName === "set") {
|
11743 | isAccessor = true;
|
11744 | this.resetPreviousNodeTrailingComments(key);
|
11745 | prop.kind = keyName;
|
11746 | if (this.match(55)) {
|
11747 | isGenerator = true;
|
11748 | this.raise(Errors.AccessorIsGenerator, {
|
11749 | at: this.state.curPosition(),
|
11750 | kind: keyName
|
11751 | });
|
11752 | this.next();
|
11753 | }
|
11754 | this.parsePropertyName(prop);
|
11755 | }
|
11756 | }
|
11757 | return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
|
11758 | }
|
11759 | getGetterSetterExpectedParamCount(method) {
|
11760 | return method.kind === "get" ? 0 : 1;
|
11761 | }
|
11762 | getObjectOrClassMethodParams(method) {
|
11763 | return method.params;
|
11764 | }
|
11765 | checkGetterSetterParams(method) {
|
11766 | var _params;
|
11767 | const paramCount = this.getGetterSetterExpectedParamCount(method);
|
11768 | const params = this.getObjectOrClassMethodParams(method);
|
11769 | if (params.length !== paramCount) {
|
11770 | this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
|
11771 | at: method
|
11772 | });
|
11773 | }
|
11774 | if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
|
11775 | this.raise(Errors.BadSetterRestParameter, {
|
11776 | at: method
|
11777 | });
|
11778 | }
|
11779 | }
|
11780 | parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
|
11781 | if (isAccessor) {
|
11782 | const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
|
11783 | this.checkGetterSetterParams(finishedProp);
|
11784 | return finishedProp;
|
11785 | }
|
11786 | if (isAsync || isGenerator || this.match(10)) {
|
11787 | if (isPattern) this.unexpected();
|
11788 | prop.kind = "method";
|
11789 | prop.method = true;
|
11790 | return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
|
11791 | }
|
11792 | }
|
11793 | parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
|
11794 | prop.shorthand = false;
|
11795 | if (this.eat(14)) {
|
11796 | prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
|
11797 | return this.finishNode(prop, "ObjectProperty");
|
11798 | }
|
11799 | if (!prop.computed && prop.key.type === "Identifier") {
|
11800 | this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
|
11801 | if (isPattern) {
|
11802 | prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
|
11803 | } else if (this.match(29)) {
|
11804 | const shorthandAssignLoc = this.state.startLoc;
|
11805 | if (refExpressionErrors != null) {
|
11806 | if (refExpressionErrors.shorthandAssignLoc === null) {
|
11807 | refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
|
11808 | }
|
11809 | } else {
|
11810 | this.raise(Errors.InvalidCoverInitializedName, {
|
11811 | at: shorthandAssignLoc
|
11812 | });
|
11813 | }
|
11814 | prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
|
11815 | } else {
|
11816 | prop.value = cloneIdentifier(prop.key);
|
11817 | }
|
11818 | prop.shorthand = true;
|
11819 | return this.finishNode(prop, "ObjectProperty");
|
11820 | }
|
11821 | }
|
11822 | parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
|
11823 | const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
|
11824 | if (!node) this.unexpected();
|
11825 | return node;
|
11826 | }
|
11827 | parsePropertyName(prop, refExpressionErrors) {
|
11828 | if (this.eat(0)) {
|
11829 | prop.computed = true;
|
11830 | prop.key = this.parseMaybeAssignAllowIn();
|
11831 | this.expect(3);
|
11832 | } else {
|
11833 | const {
|
11834 | type,
|
11835 | value
|
11836 | } = this.state;
|
11837 | let key;
|
11838 | if (tokenIsKeywordOrIdentifier(type)) {
|
11839 | key = this.parseIdentifier(true);
|
11840 | } else {
|
11841 | switch (type) {
|
11842 | case 132:
|
11843 | key = this.parseNumericLiteral(value);
|
11844 | break;
|
11845 | case 131:
|
11846 | key = this.parseStringLiteral(value);
|
11847 | break;
|
11848 | case 133:
|
11849 | key = this.parseBigIntLiteral(value);
|
11850 | break;
|
11851 | case 134:
|
11852 | key = this.parseDecimalLiteral(value);
|
11853 | break;
|
11854 | case 136:
|
11855 | {
|
11856 | const privateKeyLoc = this.state.startLoc;
|
11857 | if (refExpressionErrors != null) {
|
11858 | if (refExpressionErrors.privateKeyLoc === null) {
|
11859 | refExpressionErrors.privateKeyLoc = privateKeyLoc;
|
11860 | }
|
11861 | } else {
|
11862 | this.raise(Errors.UnexpectedPrivateField, {
|
11863 | at: privateKeyLoc
|
11864 | });
|
11865 | }
|
11866 | key = this.parsePrivateName();
|
11867 | break;
|
11868 | }
|
11869 | default:
|
11870 | this.unexpected();
|
11871 | }
|
11872 | }
|
11873 | prop.key = key;
|
11874 | if (type !== 136) {
|
11875 | prop.computed = false;
|
11876 | }
|
11877 | }
|
11878 | return prop.key;
|
11879 | }
|
11880 | initFunction(node, isAsync) {
|
11881 | node.id = null;
|
11882 | node.generator = false;
|
11883 | node.async = isAsync;
|
11884 | }
|
11885 | parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
|
11886 | this.initFunction(node, isAsync);
|
11887 | node.generator = isGenerator;
|
11888 | this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
|
11889 | this.prodParam.enter(functionFlags(isAsync, node.generator));
|
11890 | this.parseFunctionParams(node, isConstructor);
|
11891 | const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
|
11892 | this.prodParam.exit();
|
11893 | this.scope.exit();
|
11894 | return finishedNode;
|
11895 | }
|
11896 | parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
|
11897 | if (isTuple) {
|
11898 | this.expectPlugin("recordAndTuple");
|
11899 | }
|
11900 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
11901 | this.state.inFSharpPipelineDirectBody = false;
|
11902 | const node = this.startNode();
|
11903 | this.next();
|
11904 | node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
|
11905 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
11906 | return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
|
11907 | }
|
11908 | parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
|
11909 | this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
|
11910 | let flags = functionFlags(isAsync, false);
|
11911 | if (!this.match(5) && this.prodParam.hasIn) {
|
11912 | flags |= PARAM_IN;
|
11913 | }
|
11914 | this.prodParam.enter(flags);
|
11915 | this.initFunction(node, isAsync);
|
11916 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
11917 | if (params) {
|
11918 | this.state.maybeInArrowParameters = true;
|
11919 | this.setArrowFunctionParameters(node, params, trailingCommaLoc);
|
11920 | }
|
11921 | this.state.maybeInArrowParameters = false;
|
11922 | this.parseFunctionBody(node, true);
|
11923 | this.prodParam.exit();
|
11924 | this.scope.exit();
|
11925 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
11926 | return this.finishNode(node, "ArrowFunctionExpression");
|
11927 | }
|
11928 | setArrowFunctionParameters(node, params, trailingCommaLoc) {
|
11929 | this.toAssignableList(params, trailingCommaLoc, false);
|
11930 | node.params = params;
|
11931 | }
|
11932 | parseFunctionBodyAndFinish(node, type, isMethod = false) {
|
11933 | this.parseFunctionBody(node, false, isMethod);
|
11934 | return this.finishNode(node, type);
|
11935 | }
|
11936 | parseFunctionBody(node, allowExpression, isMethod = false) {
|
11937 | const isExpression = allowExpression && !this.match(5);
|
11938 | this.expressionScope.enter(newExpressionScope());
|
11939 | if (isExpression) {
|
11940 | node.body = this.parseMaybeAssign();
|
11941 | this.checkParams(node, false, allowExpression, false);
|
11942 | } else {
|
11943 | const oldStrict = this.state.strict;
|
11944 | const oldLabels = this.state.labels;
|
11945 | this.state.labels = [];
|
11946 | this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
|
11947 | node.body = this.parseBlock(true, false, hasStrictModeDirective => {
|
11948 | const nonSimple = !this.isSimpleParamList(node.params);
|
11949 | if (hasStrictModeDirective && nonSimple) {
|
11950 | this.raise(Errors.IllegalLanguageModeDirective, {
|
11951 | at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
|
11952 | });
|
11953 | }
|
11954 | const strictModeChanged = !oldStrict && this.state.strict;
|
11955 | this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
|
11956 | if (this.state.strict && node.id) {
|
11957 | this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
|
11958 | }
|
11959 | });
|
11960 | this.prodParam.exit();
|
11961 | this.state.labels = oldLabels;
|
11962 | }
|
11963 | this.expressionScope.exit();
|
11964 | }
|
11965 | isSimpleParameter(node) {
|
11966 | return node.type === "Identifier";
|
11967 | }
|
11968 | isSimpleParamList(params) {
|
11969 | for (let i = 0, len = params.length; i < len; i++) {
|
11970 | if (!this.isSimpleParameter(params[i])) return false;
|
11971 | }
|
11972 | return true;
|
11973 | }
|
11974 | checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
|
11975 | const checkClashes = !allowDuplicates && new Set();
|
11976 | const formalParameters = {
|
11977 | type: "FormalParameters"
|
11978 | };
|
11979 | for (const param of node.params) {
|
11980 | this.checkLVal(param, {
|
11981 | in: formalParameters,
|
11982 | binding: BIND_VAR,
|
11983 | checkClashes,
|
11984 | strictModeChanged
|
11985 | });
|
11986 | }
|
11987 | }
|
11988 | parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
|
11989 | const elts = [];
|
11990 | let first = true;
|
11991 | while (!this.eat(close)) {
|
11992 | if (first) {
|
11993 | first = false;
|
11994 | } else {
|
11995 | this.expect(12);
|
11996 | if (this.match(close)) {
|
11997 | if (nodeForExtra) {
|
11998 | this.addTrailingCommaExtraToNode(nodeForExtra);
|
11999 | }
|
12000 | this.next();
|
12001 | break;
|
12002 | }
|
12003 | }
|
12004 | elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
|
12005 | }
|
12006 | return elts;
|
12007 | }
|
12008 | parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
|
12009 | let elt;
|
12010 | if (this.match(12)) {
|
12011 | if (!allowEmpty) {
|
12012 | this.raise(Errors.UnexpectedToken, {
|
12013 | at: this.state.curPosition(),
|
12014 | unexpected: ","
|
12015 | });
|
12016 | }
|
12017 | elt = null;
|
12018 | } else if (this.match(21)) {
|
12019 | const spreadNodeStartLoc = this.state.startLoc;
|
12020 | elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
|
12021 | } else if (this.match(17)) {
|
12022 | this.expectPlugin("partialApplication");
|
12023 | if (!allowPlaceholder) {
|
12024 | this.raise(Errors.UnexpectedArgumentPlaceholder, {
|
12025 | at: this.state.startLoc
|
12026 | });
|
12027 | }
|
12028 | const node = this.startNode();
|
12029 | this.next();
|
12030 | elt = this.finishNode(node, "ArgumentPlaceholder");
|
12031 | } else {
|
12032 | elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
|
12033 | }
|
12034 | return elt;
|
12035 | }
|
12036 | parseIdentifier(liberal) {
|
12037 | const node = this.startNode();
|
12038 | const name = this.parseIdentifierName(liberal);
|
12039 | return this.createIdentifier(node, name);
|
12040 | }
|
12041 | createIdentifier(node, name) {
|
12042 | node.name = name;
|
12043 | node.loc.identifierName = name;
|
12044 | return this.finishNode(node, "Identifier");
|
12045 | }
|
12046 | parseIdentifierName(liberal) {
|
12047 | let name;
|
12048 | const {
|
12049 | startLoc,
|
12050 | type
|
12051 | } = this.state;
|
12052 | if (tokenIsKeywordOrIdentifier(type)) {
|
12053 | name = this.state.value;
|
12054 | } else {
|
12055 | this.unexpected();
|
12056 | }
|
12057 | const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
|
12058 | if (liberal) {
|
12059 | if (tokenIsKeyword) {
|
12060 | this.replaceToken(130);
|
12061 | }
|
12062 | } else {
|
12063 | this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
|
12064 | }
|
12065 | this.next();
|
12066 | return name;
|
12067 | }
|
12068 | checkReservedWord(word, startLoc, checkKeywords, isBinding) {
|
12069 | if (word.length > 10) {
|
12070 | return;
|
12071 | }
|
12072 | if (!canBeReservedWord(word)) {
|
12073 | return;
|
12074 | }
|
12075 | if (checkKeywords && isKeyword(word)) {
|
12076 | this.raise(Errors.UnexpectedKeyword, {
|
12077 | at: startLoc,
|
12078 | keyword: word
|
12079 | });
|
12080 | return;
|
12081 | }
|
12082 | const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
|
12083 | if (reservedTest(word, this.inModule)) {
|
12084 | this.raise(Errors.UnexpectedReservedWord, {
|
12085 | at: startLoc,
|
12086 | reservedWord: word
|
12087 | });
|
12088 | return;
|
12089 | } else if (word === "yield") {
|
12090 | if (this.prodParam.hasYield) {
|
12091 | this.raise(Errors.YieldBindingIdentifier, {
|
12092 | at: startLoc
|
12093 | });
|
12094 | return;
|
12095 | }
|
12096 | } else if (word === "await") {
|
12097 | if (this.prodParam.hasAwait) {
|
12098 | this.raise(Errors.AwaitBindingIdentifier, {
|
12099 | at: startLoc
|
12100 | });
|
12101 | return;
|
12102 | }
|
12103 | if (this.scope.inStaticBlock) {
|
12104 | this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
|
12105 | at: startLoc
|
12106 | });
|
12107 | return;
|
12108 | }
|
12109 | this.expressionScope.recordAsyncArrowParametersError({
|
12110 | at: startLoc
|
12111 | });
|
12112 | } else if (word === "arguments") {
|
12113 | if (this.scope.inClassAndNotInNonArrowFunction) {
|
12114 | this.raise(Errors.ArgumentsInClass, {
|
12115 | at: startLoc
|
12116 | });
|
12117 | return;
|
12118 | }
|
12119 | }
|
12120 | }
|
12121 | isAwaitAllowed() {
|
12122 | if (this.prodParam.hasAwait) return true;
|
12123 | if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
|
12124 | return true;
|
12125 | }
|
12126 | return false;
|
12127 | }
|
12128 | parseAwait(startLoc) {
|
12129 | const node = this.startNodeAt(startLoc);
|
12130 | this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
|
12131 | at: node
|
12132 | });
|
12133 | if (this.eat(55)) {
|
12134 | this.raise(Errors.ObsoleteAwaitStar, {
|
12135 | at: node
|
12136 | });
|
12137 | }
|
12138 | if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
|
12139 | if (this.isAmbiguousAwait()) {
|
12140 | this.ambiguousScriptDifferentAst = true;
|
12141 | } else {
|
12142 | this.sawUnambiguousESM = true;
|
12143 | }
|
12144 | }
|
12145 | if (!this.state.soloAwait) {
|
12146 | node.argument = this.parseMaybeUnary(null, true);
|
12147 | }
|
12148 | return this.finishNode(node, "AwaitExpression");
|
12149 | }
|
12150 | isAmbiguousAwait() {
|
12151 | if (this.hasPrecedingLineBreak()) return true;
|
12152 | const {
|
12153 | type
|
12154 | } = this.state;
|
12155 | return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
|
12156 | }
|
12157 | parseYield() {
|
12158 | const node = this.startNode();
|
12159 | this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
|
12160 | at: node
|
12161 | });
|
12162 | this.next();
|
12163 | let delegating = false;
|
12164 | let argument = null;
|
12165 | if (!this.hasPrecedingLineBreak()) {
|
12166 | delegating = this.eat(55);
|
12167 | switch (this.state.type) {
|
12168 | case 13:
|
12169 | case 137:
|
12170 | case 8:
|
12171 | case 11:
|
12172 | case 3:
|
12173 | case 9:
|
12174 | case 14:
|
12175 | case 12:
|
12176 | if (!delegating) break;
|
12177 | default:
|
12178 | argument = this.parseMaybeAssign();
|
12179 | }
|
12180 | }
|
12181 | node.delegate = delegating;
|
12182 | node.argument = argument;
|
12183 | return this.finishNode(node, "YieldExpression");
|
12184 | }
|
12185 | checkPipelineAtInfixOperator(left, leftStartLoc) {
|
12186 | if (this.hasPlugin(["pipelineOperator", {
|
12187 | proposal: "smart"
|
12188 | }])) {
|
12189 | if (left.type === "SequenceExpression") {
|
12190 | this.raise(Errors.PipelineHeadSequenceExpression, {
|
12191 | at: leftStartLoc
|
12192 | });
|
12193 | }
|
12194 | }
|
12195 | }
|
12196 | parseSmartPipelineBodyInStyle(childExpr, startLoc) {
|
12197 | if (this.isSimpleReference(childExpr)) {
|
12198 | const bodyNode = this.startNodeAt(startLoc);
|
12199 | bodyNode.callee = childExpr;
|
12200 | return this.finishNode(bodyNode, "PipelineBareFunction");
|
12201 | } else {
|
12202 | const bodyNode = this.startNodeAt(startLoc);
|
12203 | this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
|
12204 | bodyNode.expression = childExpr;
|
12205 | return this.finishNode(bodyNode, "PipelineTopicExpression");
|
12206 | }
|
12207 | }
|
12208 | isSimpleReference(expression) {
|
12209 | switch (expression.type) {
|
12210 | case "MemberExpression":
|
12211 | return !expression.computed && this.isSimpleReference(expression.object);
|
12212 | case "Identifier":
|
12213 | return true;
|
12214 | default:
|
12215 | return false;
|
12216 | }
|
12217 | }
|
12218 | checkSmartPipeTopicBodyEarlyErrors(startLoc) {
|
12219 | if (this.match(19)) {
|
12220 | throw this.raise(Errors.PipelineBodyNoArrow, {
|
12221 | at: this.state.startLoc
|
12222 | });
|
12223 | }
|
12224 | if (!this.topicReferenceWasUsedInCurrentContext()) {
|
12225 | this.raise(Errors.PipelineTopicUnused, {
|
12226 | at: startLoc
|
12227 | });
|
12228 | }
|
12229 | }
|
12230 | withTopicBindingContext(callback) {
|
12231 | const outerContextTopicState = this.state.topicContext;
|
12232 | this.state.topicContext = {
|
12233 | maxNumOfResolvableTopics: 1,
|
12234 | maxTopicIndex: null
|
12235 | };
|
12236 | try {
|
12237 | return callback();
|
12238 | } finally {
|
12239 | this.state.topicContext = outerContextTopicState;
|
12240 | }
|
12241 | }
|
12242 | withSmartMixTopicForbiddingContext(callback) {
|
12243 | if (this.hasPlugin(["pipelineOperator", {
|
12244 | proposal: "smart"
|
12245 | }])) {
|
12246 | const outerContextTopicState = this.state.topicContext;
|
12247 | this.state.topicContext = {
|
12248 | maxNumOfResolvableTopics: 0,
|
12249 | maxTopicIndex: null
|
12250 | };
|
12251 | try {
|
12252 | return callback();
|
12253 | } finally {
|
12254 | this.state.topicContext = outerContextTopicState;
|
12255 | }
|
12256 | } else {
|
12257 | return callback();
|
12258 | }
|
12259 | }
|
12260 | withSoloAwaitPermittingContext(callback) {
|
12261 | const outerContextSoloAwaitState = this.state.soloAwait;
|
12262 | this.state.soloAwait = true;
|
12263 | try {
|
12264 | return callback();
|
12265 | } finally {
|
12266 | this.state.soloAwait = outerContextSoloAwaitState;
|
12267 | }
|
12268 | }
|
12269 | allowInAnd(callback) {
|
12270 | const flags = this.prodParam.currentFlags();
|
12271 | const prodParamToSet = PARAM_IN & ~flags;
|
12272 | if (prodParamToSet) {
|
12273 | this.prodParam.enter(flags | PARAM_IN);
|
12274 | try {
|
12275 | return callback();
|
12276 | } finally {
|
12277 | this.prodParam.exit();
|
12278 | }
|
12279 | }
|
12280 | return callback();
|
12281 | }
|
12282 | disallowInAnd(callback) {
|
12283 | const flags = this.prodParam.currentFlags();
|
12284 | const prodParamToClear = PARAM_IN & flags;
|
12285 | if (prodParamToClear) {
|
12286 | this.prodParam.enter(flags & ~PARAM_IN);
|
12287 | try {
|
12288 | return callback();
|
12289 | } finally {
|
12290 | this.prodParam.exit();
|
12291 | }
|
12292 | }
|
12293 | return callback();
|
12294 | }
|
12295 | registerTopicReference() {
|
12296 | this.state.topicContext.maxTopicIndex = 0;
|
12297 | }
|
12298 | topicReferenceIsAllowedInCurrentContext() {
|
12299 | return this.state.topicContext.maxNumOfResolvableTopics >= 1;
|
12300 | }
|
12301 | topicReferenceWasUsedInCurrentContext() {
|
12302 | return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
|
12303 | }
|
12304 | parseFSharpPipelineBody(prec) {
|
12305 | const startLoc = this.state.startLoc;
|
12306 | this.state.potentialArrowAt = this.state.start;
|
12307 | const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
|
12308 | this.state.inFSharpPipelineDirectBody = true;
|
12309 | const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
|
12310 | this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
12311 | return ret;
|
12312 | }
|
12313 | parseModuleExpression() {
|
12314 | this.expectPlugin("moduleBlocks");
|
12315 | const node = this.startNode();
|
12316 | this.next();
|
12317 | if (!this.match(5)) {
|
12318 | this.unexpected(null, 5);
|
12319 | }
|
12320 | const program = this.startNodeAt(this.state.endLoc);
|
12321 | this.next();
|
12322 | const revertScopes = this.initializeScopes(true);
|
12323 | this.enterInitialScopes();
|
12324 | try {
|
12325 | node.body = this.parseProgram(program, 8, "module");
|
12326 | } finally {
|
12327 | revertScopes();
|
12328 | }
|
12329 | return this.finishNode(node, "ModuleExpression");
|
12330 | }
|
12331 | parsePropertyNamePrefixOperator(prop) {}
|
12332 | }
|
12333 | const loopLabel = {
|
12334 | kind: "loop"
|
12335 | },
|
12336 | switchLabel = {
|
12337 | kind: "switch"
|
12338 | };
|
12339 | const loneSurrogate = /[\uD800-\uDFFF]/u;
|
12340 | const keywordRelationalOperator = /in(?:stanceof)?/y;
|
12341 | function babel7CompatTokens(tokens, input) {
|
12342 | for (let i = 0; i < tokens.length; i++) {
|
12343 | const token = tokens[i];
|
12344 | const {
|
12345 | type
|
12346 | } = token;
|
12347 | if (typeof type === "number") {
|
12348 | {
|
12349 | if (type === 136) {
|
12350 | const {
|
12351 | loc,
|
12352 | start,
|
12353 | value,
|
12354 | end
|
12355 | } = token;
|
12356 | const hashEndPos = start + 1;
|
12357 | const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
|
12358 | tokens.splice(i, 1, new Token({
|
12359 | type: getExportedToken(27),
|
12360 | value: "#",
|
12361 | start: start,
|
12362 | end: hashEndPos,
|
12363 | startLoc: loc.start,
|
12364 | endLoc: hashEndLoc
|
12365 | }), new Token({
|
12366 | type: getExportedToken(130),
|
12367 | value: value,
|
12368 | start: hashEndPos,
|
12369 | end: end,
|
12370 | startLoc: hashEndLoc,
|
12371 | endLoc: loc.end
|
12372 | }));
|
12373 | i++;
|
12374 | continue;
|
12375 | }
|
12376 | if (tokenIsTemplate(type)) {
|
12377 | const {
|
12378 | loc,
|
12379 | start,
|
12380 | value,
|
12381 | end
|
12382 | } = token;
|
12383 | const backquoteEnd = start + 1;
|
12384 | const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
|
12385 | let startToken;
|
12386 | if (input.charCodeAt(start) === 96) {
|
12387 | startToken = new Token({
|
12388 | type: getExportedToken(22),
|
12389 | value: "`",
|
12390 | start: start,
|
12391 | end: backquoteEnd,
|
12392 | startLoc: loc.start,
|
12393 | endLoc: backquoteEndLoc
|
12394 | });
|
12395 | } else {
|
12396 | startToken = new Token({
|
12397 | type: getExportedToken(8),
|
12398 | value: "}",
|
12399 | start: start,
|
12400 | end: backquoteEnd,
|
12401 | startLoc: loc.start,
|
12402 | endLoc: backquoteEndLoc
|
12403 | });
|
12404 | }
|
12405 | let templateValue, templateElementEnd, templateElementEndLoc, endToken;
|
12406 | if (type === 24) {
|
12407 | templateElementEnd = end - 1;
|
12408 | templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
|
12409 | templateValue = value === null ? null : value.slice(1, -1);
|
12410 | endToken = new Token({
|
12411 | type: getExportedToken(22),
|
12412 | value: "`",
|
12413 | start: templateElementEnd,
|
12414 | end: end,
|
12415 | startLoc: templateElementEndLoc,
|
12416 | endLoc: loc.end
|
12417 | });
|
12418 | } else {
|
12419 | templateElementEnd = end - 2;
|
12420 | templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
|
12421 | templateValue = value === null ? null : value.slice(1, -2);
|
12422 | endToken = new Token({
|
12423 | type: getExportedToken(23),
|
12424 | value: "${",
|
12425 | start: templateElementEnd,
|
12426 | end: end,
|
12427 | startLoc: templateElementEndLoc,
|
12428 | endLoc: loc.end
|
12429 | });
|
12430 | }
|
12431 | tokens.splice(i, 1, startToken, new Token({
|
12432 | type: getExportedToken(20),
|
12433 | value: templateValue,
|
12434 | start: backquoteEnd,
|
12435 | end: templateElementEnd,
|
12436 | startLoc: backquoteEndLoc,
|
12437 | endLoc: templateElementEndLoc
|
12438 | }), endToken);
|
12439 | i += 2;
|
12440 | continue;
|
12441 | }
|
12442 | }
|
12443 | token.type = getExportedToken(type);
|
12444 | }
|
12445 | }
|
12446 | return tokens;
|
12447 | }
|
12448 | class StatementParser extends ExpressionParser {
|
12449 | parseTopLevel(file, program) {
|
12450 | file.program = this.parseProgram(program);
|
12451 | file.comments = this.state.comments;
|
12452 | if (this.options.tokens) {
|
12453 | file.tokens = babel7CompatTokens(this.tokens, this.input);
|
12454 | }
|
12455 | return this.finishNode(file, "File");
|
12456 | }
|
12457 | parseProgram(program, end = 137, sourceType = this.options.sourceType) {
|
12458 | program.sourceType = sourceType;
|
12459 | program.interpreter = this.parseInterpreterDirective();
|
12460 | this.parseBlockBody(program, true, true, end);
|
12461 | if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
|
12462 | for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
|
12463 | this.raise(Errors.ModuleExportUndefined, {
|
12464 | at,
|
12465 | localName
|
12466 | });
|
12467 | }
|
12468 | }
|
12469 | let finishedProgram;
|
12470 | if (end === 137) {
|
12471 | finishedProgram = this.finishNode(program, "Program");
|
12472 | } else {
|
12473 | finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
|
12474 | }
|
12475 | return finishedProgram;
|
12476 | }
|
12477 | stmtToDirective(stmt) {
|
12478 | const directive = stmt;
|
12479 | directive.type = "Directive";
|
12480 | directive.value = directive.expression;
|
12481 | delete directive.expression;
|
12482 | const directiveLiteral = directive.value;
|
12483 | const expressionValue = directiveLiteral.value;
|
12484 | const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
|
12485 | const val = directiveLiteral.value = raw.slice(1, -1);
|
12486 | this.addExtra(directiveLiteral, "raw", raw);
|
12487 | this.addExtra(directiveLiteral, "rawValue", val);
|
12488 | this.addExtra(directiveLiteral, "expressionValue", expressionValue);
|
12489 | directiveLiteral.type = "DirectiveLiteral";
|
12490 | return directive;
|
12491 | }
|
12492 | parseInterpreterDirective() {
|
12493 | if (!this.match(28)) {
|
12494 | return null;
|
12495 | }
|
12496 | const node = this.startNode();
|
12497 | node.value = this.state.value;
|
12498 | this.next();
|
12499 | return this.finishNode(node, "InterpreterDirective");
|
12500 | }
|
12501 | isLet() {
|
12502 | if (!this.isContextual(99)) {
|
12503 | return false;
|
12504 | }
|
12505 | return this.hasFollowingBindingAtom();
|
12506 | }
|
12507 | chStartsBindingIdentifier(ch, pos) {
|
12508 | if (isIdentifierStart(ch)) {
|
12509 | keywordRelationalOperator.lastIndex = pos;
|
12510 | if (keywordRelationalOperator.test(this.input)) {
|
12511 | const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
|
12512 | if (!isIdentifierChar(endCh) && endCh !== 92) {
|
12513 | return false;
|
12514 | }
|
12515 | }
|
12516 | return true;
|
12517 | } else if (ch === 92) {
|
12518 | return true;
|
12519 | } else {
|
12520 | return false;
|
12521 | }
|
12522 | }
|
12523 | chStartsBindingPattern(ch) {
|
12524 | return ch === 91 || ch === 123;
|
12525 | }
|
12526 | hasFollowingBindingAtom() {
|
12527 | const next = this.nextTokenStart();
|
12528 | const nextCh = this.codePointAtPos(next);
|
12529 | return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
|
12530 | }
|
12531 | hasInLineFollowingBindingIdentifier() {
|
12532 | const next = this.nextTokenInLineStart();
|
12533 | const nextCh = this.codePointAtPos(next);
|
12534 | return this.chStartsBindingIdentifier(nextCh, next);
|
12535 | }
|
12536 | startsUsingForOf() {
|
12537 | const {
|
12538 | type,
|
12539 | containsEsc
|
12540 | } = this.lookahead();
|
12541 | if (type === 101 && !containsEsc) {
|
12542 | return false;
|
12543 | } else if (tokenIsIdentifier(type) && !this.hasFollowingLineBreak()) {
|
12544 | this.expectPlugin("explicitResourceManagement");
|
12545 | return true;
|
12546 | }
|
12547 | }
|
12548 | startsAwaitUsing() {
|
12549 | let next = this.nextTokenInLineStart();
|
12550 | if (this.isUnparsedContextual(next, "using")) {
|
12551 | next = this.nextTokenInLineStartSince(next + 5);
|
12552 | const nextCh = this.codePointAtPos(next);
|
12553 | if (this.chStartsBindingIdentifier(nextCh, next)) {
|
12554 | this.expectPlugin("explicitResourceManagement");
|
12555 | return true;
|
12556 | }
|
12557 | }
|
12558 | return false;
|
12559 | }
|
12560 | parseModuleItem() {
|
12561 | return this.parseStatementLike(1 | 2 | 4 | 8);
|
12562 | }
|
12563 | parseStatementListItem() {
|
12564 | return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
|
12565 | }
|
12566 | parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
|
12567 | let flags = 0;
|
12568 | if (this.options.annexB && !this.state.strict) {
|
12569 | flags |= 4;
|
12570 | if (allowLabeledFunction) {
|
12571 | flags |= 8;
|
12572 | }
|
12573 | }
|
12574 | return this.parseStatementLike(flags);
|
12575 | }
|
12576 | parseStatement() {
|
12577 | return this.parseStatementLike(0);
|
12578 | }
|
12579 | parseStatementLike(flags) {
|
12580 | let decorators = null;
|
12581 | if (this.match(26)) {
|
12582 | decorators = this.parseDecorators(true);
|
12583 | }
|
12584 | return this.parseStatementContent(flags, decorators);
|
12585 | }
|
12586 | parseStatementContent(flags, decorators) {
|
12587 | const starttype = this.state.type;
|
12588 | const node = this.startNode();
|
12589 | const allowDeclaration = !!(flags & 2);
|
12590 | const allowFunctionDeclaration = !!(flags & 4);
|
12591 | const topLevel = flags & 1;
|
12592 | switch (starttype) {
|
12593 | case 60:
|
12594 | return this.parseBreakContinueStatement(node, true);
|
12595 | case 63:
|
12596 | return this.parseBreakContinueStatement(node, false);
|
12597 | case 64:
|
12598 | return this.parseDebuggerStatement(node);
|
12599 | case 90:
|
12600 | return this.parseDoWhileStatement(node);
|
12601 | case 91:
|
12602 | return this.parseForStatement(node);
|
12603 | case 68:
|
12604 | if (this.lookaheadCharCode() === 46) break;
|
12605 | if (!allowFunctionDeclaration) {
|
12606 | this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, {
|
12607 | at: this.state.startLoc
|
12608 | });
|
12609 | }
|
12610 | return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
|
12611 | case 80:
|
12612 | if (!allowDeclaration) this.unexpected();
|
12613 | return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
|
12614 | case 69:
|
12615 | return this.parseIfStatement(node);
|
12616 | case 70:
|
12617 | return this.parseReturnStatement(node);
|
12618 | case 71:
|
12619 | return this.parseSwitchStatement(node);
|
12620 | case 72:
|
12621 | return this.parseThrowStatement(node);
|
12622 | case 73:
|
12623 | return this.parseTryStatement(node);
|
12624 | case 96:
|
12625 | if (!this.state.containsEsc && this.startsAwaitUsing()) {
|
12626 | if (!this.isAwaitAllowed()) {
|
12627 | this.raise(Errors.AwaitUsingNotInAsyncContext, {
|
12628 | at: node
|
12629 | });
|
12630 | } else if (!allowDeclaration) {
|
12631 | this.raise(Errors.UnexpectedLexicalDeclaration, {
|
12632 | at: node
|
12633 | });
|
12634 | }
|
12635 | this.next();
|
12636 | return this.parseVarStatement(node, "await using");
|
12637 | }
|
12638 | break;
|
12639 | case 105:
|
12640 | if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifier()) {
|
12641 | break;
|
12642 | }
|
12643 | this.expectPlugin("explicitResourceManagement");
|
12644 | if (!this.scope.inModule && this.scope.inTopLevel) {
|
12645 | this.raise(Errors.UnexpectedUsingDeclaration, {
|
12646 | at: this.state.startLoc
|
12647 | });
|
12648 | } else if (!allowDeclaration) {
|
12649 | this.raise(Errors.UnexpectedLexicalDeclaration, {
|
12650 | at: this.state.startLoc
|
12651 | });
|
12652 | }
|
12653 | return this.parseVarStatement(node, "using");
|
12654 | case 99:
|
12655 | {
|
12656 | if (this.state.containsEsc) {
|
12657 | break;
|
12658 | }
|
12659 | const next = this.nextTokenStart();
|
12660 | const nextCh = this.codePointAtPos(next);
|
12661 | if (nextCh !== 91) {
|
12662 | if (!allowDeclaration && this.hasFollowingLineBreak()) break;
|
12663 | if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
|
12664 | break;
|
12665 | }
|
12666 | }
|
12667 | }
|
12668 | case 75:
|
12669 | {
|
12670 | if (!allowDeclaration) {
|
12671 | this.raise(Errors.UnexpectedLexicalDeclaration, {
|
12672 | at: this.state.startLoc
|
12673 | });
|
12674 | }
|
12675 | }
|
12676 | case 74:
|
12677 | {
|
12678 | const kind = this.state.value;
|
12679 | return this.parseVarStatement(node, kind);
|
12680 | }
|
12681 | case 92:
|
12682 | return this.parseWhileStatement(node);
|
12683 | case 76:
|
12684 | return this.parseWithStatement(node);
|
12685 | case 5:
|
12686 | return this.parseBlock();
|
12687 | case 13:
|
12688 | return this.parseEmptyStatement(node);
|
12689 | case 83:
|
12690 | {
|
12691 | const nextTokenCharCode = this.lookaheadCharCode();
|
12692 | if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
|
12693 | break;
|
12694 | }
|
12695 | }
|
12696 | case 82:
|
12697 | {
|
12698 | if (!this.options.allowImportExportEverywhere && !topLevel) {
|
12699 | this.raise(Errors.UnexpectedImportExport, {
|
12700 | at: this.state.startLoc
|
12701 | });
|
12702 | }
|
12703 | this.next();
|
12704 | let result;
|
12705 | if (starttype === 83) {
|
12706 | result = this.parseImport(node);
|
12707 | if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
|
12708 | this.sawUnambiguousESM = true;
|
12709 | }
|
12710 | } else {
|
12711 | result = this.parseExport(node, decorators);
|
12712 | if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
|
12713 | this.sawUnambiguousESM = true;
|
12714 | }
|
12715 | }
|
12716 | this.assertModuleNodeAllowed(result);
|
12717 | return result;
|
12718 | }
|
12719 | default:
|
12720 | {
|
12721 | if (this.isAsyncFunction()) {
|
12722 | if (!allowDeclaration) {
|
12723 | this.raise(Errors.AsyncFunctionInSingleStatementContext, {
|
12724 | at: this.state.startLoc
|
12725 | });
|
12726 | }
|
12727 | this.next();
|
12728 | return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
|
12729 | }
|
12730 | }
|
12731 | }
|
12732 | const maybeName = this.state.value;
|
12733 | const expr = this.parseExpression();
|
12734 | if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
|
12735 | return this.parseLabeledStatement(node, maybeName, expr, flags);
|
12736 | } else {
|
12737 | return this.parseExpressionStatement(node, expr, decorators);
|
12738 | }
|
12739 | }
|
12740 | assertModuleNodeAllowed(node) {
|
12741 | if (!this.options.allowImportExportEverywhere && !this.inModule) {
|
12742 | this.raise(Errors.ImportOutsideModule, {
|
12743 | at: node
|
12744 | });
|
12745 | }
|
12746 | }
|
12747 | decoratorsEnabledBeforeExport() {
|
12748 | if (this.hasPlugin("decorators-legacy")) return true;
|
12749 | return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
|
12750 | }
|
12751 | maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
|
12752 | if (maybeDecorators) {
|
12753 | if (classNode.decorators && classNode.decorators.length > 0) {
|
12754 | if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
|
12755 | this.raise(Errors.DecoratorsBeforeAfterExport, {
|
12756 | at: classNode.decorators[0]
|
12757 | });
|
12758 | }
|
12759 | classNode.decorators.unshift(...maybeDecorators);
|
12760 | } else {
|
12761 | classNode.decorators = maybeDecorators;
|
12762 | }
|
12763 | this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
|
12764 | if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
|
12765 | }
|
12766 | return classNode;
|
12767 | }
|
12768 | canHaveLeadingDecorator() {
|
12769 | return this.match(80);
|
12770 | }
|
12771 | parseDecorators(allowExport) {
|
12772 | const decorators = [];
|
12773 | do {
|
12774 | decorators.push(this.parseDecorator());
|
12775 | } while (this.match(26));
|
12776 | if (this.match(82)) {
|
12777 | if (!allowExport) {
|
12778 | this.unexpected();
|
12779 | }
|
12780 | if (!this.decoratorsEnabledBeforeExport()) {
|
12781 | this.raise(Errors.DecoratorExportClass, {
|
12782 | at: this.state.startLoc
|
12783 | });
|
12784 | }
|
12785 | } else if (!this.canHaveLeadingDecorator()) {
|
12786 | throw this.raise(Errors.UnexpectedLeadingDecorator, {
|
12787 | at: this.state.startLoc
|
12788 | });
|
12789 | }
|
12790 | return decorators;
|
12791 | }
|
12792 | parseDecorator() {
|
12793 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
12794 | const node = this.startNode();
|
12795 | this.next();
|
12796 | if (this.hasPlugin("decorators")) {
|
12797 | const startLoc = this.state.startLoc;
|
12798 | let expr;
|
12799 | if (this.match(10)) {
|
12800 | const startLoc = this.state.startLoc;
|
12801 | this.next();
|
12802 | expr = this.parseExpression();
|
12803 | this.expect(11);
|
12804 | expr = this.wrapParenthesis(startLoc, expr);
|
12805 | const paramsStartLoc = this.state.startLoc;
|
12806 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
12807 | if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
|
12808 | this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
|
12809 | at: paramsStartLoc
|
12810 | });
|
12811 | }
|
12812 | } else {
|
12813 | expr = this.parseIdentifier(false);
|
12814 | while (this.eat(16)) {
|
12815 | const node = this.startNodeAt(startLoc);
|
12816 | node.object = expr;
|
12817 | if (this.match(136)) {
|
12818 | this.classScope.usePrivateName(this.state.value, this.state.startLoc);
|
12819 | node.property = this.parsePrivateName();
|
12820 | } else {
|
12821 | node.property = this.parseIdentifier(true);
|
12822 | }
|
12823 | node.computed = false;
|
12824 | expr = this.finishNode(node, "MemberExpression");
|
12825 | }
|
12826 | node.expression = this.parseMaybeDecoratorArguments(expr);
|
12827 | }
|
12828 | } else {
|
12829 | node.expression = this.parseExprSubscripts();
|
12830 | }
|
12831 | return this.finishNode(node, "Decorator");
|
12832 | }
|
12833 | parseMaybeDecoratorArguments(expr) {
|
12834 | if (this.eat(10)) {
|
12835 | const node = this.startNodeAtNode(expr);
|
12836 | node.callee = expr;
|
12837 | node.arguments = this.parseCallExpressionArguments(11, false);
|
12838 | this.toReferencedList(node.arguments);
|
12839 | return this.finishNode(node, "CallExpression");
|
12840 | }
|
12841 | return expr;
|
12842 | }
|
12843 | parseBreakContinueStatement(node, isBreak) {
|
12844 | this.next();
|
12845 | if (this.isLineTerminator()) {
|
12846 | node.label = null;
|
12847 | } else {
|
12848 | node.label = this.parseIdentifier();
|
12849 | this.semicolon();
|
12850 | }
|
12851 | this.verifyBreakContinue(node, isBreak);
|
12852 | return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
12853 | }
|
12854 | verifyBreakContinue(node, isBreak) {
|
12855 | let i;
|
12856 | for (i = 0; i < this.state.labels.length; ++i) {
|
12857 | const lab = this.state.labels[i];
|
12858 | if (node.label == null || lab.name === node.label.name) {
|
12859 | if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
|
12860 | if (node.label && isBreak) break;
|
12861 | }
|
12862 | }
|
12863 | if (i === this.state.labels.length) {
|
12864 | const type = isBreak ? "BreakStatement" : "ContinueStatement";
|
12865 | this.raise(Errors.IllegalBreakContinue, {
|
12866 | at: node,
|
12867 | type
|
12868 | });
|
12869 | }
|
12870 | }
|
12871 | parseDebuggerStatement(node) {
|
12872 | this.next();
|
12873 | this.semicolon();
|
12874 | return this.finishNode(node, "DebuggerStatement");
|
12875 | }
|
12876 | parseHeaderExpression() {
|
12877 | this.expect(10);
|
12878 | const val = this.parseExpression();
|
12879 | this.expect(11);
|
12880 | return val;
|
12881 | }
|
12882 | parseDoWhileStatement(node) {
|
12883 | this.next();
|
12884 | this.state.labels.push(loopLabel);
|
12885 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
12886 | this.state.labels.pop();
|
12887 | this.expect(92);
|
12888 | node.test = this.parseHeaderExpression();
|
12889 | this.eat(13);
|
12890 | return this.finishNode(node, "DoWhileStatement");
|
12891 | }
|
12892 | parseForStatement(node) {
|
12893 | this.next();
|
12894 | this.state.labels.push(loopLabel);
|
12895 | let awaitAt = null;
|
12896 | if (this.isAwaitAllowed() && this.eatContextual(96)) {
|
12897 | awaitAt = this.state.lastTokStartLoc;
|
12898 | }
|
12899 | this.scope.enter(SCOPE_OTHER);
|
12900 | this.expect(10);
|
12901 | if (this.match(13)) {
|
12902 | if (awaitAt !== null) {
|
12903 | this.unexpected(awaitAt);
|
12904 | }
|
12905 | return this.parseFor(node, null);
|
12906 | }
|
12907 | const startsWithLet = this.isContextual(99);
|
12908 | {
|
12909 | const startsWithAwaitUsing = this.isContextual(96) && this.startsAwaitUsing();
|
12910 | const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(105) && this.startsUsingForOf();
|
12911 | const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
|
12912 | if (this.match(74) || this.match(75) || isLetOrUsing) {
|
12913 | const initNode = this.startNode();
|
12914 | let kind;
|
12915 | if (startsWithAwaitUsing) {
|
12916 | kind = "await using";
|
12917 | if (!this.isAwaitAllowed()) {
|
12918 | this.raise(Errors.AwaitUsingNotInAsyncContext, {
|
12919 | at: this.state.startLoc
|
12920 | });
|
12921 | }
|
12922 | this.next();
|
12923 | } else {
|
12924 | kind = this.state.value;
|
12925 | }
|
12926 | this.next();
|
12927 | this.parseVar(initNode, true, kind);
|
12928 | const init = this.finishNode(initNode, "VariableDeclaration");
|
12929 | const isForIn = this.match(58);
|
12930 | if (isForIn && starsWithUsingDeclaration) {
|
12931 | this.raise(Errors.ForInUsing, {
|
12932 | at: init
|
12933 | });
|
12934 | }
|
12935 | if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
|
12936 | return this.parseForIn(node, init, awaitAt);
|
12937 | }
|
12938 | if (awaitAt !== null) {
|
12939 | this.unexpected(awaitAt);
|
12940 | }
|
12941 | return this.parseFor(node, init);
|
12942 | }
|
12943 | }
|
12944 | const startsWithAsync = this.isContextual(95);
|
12945 | const refExpressionErrors = new ExpressionErrors();
|
12946 | const init = this.parseExpression(true, refExpressionErrors);
|
12947 | const isForOf = this.isContextual(101);
|
12948 | if (isForOf) {
|
12949 | if (startsWithLet) {
|
12950 | this.raise(Errors.ForOfLet, {
|
12951 | at: init
|
12952 | });
|
12953 | }
|
12954 | if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
|
12955 | this.raise(Errors.ForOfAsync, {
|
12956 | at: init
|
12957 | });
|
12958 | }
|
12959 | }
|
12960 | if (isForOf || this.match(58)) {
|
12961 | this.checkDestructuringPrivate(refExpressionErrors);
|
12962 | this.toAssignable(init, true);
|
12963 | const type = isForOf ? "ForOfStatement" : "ForInStatement";
|
12964 | this.checkLVal(init, {
|
12965 | in: {
|
12966 | type
|
12967 | }
|
12968 | });
|
12969 | return this.parseForIn(node, init, awaitAt);
|
12970 | } else {
|
12971 | this.checkExpressionErrors(refExpressionErrors, true);
|
12972 | }
|
12973 | if (awaitAt !== null) {
|
12974 | this.unexpected(awaitAt);
|
12975 | }
|
12976 | return this.parseFor(node, init);
|
12977 | }
|
12978 | parseFunctionStatement(node, isAsync, isHangingDeclaration) {
|
12979 | this.next();
|
12980 | return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
|
12981 | }
|
12982 | parseIfStatement(node) {
|
12983 | this.next();
|
12984 | node.test = this.parseHeaderExpression();
|
12985 | node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
|
12986 | node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
|
12987 | return this.finishNode(node, "IfStatement");
|
12988 | }
|
12989 | parseReturnStatement(node) {
|
12990 | if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
|
12991 | this.raise(Errors.IllegalReturn, {
|
12992 | at: this.state.startLoc
|
12993 | });
|
12994 | }
|
12995 | this.next();
|
12996 | if (this.isLineTerminator()) {
|
12997 | node.argument = null;
|
12998 | } else {
|
12999 | node.argument = this.parseExpression();
|
13000 | this.semicolon();
|
13001 | }
|
13002 | return this.finishNode(node, "ReturnStatement");
|
13003 | }
|
13004 | parseSwitchStatement(node) {
|
13005 | this.next();
|
13006 | node.discriminant = this.parseHeaderExpression();
|
13007 | const cases = node.cases = [];
|
13008 | this.expect(5);
|
13009 | this.state.labels.push(switchLabel);
|
13010 | this.scope.enter(SCOPE_OTHER);
|
13011 | let cur;
|
13012 | for (let sawDefault; !this.match(8);) {
|
13013 | if (this.match(61) || this.match(65)) {
|
13014 | const isCase = this.match(61);
|
13015 | if (cur) this.finishNode(cur, "SwitchCase");
|
13016 | cases.push(cur = this.startNode());
|
13017 | cur.consequent = [];
|
13018 | this.next();
|
13019 | if (isCase) {
|
13020 | cur.test = this.parseExpression();
|
13021 | } else {
|
13022 | if (sawDefault) {
|
13023 | this.raise(Errors.MultipleDefaultsInSwitch, {
|
13024 | at: this.state.lastTokStartLoc
|
13025 | });
|
13026 | }
|
13027 | sawDefault = true;
|
13028 | cur.test = null;
|
13029 | }
|
13030 | this.expect(14);
|
13031 | } else {
|
13032 | if (cur) {
|
13033 | cur.consequent.push(this.parseStatementListItem());
|
13034 | } else {
|
13035 | this.unexpected();
|
13036 | }
|
13037 | }
|
13038 | }
|
13039 | this.scope.exit();
|
13040 | if (cur) this.finishNode(cur, "SwitchCase");
|
13041 | this.next();
|
13042 | this.state.labels.pop();
|
13043 | return this.finishNode(node, "SwitchStatement");
|
13044 | }
|
13045 | parseThrowStatement(node) {
|
13046 | this.next();
|
13047 | if (this.hasPrecedingLineBreak()) {
|
13048 | this.raise(Errors.NewlineAfterThrow, {
|
13049 | at: this.state.lastTokEndLoc
|
13050 | });
|
13051 | }
|
13052 | node.argument = this.parseExpression();
|
13053 | this.semicolon();
|
13054 | return this.finishNode(node, "ThrowStatement");
|
13055 | }
|
13056 | parseCatchClauseParam() {
|
13057 | const param = this.parseBindingAtom();
|
13058 | this.scope.enter(this.options.annexB && param.type === "Identifier" ? SCOPE_SIMPLE_CATCH : 0);
|
13059 | this.checkLVal(param, {
|
13060 | in: {
|
13061 | type: "CatchClause"
|
13062 | },
|
13063 | binding: BIND_CATCH_PARAM
|
13064 | });
|
13065 | return param;
|
13066 | }
|
13067 | parseTryStatement(node) {
|
13068 | this.next();
|
13069 | node.block = this.parseBlock();
|
13070 | node.handler = null;
|
13071 | if (this.match(62)) {
|
13072 | const clause = this.startNode();
|
13073 | this.next();
|
13074 | if (this.match(10)) {
|
13075 | this.expect(10);
|
13076 | clause.param = this.parseCatchClauseParam();
|
13077 | this.expect(11);
|
13078 | } else {
|
13079 | clause.param = null;
|
13080 | this.scope.enter(SCOPE_OTHER);
|
13081 | }
|
13082 | clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
|
13083 | this.scope.exit();
|
13084 | node.handler = this.finishNode(clause, "CatchClause");
|
13085 | }
|
13086 | node.finalizer = this.eat(67) ? this.parseBlock() : null;
|
13087 | if (!node.handler && !node.finalizer) {
|
13088 | this.raise(Errors.NoCatchOrFinally, {
|
13089 | at: node
|
13090 | });
|
13091 | }
|
13092 | return this.finishNode(node, "TryStatement");
|
13093 | }
|
13094 | parseVarStatement(node, kind, allowMissingInitializer = false) {
|
13095 | this.next();
|
13096 | this.parseVar(node, false, kind, allowMissingInitializer);
|
13097 | this.semicolon();
|
13098 | return this.finishNode(node, "VariableDeclaration");
|
13099 | }
|
13100 | parseWhileStatement(node) {
|
13101 | this.next();
|
13102 | node.test = this.parseHeaderExpression();
|
13103 | this.state.labels.push(loopLabel);
|
13104 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
13105 | this.state.labels.pop();
|
13106 | return this.finishNode(node, "WhileStatement");
|
13107 | }
|
13108 | parseWithStatement(node) {
|
13109 | if (this.state.strict) {
|
13110 | this.raise(Errors.StrictWith, {
|
13111 | at: this.state.startLoc
|
13112 | });
|
13113 | }
|
13114 | this.next();
|
13115 | node.object = this.parseHeaderExpression();
|
13116 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
13117 | return this.finishNode(node, "WithStatement");
|
13118 | }
|
13119 | parseEmptyStatement(node) {
|
13120 | this.next();
|
13121 | return this.finishNode(node, "EmptyStatement");
|
13122 | }
|
13123 | parseLabeledStatement(node, maybeName, expr, flags) {
|
13124 | for (const label of this.state.labels) {
|
13125 | if (label.name === maybeName) {
|
13126 | this.raise(Errors.LabelRedeclaration, {
|
13127 | at: expr,
|
13128 | labelName: maybeName
|
13129 | });
|
13130 | }
|
13131 | }
|
13132 | const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
|
13133 | for (let i = this.state.labels.length - 1; i >= 0; i--) {
|
13134 | const label = this.state.labels[i];
|
13135 | if (label.statementStart === node.start) {
|
13136 | label.statementStart = this.state.start;
|
13137 | label.kind = kind;
|
13138 | } else {
|
13139 | break;
|
13140 | }
|
13141 | }
|
13142 | this.state.labels.push({
|
13143 | name: maybeName,
|
13144 | kind: kind,
|
13145 | statementStart: this.state.start
|
13146 | });
|
13147 | node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
|
13148 | this.state.labels.pop();
|
13149 | node.label = expr;
|
13150 | return this.finishNode(node, "LabeledStatement");
|
13151 | }
|
13152 | parseExpressionStatement(node, expr, decorators) {
|
13153 | node.expression = expr;
|
13154 | this.semicolon();
|
13155 | return this.finishNode(node, "ExpressionStatement");
|
13156 | }
|
13157 | parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
|
13158 | const node = this.startNode();
|
13159 | if (allowDirectives) {
|
13160 | this.state.strictErrors.clear();
|
13161 | }
|
13162 | this.expect(5);
|
13163 | if (createNewLexicalScope) {
|
13164 | this.scope.enter(SCOPE_OTHER);
|
13165 | }
|
13166 | this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
|
13167 | if (createNewLexicalScope) {
|
13168 | this.scope.exit();
|
13169 | }
|
13170 | return this.finishNode(node, "BlockStatement");
|
13171 | }
|
13172 | isValidDirective(stmt) {
|
13173 | return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
|
13174 | }
|
13175 | parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
|
13176 | const body = node.body = [];
|
13177 | const directives = node.directives = [];
|
13178 | this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
|
13179 | }
|
13180 | parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
|
13181 | const oldStrict = this.state.strict;
|
13182 | let hasStrictModeDirective = false;
|
13183 | let parsedNonDirective = false;
|
13184 | while (!this.match(end)) {
|
13185 | const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
|
13186 | if (directives && !parsedNonDirective) {
|
13187 | if (this.isValidDirective(stmt)) {
|
13188 | const directive = this.stmtToDirective(stmt);
|
13189 | directives.push(directive);
|
13190 | if (!hasStrictModeDirective && directive.value.value === "use strict") {
|
13191 | hasStrictModeDirective = true;
|
13192 | this.setStrict(true);
|
13193 | }
|
13194 | continue;
|
13195 | }
|
13196 | parsedNonDirective = true;
|
13197 | this.state.strictErrors.clear();
|
13198 | }
|
13199 | body.push(stmt);
|
13200 | }
|
13201 | if (afterBlockParse) {
|
13202 | afterBlockParse.call(this, hasStrictModeDirective);
|
13203 | }
|
13204 | if (!oldStrict) {
|
13205 | this.setStrict(false);
|
13206 | }
|
13207 | this.next();
|
13208 | }
|
13209 | parseFor(node, init) {
|
13210 | node.init = init;
|
13211 | this.semicolon(false);
|
13212 | node.test = this.match(13) ? null : this.parseExpression();
|
13213 | this.semicolon(false);
|
13214 | node.update = this.match(11) ? null : this.parseExpression();
|
13215 | this.expect(11);
|
13216 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
13217 | this.scope.exit();
|
13218 | this.state.labels.pop();
|
13219 | return this.finishNode(node, "ForStatement");
|
13220 | }
|
13221 | parseForIn(node, init, awaitAt) {
|
13222 | const isForIn = this.match(58);
|
13223 | this.next();
|
13224 | if (isForIn) {
|
13225 | if (awaitAt !== null) this.unexpected(awaitAt);
|
13226 | } else {
|
13227 | node.await = awaitAt !== null;
|
13228 | }
|
13229 | if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
|
13230 | this.raise(Errors.ForInOfLoopInitializer, {
|
13231 | at: init,
|
13232 | type: isForIn ? "ForInStatement" : "ForOfStatement"
|
13233 | });
|
13234 | }
|
13235 | if (init.type === "AssignmentPattern") {
|
13236 | this.raise(Errors.InvalidLhs, {
|
13237 | at: init,
|
13238 | ancestor: {
|
13239 | type: "ForStatement"
|
13240 | }
|
13241 | });
|
13242 | }
|
13243 | node.left = init;
|
13244 | node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
|
13245 | this.expect(11);
|
13246 | node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
|
13247 | this.scope.exit();
|
13248 | this.state.labels.pop();
|
13249 | return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
|
13250 | }
|
13251 | parseVar(node, isFor, kind, allowMissingInitializer = false) {
|
13252 | const declarations = node.declarations = [];
|
13253 | node.kind = kind;
|
13254 | for (;;) {
|
13255 | const decl = this.startNode();
|
13256 | this.parseVarId(decl, kind);
|
13257 | decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
|
13258 | if (decl.init === null && !allowMissingInitializer) {
|
13259 | if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
|
13260 | this.raise(Errors.DeclarationMissingInitializer, {
|
13261 | at: this.state.lastTokEndLoc,
|
13262 | kind: "destructuring"
|
13263 | });
|
13264 | } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
|
13265 | this.raise(Errors.DeclarationMissingInitializer, {
|
13266 | at: this.state.lastTokEndLoc,
|
13267 | kind: "const"
|
13268 | });
|
13269 | }
|
13270 | }
|
13271 | declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
13272 | if (!this.eat(12)) break;
|
13273 | }
|
13274 | return node;
|
13275 | }
|
13276 | parseVarId(decl, kind) {
|
13277 | const id = this.parseBindingAtom();
|
13278 | this.checkLVal(id, {
|
13279 | in: {
|
13280 | type: "VariableDeclarator"
|
13281 | },
|
13282 | binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
|
13283 | });
|
13284 | decl.id = id;
|
13285 | }
|
13286 | parseAsyncFunctionExpression(node) {
|
13287 | return this.parseFunction(node, 8);
|
13288 | }
|
13289 | parseFunction(node, flags = 0) {
|
13290 | const hangingDeclaration = flags & 2;
|
13291 | const isDeclaration = !!(flags & 1);
|
13292 | const requireId = isDeclaration && !(flags & 4);
|
13293 | const isAsync = !!(flags & 8);
|
13294 | this.initFunction(node, isAsync);
|
13295 | if (this.match(55)) {
|
13296 | if (hangingDeclaration) {
|
13297 | this.raise(Errors.GeneratorInSingleStatementContext, {
|
13298 | at: this.state.startLoc
|
13299 | });
|
13300 | }
|
13301 | this.next();
|
13302 | node.generator = true;
|
13303 | }
|
13304 | if (isDeclaration) {
|
13305 | node.id = this.parseFunctionId(requireId);
|
13306 | }
|
13307 | const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
|
13308 | this.state.maybeInArrowParameters = false;
|
13309 | this.scope.enter(SCOPE_FUNCTION);
|
13310 | this.prodParam.enter(functionFlags(isAsync, node.generator));
|
13311 | if (!isDeclaration) {
|
13312 | node.id = this.parseFunctionId();
|
13313 | }
|
13314 | this.parseFunctionParams(node, false);
|
13315 | this.withSmartMixTopicForbiddingContext(() => {
|
13316 | this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
|
13317 | });
|
13318 | this.prodParam.exit();
|
13319 | this.scope.exit();
|
13320 | if (isDeclaration && !hangingDeclaration) {
|
13321 | this.registerFunctionStatementId(node);
|
13322 | }
|
13323 | this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
|
13324 | return node;
|
13325 | }
|
13326 | parseFunctionId(requireId) {
|
13327 | return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
|
13328 | }
|
13329 | parseFunctionParams(node, isConstructor) {
|
13330 | this.expect(10);
|
13331 | this.expressionScope.enter(newParameterDeclarationScope());
|
13332 | node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
|
13333 | this.expressionScope.exit();
|
13334 | }
|
13335 | registerFunctionStatementId(node) {
|
13336 | if (!node.id) return;
|
13337 | this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
|
13338 | }
|
13339 | parseClass(node, isStatement, optionalId) {
|
13340 | this.next();
|
13341 | const oldStrict = this.state.strict;
|
13342 | this.state.strict = true;
|
13343 | this.parseClassId(node, isStatement, optionalId);
|
13344 | this.parseClassSuper(node);
|
13345 | node.body = this.parseClassBody(!!node.superClass, oldStrict);
|
13346 | return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
|
13347 | }
|
13348 | isClassProperty() {
|
13349 | return this.match(29) || this.match(13) || this.match(8);
|
13350 | }
|
13351 | isClassMethod() {
|
13352 | return this.match(10);
|
13353 | }
|
13354 | isNonstaticConstructor(method) {
|
13355 | return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
|
13356 | }
|
13357 | parseClassBody(hadSuperClass, oldStrict) {
|
13358 | this.classScope.enter();
|
13359 | const state = {
|
13360 | hadConstructor: false,
|
13361 | hadSuperClass
|
13362 | };
|
13363 | let decorators = [];
|
13364 | const classBody = this.startNode();
|
13365 | classBody.body = [];
|
13366 | this.expect(5);
|
13367 | this.withSmartMixTopicForbiddingContext(() => {
|
13368 | while (!this.match(8)) {
|
13369 | if (this.eat(13)) {
|
13370 | if (decorators.length > 0) {
|
13371 | throw this.raise(Errors.DecoratorSemicolon, {
|
13372 | at: this.state.lastTokEndLoc
|
13373 | });
|
13374 | }
|
13375 | continue;
|
13376 | }
|
13377 | if (this.match(26)) {
|
13378 | decorators.push(this.parseDecorator());
|
13379 | continue;
|
13380 | }
|
13381 | const member = this.startNode();
|
13382 | if (decorators.length) {
|
13383 | member.decorators = decorators;
|
13384 | this.resetStartLocationFromNode(member, decorators[0]);
|
13385 | decorators = [];
|
13386 | }
|
13387 | this.parseClassMember(classBody, member, state);
|
13388 | if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
|
13389 | this.raise(Errors.DecoratorConstructor, {
|
13390 | at: member
|
13391 | });
|
13392 | }
|
13393 | }
|
13394 | });
|
13395 | this.state.strict = oldStrict;
|
13396 | this.next();
|
13397 | if (decorators.length) {
|
13398 | throw this.raise(Errors.TrailingDecorator, {
|
13399 | at: this.state.startLoc
|
13400 | });
|
13401 | }
|
13402 | this.classScope.exit();
|
13403 | return this.finishNode(classBody, "ClassBody");
|
13404 | }
|
13405 | parseClassMemberFromModifier(classBody, member) {
|
13406 | const key = this.parseIdentifier(true);
|
13407 | if (this.isClassMethod()) {
|
13408 | const method = member;
|
13409 | method.kind = "method";
|
13410 | method.computed = false;
|
13411 | method.key = key;
|
13412 | method.static = false;
|
13413 | this.pushClassMethod(classBody, method, false, false, false, false);
|
13414 | return true;
|
13415 | } else if (this.isClassProperty()) {
|
13416 | const prop = member;
|
13417 | prop.computed = false;
|
13418 | prop.key = key;
|
13419 | prop.static = false;
|
13420 | classBody.body.push(this.parseClassProperty(prop));
|
13421 | return true;
|
13422 | }
|
13423 | this.resetPreviousNodeTrailingComments(key);
|
13424 | return false;
|
13425 | }
|
13426 | parseClassMember(classBody, member, state) {
|
13427 | const isStatic = this.isContextual(104);
|
13428 | if (isStatic) {
|
13429 | if (this.parseClassMemberFromModifier(classBody, member)) {
|
13430 | return;
|
13431 | }
|
13432 | if (this.eat(5)) {
|
13433 | this.parseClassStaticBlock(classBody, member);
|
13434 | return;
|
13435 | }
|
13436 | }
|
13437 | this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
|
13438 | }
|
13439 | parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
|
13440 | const publicMethod = member;
|
13441 | const privateMethod = member;
|
13442 | const publicProp = member;
|
13443 | const privateProp = member;
|
13444 | const accessorProp = member;
|
13445 | const method = publicMethod;
|
13446 | const publicMember = publicMethod;
|
13447 | member.static = isStatic;
|
13448 | this.parsePropertyNamePrefixOperator(member);
|
13449 | if (this.eat(55)) {
|
13450 | method.kind = "method";
|
13451 | const isPrivateName = this.match(136);
|
13452 | this.parseClassElementName(method);
|
13453 | if (isPrivateName) {
|
13454 | this.pushClassPrivateMethod(classBody, privateMethod, true, false);
|
13455 | return;
|
13456 | }
|
13457 | if (this.isNonstaticConstructor(publicMethod)) {
|
13458 | this.raise(Errors.ConstructorIsGenerator, {
|
13459 | at: publicMethod.key
|
13460 | });
|
13461 | }
|
13462 | this.pushClassMethod(classBody, publicMethod, true, false, false, false);
|
13463 | return;
|
13464 | }
|
13465 | const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
|
13466 | const isPrivate = this.match(136);
|
13467 | const key = this.parseClassElementName(member);
|
13468 | const maybeQuestionTokenStartLoc = this.state.startLoc;
|
13469 | this.parsePostMemberNameModifiers(publicMember);
|
13470 | if (this.isClassMethod()) {
|
13471 | method.kind = "method";
|
13472 | if (isPrivate) {
|
13473 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
13474 | return;
|
13475 | }
|
13476 | const isConstructor = this.isNonstaticConstructor(publicMethod);
|
13477 | let allowsDirectSuper = false;
|
13478 | if (isConstructor) {
|
13479 | publicMethod.kind = "constructor";
|
13480 | if (state.hadConstructor && !this.hasPlugin("typescript")) {
|
13481 | this.raise(Errors.DuplicateConstructor, {
|
13482 | at: key
|
13483 | });
|
13484 | }
|
13485 | if (isConstructor && this.hasPlugin("typescript") && member.override) {
|
13486 | this.raise(Errors.OverrideOnConstructor, {
|
13487 | at: key
|
13488 | });
|
13489 | }
|
13490 | state.hadConstructor = true;
|
13491 | allowsDirectSuper = state.hadSuperClass;
|
13492 | }
|
13493 | this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
|
13494 | } else if (this.isClassProperty()) {
|
13495 | if (isPrivate) {
|
13496 | this.pushClassPrivateProperty(classBody, privateProp);
|
13497 | } else {
|
13498 | this.pushClassProperty(classBody, publicProp);
|
13499 | }
|
13500 | } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
|
13501 | this.resetPreviousNodeTrailingComments(key);
|
13502 | const isGenerator = this.eat(55);
|
13503 | if (publicMember.optional) {
|
13504 | this.unexpected(maybeQuestionTokenStartLoc);
|
13505 | }
|
13506 | method.kind = "method";
|
13507 | const isPrivate = this.match(136);
|
13508 | this.parseClassElementName(method);
|
13509 | this.parsePostMemberNameModifiers(publicMember);
|
13510 | if (isPrivate) {
|
13511 | this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
|
13512 | } else {
|
13513 | if (this.isNonstaticConstructor(publicMethod)) {
|
13514 | this.raise(Errors.ConstructorIsAsync, {
|
13515 | at: publicMethod.key
|
13516 | });
|
13517 | }
|
13518 | this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
|
13519 | }
|
13520 | } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
|
13521 | this.resetPreviousNodeTrailingComments(key);
|
13522 | method.kind = key.name;
|
13523 | const isPrivate = this.match(136);
|
13524 | this.parseClassElementName(publicMethod);
|
13525 | if (isPrivate) {
|
13526 | this.pushClassPrivateMethod(classBody, privateMethod, false, false);
|
13527 | } else {
|
13528 | if (this.isNonstaticConstructor(publicMethod)) {
|
13529 | this.raise(Errors.ConstructorIsAccessor, {
|
13530 | at: publicMethod.key
|
13531 | });
|
13532 | }
|
13533 | this.pushClassMethod(classBody, publicMethod, false, false, false, false);
|
13534 | }
|
13535 | this.checkGetterSetterParams(publicMethod);
|
13536 | } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
|
13537 | this.expectPlugin("decoratorAutoAccessors");
|
13538 | this.resetPreviousNodeTrailingComments(key);
|
13539 | const isPrivate = this.match(136);
|
13540 | this.parseClassElementName(publicProp);
|
13541 | this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
|
13542 | } else if (this.isLineTerminator()) {
|
13543 | if (isPrivate) {
|
13544 | this.pushClassPrivateProperty(classBody, privateProp);
|
13545 | } else {
|
13546 | this.pushClassProperty(classBody, publicProp);
|
13547 | }
|
13548 | } else {
|
13549 | this.unexpected();
|
13550 | }
|
13551 | }
|
13552 | parseClassElementName(member) {
|
13553 | const {
|
13554 | type,
|
13555 | value
|
13556 | } = this.state;
|
13557 | if ((type === 130 || type === 131) && member.static && value === "prototype") {
|
13558 | this.raise(Errors.StaticPrototype, {
|
13559 | at: this.state.startLoc
|
13560 | });
|
13561 | }
|
13562 | if (type === 136) {
|
13563 | if (value === "constructor") {
|
13564 | this.raise(Errors.ConstructorClassPrivateField, {
|
13565 | at: this.state.startLoc
|
13566 | });
|
13567 | }
|
13568 | const key = this.parsePrivateName();
|
13569 | member.key = key;
|
13570 | return key;
|
13571 | }
|
13572 | return this.parsePropertyName(member);
|
13573 | }
|
13574 | parseClassStaticBlock(classBody, member) {
|
13575 | var _member$decorators;
|
13576 | this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
|
13577 | const oldLabels = this.state.labels;
|
13578 | this.state.labels = [];
|
13579 | this.prodParam.enter(PARAM);
|
13580 | const body = member.body = [];
|
13581 | this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
|
13582 | this.prodParam.exit();
|
13583 | this.scope.exit();
|
13584 | this.state.labels = oldLabels;
|
13585 | classBody.body.push(this.finishNode(member, "StaticBlock"));
|
13586 | if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
|
13587 | this.raise(Errors.DecoratorStaticBlock, {
|
13588 | at: member
|
13589 | });
|
13590 | }
|
13591 | }
|
13592 | pushClassProperty(classBody, prop) {
|
13593 | if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
|
13594 | this.raise(Errors.ConstructorClassField, {
|
13595 | at: prop.key
|
13596 | });
|
13597 | }
|
13598 | classBody.body.push(this.parseClassProperty(prop));
|
13599 | }
|
13600 | pushClassPrivateProperty(classBody, prop) {
|
13601 | const node = this.parseClassPrivateProperty(prop);
|
13602 | classBody.body.push(node);
|
13603 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
|
13604 | }
|
13605 | pushClassAccessorProperty(classBody, prop, isPrivate) {
|
13606 | if (!isPrivate && !prop.computed) {
|
13607 | const key = prop.key;
|
13608 | if (key.name === "constructor" || key.value === "constructor") {
|
13609 | this.raise(Errors.ConstructorClassField, {
|
13610 | at: key
|
13611 | });
|
13612 | }
|
13613 | }
|
13614 | const node = this.parseClassAccessorProperty(prop);
|
13615 | classBody.body.push(node);
|
13616 | if (isPrivate) {
|
13617 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
|
13618 | }
|
13619 | }
|
13620 | pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
|
13621 | classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
|
13622 | }
|
13623 | pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
|
13624 | const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
|
13625 | classBody.body.push(node);
|
13626 | 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;
|
13627 | this.declareClassPrivateMethodInScope(node, kind);
|
13628 | }
|
13629 | declareClassPrivateMethodInScope(node, kind) {
|
13630 | this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
|
13631 | }
|
13632 | parsePostMemberNameModifiers(methodOrProp) {}
|
13633 | parseClassPrivateProperty(node) {
|
13634 | this.parseInitializer(node);
|
13635 | this.semicolon();
|
13636 | return this.finishNode(node, "ClassPrivateProperty");
|
13637 | }
|
13638 | parseClassProperty(node) {
|
13639 | this.parseInitializer(node);
|
13640 | this.semicolon();
|
13641 | return this.finishNode(node, "ClassProperty");
|
13642 | }
|
13643 | parseClassAccessorProperty(node) {
|
13644 | this.parseInitializer(node);
|
13645 | this.semicolon();
|
13646 | return this.finishNode(node, "ClassAccessorProperty");
|
13647 | }
|
13648 | parseInitializer(node) {
|
13649 | this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
|
13650 | this.expressionScope.enter(newExpressionScope());
|
13651 | this.prodParam.enter(PARAM);
|
13652 | node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
|
13653 | this.expressionScope.exit();
|
13654 | this.prodParam.exit();
|
13655 | this.scope.exit();
|
13656 | }
|
13657 | parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
|
13658 | if (tokenIsIdentifier(this.state.type)) {
|
13659 | node.id = this.parseIdentifier();
|
13660 | if (isStatement) {
|
13661 | this.declareNameFromIdentifier(node.id, bindingType);
|
13662 | }
|
13663 | } else {
|
13664 | if (optionalId || !isStatement) {
|
13665 | node.id = null;
|
13666 | } else {
|
13667 | throw this.raise(Errors.MissingClassName, {
|
13668 | at: this.state.startLoc
|
13669 | });
|
13670 | }
|
13671 | }
|
13672 | }
|
13673 | parseClassSuper(node) {
|
13674 | node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
|
13675 | }
|
13676 | parseExport(node, decorators) {
|
13677 | const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
|
13678 | const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
|
13679 | const parseAfterDefault = !hasDefault || this.eat(12);
|
13680 | const hasStar = parseAfterDefault && this.eatExportStar(node);
|
13681 | const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
|
13682 | const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
|
13683 | const isFromRequired = hasDefault || hasStar;
|
13684 | if (hasStar && !hasNamespace) {
|
13685 | if (hasDefault) this.unexpected();
|
13686 | if (decorators) {
|
13687 | throw this.raise(Errors.UnsupportedDecoratorExport, {
|
13688 | at: node
|
13689 | });
|
13690 | }
|
13691 | this.parseExportFrom(node, true);
|
13692 | return this.finishNode(node, "ExportAllDeclaration");
|
13693 | }
|
13694 | const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
|
13695 | if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
|
13696 | this.unexpected(null, 5);
|
13697 | }
|
13698 | if (hasNamespace && parseAfterNamespace) {
|
13699 | this.unexpected(null, 97);
|
13700 | }
|
13701 | let hasDeclaration;
|
13702 | if (isFromRequired || hasSpecifiers) {
|
13703 | hasDeclaration = false;
|
13704 | if (decorators) {
|
13705 | throw this.raise(Errors.UnsupportedDecoratorExport, {
|
13706 | at: node
|
13707 | });
|
13708 | }
|
13709 | this.parseExportFrom(node, isFromRequired);
|
13710 | } else {
|
13711 | hasDeclaration = this.maybeParseExportDeclaration(node);
|
13712 | }
|
13713 | if (isFromRequired || hasSpecifiers || hasDeclaration) {
|
13714 | var _node2$declaration;
|
13715 | const node2 = node;
|
13716 | this.checkExport(node2, true, false, !!node2.source);
|
13717 | if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
|
13718 | this.maybeTakeDecorators(decorators, node2.declaration, node2);
|
13719 | } else if (decorators) {
|
13720 | throw this.raise(Errors.UnsupportedDecoratorExport, {
|
13721 | at: node
|
13722 | });
|
13723 | }
|
13724 | return this.finishNode(node2, "ExportNamedDeclaration");
|
13725 | }
|
13726 | if (this.eat(65)) {
|
13727 | const node2 = node;
|
13728 | const decl = this.parseExportDefaultExpression();
|
13729 | node2.declaration = decl;
|
13730 | if (decl.type === "ClassDeclaration") {
|
13731 | this.maybeTakeDecorators(decorators, decl, node2);
|
13732 | } else if (decorators) {
|
13733 | throw this.raise(Errors.UnsupportedDecoratorExport, {
|
13734 | at: node
|
13735 | });
|
13736 | }
|
13737 | this.checkExport(node2, true, true);
|
13738 | return this.finishNode(node2, "ExportDefaultDeclaration");
|
13739 | }
|
13740 | this.unexpected(null, 5);
|
13741 | }
|
13742 | eatExportStar(node) {
|
13743 | return this.eat(55);
|
13744 | }
|
13745 | maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
|
13746 | if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
|
13747 | this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
|
13748 | const id = maybeDefaultIdentifier || this.parseIdentifier(true);
|
13749 | const specifier = this.startNodeAtNode(id);
|
13750 | specifier.exported = id;
|
13751 | node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
|
13752 | return true;
|
13753 | }
|
13754 | return false;
|
13755 | }
|
13756 | maybeParseExportNamespaceSpecifier(node) {
|
13757 | if (this.isContextual(93)) {
|
13758 | if (!node.specifiers) node.specifiers = [];
|
13759 | const specifier = this.startNodeAt(this.state.lastTokStartLoc);
|
13760 | this.next();
|
13761 | specifier.exported = this.parseModuleExportName();
|
13762 | node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
|
13763 | return true;
|
13764 | }
|
13765 | return false;
|
13766 | }
|
13767 | maybeParseExportNamedSpecifiers(node) {
|
13768 | if (this.match(5)) {
|
13769 | if (!node.specifiers) node.specifiers = [];
|
13770 | const isTypeExport = node.exportKind === "type";
|
13771 | node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
|
13772 | node.source = null;
|
13773 | node.declaration = null;
|
13774 | if (this.hasPlugin("importAssertions")) {
|
13775 | node.assertions = [];
|
13776 | }
|
13777 | return true;
|
13778 | }
|
13779 | return false;
|
13780 | }
|
13781 | maybeParseExportDeclaration(node) {
|
13782 | if (this.shouldParseExportDeclaration()) {
|
13783 | node.specifiers = [];
|
13784 | node.source = null;
|
13785 | if (this.hasPlugin("importAssertions")) {
|
13786 | node.assertions = [];
|
13787 | }
|
13788 | node.declaration = this.parseExportDeclaration(node);
|
13789 | return true;
|
13790 | }
|
13791 | return false;
|
13792 | }
|
13793 | isAsyncFunction() {
|
13794 | if (!this.isContextual(95)) return false;
|
13795 | const next = this.nextTokenInLineStart();
|
13796 | return this.isUnparsedContextual(next, "function");
|
13797 | }
|
13798 | parseExportDefaultExpression() {
|
13799 | const expr = this.startNode();
|
13800 | if (this.match(68)) {
|
13801 | this.next();
|
13802 | return this.parseFunction(expr, 1 | 4);
|
13803 | } else if (this.isAsyncFunction()) {
|
13804 | this.next();
|
13805 | this.next();
|
13806 | return this.parseFunction(expr, 1 | 4 | 8);
|
13807 | }
|
13808 | if (this.match(80)) {
|
13809 | return this.parseClass(expr, true, true);
|
13810 | }
|
13811 | if (this.match(26)) {
|
13812 | if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
|
13813 | this.raise(Errors.DecoratorBeforeExport, {
|
13814 | at: this.state.startLoc
|
13815 | });
|
13816 | }
|
13817 | return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
|
13818 | }
|
13819 | if (this.match(75) || this.match(74) || this.isLet()) {
|
13820 | throw this.raise(Errors.UnsupportedDefaultExport, {
|
13821 | at: this.state.startLoc
|
13822 | });
|
13823 | }
|
13824 | const res = this.parseMaybeAssignAllowIn();
|
13825 | this.semicolon();
|
13826 | return res;
|
13827 | }
|
13828 | parseExportDeclaration(node) {
|
13829 | if (this.match(80)) {
|
13830 | const node = this.parseClass(this.startNode(), true, false);
|
13831 | return node;
|
13832 | }
|
13833 | return this.parseStatementListItem();
|
13834 | }
|
13835 | isExportDefaultSpecifier() {
|
13836 | const {
|
13837 | type
|
13838 | } = this.state;
|
13839 | if (tokenIsIdentifier(type)) {
|
13840 | if (type === 95 && !this.state.containsEsc || type === 99) {
|
13841 | return false;
|
13842 | }
|
13843 | if ((type === 128 || type === 127) && !this.state.containsEsc) {
|
13844 | const {
|
13845 | type: nextType
|
13846 | } = this.lookahead();
|
13847 | if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
|
13848 | this.expectOnePlugin(["flow", "typescript"]);
|
13849 | return false;
|
13850 | }
|
13851 | }
|
13852 | } else if (!this.match(65)) {
|
13853 | return false;
|
13854 | }
|
13855 | const next = this.nextTokenStart();
|
13856 | const hasFrom = this.isUnparsedContextual(next, "from");
|
13857 | if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
|
13858 | return true;
|
13859 | }
|
13860 | if (this.match(65) && hasFrom) {
|
13861 | const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
|
13862 | return nextAfterFrom === 34 || nextAfterFrom === 39;
|
13863 | }
|
13864 | return false;
|
13865 | }
|
13866 | parseExportFrom(node, expect) {
|
13867 | if (this.eatContextual(97)) {
|
13868 | node.source = this.parseImportSource();
|
13869 | this.checkExport(node);
|
13870 | this.maybeParseImportAttributes(node);
|
13871 | this.checkJSONModuleImport(node);
|
13872 | } else if (expect) {
|
13873 | this.unexpected();
|
13874 | }
|
13875 | this.semicolon();
|
13876 | }
|
13877 | shouldParseExportDeclaration() {
|
13878 | const {
|
13879 | type
|
13880 | } = this.state;
|
13881 | if (type === 26) {
|
13882 | this.expectOnePlugin(["decorators", "decorators-legacy"]);
|
13883 | if (this.hasPlugin("decorators")) {
|
13884 | if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
|
13885 | this.raise(Errors.DecoratorBeforeExport, {
|
13886 | at: this.state.startLoc
|
13887 | });
|
13888 | }
|
13889 | return true;
|
13890 | }
|
13891 | }
|
13892 | return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
|
13893 | }
|
13894 | checkExport(node, checkNames, isDefault, isFrom) {
|
13895 | if (checkNames) {
|
13896 | if (isDefault) {
|
13897 | this.checkDuplicateExports(node, "default");
|
13898 | if (this.hasPlugin("exportDefaultFrom")) {
|
13899 | var _declaration$extra;
|
13900 | const declaration = node.declaration;
|
13901 | if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
|
13902 | this.raise(Errors.ExportDefaultFromAsIdentifier, {
|
13903 | at: declaration
|
13904 | });
|
13905 | }
|
13906 | }
|
13907 | } else if (node.specifiers && node.specifiers.length) {
|
13908 | for (const specifier of node.specifiers) {
|
13909 | const {
|
13910 | exported
|
13911 | } = specifier;
|
13912 | const exportName = exported.type === "Identifier" ? exported.name : exported.value;
|
13913 | this.checkDuplicateExports(specifier, exportName);
|
13914 | if (!isFrom && specifier.local) {
|
13915 | const {
|
13916 | local
|
13917 | } = specifier;
|
13918 | if (local.type !== "Identifier") {
|
13919 | this.raise(Errors.ExportBindingIsString, {
|
13920 | at: specifier,
|
13921 | localName: local.value,
|
13922 | exportName
|
13923 | });
|
13924 | } else {
|
13925 | this.checkReservedWord(local.name, local.loc.start, true, false);
|
13926 | this.scope.checkLocalExport(local);
|
13927 | }
|
13928 | }
|
13929 | }
|
13930 | } else if (node.declaration) {
|
13931 | if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
|
13932 | const id = node.declaration.id;
|
13933 | if (!id) throw new Error("Assertion failure");
|
13934 | this.checkDuplicateExports(node, id.name);
|
13935 | } else if (node.declaration.type === "VariableDeclaration") {
|
13936 | for (const declaration of node.declaration.declarations) {
|
13937 | this.checkDeclaration(declaration.id);
|
13938 | }
|
13939 | }
|
13940 | }
|
13941 | }
|
13942 | }
|
13943 | checkDeclaration(node) {
|
13944 | if (node.type === "Identifier") {
|
13945 | this.checkDuplicateExports(node, node.name);
|
13946 | } else if (node.type === "ObjectPattern") {
|
13947 | for (const prop of node.properties) {
|
13948 | this.checkDeclaration(prop);
|
13949 | }
|
13950 | } else if (node.type === "ArrayPattern") {
|
13951 | for (const elem of node.elements) {
|
13952 | if (elem) {
|
13953 | this.checkDeclaration(elem);
|
13954 | }
|
13955 | }
|
13956 | } else if (node.type === "ObjectProperty") {
|
13957 | this.checkDeclaration(node.value);
|
13958 | } else if (node.type === "RestElement") {
|
13959 | this.checkDeclaration(node.argument);
|
13960 | } else if (node.type === "AssignmentPattern") {
|
13961 | this.checkDeclaration(node.left);
|
13962 | }
|
13963 | }
|
13964 | checkDuplicateExports(node, exportName) {
|
13965 | if (this.exportedIdentifiers.has(exportName)) {
|
13966 | if (exportName === "default") {
|
13967 | this.raise(Errors.DuplicateDefaultExport, {
|
13968 | at: node
|
13969 | });
|
13970 | } else {
|
13971 | this.raise(Errors.DuplicateExport, {
|
13972 | at: node,
|
13973 | exportName
|
13974 | });
|
13975 | }
|
13976 | }
|
13977 | this.exportedIdentifiers.add(exportName);
|
13978 | }
|
13979 | parseExportSpecifiers(isInTypeExport) {
|
13980 | const nodes = [];
|
13981 | let first = true;
|
13982 | this.expect(5);
|
13983 | while (!this.eat(8)) {
|
13984 | if (first) {
|
13985 | first = false;
|
13986 | } else {
|
13987 | this.expect(12);
|
13988 | if (this.eat(8)) break;
|
13989 | }
|
13990 | const isMaybeTypeOnly = this.isContextual(128);
|
13991 | const isString = this.match(131);
|
13992 | const node = this.startNode();
|
13993 | node.local = this.parseModuleExportName();
|
13994 | nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
|
13995 | }
|
13996 | return nodes;
|
13997 | }
|
13998 | parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
|
13999 | if (this.eatContextual(93)) {
|
14000 | node.exported = this.parseModuleExportName();
|
14001 | } else if (isString) {
|
14002 | node.exported = cloneStringLiteral(node.local);
|
14003 | } else if (!node.exported) {
|
14004 | node.exported = cloneIdentifier(node.local);
|
14005 | }
|
14006 | return this.finishNode(node, "ExportSpecifier");
|
14007 | }
|
14008 | parseModuleExportName() {
|
14009 | if (this.match(131)) {
|
14010 | const result = this.parseStringLiteral(this.state.value);
|
14011 | const surrogate = result.value.match(loneSurrogate);
|
14012 | if (surrogate) {
|
14013 | this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
|
14014 | at: result,
|
14015 | surrogateCharCode: surrogate[0].charCodeAt(0)
|
14016 | });
|
14017 | }
|
14018 | return result;
|
14019 | }
|
14020 | return this.parseIdentifier(true);
|
14021 | }
|
14022 | isJSONModuleImport(node) {
|
14023 | if (node.assertions != null) {
|
14024 | return node.assertions.some(({
|
14025 | key,
|
14026 | value
|
14027 | }) => {
|
14028 | return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
|
14029 | });
|
14030 | }
|
14031 | return false;
|
14032 | }
|
14033 | checkImportReflection(node) {
|
14034 | if (node.module) {
|
14035 | var _node$assertions;
|
14036 | if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
|
14037 | this.raise(Errors.ImportReflectionNotBinding, {
|
14038 | at: node.specifiers[0].loc.start
|
14039 | });
|
14040 | }
|
14041 | if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
|
14042 | this.raise(Errors.ImportReflectionHasAssertion, {
|
14043 | at: node.specifiers[0].loc.start
|
14044 | });
|
14045 | }
|
14046 | }
|
14047 | }
|
14048 | checkJSONModuleImport(node) {
|
14049 | if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
|
14050 | const {
|
14051 | specifiers
|
14052 | } = node;
|
14053 | if (specifiers != null) {
|
14054 | const nonDefaultNamedSpecifier = specifiers.find(specifier => {
|
14055 | let imported;
|
14056 | if (specifier.type === "ExportSpecifier") {
|
14057 | imported = specifier.local;
|
14058 | } else if (specifier.type === "ImportSpecifier") {
|
14059 | imported = specifier.imported;
|
14060 | }
|
14061 | if (imported !== undefined) {
|
14062 | return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
|
14063 | }
|
14064 | });
|
14065 | if (nonDefaultNamedSpecifier !== undefined) {
|
14066 | this.raise(Errors.ImportJSONBindingNotDefault, {
|
14067 | at: nonDefaultNamedSpecifier.loc.start
|
14068 | });
|
14069 | }
|
14070 | }
|
14071 | }
|
14072 | }
|
14073 | isPotentialImportPhase(isExport) {
|
14074 | return !isExport && this.isContextual(125);
|
14075 | }
|
14076 | applyImportPhase(node, isExport, phase, loc) {
|
14077 | if (isExport) {
|
14078 | return;
|
14079 | }
|
14080 | if (phase === "module") {
|
14081 | this.expectPlugin("importReflection", loc);
|
14082 | node.module = true;
|
14083 | } else if (this.hasPlugin("importReflection")) {
|
14084 | node.module = false;
|
14085 | }
|
14086 | }
|
14087 | parseMaybeImportPhase(node, isExport) {
|
14088 | if (!this.isPotentialImportPhase(isExport)) {
|
14089 | this.applyImportPhase(node, isExport, null);
|
14090 | return null;
|
14091 | }
|
14092 | const phaseIdentifier = this.parseIdentifier(true);
|
14093 | const {
|
14094 | type
|
14095 | } = this.state;
|
14096 | const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
|
14097 | if (isImportPhase) {
|
14098 | this.resetPreviousIdentifierLeadingComments(phaseIdentifier);
|
14099 | this.applyImportPhase(node, isExport, phaseIdentifier.name, phaseIdentifier.loc.start);
|
14100 | return null;
|
14101 | } else {
|
14102 | this.applyImportPhase(node, isExport, null);
|
14103 | return phaseIdentifier;
|
14104 | }
|
14105 | }
|
14106 | isPrecedingIdImportPhase(phase) {
|
14107 | const {
|
14108 | type
|
14109 | } = this.state;
|
14110 | return tokenIsIdentifier(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
|
14111 | }
|
14112 | parseImport(node) {
|
14113 | if (this.match(131)) {
|
14114 | return this.parseImportSourceAndAttributes(node);
|
14115 | }
|
14116 | return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
|
14117 | }
|
14118 | parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
|
14119 | node.specifiers = [];
|
14120 | const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
|
14121 | const parseNext = !hasDefault || this.eat(12);
|
14122 | const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
|
14123 | if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
|
14124 | this.expectContextual(97);
|
14125 | return this.parseImportSourceAndAttributes(node);
|
14126 | }
|
14127 | parseImportSourceAndAttributes(node) {
|
14128 | var _node$specifiers;
|
14129 | (_node$specifiers = node.specifiers) != null ? _node$specifiers : node.specifiers = [];
|
14130 | node.source = this.parseImportSource();
|
14131 | this.maybeParseImportAttributes(node);
|
14132 | this.checkImportReflection(node);
|
14133 | this.checkJSONModuleImport(node);
|
14134 | this.semicolon();
|
14135 | return this.finishNode(node, "ImportDeclaration");
|
14136 | }
|
14137 | parseImportSource() {
|
14138 | if (!this.match(131)) this.unexpected();
|
14139 | return this.parseExprAtom();
|
14140 | }
|
14141 | parseImportSpecifierLocal(node, specifier, type) {
|
14142 | specifier.local = this.parseIdentifier();
|
14143 | node.specifiers.push(this.finishImportSpecifier(specifier, type));
|
14144 | }
|
14145 | finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
|
14146 | this.checkLVal(specifier.local, {
|
14147 | in: {
|
14148 | type
|
14149 | },
|
14150 | binding: bindingType
|
14151 | });
|
14152 | return this.finishNode(specifier, type);
|
14153 | }
|
14154 | parseImportAttributes() {
|
14155 | this.expect(5);
|
14156 | const attrs = [];
|
14157 | const attrNames = new Set();
|
14158 | do {
|
14159 | if (this.match(8)) {
|
14160 | break;
|
14161 | }
|
14162 | const node = this.startNode();
|
14163 | const keyName = this.state.value;
|
14164 | if (attrNames.has(keyName)) {
|
14165 | this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
|
14166 | at: this.state.startLoc,
|
14167 | key: keyName
|
14168 | });
|
14169 | }
|
14170 | attrNames.add(keyName);
|
14171 | if (this.match(131)) {
|
14172 | node.key = this.parseStringLiteral(keyName);
|
14173 | } else {
|
14174 | node.key = this.parseIdentifier(true);
|
14175 | }
|
14176 | this.expect(14);
|
14177 | if (!this.match(131)) {
|
14178 | throw this.raise(Errors.ModuleAttributeInvalidValue, {
|
14179 | at: this.state.startLoc
|
14180 | });
|
14181 | }
|
14182 | node.value = this.parseStringLiteral(this.state.value);
|
14183 | attrs.push(this.finishNode(node, "ImportAttribute"));
|
14184 | } while (this.eat(12));
|
14185 | this.expect(8);
|
14186 | return attrs;
|
14187 | }
|
14188 | parseModuleAttributes() {
|
14189 | const attrs = [];
|
14190 | const attributes = new Set();
|
14191 | do {
|
14192 | const node = this.startNode();
|
14193 | node.key = this.parseIdentifier(true);
|
14194 | if (node.key.name !== "type") {
|
14195 | this.raise(Errors.ModuleAttributeDifferentFromType, {
|
14196 | at: node.key
|
14197 | });
|
14198 | }
|
14199 | if (attributes.has(node.key.name)) {
|
14200 | this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
|
14201 | at: node.key,
|
14202 | key: node.key.name
|
14203 | });
|
14204 | }
|
14205 | attributes.add(node.key.name);
|
14206 | this.expect(14);
|
14207 | if (!this.match(131)) {
|
14208 | throw this.raise(Errors.ModuleAttributeInvalidValue, {
|
14209 | at: this.state.startLoc
|
14210 | });
|
14211 | }
|
14212 | node.value = this.parseStringLiteral(this.state.value);
|
14213 | attrs.push(this.finishNode(node, "ImportAttribute"));
|
14214 | } while (this.eat(12));
|
14215 | return attrs;
|
14216 | }
|
14217 | maybeParseImportAttributes(node) {
|
14218 | let attributes;
|
14219 | let useWith = false;
|
14220 | if (this.match(76)) {
|
14221 | if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
|
14222 | return;
|
14223 | }
|
14224 | this.next();
|
14225 | {
|
14226 | if (this.hasPlugin("moduleAttributes")) {
|
14227 | attributes = this.parseModuleAttributes();
|
14228 | } else {
|
14229 | this.expectImportAttributesPlugin();
|
14230 | attributes = this.parseImportAttributes();
|
14231 | }
|
14232 | }
|
14233 | useWith = true;
|
14234 | } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
|
14235 | if (this.hasPlugin("importAttributes")) {
|
14236 | if (this.getPluginOption("importAttributes", "deprecatedAssertSyntax") !== true) {
|
14237 | this.raise(Errors.ImportAttributesUseAssert, {
|
14238 | at: this.state.startLoc
|
14239 | });
|
14240 | }
|
14241 | this.addExtra(node, "deprecatedAssertSyntax", true);
|
14242 | } else {
|
14243 | this.expectOnePlugin(["importAttributes", "importAssertions"]);
|
14244 | }
|
14245 | this.next();
|
14246 | attributes = this.parseImportAttributes();
|
14247 | } else if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
|
14248 | attributes = [];
|
14249 | } else {
|
14250 | if (this.hasPlugin("moduleAttributes")) {
|
14251 | attributes = [];
|
14252 | } else return;
|
14253 | }
|
14254 | if (!useWith && this.hasPlugin("importAssertions")) {
|
14255 | node.assertions = attributes;
|
14256 | } else {
|
14257 | node.attributes = attributes;
|
14258 | }
|
14259 | }
|
14260 | maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
|
14261 | if (maybeDefaultIdentifier) {
|
14262 | const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
|
14263 | specifier.local = maybeDefaultIdentifier;
|
14264 | node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
|
14265 | return true;
|
14266 | } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
|
14267 | this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
|
14268 | return true;
|
14269 | }
|
14270 | return false;
|
14271 | }
|
14272 | maybeParseStarImportSpecifier(node) {
|
14273 | if (this.match(55)) {
|
14274 | const specifier = this.startNode();
|
14275 | this.next();
|
14276 | this.expectContextual(93);
|
14277 | this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
|
14278 | return true;
|
14279 | }
|
14280 | return false;
|
14281 | }
|
14282 | parseNamedImportSpecifiers(node) {
|
14283 | let first = true;
|
14284 | this.expect(5);
|
14285 | while (!this.eat(8)) {
|
14286 | if (first) {
|
14287 | first = false;
|
14288 | } else {
|
14289 | if (this.eat(14)) {
|
14290 | throw this.raise(Errors.DestructureNamedImport, {
|
14291 | at: this.state.startLoc
|
14292 | });
|
14293 | }
|
14294 | this.expect(12);
|
14295 | if (this.eat(8)) break;
|
14296 | }
|
14297 | const specifier = this.startNode();
|
14298 | const importedIsString = this.match(131);
|
14299 | const isMaybeTypeOnly = this.isContextual(128);
|
14300 | specifier.imported = this.parseModuleExportName();
|
14301 | const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
|
14302 | node.specifiers.push(importSpecifier);
|
14303 | }
|
14304 | }
|
14305 | parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
|
14306 | if (this.eatContextual(93)) {
|
14307 | specifier.local = this.parseIdentifier();
|
14308 | } else {
|
14309 | const {
|
14310 | imported
|
14311 | } = specifier;
|
14312 | if (importedIsString) {
|
14313 | throw this.raise(Errors.ImportBindingIsString, {
|
14314 | at: specifier,
|
14315 | importName: imported.value
|
14316 | });
|
14317 | }
|
14318 | this.checkReservedWord(imported.name, specifier.loc.start, true, true);
|
14319 | if (!specifier.local) {
|
14320 | specifier.local = cloneIdentifier(imported);
|
14321 | }
|
14322 | }
|
14323 | return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
|
14324 | }
|
14325 | isThisParam(param) {
|
14326 | return param.type === "Identifier" && param.name === "this";
|
14327 | }
|
14328 | }
|
14329 | class Parser extends StatementParser {
|
14330 | constructor(options, input) {
|
14331 | options = getOptions(options);
|
14332 | super(options, input);
|
14333 | this.options = options;
|
14334 | this.initializeScopes();
|
14335 | this.plugins = pluginsMap(this.options.plugins);
|
14336 | this.filename = options.sourceFilename;
|
14337 | }
|
14338 | getScopeHandler() {
|
14339 | return ScopeHandler;
|
14340 | }
|
14341 | parse() {
|
14342 | this.enterInitialScopes();
|
14343 | const file = this.startNode();
|
14344 | const program = this.startNode();
|
14345 | this.nextToken();
|
14346 | file.errors = null;
|
14347 | this.parseTopLevel(file, program);
|
14348 | file.errors = this.state.errors;
|
14349 | return file;
|
14350 | }
|
14351 | }
|
14352 | function pluginsMap(plugins) {
|
14353 | const pluginMap = new Map();
|
14354 | for (const plugin of plugins) {
|
14355 | const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
|
14356 | if (!pluginMap.has(name)) pluginMap.set(name, options || {});
|
14357 | }
|
14358 | return pluginMap;
|
14359 | }
|
14360 | function parse(input, options) {
|
14361 | var _options;
|
14362 | if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
|
14363 | options = Object.assign({}, options);
|
14364 | try {
|
14365 | options.sourceType = "module";
|
14366 | const parser = getParser(options, input);
|
14367 | const ast = parser.parse();
|
14368 | if (parser.sawUnambiguousESM) {
|
14369 | return ast;
|
14370 | }
|
14371 | if (parser.ambiguousScriptDifferentAst) {
|
14372 | try {
|
14373 | options.sourceType = "script";
|
14374 | return getParser(options, input).parse();
|
14375 | } catch (_unused) {}
|
14376 | } else {
|
14377 | ast.program.sourceType = "script";
|
14378 | }
|
14379 | return ast;
|
14380 | } catch (moduleError) {
|
14381 | try {
|
14382 | options.sourceType = "script";
|
14383 | return getParser(options, input).parse();
|
14384 | } catch (_unused2) {}
|
14385 | throw moduleError;
|
14386 | }
|
14387 | } else {
|
14388 | return getParser(options, input).parse();
|
14389 | }
|
14390 | }
|
14391 | function parseExpression(input, options) {
|
14392 | const parser = getParser(options, input);
|
14393 | if (parser.options.strictMode) {
|
14394 | parser.state.strict = true;
|
14395 | }
|
14396 | return parser.getExpression();
|
14397 | }
|
14398 | function generateExportedTokenTypes(internalTokenTypes) {
|
14399 | const tokenTypes = {};
|
14400 | for (const typeName of Object.keys(internalTokenTypes)) {
|
14401 | tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
|
14402 | }
|
14403 | return tokenTypes;
|
14404 | }
|
14405 | const tokTypes = generateExportedTokenTypes(tt);
|
14406 | function getParser(options, input) {
|
14407 | let cls = Parser;
|
14408 | if (options != null && options.plugins) {
|
14409 | validatePlugins(options.plugins);
|
14410 | cls = getParserClass(options.plugins);
|
14411 | }
|
14412 | return new cls(options, input);
|
14413 | }
|
14414 | const parserClassCache = {};
|
14415 | function getParserClass(pluginsFromOptions) {
|
14416 | const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
|
14417 | const key = pluginList.join("/");
|
14418 | let cls = parserClassCache[key];
|
14419 | if (!cls) {
|
14420 | cls = Parser;
|
14421 | for (const plugin of pluginList) {
|
14422 | cls = mixinPlugins[plugin](cls);
|
14423 | }
|
14424 | parserClassCache[key] = cls;
|
14425 | }
|
14426 | return cls;
|
14427 | }
|
14428 | exports.parse = parse;
|
14429 | exports.parseExpression = parseExpression;
|
14430 | exports.tokTypes = tokTypes;
|
14431 | //# sourceMappingURL=index.js.map
|