hermes-parser 0.32.0 → 0.33.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 (55) hide show
  1. package/README.md +3 -0
  2. package/dist/HermesASTAdapter.js +1 -1
  3. package/dist/HermesASTAdapter.js.flow +1 -1
  4. package/dist/HermesParser.js +2 -2
  5. package/dist/HermesParser.js.flow +2 -0
  6. package/dist/HermesParserDeserializer.js +0 -1
  7. package/dist/HermesParserDeserializer.js.flow +0 -1
  8. package/dist/HermesParserNodeDeserializers.js +109 -5
  9. package/dist/HermesParserWASM.js +1 -1
  10. package/dist/HermesParserWASM.js.flow +11 -1
  11. package/dist/ParserOptions.js +1 -1
  12. package/dist/ParserOptions.js.flow +10 -0
  13. package/dist/babel/TransformESTreeToBabel.js +79 -3
  14. package/dist/babel/TransformESTreeToBabel.js.flow +76 -2
  15. package/dist/estree/StripFlowTypes.js +1 -1
  16. package/dist/estree/StripFlowTypes.js.flow +1 -1
  17. package/dist/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
  18. package/dist/estree/{StripComponentSyntax.js.flow → TransformComponentSyntax.js.flow} +1 -1
  19. package/dist/estree/TransformEnumSyntax.js +106 -0
  20. package/dist/estree/TransformEnumSyntax.js.flow +125 -0
  21. package/dist/estree/TransformMatchSyntax.js +124 -56
  22. package/dist/estree/TransformMatchSyntax.js.flow +124 -46
  23. package/dist/estree/TransformRecordSyntax.js +294 -0
  24. package/dist/estree/TransformRecordSyntax.js.flow +308 -0
  25. package/dist/generated/ESTreeVisitorKeys.js +16 -4
  26. package/dist/generated/ParserVisitorKeys.js +45 -4
  27. package/dist/index.js +13 -3
  28. package/dist/index.js.flow +13 -3
  29. package/dist/src/HermesASTAdapter.js +1 -1
  30. package/dist/src/HermesParser.js +2 -2
  31. package/dist/src/HermesParserDeserializer.js +0 -1
  32. package/dist/src/HermesParserNodeDeserializers.js +109 -5
  33. package/dist/src/ParserOptions.js +1 -1
  34. package/dist/src/babel/TransformESTreeToBabel.js +79 -3
  35. package/dist/src/estree/StripFlowTypes.js +1 -1
  36. package/dist/src/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
  37. package/dist/src/estree/TransformEnumSyntax.js +106 -0
  38. package/dist/src/estree/TransformMatchSyntax.js +124 -56
  39. package/dist/src/estree/TransformRecordSyntax.js +294 -0
  40. package/dist/src/generated/ESTreeVisitorKeys.js +16 -4
  41. package/dist/src/generated/ParserVisitorKeys.js +45 -4
  42. package/dist/src/index.js +13 -3
  43. package/dist/src/transform/SimpleTransform.js +20 -4
  44. package/dist/src/transform/astNodeMutationHelpers.js +7 -2
  45. package/dist/src/utils/GenID.js +28 -23
  46. package/dist/src/utils/isReservedWord.js +62 -0
  47. package/dist/transform/SimpleTransform.js +20 -4
  48. package/dist/transform/SimpleTransform.js.flow +34 -8
  49. package/dist/transform/astNodeMutationHelpers.js +7 -2
  50. package/dist/transform/astNodeMutationHelpers.js.flow +10 -2
  51. package/dist/utils/GenID.js +28 -23
  52. package/dist/utils/GenID.js.flow +23 -22
  53. package/dist/utils/isReservedWord.js +62 -0
  54. package/dist/utils/isReservedWord.js.flow +57 -0
  55. package/package.json +2 -2
@@ -25,20 +25,22 @@ var _createSyntaxError = require("../utils/createSyntaxError");
25
25
 
26
26
  var _Builders = require("../utils/Builders");
27
27
 
28
- var _GenID = require("../utils/GenID");
28
+ var _GenID = _interopRequireDefault(require("../utils/GenID"));
29
+
30
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
31
 
30
32
  /**
31
33
  * Generated identifiers.
32
34
  * `GenID` is initialized in the transform.
33
35
  */
34
- let GenID = null;
36
+ let genID = null;
35
37
 
36
38
  function genIdent() {
37
- if (GenID == null) {
39
+ if (genID == null) {
38
40
  throw Error('GenID must be initialized at the start of the transform.');
39
41
  }
40
42
 
41
- return (0, _Builders.ident)(GenID.genID());
43
+ return (0, _Builders.ident)(genID.id());
42
44
  }
43
45
  /**
44
46
  * A series of properties.
@@ -138,6 +140,7 @@ function needsPropExistsCond(pattern) {
138
140
  case 'MatchLiteralPattern':
139
141
  case 'MatchUnaryPattern':
140
142
  case 'MatchObjectPattern':
143
+ case 'MatchInstancePattern':
141
144
  case 'MatchArrayPattern':
142
145
  return false;
143
146
 
@@ -158,6 +161,78 @@ function needsPropExistsCond(pattern) {
158
161
  }
159
162
  }
160
163
  }
164
+ /**
165
+ * Analyzes properties of both object patterns and instance patterns.
166
+ */
167
+
168
+
169
+ function analyzeProperties(key, pattern, seenBindingNames, properties, rest) {
170
+ const conditions = [];
171
+ const bindings = [];
172
+ const objKeys = [];
173
+ const seenNames = new Set();
174
+ properties.forEach(prop => {
175
+ const {
176
+ key: objKey,
177
+ pattern: propPattern
178
+ } = prop;
179
+ objKeys.push(objKey);
180
+ const name = objKeyToString(objKey);
181
+
182
+ if (seenNames.has(name)) {
183
+ throw (0, _createSyntaxError.createSyntaxError)(propPattern, `Duplicate property name '${name}' in match object pattern.`);
184
+ }
185
+
186
+ seenNames.add(name);
187
+ const propKey = key.concat(objKey);
188
+
189
+ if (needsPropExistsCond(propPattern)) {
190
+ conditions.push({
191
+ type: 'prop-exists',
192
+ key,
193
+ propName: name
194
+ });
195
+ }
196
+
197
+ const {
198
+ conditions: childConditions,
199
+ bindings: childBindings
200
+ } = analyzePattern(propPattern, propKey, seenBindingNames);
201
+ conditions.push(...childConditions);
202
+ bindings.push(...childBindings);
203
+ });
204
+
205
+ if (rest != null && rest.argument != null) {
206
+ const {
207
+ id,
208
+ kind
209
+ } = rest.argument;
210
+ checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
211
+ checkBindingKind(pattern, kind);
212
+ bindings.push({
213
+ type: 'object-rest',
214
+ key,
215
+ exclude: objKeys,
216
+ kind,
217
+ id
218
+ });
219
+ }
220
+
221
+ return {
222
+ conditions,
223
+ bindings
224
+ };
225
+ }
226
+
227
+ function constructorExpression(constructor) {
228
+ switch (constructor.type) {
229
+ case 'MatchIdentifierPattern':
230
+ return constructor.id;
231
+
232
+ case 'MatchMemberPattern':
233
+ return convertMemberPattern(constructor);
234
+ }
235
+ }
161
236
  /**
162
237
  * Analyzes a match pattern, and produced both the conditions and bindings
163
238
  * produced by that pattern.
@@ -360,60 +435,38 @@ function analyzePattern(pattern, key, seenBindingNames) {
360
435
  properties,
361
436
  rest
362
437
  } = pattern;
438
+ const {
439
+ conditions: propertyConditions,
440
+ bindings
441
+ } = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
363
442
  const conditions = [{
364
443
  type: 'object',
365
444
  key
366
- }];
367
- const bindings = [];
368
- const objKeys = [];
369
- const seenNames = new Set();
370
- properties.forEach(prop => {
371
- const {
372
- key: objKey,
373
- pattern: propPattern
374
- } = prop;
375
- objKeys.push(objKey);
376
- const name = objKeyToString(objKey);
377
-
378
- if (seenNames.has(name)) {
379
- throw (0, _createSyntaxError.createSyntaxError)(propPattern, `Duplicate property name '${name}' in match object pattern.`);
380
- }
381
-
382
- seenNames.add(name);
383
- const propKey = key.concat(objKey);
445
+ }, ...propertyConditions];
446
+ return {
447
+ conditions,
448
+ bindings
449
+ };
450
+ }
384
451
 
385
- if (needsPropExistsCond(propPattern)) {
386
- conditions.push({
387
- type: 'prop-exists',
388
- key,
389
- propName: name
390
- });
452
+ case 'MatchInstancePattern':
453
+ {
454
+ const {
455
+ targetConstructor,
456
+ properties: {
457
+ properties,
458
+ rest
391
459
  }
392
-
393
- const {
394
- conditions: childConditions,
395
- bindings: childBindings
396
- } = analyzePattern(propPattern, propKey, seenBindingNames);
397
- conditions.push(...childConditions);
398
- bindings.push(...childBindings);
399
- });
400
-
401
- if (rest != null && rest.argument != null) {
402
- const {
403
- id,
404
- kind
405
- } = rest.argument;
406
- checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
407
- checkBindingKind(pattern, kind);
408
- bindings.push({
409
- type: 'object-rest',
410
- key,
411
- exclude: objKeys,
412
- kind,
413
- id
414
- });
415
- }
416
-
460
+ } = pattern;
461
+ const {
462
+ conditions: propertyConditions,
463
+ bindings
464
+ } = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
465
+ const conditions = [{
466
+ type: 'instanceof',
467
+ key,
468
+ constructor: constructorExpression(targetConstructor)
469
+ }, ...propertyConditions];
417
470
  return {
418
471
  conditions,
419
472
  bindings
@@ -567,6 +620,21 @@ function testsOfCondition(root, condition) {
567
620
  return [(0, _Builders.disjunction)([(0, _Builders.conjunction)([typeofObject, notNull]), typeofFunction])];
568
621
  }
569
622
 
623
+ case 'instanceof':
624
+ {
625
+ const {
626
+ key,
627
+ constructor
628
+ } = condition;
629
+ return [{
630
+ type: 'BinaryExpression',
631
+ left: expressionOfKey(root, key),
632
+ right: constructor,
633
+ operator: 'instanceof',
634
+ ...(0, _Builders.etc)()
635
+ }];
636
+ }
637
+
570
638
  case 'prop-exists':
571
639
  {
572
640
  // <propName> in <x>
@@ -968,7 +1036,7 @@ function mapMatchStatement(node) {
968
1036
  function transformProgram(program, _options) {
969
1037
  // Initialize so each file transformed starts freshly incrementing the
970
1038
  // variable name counter, and has its own usage tracking.
971
- GenID = (0, _GenID.createGenID)('m');
1039
+ genID = new _GenID.default('m');
972
1040
  return _SimpleTransform.SimpleTransform.transformProgram(program, {
973
1041
  transform(node) {
974
1042
  switch (node.type) {
@@ -987,11 +1055,11 @@ function transformProgram(program, _options) {
987
1055
  // A rudimentary check to avoid some collisions with our generated
988
1056
  // variable names. Ideally, we would have access a scope analyzer
989
1057
  // inside the transform instead.
990
- if (GenID == null) {
1058
+ if (genID == null) {
991
1059
  throw Error('GenID must be initialized at the start of the transform.');
992
1060
  }
993
1061
 
994
- GenID.addUsage(node.name);
1062
+ genID.addUsage(node.name);
995
1063
  return node;
996
1064
  }
997
1065
 
@@ -24,8 +24,11 @@ import type {
24
24
  Identifier,
25
25
  Literal,
26
26
  MatchExpression,
27
+ MatchIdentifierPattern,
27
28
  MatchMemberPattern,
29
+ MatchObjectPatternProperty,
28
30
  MatchPattern,
31
+ MatchRestPattern,
29
32
  MatchStatement,
30
33
  MemberExpression,
31
34
  ObjectPattern,
@@ -57,18 +60,18 @@ import {
57
60
  typeofExpression,
58
61
  variableDeclaration,
59
62
  } from '../utils/Builders';
60
- import {createGenID} from '../utils/GenID';
63
+ import GenID from '../utils/GenID';
61
64
 
62
65
  /**
63
66
  * Generated identifiers.
64
67
  * `GenID` is initialized in the transform.
65
68
  */
66
- let GenID: ?ReturnType<typeof createGenID> = null;
69
+ let genID: GenID | null = null;
67
70
  function genIdent(): Identifier {
68
- if (GenID == null) {
71
+ if (genID == null) {
69
72
  throw Error('GenID must be initialized at the start of the transform.');
70
73
  }
71
- return ident(GenID.genID());
74
+ return ident(genID.id());
72
75
  }
73
76
 
74
77
  /**
@@ -86,6 +89,7 @@ type Condition =
86
89
  | {type: 'is-nan', key: Key}
87
90
  | {type: 'array', key: Key, length: number, lengthOp: 'eq' | 'gte'}
88
91
  | {type: 'object', key: Key}
92
+ | {type: 'instanceof', key: Key, constructor: Expression}
89
93
  | {type: 'prop-exists', key: Key, propName: string}
90
94
  | {type: 'or', orConditions: Array<Array<Condition>>};
91
95
 
@@ -194,6 +198,7 @@ function needsPropExistsCond(pattern: MatchPattern): boolean {
194
198
  case 'MatchLiteralPattern':
195
199
  case 'MatchUnaryPattern':
196
200
  case 'MatchObjectPattern':
201
+ case 'MatchInstancePattern':
197
202
  case 'MatchArrayPattern':
198
203
  return false;
199
204
  case 'MatchAsPattern': {
@@ -207,6 +212,74 @@ function needsPropExistsCond(pattern: MatchPattern): boolean {
207
212
  }
208
213
  }
209
214
 
215
+ /**
216
+ * Analyzes properties of both object patterns and instance patterns.
217
+ */
218
+ function analyzeProperties(
219
+ key: Key,
220
+ pattern: MatchPattern,
221
+ seenBindingNames: Set<string>,
222
+ properties: ReadonlyArray<MatchObjectPatternProperty>,
223
+ rest: MatchRestPattern | null,
224
+ ): {
225
+ conditions: Array<Condition>,
226
+ bindings: Array<Binding>,
227
+ } {
228
+ const conditions: Array<Condition> = [];
229
+ const bindings: Array<Binding> = [];
230
+ const objKeys: Array<Identifier | Literal> = [];
231
+ const seenNames = new Set<string>();
232
+
233
+ properties.forEach(prop => {
234
+ const {key: objKey, pattern: propPattern} = prop;
235
+ objKeys.push(objKey);
236
+ const name = objKeyToString(objKey);
237
+ if (seenNames.has(name)) {
238
+ throw createSyntaxError(
239
+ propPattern,
240
+ `Duplicate property name '${name}' in match object pattern.`,
241
+ );
242
+ }
243
+ seenNames.add(name);
244
+ const propKey: Key = key.concat(objKey);
245
+ if (needsPropExistsCond(propPattern)) {
246
+ conditions.push({
247
+ type: 'prop-exists',
248
+ key,
249
+ propName: name,
250
+ });
251
+ }
252
+ const {conditions: childConditions, bindings: childBindings} =
253
+ analyzePattern(propPattern, propKey, seenBindingNames);
254
+ conditions.push(...childConditions);
255
+ bindings.push(...childBindings);
256
+ });
257
+ if (rest != null && rest.argument != null) {
258
+ const {id, kind} = rest.argument;
259
+ checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
260
+ checkBindingKind(pattern, kind);
261
+ bindings.push({
262
+ type: 'object-rest',
263
+ key,
264
+ exclude: objKeys,
265
+ kind,
266
+ id,
267
+ });
268
+ }
269
+ return {conditions, bindings};
270
+ }
271
+
272
+ function constructorExpression(
273
+ constructor: MatchIdentifierPattern | MatchMemberPattern,
274
+ ): Expression {
275
+ switch (constructor.type) {
276
+ case 'MatchIdentifierPattern':
277
+ return constructor.id;
278
+ case 'MatchMemberPattern':
279
+ return convertMemberPattern(constructor);
280
+ }
281
+ }
282
+
210
283
  /**
211
284
  * Analyzes a match pattern, and produced both the conditions and bindings
212
285
  * produced by that pattern.
@@ -317,46 +390,39 @@ function analyzePattern(
317
390
  }
318
391
  case 'MatchObjectPattern': {
319
392
  const {properties, rest} = pattern;
320
- const conditions: Array<Condition> = [{type: 'object', key}];
321
- const bindings: Array<Binding> = [];
322
- const objKeys: Array<Identifier | Literal> = [];
323
- const seenNames = new Set<string>();
324
- properties.forEach(prop => {
325
- const {key: objKey, pattern: propPattern} = prop;
326
- objKeys.push(objKey);
327
- const name = objKeyToString(objKey);
328
- if (seenNames.has(name)) {
329
- throw createSyntaxError(
330
- propPattern,
331
- `Duplicate property name '${name}' in match object pattern.`,
332
- );
333
- }
334
- seenNames.add(name);
335
- const propKey: Key = key.concat(objKey);
336
- if (needsPropExistsCond(propPattern)) {
337
- conditions.push({
338
- type: 'prop-exists',
339
- key,
340
- propName: name,
341
- });
342
- }
343
- const {conditions: childConditions, bindings: childBindings} =
344
- analyzePattern(propPattern, propKey, seenBindingNames);
345
- conditions.push(...childConditions);
346
- bindings.push(...childBindings);
347
- });
348
- if (rest != null && rest.argument != null) {
349
- const {id, kind} = rest.argument;
350
- checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
351
- checkBindingKind(pattern, kind);
352
- bindings.push({
353
- type: 'object-rest',
393
+ const {conditions: propertyConditions, bindings} = analyzeProperties(
394
+ key,
395
+ pattern,
396
+ seenBindingNames,
397
+ properties,
398
+ rest,
399
+ );
400
+ const conditions: Array<Condition> = [
401
+ {type: 'object', key},
402
+ ...propertyConditions,
403
+ ];
404
+ return {conditions, bindings};
405
+ }
406
+ case 'MatchInstancePattern': {
407
+ const {
408
+ targetConstructor,
409
+ properties: {properties, rest},
410
+ } = pattern;
411
+ const {conditions: propertyConditions, bindings} = analyzeProperties(
412
+ key,
413
+ pattern,
414
+ seenBindingNames,
415
+ properties,
416
+ rest,
417
+ );
418
+ const conditions: Array<Condition> = [
419
+ {
420
+ type: 'instanceof',
354
421
  key,
355
- exclude: objKeys,
356
- kind,
357
- id,
358
- });
359
- }
422
+ constructor: constructorExpression(targetConstructor),
423
+ },
424
+ ...propertyConditions,
425
+ ];
360
426
  return {conditions, bindings};
361
427
  }
362
428
  case 'MatchOrPattern': {
@@ -499,6 +565,18 @@ function testsOfCondition(
499
565
  disjunction([conjunction([typeofObject, notNull]), typeofFunction]),
500
566
  ];
501
567
  }
568
+ case 'instanceof': {
569
+ const {key, constructor} = condition;
570
+ return [
571
+ {
572
+ type: 'BinaryExpression',
573
+ left: expressionOfKey(root, key),
574
+ right: constructor,
575
+ operator: 'instanceof',
576
+ ...etc(),
577
+ },
578
+ ];
579
+ }
502
580
  case 'prop-exists': {
503
581
  // <propName> in <x>
504
582
  const {key, propName} = condition;
@@ -881,7 +959,7 @@ export function transformProgram(
881
959
  ): Program {
882
960
  // Initialize so each file transformed starts freshly incrementing the
883
961
  // variable name counter, and has its own usage tracking.
884
- GenID = createGenID('m');
962
+ genID = new GenID('m');
885
963
  return SimpleTransform.transformProgram(program, {
886
964
  transform(node: ESNode) {
887
965
  switch (node.type) {
@@ -895,12 +973,12 @@ export function transformProgram(
895
973
  // A rudimentary check to avoid some collisions with our generated
896
974
  // variable names. Ideally, we would have access a scope analyzer
897
975
  // inside the transform instead.
898
- if (GenID == null) {
976
+ if (genID == null) {
899
977
  throw Error(
900
978
  'GenID must be initialized at the start of the transform.',
901
979
  );
902
980
  }
903
- GenID.addUsage(node.name);
981
+ genID.addUsage(node.name);
904
982
  return node;
905
983
  }
906
984
  default: {