meriyah 5.0.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 (69) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/README.md +38 -14
  3. package/dist/meriyah.cjs +2356 -2070
  4. package/dist/meriyah.min.mjs +1 -0
  5. package/dist/{meriyah.esm.js → meriyah.mjs} +2356 -2070
  6. package/dist/meriyah.umd.js +2356 -2070
  7. package/dist/meriyah.umd.min.js +1 -1
  8. package/dist/src/common.d.ts +71 -47
  9. package/dist/src/common.d.ts.map +1 -1
  10. package/dist/src/errors.d.ts +186 -177
  11. package/dist/src/errors.d.ts.map +1 -1
  12. package/dist/src/estree.d.ts +15 -6
  13. package/dist/src/estree.d.ts.map +1 -1
  14. package/dist/src/lexer/charClassifier.d.ts +2 -2
  15. package/dist/src/lexer/charClassifier.d.ts.map +1 -1
  16. package/dist/src/lexer/common.d.ts +1 -2
  17. package/dist/src/lexer/common.d.ts.map +1 -1
  18. package/dist/src/lexer/identifier.d.ts.map +1 -1
  19. package/dist/src/lexer/index.d.ts +1 -1
  20. package/dist/src/lexer/index.d.ts.map +1 -1
  21. package/dist/src/lexer/jsx.d.ts +2 -2
  22. package/dist/src/lexer/jsx.d.ts.map +1 -1
  23. package/dist/src/lexer/numeric.d.ts.map +1 -1
  24. package/dist/src/lexer/regexp.d.ts.map +1 -1
  25. package/dist/src/lexer/scan.d.ts.map +1 -1
  26. package/dist/src/lexer/string.d.ts +1 -1
  27. package/dist/src/lexer/string.d.ts.map +1 -1
  28. package/dist/src/lexer/template.d.ts.map +1 -1
  29. package/dist/src/parser.d.ts +72 -72
  30. package/dist/src/parser.d.ts.map +1 -1
  31. package/dist/src/token.d.ts +115 -115
  32. package/dist/src/token.d.ts.map +1 -1
  33. package/package.json +25 -34
  34. package/dist/meriyah.amd.js +0 -8964
  35. package/dist/meriyah.amd.min.js +0 -1
  36. package/dist/meriyah.cjs.js +0 -8962
  37. package/dist/meriyah.cjs.min.js +0 -1
  38. package/dist/meriyah.esm.min.js +0 -1
  39. package/dist/meriyah.esm.min.mjs +0 -1
  40. package/dist/meriyah.esm.mjs +0 -8956
  41. package/dist/meriyah.iife.js +0 -8967
  42. package/dist/meriyah.iife.min.js +0 -1
  43. package/dist/meriyah.min.cjs +0 -1
  44. package/dist/meriyah.system.js +0 -8970
  45. package/dist/meriyah.system.min.js +0 -1
  46. package/dist/meriyah.umd.cjs +0 -8968
  47. package/dist/meriyah.umd.es5.js +0 -9022
  48. package/dist/meriyah.umd.es5.min.js +0 -1
  49. package/dist/meriyah.umd.min.cjs +0 -1
  50. package/src/chars.ts +0 -155
  51. package/src/common.ts +0 -834
  52. package/src/errors.ts +0 -421
  53. package/src/estree.ts +0 -827
  54. package/src/lexer/charClassifier.ts +0 -449
  55. package/src/lexer/comments.ts +0 -178
  56. package/src/lexer/common.ts +0 -140
  57. package/src/lexer/decodeHTML.ts +0 -2184
  58. package/src/lexer/identifier.ts +0 -196
  59. package/src/lexer/index.ts +0 -32
  60. package/src/lexer/jsx.ts +0 -127
  61. package/src/lexer/numeric.ts +0 -259
  62. package/src/lexer/regexp.ts +0 -156
  63. package/src/lexer/scan.ts +0 -657
  64. package/src/lexer/string.ts +0 -242
  65. package/src/lexer/template.ts +0 -108
  66. package/src/meriyah.ts +0 -28
  67. package/src/parser.ts +0 -9358
  68. package/src/token.ts +0 -307
  69. package/src/unicode.ts +0 -36
package/src/common.ts DELETED
@@ -1,834 +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 << 20,
32
- InAwaitContext = 1 << 21,
33
- InArgumentList = 1 << 22,
34
- InConstructor = 1 << 23,
35
- InMethod = 1 << 24,
36
- AllowNewTarget = 1 << 25,
37
- DisallowIn = 1 << 26,
38
- AllowEscapedKeyword = 1 << 27,
39
- OptionsUniqueKeyInPattern = 1 << 28
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 =
173
- | void
174
- | Comment[]
175
- | ((type: string, value: string, start: number, end: number, loc: SourceLocation) => any);
176
-
177
- /**
178
- * The type of the `onInsertedSemicolon` option.
179
- */
180
- export type OnInsertedSemicolon = void | ((pos: number) => any);
181
-
182
- /**
183
- * The type of the `onToken` option.
184
- */
185
- export type OnToken = void | Token[] | ((token: string, start: number, end: number, loc: SourceLocation) => any);
186
-
187
- /**
188
- * Lexical scope interface
189
- */
190
- export interface ScopeState {
191
- parent: ScopeState | undefined;
192
- type: ScopeKind;
193
- scopeError?: ScopeError | null;
194
- }
195
-
196
- /** Scope error interface */
197
- export interface ScopeError {
198
- type: Errors;
199
- params: string[];
200
- index: number;
201
- line: number;
202
- column: number;
203
- }
204
-
205
- /**
206
- * The parser interface.
207
- */
208
- export interface ParserState {
209
- source: string;
210
- flags: Flags;
211
- index: number;
212
- line: number;
213
- column: number;
214
- tokenPos: number;
215
- startPos: number;
216
- startColumn: number;
217
- startLine: number;
218
- colPos: number;
219
- linePos: number;
220
- end: number;
221
- getToken(): Token;
222
- setToken(token: Token): Token;
223
- onComment: any;
224
- onInsertedSemicolon: any;
225
- onToken: any;
226
- tokenValue: any;
227
- tokenRaw: string;
228
- tokenRegExp: void | {
229
- pattern: string;
230
- flags: string;
231
- };
232
- sourceFile: string | void;
233
- assignable: AssignmentKind | DestructuringKind;
234
- destructible: AssignmentKind | DestructuringKind;
235
- currentChar: number;
236
- exportedNames: any;
237
- exportedBindings: any;
238
- leadingDecorators: Decorator[];
239
- }
240
-
241
- /**
242
- * Check for automatic semicolon insertion according to the rules
243
- * given in `ECMA-262, section 11.9`.
244
- *
245
- * @param parser Parser object
246
- * @param context Context masks
247
- */
248
-
249
- export function matchOrInsertSemicolon(parser: ParserState, context: Context): void {
250
- if ((parser.flags & Flags.NewLine) === 0 && (parser.getToken() & Token.IsAutoSemicolon) !== Token.IsAutoSemicolon) {
251
- report(parser, Errors.UnexpectedToken, KeywordDescTable[parser.getToken() & 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.getToken() & 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.getToken() !== 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.getToken() !== 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.getToken() !== 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;
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
- }
328
- case 'ObjectExpression': {
329
- node.type = 'ObjectPattern';
330
- const { properties } = node;
331
- for (let i = 0, n = properties.length; i < n; ++i) {
332
- reinterpretToPattern(state, properties[i]);
333
- }
334
- return;
335
- }
336
- case 'AssignmentExpression':
337
- node.type = 'AssignmentPattern';
338
- if (node.operator !== '=') report(state, Errors.InvalidDestructuringTarget);
339
- delete node.operator;
340
- reinterpretToPattern(state, node.left);
341
- return;
342
- case 'Property':
343
- reinterpretToPattern(state, node.value);
344
- return;
345
- case 'SpreadElement':
346
- node.type = 'RestElement';
347
- reinterpretToPattern(state, node.argument);
348
- // No default
349
- }
350
- }
351
-
352
- /**
353
- * Validates binding identifier
354
- *
355
- * @param parser Parser state
356
- * @param context Context masks
357
- * @param type Binding type
358
- * @param token Token
359
- */
360
-
361
- export function validateBindingIdentifier(
362
- parser: ParserState,
363
- context: Context,
364
- kind: BindingKind,
365
- t: Token,
366
- skipEvalArgCheck: 0 | 1
367
- ): void {
368
- if (context & Context.Strict) {
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(parser: ParserState, context: Context, t: Token): void {
399
- if (context & Context.Strict) {
400
- if ((t & Token.FutureReserved) === Token.FutureReserved) {
401
- report(parser, Errors.UnexpectedStrictReserved);
402
- }
403
-
404
- if ((t & Token.IsEvalOrArguments) === Token.IsEvalOrArguments) {
405
- report(parser, Errors.StrictEvalArguments);
406
- }
407
-
408
- if (t === Token.EscapedFutureReserved) {
409
- report(parser, Errors.InvalidEscapedKeyword);
410
- }
411
-
412
- if (t === Token.EscapedReserved) {
413
- report(parser, Errors.InvalidEscapedKeyword);
414
- }
415
- }
416
-
417
- if ((t & Token.Reserved) === Token.Reserved) {
418
- report(parser, Errors.KeywordNotId);
419
- }
420
-
421
- if (context & (Context.InAwaitContext | Context.Module) && t === Token.AwaitKeyword) {
422
- report(parser, Errors.AwaitOutsideAsync);
423
- }
424
-
425
- if (context & (Context.InYieldContext | Context.Strict) && t === Token.YieldKeyword) {
426
- report(parser, Errors.DisallowedInContext, 'yield');
427
- }
428
- }
429
-
430
- /**
431
- * Validates binding identifier
432
- *
433
- * @param parser Parser state
434
- * @param context Context masks
435
- * @param t Token
436
- */
437
-
438
- export function isStrictReservedWord(parser: ParserState, context: Context, t: Token): boolean {
439
- if (t === Token.AwaitKeyword) {
440
- if (context & (Context.InAwaitContext | Context.Module)) report(parser, Errors.AwaitOutsideAsync);
441
- parser.destructible |= DestructuringKind.Await;
442
- }
443
-
444
- if (t === Token.YieldKeyword && context & Context.InYieldContext) report(parser, Errors.DisallowedInContext, 'yield');
445
-
446
- return (
447
- (t & Token.Reserved) === Token.Reserved ||
448
- (t & Token.FutureReserved) === Token.FutureReserved ||
449
- t == Token.EscapedFutureReserved
450
- );
451
- }
452
-
453
- /**
454
- * Checks if the property has any private field key
455
- *
456
- * @param parser Parser object
457
- * @param context Context masks
458
- */
459
- export function isPropertyWithPrivateFieldKey(expr: any): boolean {
460
- return !expr.property ? false : expr.property.type === 'PrivateIdentifier';
461
- }
462
-
463
- /**
464
- * Checks if a label in `LabelledStatement` are valid or not
465
- *
466
- * @param parser Parser state
467
- * @param labels Object holding the labels
468
- * @param name Current label
469
- * @param isIterationStatement
470
- */
471
- export function isValidLabel(parser: ParserState, labels: any, name: string, isIterationStatement: 0 | 1): 0 | 1 {
472
- while (labels) {
473
- if (labels['$' + name]) {
474
- if (isIterationStatement) report(parser, Errors.InvalidNestedStatement);
475
- return 1;
476
- }
477
- if (isIterationStatement && labels.loop) isIterationStatement = 0;
478
- labels = labels['$'];
479
- }
480
-
481
- return 0;
482
- }
483
-
484
- /**
485
- * Checks if current label already have been declrared, and if not
486
- * declare it
487
- *
488
- * @param parser Parser state
489
- * @param labels Object holding the labels
490
- * @param name Current label
491
- */
492
- export function validateAndDeclareLabel(parser: ParserState, labels: any, name: string): void {
493
- let set = labels;
494
- while (set) {
495
- if (set['$' + name]) report(parser, Errors.LabelRedeclaration, name);
496
- set = set['$'];
497
- }
498
-
499
- labels['$' + name] = 1;
500
- }
501
-
502
- export function finishNode<T extends Node>(
503
- parser: ParserState,
504
- context: Context,
505
- start: number,
506
- line: number,
507
- column: number,
508
- node: T
509
- ): T {
510
- if (context & Context.OptionsRanges) {
511
- node.start = start;
512
- node.end = parser.startPos;
513
- node.range = [start, parser.startPos];
514
- }
515
-
516
- if (context & Context.OptionsLoc) {
517
- node.loc = {
518
- start: {
519
- line,
520
- column
521
- },
522
- end: {
523
- line: parser.startLine,
524
- column: parser.startColumn
525
- }
526
- };
527
-
528
- if (parser.sourceFile) {
529
- node.loc.source = parser.sourceFile;
530
- }
531
- }
532
-
533
- return node;
534
- }
535
-
536
- /** @internal */
537
- export function isEqualTagName(elementName: any): any {
538
- switch (elementName.type) {
539
- case 'JSXIdentifier':
540
- return elementName.name;
541
- case 'JSXNamespacedName':
542
- return elementName.namespace + ':' + elementName.name;
543
- case 'JSXMemberExpression':
544
- return isEqualTagName(elementName.object) + '.' + isEqualTagName(elementName.property);
545
- /* istanbul ignore next */
546
- default:
547
- // ignore
548
- }
549
- }
550
-
551
- /**
552
- * Create a parsing scope for arrow head, and add lexical binding
553
- *
554
- * @param parser Parser state
555
- * @param context Context masks
556
- * @param value Binding name to be declared
557
- */
558
- export function createArrowHeadParsingScope(parser: ParserState, context: Context, value: string): ScopeState {
559
- const scope = addChildScope(createScope(), ScopeKind.ArrowParams);
560
- addBlockName(parser, context, scope, value, BindingKind.ArgumentList, Origin.None);
561
- return scope;
562
- }
563
-
564
- /**
565
- * Record duplicate binding errors that may occur in a arrow head or function parameters
566
- *
567
- * @param parser Parser state
568
- * @param type Errors type
569
- */
570
- export function recordScopeError(parser: ParserState, type: Errors, ...params: string[]): ScopeError {
571
- const { index, line, column } = parser;
572
- return {
573
- type,
574
- params,
575
- index,
576
- line,
577
- column
578
- };
579
- }
580
-
581
- /**
582
- * Creates a block scope
583
- */
584
- export function createScope(): ScopeState {
585
- return {
586
- parent: void 0,
587
- type: ScopeKind.Block
588
- };
589
- }
590
-
591
- /**
592
- * Inherit scope
593
- *
594
- * @param scope Parser object
595
- * @param type Scope kind
596
- */
597
- export function addChildScope(parent: ScopeState | undefined, type: ScopeKind): ScopeState {
598
- return {
599
- parent,
600
- type,
601
- scopeError: void 0
602
- };
603
- }
604
-
605
- /**
606
- * Adds either a var binding or a block scoped binding.
607
- *
608
- * @param parser Parser state
609
- * @param context Context masks
610
- * @param scope Scope state
611
- * @param name Binding name
612
- * @param type Binding kind
613
- * @param origin Binding Origin
614
- */
615
- export function addVarOrBlock(
616
- parser: ParserState,
617
- context: Context,
618
- scope: ScopeState,
619
- name: string,
620
- kind: BindingKind,
621
- origin: Origin
622
- ) {
623
- if (kind & BindingKind.Variable) {
624
- addVarName(parser, context, scope, name, kind);
625
- } else {
626
- addBlockName(parser, context, scope, name, kind, origin);
627
- }
628
- if (origin & Origin.Export) {
629
- declareUnboundVariable(parser, name);
630
- }
631
- }
632
-
633
- /**
634
- * Adds block scoped binding
635
- *
636
- * @param parser Parser state
637
- * @param context Context masks
638
- * @param scope Scope state
639
- * @param name Binding name
640
- * @param type Binding kind
641
- * @param origin Binding Origin
642
- */
643
- export function addBlockName(
644
- parser: ParserState,
645
- context: Context,
646
- scope: any,
647
- name: string,
648
- kind: BindingKind,
649
- origin: Origin
650
- ) {
651
- const value = (scope as any)['#' + name];
652
-
653
- if (value && (value & BindingKind.Empty) === 0) {
654
- if (kind & BindingKind.ArgumentList) {
655
- scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
656
- } else if (
657
- context & Context.OptionsWebCompat &&
658
- value & BindingKind.FunctionLexical &&
659
- origin & Origin.BlockStatement
660
- ) {
661
- // No op
662
- } else {
663
- report(parser, Errors.DuplicateBinding, name);
664
- }
665
- }
666
-
667
- if (
668
- scope.type & ScopeKind.FunctionBody &&
669
- (scope as any).parent['#' + name] &&
670
- ((scope as any).parent['#' + name] & BindingKind.Empty) === 0
671
- ) {
672
- report(parser, Errors.DuplicateBinding, name);
673
- }
674
-
675
- if (scope.type & ScopeKind.ArrowParams && value && (value & BindingKind.Empty) === 0) {
676
- if (kind & BindingKind.ArgumentList) {
677
- scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
678
- }
679
- }
680
-
681
- if (scope.type & ScopeKind.CatchBlock) {
682
- if ((scope as any).parent['#' + name] & BindingKind.CatchIdentifierOrPattern)
683
- report(parser, Errors.ShadowedCatchClause, name);
684
- }
685
-
686
- (scope as any)['#' + name] = kind;
687
- }
688
-
689
- /**
690
- * Adds a variable binding
691
- *
692
- * @param parser Parser state
693
- * @param context Context masks
694
- * @param scope Scope state
695
- * @param name Binding name
696
- * @param type Binding kind
697
- */
698
- export function addVarName(
699
- parser: ParserState,
700
- context: Context,
701
- scope: ScopeState,
702
- name: string,
703
- kind: BindingKind
704
- ): void {
705
- let currentScope: any = scope;
706
-
707
- while (currentScope && (currentScope.type & ScopeKind.FunctionRoot) === 0) {
708
- const value: ScopeKind = currentScope['#' + name];
709
-
710
- if (value & BindingKind.LexicalBinding) {
711
- if (
712
- context & Context.OptionsWebCompat &&
713
- (context & Context.Strict) === 0 &&
714
- ((kind & BindingKind.FunctionStatement && value & BindingKind.LexicalOrFunction) ||
715
- (value & BindingKind.FunctionStatement && kind & BindingKind.LexicalOrFunction))
716
- ) {
717
- // No op
718
- } else {
719
- report(parser, Errors.DuplicateBinding, name);
720
- }
721
- }
722
- if (currentScope === scope) {
723
- if (value & BindingKind.ArgumentList && kind & BindingKind.ArgumentList) {
724
- currentScope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
725
- }
726
- }
727
- if (value & (BindingKind.CatchIdentifier | BindingKind.CatchPattern)) {
728
- if (
729
- (value & BindingKind.CatchIdentifier) === 0 ||
730
- (context & Context.OptionsWebCompat) === 0 ||
731
- context & Context.Strict
732
- ) {
733
- report(parser, Errors.DuplicateBinding, name);
734
- }
735
- }
736
-
737
- currentScope['#' + name] = kind;
738
-
739
- currentScope = currentScope.parent;
740
- }
741
- }
742
-
743
- /**
744
- * Appends a name to the `ExportedNames` of the `ExportsList`, and checks
745
- * for duplicates
746
- *
747
- * @see [Link](https://tc39.github.io/ecma262/$sec-exports-static-semantics-exportednames)
748
- *
749
- * @param parser Parser object
750
- * @param name Exported name
751
- */
752
- export function declareUnboundVariable(parser: ParserState, name: string): void {
753
- if (parser.exportedNames !== void 0 && name !== '') {
754
- if (parser.exportedNames['#' + name]) {
755
- report(parser, Errors.DuplicateExportBinding, name);
756
- }
757
- parser.exportedNames['#' + name] = 1;
758
- }
759
- }
760
-
761
- /**
762
- * Appends a name to the `ExportedBindings` of the `ExportsList`,
763
- *
764
- * @see [Link](https://tc39.es/ecma262/$sec-exports-static-semantics-exportedbindings)
765
- *
766
- * @param parser Parser object
767
- * @param name Exported binding name
768
- */
769
- export function addBindingToExports(parser: ParserState, name: string): void {
770
- if (parser.exportedBindings !== void 0 && name !== '') {
771
- parser.exportedBindings['#' + name] = 1;
772
- }
773
- }
774
-
775
- export function pushComment(context: Context, array: any[]): any {
776
- return function (type: string, value: string, start: number, end: number, loc: SourceLocation) {
777
- const comment: any = {
778
- type,
779
- value
780
- };
781
-
782
- if (context & Context.OptionsRanges) {
783
- comment.start = start;
784
- comment.end = end;
785
- comment.range = [start, end];
786
- }
787
- if (context & Context.OptionsLoc) {
788
- comment.loc = loc;
789
- }
790
- array.push(comment);
791
- };
792
- }
793
-
794
- export function pushToken(context: Context, array: any[]): any {
795
- return function (token: string, start: number, end: number, loc: SourceLocation) {
796
- const tokens: any = {
797
- token
798
- };
799
-
800
- if (context & Context.OptionsRanges) {
801
- tokens.start = start;
802
- tokens.end = end;
803
- tokens.range = [start, end];
804
- }
805
- if (context & Context.OptionsLoc) {
806
- tokens.loc = loc;
807
- }
808
- array.push(tokens);
809
- };
810
- }
811
-
812
- export function isValidIdentifier(context: Context, t: Token): boolean {
813
- if (context & (Context.Strict | Context.InYieldContext)) {
814
- // Module code is also "strict mode code"
815
- if (context & Context.Module && t === Token.AwaitKeyword) return false;
816
- if (context & Context.InYieldContext && t === Token.YieldKeyword) return false;
817
- return (t & Token.IsIdentifier) === Token.IsIdentifier || (t & Token.Contextual) === Token.Contextual;
818
- }
819
-
820
- return (
821
- (t & Token.IsIdentifier) === Token.IsIdentifier ||
822
- (t & Token.Contextual) === Token.Contextual ||
823
- (t & Token.FutureReserved) === Token.FutureReserved
824
- );
825
- }
826
-
827
- export function classifyIdentifier(parser: ParserState, context: Context, t: Token): any {
828
- if ((t & Token.IsEvalOrArguments) === Token.IsEvalOrArguments) {
829
- if (context & Context.Strict) report(parser, Errors.StrictEvalArguments);
830
- parser.flags |= Flags.StrictEvalArguments;
831
- }
832
-
833
- if (!isValidIdentifier(context, t)) report(parser, Errors.Unexpected);
834
- }