meriyah 4.5.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/CHANGELOG.md +415 -445
  2. package/README.md +52 -34
  3. package/dist/meriyah.cjs +9203 -8807
  4. package/dist/meriyah.min.mjs +1 -0
  5. package/dist/{meriyah.esm.js → meriyah.mjs} +9203 -8807
  6. package/dist/meriyah.umd.js +9203 -8807
  7. package/dist/meriyah.umd.min.js +1 -1
  8. package/dist/src/chars.d.ts +135 -135
  9. package/dist/src/common.d.ts +225 -200
  10. package/dist/src/common.d.ts.map +1 -1
  11. package/dist/src/errors.d.ts +197 -187
  12. package/dist/src/errors.d.ts.map +1 -1
  13. package/dist/src/estree.d.ts +524 -507
  14. package/dist/src/estree.d.ts.map +1 -1
  15. package/dist/src/lexer/charClassifier.d.ts +24 -24
  16. package/dist/src/lexer/charClassifier.d.ts.map +1 -1
  17. package/dist/src/lexer/comments.d.ts +14 -14
  18. package/dist/src/lexer/common.d.ts +25 -26
  19. package/dist/src/lexer/common.d.ts.map +1 -1
  20. package/dist/src/lexer/decodeHTML.d.ts +1 -1
  21. package/dist/src/lexer/decodeHTML.d.ts.map +1 -1
  22. package/dist/src/lexer/identifier.d.ts +8 -8
  23. package/dist/src/lexer/identifier.d.ts.map +1 -1
  24. package/dist/src/lexer/index.d.ts +9 -9
  25. package/dist/src/lexer/index.d.ts.map +1 -1
  26. package/dist/src/lexer/jsx.d.ts +6 -6
  27. package/dist/src/lexer/jsx.d.ts.map +1 -1
  28. package/dist/src/lexer/numeric.d.ts +5 -5
  29. package/dist/src/lexer/numeric.d.ts.map +1 -1
  30. package/dist/src/lexer/regexp.d.ts +3 -3
  31. package/dist/src/lexer/regexp.d.ts.map +1 -1
  32. package/dist/src/lexer/scan.d.ts +6 -6
  33. package/dist/src/lexer/scan.d.ts.map +1 -1
  34. package/dist/src/lexer/string.d.ts +12 -12
  35. package/dist/src/lexer/string.d.ts.map +1 -1
  36. package/dist/src/lexer/template.d.ts +4 -4
  37. package/dist/src/lexer/template.d.ts.map +1 -1
  38. package/dist/src/meriyah.d.ts +7 -7
  39. package/dist/src/meriyah.d.ts.map +1 -1
  40. package/dist/src/parser.d.ts +119 -118
  41. package/dist/src/parser.d.ts.map +1 -1
  42. package/dist/src/token.d.ts +167 -167
  43. package/dist/src/token.d.ts.map +1 -1
  44. package/dist/src/unicode.d.ts +5 -5
  45. package/package.json +44 -48
  46. package/dist/meriyah.amd.js +0 -8854
  47. package/dist/meriyah.amd.min.js +0 -1
  48. package/dist/meriyah.cjs.js +0 -8852
  49. package/dist/meriyah.cjs.min.js +0 -1
  50. package/dist/meriyah.esm.min.js +0 -1
  51. package/dist/meriyah.esm.min.mjs +0 -1
  52. package/dist/meriyah.esm.mjs +0 -8846
  53. package/dist/meriyah.iife.js +0 -8857
  54. package/dist/meriyah.iife.min.js +0 -1
  55. package/dist/meriyah.min.cjs +0 -1
  56. package/dist/meriyah.system.js +0 -8860
  57. package/dist/meriyah.system.min.js +0 -1
  58. package/dist/meriyah.umd.cjs +0 -8858
  59. package/dist/meriyah.umd.es5.js +0 -8927
  60. package/dist/meriyah.umd.es5.min.js +0 -1
  61. package/dist/meriyah.umd.min.cjs +0 -1
  62. package/src/chars.ts +0 -155
  63. package/src/common.ts +0 -841
  64. package/src/errors.ts +0 -419
  65. package/src/estree.ts +0 -817
  66. package/src/lexer/charClassifier.ts +0 -449
  67. package/src/lexer/comments.ts +0 -178
  68. package/src/lexer/common.ts +0 -140
  69. package/src/lexer/decodeHTML.ts +0 -2186
  70. package/src/lexer/identifier.ts +0 -196
  71. package/src/lexer/index.ts +0 -32
  72. package/src/lexer/jsx.ts +0 -126
  73. package/src/lexer/numeric.ts +0 -259
  74. package/src/lexer/regexp.ts +0 -156
  75. package/src/lexer/scan.ts +0 -655
  76. package/src/lexer/string.ts +0 -242
  77. package/src/lexer/template.ts +0 -108
  78. package/src/meriyah.ts +0 -29
  79. package/src/parser.ts +0 -9210
  80. package/src/token.ts +0 -307
  81. package/src/unicode.ts +0 -36
package/src/common.ts DELETED
@@ -1,841 +0,0 @@
1
- import { Token, KeywordDescTable } from './token';
2
- import { Errors, report } from './errors';
3
- import { Node, Comment, Decorator, SourceLocation } from './estree';
4
- import { nextToken } from './lexer/scan';
5
-
6
- /**
7
- * The core context, passed around everywhere as a simple immutable bit set
8
- */
9
- export const enum Context {
10
- None = 0,
11
- OptionsNext = 1 << 0,
12
- OptionsRanges = 1 << 1,
13
- OptionsLoc = 1 << 2,
14
- OptionsDirectives = 1 << 3,
15
- OptionsJSX = 1 << 4,
16
- OptionsGlobalReturn = 1 << 5,
17
- OptionsLexical = 1 << 6,
18
- OptionsPreserveParens = 1 << 7,
19
- OptionsWebCompat = 1 << 8,
20
- OptionsRaw = 1 << 9,
21
- Strict = 1 << 10,
22
- Module = 1 << 11, // Current code should be parsed as a module body
23
- InSwitch = 1 << 12,
24
- InGlobal = 1 << 13,
25
- InClass = 1 << 14,
26
- AllowRegExp = 1 << 15,
27
- TaggedTemplate = 1 << 16,
28
- InIteration = 1 << 17,
29
- SuperProperty = 1 << 18,
30
- SuperCall = 1 << 19,
31
- InYieldContext = 1 << 21,
32
- InAwaitContext = 1 << 22,
33
- InArgumentList = 1 << 23,
34
- InConstructor = 1 << 24,
35
- InMethod = 1 << 25,
36
- AllowNewTarget = 1 << 26,
37
- DisallowIn = 1 << 27,
38
- AllowEscapedKeyword = 1 << 28,
39
- OptionsUniqueKeyInPattern = 1 << 29,
40
- }
41
-
42
- /**
43
- * Masks to track the property kind
44
- */
45
- export const enum PropertyKind {
46
- None = 0,
47
- Method = 1 << 0,
48
- Computed = 1 << 1,
49
- Shorthand = 1 << 2,
50
- Generator = 1 << 3,
51
- Async = 1 << 4,
52
- Static = 1 << 5,
53
- Constructor = 1 << 6,
54
- ClassField = 1 << 7,
55
- Getter = 1 << 8,
56
- Setter = 1 << 9,
57
- Extends = 1 << 10,
58
- Literal = 1 << 11,
59
- PrivateField = 1 << 12,
60
- GetSet = Getter | Setter
61
- }
62
-
63
- /**
64
- * Masks to track the binding kind
65
- */
66
- export const enum BindingKind {
67
- None = 0,
68
- ArgumentList = 1 << 0,
69
- Empty = 1 << 1,
70
- Variable = 1 << 2,
71
- Let = 1 << 3,
72
- Const = 1 << 4,
73
- Class = 1 << 5,
74
- FunctionLexical = 1 << 6,
75
- FunctionStatement = 1 << 7,
76
- CatchPattern = 1 << 8,
77
- CatchIdentifier = 1 << 9,
78
- CatchIdentifierOrPattern = CatchIdentifier | CatchPattern,
79
- LexicalOrFunction = Variable | FunctionLexical,
80
- LexicalBinding = Let | Const | FunctionLexical | FunctionStatement | Class
81
- }
82
-
83
- /**
84
- * The masks to track where something begins. E.g. statements, declarations or arrows.
85
- */
86
- export const enum Origin {
87
- None = 0,
88
- Statement = 1 << 0,
89
- BlockStatement = 1 << 1,
90
- TopLevel = 1 << 2,
91
- Declaration = 1 << 3,
92
- Arrow = 1 << 4,
93
- ForStatement = 1 << 5,
94
- Export = 1 << 6,
95
- }
96
-
97
- /**
98
- * Masks to track the assignment kind
99
- */
100
- export const enum AssignmentKind {
101
- None = 0,
102
- Assignable = 1 << 0,
103
- CannotAssign = 1 << 1
104
- }
105
-
106
- /**
107
- * Masks to track the destructuring kind
108
- */
109
- export const enum DestructuringKind {
110
- None = 0,
111
- HasToDestruct = 1 << 3,
112
- // "Cannot" rather than "can" so that this flag can be ORed together across
113
- // multiple characters.
114
- CannotDestruct = 1 << 4,
115
- // Only destructible if assignable
116
- Assignable = 1 << 5,
117
- // `__proto__` is a special case and only valid to parse if destructible
118
- SeenProto = 1 << 6,
119
- Await = 1 << 7,
120
- Yield = 1 << 8
121
- }
122
-
123
- /**
124
- * The mutable parser flags, in case any flags need passed by reference.
125
- */
126
- export const enum Flags {
127
- None = 0,
128
- NewLine = 1 << 0,
129
- HasConstructor = 1 << 5,
130
- Octals = 1 << 6,
131
- SimpleParameterList = 1 << 7,
132
- HasStrictReserved = 1 << 8,
133
- StrictEvalArguments = 1 << 9,
134
- DisallowCall = 1 << 10,
135
- HasOptionalChaining = 1 << 11
136
- }
137
-
138
- export const enum HoistedClassFlags {
139
- None,
140
- Hoisted = 1 << 0,
141
- Export = 1 << 1
142
- }
143
-
144
- export const enum HoistedFunctionFlags {
145
- None,
146
- Hoisted = 1 << 0,
147
- Export = 1 << 1
148
- }
149
-
150
- /**
151
- * Scope kinds
152
- */
153
- export const enum ScopeKind {
154
- None = 0,
155
- ForStatement = 1 << 0,
156
- Block = 1 << 1,
157
- CatchStatement = 1 << 2,
158
- SwitchStatement = 1 << 3,
159
- ArgList = 1 << 4,
160
- TryStatement = 1 << 5,
161
- CatchBlock = 1 << 6,
162
- FunctionBody = 1 << 7,
163
- FunctionRoot = 1 << 8,
164
- FunctionParams = 1 << 9,
165
- ArrowParams = 1 << 10,
166
- CatchIdentifier = 1 << 11,
167
- }
168
-
169
- /**
170
- * The type of the `onComment` option.
171
- */
172
- export type OnComment = void | Comment[] | ((type: string, value: string, start: number, end: number, loc: SourceLocation) => any);
173
-
174
- /**
175
- * The type of the `onInsertedSemicolon` option.
176
- */
177
- export type OnInsertedSemicolon = void | ((pos: number) => any);
178
-
179
- /**
180
- * The type of the `onToken` option.
181
- */
182
- export type OnToken = void | Token[] | ((token: string, start: number, end: number, loc: SourceLocation) => any);
183
-
184
- /**
185
- * Lexical scope interface
186
- */
187
- export interface ScopeState {
188
- parent: ScopeState | undefined;
189
- type: ScopeKind;
190
- scopeError?: ScopeError | null;
191
- }
192
-
193
- /** Scope error interface */
194
- export interface ScopeError {
195
- type: Errors;
196
- params: string[];
197
- index: number;
198
- line: number;
199
- column: number;
200
- }
201
-
202
- /**
203
- * The parser interface.
204
- */
205
- export interface ParserState {
206
- source: string;
207
- flags: Flags;
208
- index: number;
209
- line: number;
210
- column: number;
211
- tokenPos: number;
212
- startPos: number;
213
- startColumn: number;
214
- startLine: number;
215
- colPos: number;
216
- linePos: number;
217
- end: number;
218
- token: Token;
219
- onComment: any;
220
- onInsertedSemicolon: any;
221
- onToken: any;
222
- tokenValue: any;
223
- tokenRaw: string;
224
- tokenRegExp: void | {
225
- pattern: string;
226
- flags: string;
227
- };
228
- sourceFile: string | void;
229
- assignable: AssignmentKind | DestructuringKind;
230
- destructible: AssignmentKind | DestructuringKind;
231
- currentChar: number;
232
- exportedNames: any;
233
- exportedBindings: any;
234
- leadingDecorators: Decorator[];
235
- }
236
-
237
- /**
238
- * Check for automatic semicolon insertion according to the rules
239
- * given in `ECMA-262, section 11.9`.
240
- *
241
- * @param parser Parser object
242
- * @param context Context masks
243
- */
244
-
245
- export function matchOrInsertSemicolon(parser: ParserState, context: Context): void {
246
-
247
- if (
248
- (parser.flags & Flags.NewLine) === 0 &&
249
- (parser.token & Token.IsAutoSemicolon) !== Token.IsAutoSemicolon
250
- ) {
251
- report(parser, Errors.UnexpectedToken, KeywordDescTable[parser.token & Token.Type]);
252
- }
253
-
254
- if (!consumeOpt(parser, context, Token.Semicolon)) {
255
- // Automatic semicolon insertion has occurred
256
- parser.onInsertedSemicolon?.(parser.startPos);
257
- }
258
- }
259
-
260
- export function isValidStrictMode(parser: ParserState, index: number, tokenPos: number, tokenValue: string): 0 | 1 {
261
- if (index - tokenPos < 13 && tokenValue === 'use strict') {
262
- if ((parser.token & Token.IsAutoSemicolon) === Token.IsAutoSemicolon || parser.flags & Flags.NewLine) {
263
- return 1;
264
- }
265
- }
266
- return 0;
267
- }
268
-
269
- /**
270
- * Consumes the current token if the current token kind is
271
- * the specified `kind` and returns `0`. Otherwise returns `1`.
272
- *
273
- * @param parser Parser state
274
- * @param context Context masks
275
- * @param token The type of token to consume
276
- */
277
- export function optionalBit(parser: ParserState, context: Context, t: Token): 0 | 1 {
278
- if (parser.token !== t) return 0;
279
- nextToken(parser, context);
280
- return 1;
281
- }
282
-
283
- /** Consumes the current token if the current token kind is
284
- * the specified `kind` and returns `true`. Otherwise returns
285
- * `false`.
286
- *
287
- * @param parser Parser state
288
- * @param context Context masks
289
- * @param token The type of token to consume
290
- */
291
- export function consumeOpt(parser: ParserState, context: Context, t: Token): boolean {
292
- if (parser.token !== t) return false;
293
- nextToken(parser, context);
294
- return true;
295
- }
296
-
297
- /**
298
- * Consumes the current token. If the current token kind is not
299
- * the specified `kind`, an error will be reported.
300
- *
301
- * @param parser Parser state
302
- * @param context Context masks
303
- * @param t The type of token to consume
304
- */
305
- export function consume(parser: ParserState, context: Context, t: Token): void {
306
- if (parser.token !== t) report(parser, Errors.ExpectedToken, KeywordDescTable[t & Token.Type]);
307
- nextToken(parser, context);
308
- }
309
-
310
- /**
311
- * Transforms a `LeftHandSideExpression` into a `AssignmentPattern` if possible,
312
- * otherwise it returns the original tree.
313
- *
314
- * @param parser Parser state
315
- * @param {*} node
316
- */
317
- export function reinterpretToPattern(state: ParserState, node: any): void {
318
- switch (node.type) {
319
- case 'ArrayExpression':
320
- node.type = 'ArrayPattern';
321
- const elements = node.elements;
322
- for (let i = 0, n = elements.length; i < n; ++i) {
323
- const element = elements[i];
324
- if (element) reinterpretToPattern(state, element);
325
- }
326
- return;
327
- case 'ObjectExpression':
328
- node.type = 'ObjectPattern';
329
- const properties = node.properties;
330
- for (let i = 0, n = properties.length; i < n; ++i) {
331
- reinterpretToPattern(state, properties[i]);
332
- }
333
- return;
334
- case 'AssignmentExpression':
335
- node.type = 'AssignmentPattern';
336
- if (node.operator !== '=') report(state, Errors.InvalidDestructuringTarget);
337
- delete node.operator;
338
- reinterpretToPattern(state, node.left);
339
- return;
340
- case 'Property':
341
- reinterpretToPattern(state, node.value);
342
- return;
343
- case 'SpreadElement':
344
- node.type = 'RestElement';
345
- reinterpretToPattern(state, node.argument);
346
- default: // ignore
347
- }
348
- }
349
-
350
- /**
351
- * Validates binding identifier
352
- *
353
- * @param parser Parser state
354
- * @param context Context masks
355
- * @param type Binding type
356
- * @param token Token
357
- */
358
-
359
- export function validateBindingIdentifier(
360
- parser: ParserState,
361
- context: Context,
362
- kind: BindingKind,
363
- t: Token,
364
- skipEvalArgCheck: 0 | 1
365
- ): void {
366
-
367
- if (context & Context.Strict) {
368
-
369
- if ((t & Token.FutureReserved) === Token.FutureReserved) {
370
- report(parser, Errors.UnexpectedStrictReserved);
371
- }
372
-
373
- if (!skipEvalArgCheck && (t & Token.IsEvalOrArguments) === Token.IsEvalOrArguments) {
374
- report(parser, Errors.StrictEvalArguments);
375
- }
376
- }
377
-
378
- if ((t & Token.Reserved) === Token.Reserved) {
379
- report(parser, Errors.KeywordNotId);
380
- }
381
-
382
- // The BoundNames of LexicalDeclaration and ForDeclaration must not
383
- // contain 'let'. (CatchParameter is the only lexical binding form
384
- // without this restriction.)
385
- if (kind & (BindingKind.Let | BindingKind.Const) && t === Token.LetKeyword) {
386
- report(parser, Errors.InvalidLetConstBinding);
387
- }
388
-
389
- if (context & (Context.InAwaitContext | Context.Module) && t === Token.AwaitKeyword) {
390
- report(parser, Errors.AwaitOutsideAsync);
391
- }
392
-
393
- if (context & (Context.InYieldContext | Context.Strict) && t === Token.YieldKeyword) {
394
- report(parser, Errors.DisallowedInContext, 'yield');
395
- }
396
- }
397
-
398
- export function validateFunctionName(
399
- parser: ParserState,
400
- context: Context,
401
- t: Token
402
- ): void {
403
-
404
- if (context & Context.Strict) {
405
-
406
- if ((t & Token.FutureReserved) === Token.FutureReserved) {
407
- report(parser, Errors.UnexpectedStrictReserved);
408
- }
409
-
410
- if ((t & Token.IsEvalOrArguments) === Token.IsEvalOrArguments) {
411
- report(parser, Errors.StrictEvalArguments);
412
- }
413
-
414
- if (t === Token.EscapedFutureReserved) {
415
- report(parser, Errors.InvalidEscapedKeyword);
416
- }
417
-
418
- if (t === Token.EscapedReserved) {
419
- report(parser, Errors.InvalidEscapedKeyword);
420
- }
421
- }
422
-
423
- if ((t & Token.Reserved) === Token.Reserved) {
424
- report(parser, Errors.KeywordNotId);
425
- }
426
-
427
- if (context & (Context.InAwaitContext | Context.Module) && t === Token.AwaitKeyword) {
428
- report(parser, Errors.AwaitOutsideAsync);
429
- }
430
-
431
- if (context & (Context.InYieldContext | Context.Strict) && t === Token.YieldKeyword) {
432
- report(parser, Errors.DisallowedInContext, 'yield');
433
- }
434
- }
435
-
436
- /**
437
- * Validates binding identifier
438
- *
439
- * @param parser Parser state
440
- * @param context Context masks
441
- * @param t Token
442
- */
443
-
444
- export function isStrictReservedWord(parser: ParserState, context: Context, t: Token): boolean {
445
- if (t === Token.AwaitKeyword) {
446
- if (context & (Context.InAwaitContext | Context.Module)) report(parser, Errors.AwaitOutsideAsync);
447
- parser.destructible |= DestructuringKind.Await;
448
- }
449
-
450
- if (t === Token.YieldKeyword && context & Context.InYieldContext) report(parser, Errors.DisallowedInContext, 'yield');
451
-
452
- return (
453
- (t & Token.Reserved) === Token.Reserved ||
454
- (t & Token.FutureReserved) === Token.FutureReserved ||
455
- t == Token.EscapedFutureReserved
456
- );
457
- }
458
-
459
- /**
460
- * Checks if the property has any private field key
461
- *
462
- * @param parser Parser object
463
- * @param context Context masks
464
- */
465
- export function isPropertyWithPrivateFieldKey(expr: any): boolean {
466
- return !expr.property ? false : expr.property.type === 'PrivateIdentifier';
467
- }
468
-
469
- /**
470
- * Checks if a label in `LabelledStatement` are valid or not
471
- *
472
- * @param parser Parser state
473
- * @param labels Object holding the labels
474
- * @param name Current label
475
- * @param isIterationStatement
476
- */
477
- export function isValidLabel(parser: ParserState, labels: any, name: string, isIterationStatement: 0 | 1): 0 | 1 {
478
- while (labels) {
479
- if (labels['$' + name]) {
480
- if (isIterationStatement) report(parser, Errors.InvalidNestedStatement);
481
- return 1;
482
- }
483
- if (isIterationStatement && labels.loop) isIterationStatement = 0;
484
- labels = labels['$'];
485
- }
486
-
487
- return 0;
488
- }
489
-
490
- /**
491
- * Checks if current label already have been declrared, and if not
492
- * declare it
493
- *
494
- * @param parser Parser state
495
- * @param labels Object holding the labels
496
- * @param name Current label
497
- */
498
- export function validateAndDeclareLabel(parser: ParserState, labels: any, name: string): void {
499
- let set = labels;
500
- while (set) {
501
- if (set['$' + name]) report(parser, Errors.LabelRedeclaration, name);
502
- set = set['$'];
503
- }
504
-
505
- labels['$' + name] = 1;
506
- }
507
-
508
- export function finishNode<T extends Node>(
509
- parser: ParserState,
510
- context: Context,
511
- start: number,
512
- line: number,
513
- column: number,
514
- node: T
515
- ): T {
516
- if (context & Context.OptionsRanges) {
517
- node.start = start;
518
- node.end = parser.startPos;
519
- node.range = [start, parser.startPos];
520
- }
521
-
522
- if (context & Context.OptionsLoc) {
523
- node.loc = {
524
- start: {
525
- line,
526
- column
527
- },
528
- end: {
529
- line: parser.startLine,
530
- column: parser.startColumn
531
- }
532
- };
533
-
534
- if (parser.sourceFile) {
535
- node.loc.source = parser.sourceFile;
536
- }
537
- }
538
-
539
- return node;
540
- }
541
-
542
- /** @internal */
543
- export function isEqualTagName(elementName: any): any {
544
- switch (elementName.type) {
545
- case 'JSXIdentifier':
546
- return elementName.name;
547
- case 'JSXNamespacedName':
548
- return elementName.namespace + ':' + elementName.name;
549
- case 'JSXMemberExpression':
550
- return isEqualTagName(elementName.object) + '.' + isEqualTagName(elementName.property);
551
- /* istanbul ignore next */
552
- default:
553
- // ignore
554
- }
555
- }
556
-
557
- /**
558
- * Create a parsing scope for arrow head, and add lexical binding
559
- *
560
- * @param parser Parser state
561
- * @param context Context masks
562
- * @param value Binding name to be declared
563
- */
564
- export function createArrowHeadParsingScope(parser: ParserState, context: Context, value: string): ScopeState {
565
- const scope = addChildScope(createScope(), ScopeKind.ArrowParams);
566
- addBlockName(parser, context, scope, value, BindingKind.ArgumentList, Origin.None);
567
- return scope;
568
- }
569
-
570
- /**
571
- * Record duplicate binding errors that may occur in a arrow head or function parameters
572
- *
573
- * @param parser Parser state
574
- * @param type Errors type
575
- */
576
- export function recordScopeError(parser: ParserState, type: Errors, ...params: string[]): ScopeError {
577
- const { index, line, column } = parser;
578
- return {
579
- type,
580
- params,
581
- index,
582
- line,
583
- column
584
- };
585
- }
586
-
587
- /**
588
- * Creates a block scope
589
- */
590
- export function createScope(): ScopeState {
591
- return {
592
- parent: void 0,
593
- type: ScopeKind.Block
594
- };
595
- }
596
-
597
- /**
598
- * Inherit scope
599
- *
600
- * @param scope Parser object
601
- * @param type Scope kind
602
- */
603
- export function addChildScope(parent: ScopeState | undefined, type: ScopeKind): ScopeState {
604
- return {
605
- parent,
606
- type,
607
- scopeError: void 0
608
- };
609
- }
610
-
611
- /**
612
- * Adds either a var binding or a block scoped binding.
613
- *
614
- * @param parser Parser state
615
- * @param context Context masks
616
- * @param scope Scope state
617
- * @param name Binding name
618
- * @param type Binding kind
619
- * @param origin Binding Origin
620
- */
621
- export function addVarOrBlock(
622
- parser: ParserState,
623
- context: Context,
624
- scope: ScopeState,
625
- name: string,
626
- kind: BindingKind,
627
- origin: Origin
628
- ) {
629
- if (kind & BindingKind.Variable) {
630
- addVarName(parser, context, scope, name, kind);
631
- } else {
632
- addBlockName(parser, context, scope, name, kind, origin);
633
- }
634
- if (origin & Origin.Export) {
635
- declareUnboundVariable(parser, name);
636
- }
637
- }
638
-
639
- /**
640
- * Adds block scoped binding
641
- *
642
- * @param parser Parser state
643
- * @param context Context masks
644
- * @param scope Scope state
645
- * @param name Binding name
646
- * @param type Binding kind
647
- * @param origin Binding Origin
648
- */
649
- export function addBlockName(
650
- parser: ParserState,
651
- context: Context,
652
- scope: any,
653
- name: string,
654
- kind: BindingKind,
655
- origin: Origin
656
- ) {
657
- const value = (scope as any)['#' + name];
658
-
659
- if (value && (value & BindingKind.Empty) === 0) {
660
- if (kind & BindingKind.ArgumentList) {
661
- scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
662
- } else if (
663
- context & Context.OptionsWebCompat &&
664
- value & BindingKind.FunctionLexical &&
665
- origin & Origin.BlockStatement
666
- ) {
667
- } else {
668
- report(parser, Errors.DuplicateBinding, name);
669
- }
670
- }
671
-
672
- if (
673
- scope.type & ScopeKind.FunctionBody &&
674
- ((scope as any).parent['#' + name] && ((scope as any).parent['#' + name] & BindingKind.Empty) === 0)
675
- ) {
676
- report(parser, Errors.DuplicateBinding, name);
677
- }
678
-
679
- if (scope.type & ScopeKind.ArrowParams && value && (value & BindingKind.Empty) === 0) {
680
- if (kind & BindingKind.ArgumentList) {
681
- scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
682
- }
683
- }
684
-
685
- if (scope.type & ScopeKind.CatchBlock) {
686
- if ((scope as any).parent['#' + name] & BindingKind.CatchIdentifierOrPattern)
687
- report(parser, Errors.ShadowedCatchClause, name);
688
- }
689
-
690
- (scope as any)['#' + name] = kind;
691
- }
692
-
693
- /**
694
- * Adds a variable binding
695
- *
696
- * @param parser Parser state
697
- * @param context Context masks
698
- * @param scope Scope state
699
- * @param name Binding name
700
- * @param type Binding kind
701
- */
702
- export function addVarName(
703
- parser: ParserState,
704
- context: Context,
705
- scope: ScopeState,
706
- name: string,
707
- kind: BindingKind
708
- ): void {
709
- let currentScope: any = scope;
710
-
711
- while (currentScope && (currentScope.type & ScopeKind.FunctionRoot) === 0) {
712
- const value: ScopeKind = currentScope['#' + name];
713
-
714
- if (value & BindingKind.LexicalBinding) {
715
- if (
716
- context & Context.OptionsWebCompat &&
717
- (context & Context.Strict) === 0 &&
718
- ((kind & BindingKind.FunctionStatement && value & BindingKind.LexicalOrFunction) ||
719
- (value & BindingKind.FunctionStatement && kind & BindingKind.LexicalOrFunction))
720
- ) {
721
- } else {
722
- report(parser, Errors.DuplicateBinding, name);
723
- }
724
- }
725
- if (currentScope === scope) {
726
- if (value & BindingKind.ArgumentList && kind & BindingKind.ArgumentList) {
727
- currentScope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
728
- }
729
- }
730
- if (value & (BindingKind.CatchIdentifier | BindingKind.CatchPattern)) {
731
- if (
732
- (value & BindingKind.CatchIdentifier) === 0 ||
733
- (context & Context.OptionsWebCompat) === 0 ||
734
- context & Context.Strict
735
- ) {
736
- report(parser, Errors.DuplicateBinding, name);
737
- }
738
- }
739
-
740
- currentScope['#' + name] = kind;
741
-
742
- currentScope = currentScope.parent;
743
- }
744
- }
745
-
746
- /**
747
- * Appends a name to the `ExportedNames` of the `ExportsList`, and checks
748
- * for duplicates
749
- *
750
- * @see [Link](https://tc39.github.io/ecma262/$sec-exports-static-semantics-exportednames)
751
- *
752
- * @param parser Parser object
753
- * @param name Exported name
754
- */
755
- export function declareUnboundVariable(parser: ParserState, name: string): void {
756
- if (parser.exportedNames !== void 0 && name !== '') {
757
- if (parser.exportedNames['#' + name]) {
758
- report(parser, Errors.DuplicateExportBinding, name);
759
- }
760
- parser.exportedNames['#' + name] = 1;
761
- }
762
- }
763
-
764
- /**
765
- * Appends a name to the `ExportedBindings` of the `ExportsList`,
766
- *
767
- * @see [Link](https://tc39.es/ecma262/$sec-exports-static-semantics-exportedbindings)
768
- *
769
- * @param parser Parser object
770
- * @param name Exported binding name
771
- */
772
- export function addBindingToExports(parser: ParserState, name: string): void {
773
- if (parser.exportedBindings !== void 0 && name !== '') {
774
- parser.exportedBindings['#' + name] = 1;
775
- }
776
- }
777
-
778
- export function pushComment(context: Context, array: any[]): any {
779
- return function(type: string, value: string, start: number, end: number, loc: SourceLocation) {
780
- const comment: any = {
781
- type,
782
- value
783
- };
784
-
785
- if (context & Context.OptionsRanges) {
786
- comment.start = start;
787
- comment.end = end;
788
- comment.range = [start, end];
789
- }
790
- if (context & Context.OptionsLoc) {
791
- comment.loc = loc;
792
- }
793
- array.push(comment);
794
- };
795
- }
796
-
797
- export function pushToken(context: Context, array: any[]): any {
798
- return function(token: string, start: number, end: number, loc: SourceLocation) {
799
- const tokens: any = {
800
- token
801
- };
802
-
803
- if (context & Context.OptionsRanges) {
804
- tokens.start = start;
805
- tokens.end = end;
806
- tokens.range = [start, end];
807
- }
808
- if (context & Context.OptionsLoc) {
809
- tokens.loc = loc;
810
- }
811
- array.push(tokens);
812
- };
813
- }
814
-
815
- export function isValidIdentifier(context: Context, t: Token): boolean {
816
- if (context & (Context.Strict | Context.InYieldContext)) {
817
- // Module code is also "strict mode code"
818
- if (context & Context.Module && t === Token.AwaitKeyword) return false;
819
- if (context & Context.InYieldContext && t === Token.YieldKeyword) return false;
820
- return (t & Token.IsIdentifier) === Token.IsIdentifier || (t & Token.Contextual) === Token.Contextual;
821
- }
822
-
823
- return (
824
- (t & Token.IsIdentifier) === Token.IsIdentifier ||
825
- (t & Token.Contextual) === Token.Contextual ||
826
- (t & Token.FutureReserved) === Token.FutureReserved
827
- );
828
- }
829
-
830
- export function classifyIdentifier(
831
- parser: ParserState,
832
- context: Context,
833
- t: Token
834
- ): any {
835
- if ((t & Token.IsEvalOrArguments) === Token.IsEvalOrArguments) {
836
- if (context & Context.Strict) report(parser, Errors.StrictEvalArguments);
837
- parser.flags |= Flags.StrictEvalArguments;
838
- }
839
-
840
- if (!isValidIdentifier(context, t)) report(parser, Errors.Unexpected);
841
- }