aboutsummaryrefslogtreecommitdiff
path: root/node_modules/meriyah/src/errors.ts
blob: 8424b6fb004ba684b0fa8edeab1aaf9daf9ace02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { ParserState } from './common';

export const enum Errors {
  Unexpected,
  StrictOctalEscape,
  TemplateOctalLiteral,
  InvalidPrivateIdentifier,
  InvalidUnicodeEscapeSequence,
  InvalidCodePoint,
  InvalidHexEscapeSequence,
  StrictDecimalWithLeadingZero,
  StrictOctalLiteral,
  ExpectedNumberInRadix,
  MissingExponent,
  InvalidBigInt,
  IDStartAfterNumber,
  InvalidEightAndNine,
  UnterminatedString,
  UnterminatedTemplate,
  UnterminatedComment,
  InvalidDynamicUnicode,
  IllegalCharacter,
  MissingHexDigits,
  InvalidImplicitOctals,
  InvalidStringLT,
  InvalidEscapeIdentifier,
  ExpectedToken,
  CantAssignTo,
  InvalidLHSAsyncArrow,
  SuperNoConstructor,
  InvalidSuperProperty,
  UnexpectedToken,
  AwaitInParameter,
  YieldInParameter,
  InvalidExponentiationLHS,
  UnterminatedRegExp,
  UnexpectedTokenRegExpFlag,
  DuplicateRegExpFlag,
  AccessorWrongArgs,
  BadSetterRestParameter,
  DeclNoName,
  StrictFunctionName,
  RestMissingArg,
  InvalidGeneratorGetter,
  InvalidComputedPropName,
  InvalidGetSetGenerator,
  InvalidAsyncGetter,
  InvalidGenMethodShorthand,
  InvalidLineBreak,
  InvalidArrowDestructLHS,
  InvalidBindingDestruct,
  InvalidAsyncArrow,
  StaticPrototype,
  InvalidConstructor,
  DuplicateConstructor,
  InvalidIncDecTarget,
  InvalidIncDecNew,
  InvalidAssignmentTarget,
  InvalidRestTrailing,
  DeclarationMissingInitializer,
  ForInOfLoopInitializer,
  ForInOfLoopMultiBindings,
  InvalidShorthandPropInit,
  DuplicateProto,
  InvalidLetBoundName,
  InvalidNewUnary,
  IllegalUseStrict,
  DisallowedLetInStrict,
  IllegalContinue,
  IllegalBreak,
  InvalidLetBracket,
  InvalidDestructuringTarget,
  RestDefaultInitializer,
  InvalidRestNotLast,
  InvalidRestArg,
  InvalidRestDefault,
  StrictFunction,
  SloppyFunction,
  WebCompatFunction,
  ClassForbiddenAsStatement,
  CantAssignToInOfForLoop,
  InvalidAssignmentInOfForLoop,
  InvalidForAwait,
  InvalidTemplateContinuation,
  RestrictedLetProduction,
  UnexpectedLetStrictReserved,
  InvalidCatchParams,
  InvalidCatchParamDefault,
  NoCatchOrFinally,
  MultipleDefaultsInSwitch,
  NewlineAfterThrow,
  StrictWith,
  IllegalReturn,
  InvalidForLHSBinding,
  InvalidNewTarget,
  InvalidEscapedKeyword,
  MissingPrivateIdentifier,
  DisallowedInContext,
  AwaitOutsideAsync,
  InvalidStrictLet,
  InvalidLetConstBinding,
  InvalidLetClassName,
  KeywordNotId,
  InvalidImportExportSloppy,
  UnicodeOverflow,
  InvalidExportImportSource,
  InvalidKeywordAsAlias,
  InvalidDefaultImport,
  TrailingDecorators,
  GeneratorConstructor,
  AwaitOrYieldIdentInModule,
  HtmlCommentInWebCompat,
  StrictInvalidLetInExprPos,
  NotAssignableLetArgs,
  ForOfLet,
  InvalidInvokedBlockBodyArrow,
  InvalidAccessedBlockBodyArrow,
  UnexpectedStrictReserved,
  StrictEvalArguments,
  InvalidDecoratorSemicolon,
  StrictDelete,
  InvalidPatternTail,
  AsyncFunctionInSingleStatementContext,
  InvalidTernaryYield,
  InvalidArrowPostfix,
  InvalidObjLitKeyStar,
  DeletePrivateField,
  InvalidStaticClassFieldConstructor,
  InvalidClassFieldConstructor,
  InvalidClassFieldArgEval,
  InvalidGeneratorFunction,
  AsyncRestrictedProd,
  UnexpectedCharAfterObjLit,
  InvalidObjLitKey,
  InvalidKeyToken,
  LabelRedeclaration,
  InvalidNestedStatement,
  UnknownLabel,
  InvalidImportTail,
  ImportNotOneArg,
  InvalidImportNew,
  InvalidSpreadInImport,
  UncompleteArrow,
  DuplicateBinding,
  DuplicateExportBinding,
  UndeclaredExportedBinding,
  UnexpectedPrivateField,
  DuplicateLetConstBinding,
  CantAssignToValidRHS,
  ContinuousNumericSeparator,
  TrailingNumericSeparator,
  InvalidJSXAttributeValue,
  ExpectedJSXClosingTag,
  AdjacentJSXElements,
  InvalidNonEmptyJSXExpr,
  DuplicateIdentifier,
  ShadowedCatchClause,
  InvalidDotProperty,
  UnclosedSpreadElement,
  CatchWithoutTry,
  FinallyWithoutTry,
  UnCorrespondingFragmentTag,
  InvalidCoalescing,
  OptionalChainingNoTemplate,
  OptionalChainingNoSuper,
  OptionalChainingNoNew,
  ImportMetaOutsideModule,
  InvalidLeadingDecorator
}

export const errorMessages: {
  [key: string]: string;
} = {
  [Errors.Unexpected]: 'Unexpected token',
  [Errors.UnexpectedToken]: "Unexpected token: '%0'",
  [Errors.StrictOctalEscape]: 'Octal escape sequences are not allowed in strict mode',
  [Errors.TemplateOctalLiteral]: 'Octal escape sequences are not allowed in template strings',
  [Errors.InvalidPrivateIdentifier]: 'Unexpected token `#`',
  [Errors.InvalidUnicodeEscapeSequence]: 'Illegal Unicode escape sequence',
  [Errors.InvalidCodePoint]: 'Invalid code point %0',
  [Errors.InvalidHexEscapeSequence]: 'Invalid hexadecimal escape sequence',
  [Errors.StrictOctalLiteral]: 'Octal literals are not allowed in strict mode',
  [Errors.StrictDecimalWithLeadingZero]: 'Decimal integer literals with a leading zero are forbidden in strict mode',
  [Errors.ExpectedNumberInRadix]: 'Expected number in radix %0',
  [Errors.CantAssignToValidRHS]: 'Invalid left-hand side assignment to a destructible right-hand side',
  [Errors.MissingExponent]: 'Non-number found after exponent indicator',
  [Errors.InvalidBigInt]: 'Invalid BigIntLiteral',
  [Errors.IDStartAfterNumber]: 'No identifiers allowed directly after numeric literal',
  [Errors.InvalidEightAndNine]: 'Escapes \\8 or \\9 are not syntactically valid escapes',
  [Errors.UnterminatedString]: 'Unterminated string literal',
  [Errors.UnterminatedTemplate]: 'Unterminated template literal',
  [Errors.UnterminatedComment]: 'Multiline comment was not closed properly',
  [Errors.InvalidDynamicUnicode]: 'The identifier contained dynamic unicode escape that was not closed',
  [Errors.IllegalCharacter]: "Illegal character '%0'",
  [Errors.MissingHexDigits]: 'Missing hexadecimal digits',
  [Errors.InvalidImplicitOctals]: 'Invalid implicit octal',
  [Errors.InvalidStringLT]: 'Invalid line break in string literal',
  [Errors.InvalidEscapeIdentifier]: 'Only unicode escapes are legal in identifier names',
  [Errors.ExpectedToken]: "Expected '%0'",
  [Errors.CantAssignTo]: 'Invalid left-hand side in assignment',
  [Errors.InvalidLHSAsyncArrow]: 'Invalid left-hand side in async arrow',
  [Errors.SuperNoConstructor]:
    'Calls to super must be in the "constructor" method of a class expression or class declaration that has a superclass',
  [Errors.InvalidSuperProperty]: 'Member access on super must be in a method',
  [Errors.AwaitInParameter]: 'Await expression not allowed in formal parameter',
  [Errors.YieldInParameter]: 'Yield expression not allowed in formal parameter',
  [Errors.InvalidEscapedKeyword]: "Unexpected token: 'escaped keyword'",
  [Errors.InvalidExponentiationLHS]:
    'Unary expressions as the left operand of an exponentiation expression must be disambiguated with parentheses',
  [Errors.AsyncFunctionInSingleStatementContext]:
    'Async functions can only be declared at the top level or inside a block',
  [Errors.UnterminatedRegExp]: 'Unterminated regular expression',
  [Errors.UnexpectedTokenRegExpFlag]: 'Unexpected regular expression flag',
  [Errors.DuplicateRegExpFlag]: "Duplicate regular expression flag '%0'",
  [Errors.AccessorWrongArgs]: '%0 functions must have exactly %1 argument%2',
  [Errors.BadSetterRestParameter]: 'Setter function argument must not be a rest parameter',
  [Errors.DeclNoName]: '%0 declaration must have a name in this context',
  [Errors.StrictFunctionName]:
    'Function name may not contain any reserved words or be eval or arguments in strict mode',
  [Errors.RestMissingArg]: 'The rest operator is missing an argument',
  [Errors.InvalidGeneratorGetter]: 'A getter cannot be a generator',
  [Errors.InvalidComputedPropName]: 'A computed property name must be followed by a colon or paren',
  [Errors.InvalidObjLitKey]: 'Object literal keys that are strings or numbers must be a method or have a colon',
  [Errors.InvalidAsyncGetter]: 'Found `* async x(){}` but this should be `async * x(){}`',
  [Errors.InvalidGetSetGenerator]: 'Getters and setters can not be generators',
  [Errors.InvalidGenMethodShorthand]: "'%0' can not be generator method",
  [Errors.InvalidLineBreak]: "No line break is allowed after '=>'",
  [Errors.InvalidArrowDestructLHS]: 'The left-hand side of the arrow can only be destructed through assignment',
  [Errors.InvalidBindingDestruct]: 'The binding declaration is not destructible',
  [Errors.InvalidAsyncArrow]: 'Async arrow can not be followed by new expression',
  [Errors.StaticPrototype]: "Classes may not have a static property named 'prototype'",
  [Errors.InvalidConstructor]: 'Class constructor may not be a %0',
  [Errors.DuplicateConstructor]: 'Duplicate constructor method in class',
  [Errors.InvalidIncDecTarget]: 'Invalid increment/decrement operand',
  [Errors.InvalidIncDecNew]: 'Invalid use of `new` keyword on an increment/decrement expression',
  [Errors.InvalidAssignmentTarget]: '`=>` is an invalid assignment target',
  [Errors.InvalidRestTrailing]: 'Rest element may not have a trailing comma',
  [Errors.DeclarationMissingInitializer]: 'Missing initializer in %0 declaration',
  [Errors.ForInOfLoopInitializer]: "'for-%0' loop head declarations can not have an initializer",
  [Errors.ForInOfLoopMultiBindings]: 'Invalid left-hand side in for-%0 loop: Must have a single binding',
  [Errors.InvalidShorthandPropInit]: 'Invalid shorthand property initializer',
  [Errors.DuplicateProto]: 'Property name __proto__ appears more than once in object literal',
  [Errors.InvalidLetBoundName]: 'Let is disallowed as a lexically bound name',
  [Errors.InvalidNewUnary]: "Invalid use of '%0' inside new expression",
  [Errors.IllegalUseStrict]: "Illegal 'use strict' directive in function with non-simple parameter list",
  [Errors.DisallowedLetInStrict]: 'Identifier "let" disallowed as left-hand side expression in strict mode',
  [Errors.IllegalContinue]: 'Illegal continue statement',
  [Errors.IllegalBreak]: 'Illegal break statement',
  [Errors.InvalidLetBracket]: 'Cannot have `let[...]` as a var name in strict mode',
  [Errors.InvalidDestructuringTarget]: 'Invalid destructuring assignment target',
  [Errors.RestDefaultInitializer]: 'Rest parameter may not have a default initializer',
  [Errors.InvalidRestNotLast]: 'The rest argument must the be last parameter',
  [Errors.InvalidRestArg]: 'Invalid rest argument',
  [Errors.StrictFunction]: 'In strict mode code, functions can only be declared at top level or inside a block',
  [Errors.SloppyFunction]:
    'In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement',
  [Errors.WebCompatFunction]:
    'Without web compatibility enabled functions can not be declared at top level, inside a block, or as the body of an if statement',
  [Errors.ClassForbiddenAsStatement]: "Class declaration can't appear in single-statement context",
  [Errors.CantAssignToInOfForLoop]: 'Invalid left-hand side in for-%0',
  [Errors.InvalidAssignmentInOfForLoop]: 'Invalid assignment in for-%0',
  [Errors.InvalidForAwait]: 'for await (... of ...) is only valid in async functions and async generators',
  [Errors.InvalidTemplateContinuation]:
    'The first token after the template expression should be a continuation of the template',
  [Errors.UnexpectedLetStrictReserved]:
    '`let` declaration not allowed here and `let` cannot be a regular var name in strict mode',
  [Errors.RestrictedLetProduction]: '`let \n [` is a restricted production at the start of a statement',
  [Errors.InvalidCatchParams]: 'Catch clause requires exactly one parameter, not more (and no trailing comma)',
  [Errors.InvalidCatchParamDefault]: 'Catch clause parameter does not support default values',
  [Errors.NoCatchOrFinally]: 'Missing catch or finally after try',
  [Errors.MultipleDefaultsInSwitch]: 'More than one default clause in switch statement',
  [Errors.NewlineAfterThrow]: 'Illegal newline after throw',
  [Errors.StrictWith]: 'Strict mode code may not include a with statement',
  [Errors.IllegalReturn]: 'Illegal return statement',
  [Errors.InvalidForLHSBinding]: 'The left hand side of the for-header binding declaration is not destructible',
  [Errors.InvalidNewTarget]: 'new.target only allowed within functions',
  [Errors.MissingPrivateIdentifier]: "'#' not followed by identifier",
  [Errors.KeywordNotId]: 'Invalid keyword',
  [Errors.InvalidLetClassName]: "Can not use 'let' as a class name",
  [Errors.InvalidLetConstBinding]: "'A lexical declaration can't define a 'let' binding",
  [Errors.InvalidStrictLet]: 'Can not use `let` as variable name in strict mode',
  [Errors.DisallowedInContext]: "'%0' may not be used as an identifier in this context",
  [Errors.AwaitOutsideAsync]: 'Await is only valid in async functions',
  [Errors.InvalidImportExportSloppy]: 'The %0 keyword can only be used with the module goal',
  [Errors.UnicodeOverflow]: 'Unicode codepoint must not be greater than 0x10FFFF',
  [Errors.InvalidExportImportSource]: '%0 source must be string',
  [Errors.InvalidKeywordAsAlias]: 'Only a identifier can be used to indicate alias',
  [Errors.InvalidDefaultImport]: "Only '*' or '{...}' can be imported after default",
  [Errors.TrailingDecorators]: 'Trailing decorator may be followed by method',
  [Errors.GeneratorConstructor]: "Decorators can't be used with a constructor",
  [Errors.HtmlCommentInWebCompat]: 'HTML comments are only allowed with web compatibility (Annex B)',
  [Errors.StrictInvalidLetInExprPos]: "The identifier 'let' must not be in expression position in strict mode",
  [Errors.NotAssignableLetArgs]: 'Cannot assign to `eval` and `arguments` in strict mode',
  [Errors.ForOfLet]: "The left-hand side of a for-of loop may not start with 'let'",
  [Errors.InvalidInvokedBlockBodyArrow]: 'Block body arrows can not be immediately invoked without a group',
  [Errors.InvalidAccessedBlockBodyArrow]: 'Block body arrows can not be immediately accessed without a group',
  [Errors.UnexpectedStrictReserved]: 'Unexpected strict mode reserved word',
  [Errors.StrictEvalArguments]: 'Unexpected eval or arguments in strict mode',
  [Errors.InvalidDecoratorSemicolon]: 'Decorators must not be followed by a semicolon',
  [Errors.StrictDelete]: 'Calling delete on expression not allowed in strict mode',
  [Errors.InvalidPatternTail]: 'Pattern can not have a tail',
  [Errors.InvalidTernaryYield]: 'Can not have a `yield` expression on the left side of a ternary',
  [Errors.InvalidArrowPostfix]: 'An arrow function can not have a postfix update operator',
  [Errors.InvalidObjLitKeyStar]: 'Invalid object literal key character after generator star',
  [Errors.DeletePrivateField]: 'Private fields can not be deleted',
  [Errors.InvalidClassFieldConstructor]: 'Classes may not have a field called constructor',
  [Errors.InvalidStaticClassFieldConstructor]: 'Classes may not have a private element named constructor',
  [Errors.InvalidClassFieldArgEval]: 'A class field initializer may not contain arguments',
  [Errors.InvalidGeneratorFunction]: 'Generators can only be declared at the top level or inside a block',
  [Errors.AsyncRestrictedProd]: 'Async methods are a restricted production and cannot have a newline following it',
  [Errors.UnexpectedCharAfterObjLit]: 'Unexpected character after object literal property name',
  [Errors.InvalidKeyToken]: 'Invalid key token',
  [Errors.LabelRedeclaration]: "Label '%0' has already been declared",
  [Errors.InvalidNestedStatement]: 'continue statement must be nested within an iteration statement',
  [Errors.UnknownLabel]: "Undefined label '%0'",
  [Errors.InvalidImportTail]: 'Trailing comma is disallowed inside import(...) arguments',
  [Errors.ImportNotOneArg]: 'import() requires exactly one argument',
  [Errors.InvalidImportNew]: 'Cannot use new with import(...)',
  [Errors.InvalidSpreadInImport]: '... is not allowed in import()',
  [Errors.UncompleteArrow]: "Expected '=>'",
  [Errors.DuplicateBinding]: "Duplicate binding '%0'",
  [Errors.DuplicateExportBinding]: "Cannot export a duplicate name '%0'",
  [Errors.DuplicateLetConstBinding]: 'Duplicate %0 for-binding',
  [Errors.UndeclaredExportedBinding]: "Exported binding '%0' needs to refer to a top-level declared variable",
  [Errors.UnexpectedPrivateField]: 'Unexpected private field',
  [Errors.TrailingNumericSeparator]: 'Numeric separators are not allowed at the end of numeric literals',
  [Errors.ContinuousNumericSeparator]: 'Only one underscore is allowed as numeric separator',
  [Errors.InvalidJSXAttributeValue]: 'JSX value should be either an expression or a quoted JSX text',
  [Errors.ExpectedJSXClosingTag]: 'Expected corresponding JSX closing tag for %0',
  [Errors.AdjacentJSXElements]: 'Adjacent JSX elements must be wrapped in an enclosing tag',
  [Errors.InvalidNonEmptyJSXExpr]: "JSX attributes must only be assigned a non-empty 'expression'",
  [Errors.DuplicateIdentifier]: "'%0' has already been declared",
  [Errors.ShadowedCatchClause]: "'%0' shadowed a catch clause binding",
  [Errors.InvalidDotProperty]: 'Dot property must be an identifier',
  [Errors.UnclosedSpreadElement]: 'Encountered invalid input after spread/rest argument',
  [Errors.CatchWithoutTry]: 'Catch without try',
  [Errors.FinallyWithoutTry]: 'Finally without try',
  [Errors.UnCorrespondingFragmentTag]: 'Expected corresponding closing tag for JSX fragment',
  [Errors.InvalidCoalescing]:
    'Coalescing and logical operators used together in the same expression must be disambiguated with parentheses',
  [Errors.OptionalChainingNoTemplate]: 'Invalid tagged template on optional chain',
  [Errors.OptionalChainingNoSuper]: 'Invalid optional chain from super property',
  [Errors.OptionalChainingNoNew]: 'Invalid optional chain from new expression',
  [Errors.ImportMetaOutsideModule]: 'Cannot use "import.meta" outside a module',
  [Errors.InvalidLeadingDecorator]: 'Leading decorators must be attached to a class declaration'
};

export class ParseError extends SyntaxError {
  public loc: {
    line: ParseError['line'];
    column: ParseError['column'];
  };
  public index: number;
  public line: number;
  public column: number;
  public description: string;
  /*eslint-disable*/
  constructor(startindex: number, line: number, column: number, type: Errors, ...params: string[]) {
    const message =
      '[' + line + ':' + column + ']: ' + errorMessages[type].replace(/%(\d+)/g, (_: string, i: number) => params[i]);
    super(`${message}`);
    this.index = startindex;
    this.line = line;
    this.column = column;
    this.description = message;
    /* Acorn compat */
    this.loc = {
      line,
      column
    } as any;
  }
}
/**
 * Throws an error
 *
 * @export
 * @param {ParserState} state
 * @param {Errors} type
 * @param {...string[]} params
 * @returns {never}
 */
export function report(parser: ParserState, type: Errors, ...params: string[]): never {
  throw new ParseError(parser.index, parser.line, parser.column, type, ...params);
}

export function reportScopeError(scope: any): never {
  throw new ParseError(scope.index, scope.line, scope.column, scope.type, scope.params);
}

/**
 * Throws an error at a given position
 *
 * @export
 * @param {ParserState} state
 * @param {number} index
 * @param {number} line
 * @param {number} column
 * @param {Errors} type
 * @param {...string[]} params
 */
export function reportMessageAt(index: number, line: number, column: number, type: Errors, ...params: string[]): never {
  throw new ParseError(index, line, column, type, ...params);
}

/**
 * Throws an error at a given position
 *
 * @export
 * @param {ParserState} state
 * @param {number} index
 * @param {number} line
 * @param {number} column
 * @param {Errors} type
 */
export function reportScannerError(index: number, line: number, column: number, type: Errors): never {
  throw new ParseError(index, line, column, type);
}