hermes-parser 0.32.1 → 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 (36) hide show
  1. package/dist/HermesParser.js +2 -2
  2. package/dist/HermesParser.js.flow +2 -0
  3. package/dist/HermesParserNodeDeserializers.js +109 -5
  4. package/dist/HermesParserWASM.js +1 -1
  5. package/dist/HermesParserWASM.js.flow +11 -1
  6. package/dist/ParserOptions.js +1 -1
  7. package/dist/ParserOptions.js.flow +2 -0
  8. package/dist/babel/TransformESTreeToBabel.js +76 -3
  9. package/dist/babel/TransformESTreeToBabel.js.flow +73 -2
  10. package/dist/estree/StripFlowTypes.js +1 -1
  11. package/dist/estree/StripFlowTypes.js.flow +1 -1
  12. package/dist/estree/TransformMatchSyntax.js +124 -56
  13. package/dist/estree/TransformMatchSyntax.js.flow +124 -46
  14. package/dist/estree/TransformRecordSyntax.js +294 -0
  15. package/dist/estree/TransformRecordSyntax.js.flow +308 -0
  16. package/dist/generated/ESTreeVisitorKeys.js +16 -4
  17. package/dist/generated/ParserVisitorKeys.js +45 -4
  18. package/dist/index.js +8 -1
  19. package/dist/index.js.flow +7 -0
  20. package/dist/src/HermesParser.js +2 -2
  21. package/dist/src/HermesParserNodeDeserializers.js +109 -5
  22. package/dist/src/ParserOptions.js +1 -1
  23. package/dist/src/babel/TransformESTreeToBabel.js +76 -3
  24. package/dist/src/estree/StripFlowTypes.js +1 -1
  25. package/dist/src/estree/TransformMatchSyntax.js +124 -56
  26. package/dist/src/estree/TransformRecordSyntax.js +294 -0
  27. package/dist/src/generated/ESTreeVisitorKeys.js +16 -4
  28. package/dist/src/generated/ParserVisitorKeys.js +45 -4
  29. package/dist/src/index.js +8 -1
  30. package/dist/src/utils/GenID.js +28 -23
  31. package/dist/src/utils/isReservedWord.js +62 -0
  32. package/dist/utils/GenID.js +28 -23
  33. package/dist/utils/GenID.js.flow +23 -22
  34. package/dist/utils/isReservedWord.js +62 -0
  35. package/dist/utils/isReservedWord.js.flow +57 -0
  36. package/package.json +2 -2
@@ -44,7 +44,16 @@ export type HermesParserWASM = $ReadOnly<{
44
44
  (
45
45
  'hermesParse',
46
46
  'number',
47
- ['number', 'number', 'number', 'number', 'number', 'number', 'number'],
47
+ [
48
+ 'number',
49
+ 'number',
50
+ 'number',
51
+ 'number',
52
+ 'number',
53
+ 'number',
54
+ 'number',
55
+ 'number',
56
+ ],
48
57
  ): (
49
58
  number,
50
59
  number,
@@ -53,6 +62,7 @@ export type HermesParserWASM = $ReadOnly<{
53
62
  CBoolean,
54
63
  CBoolean,
55
64
  CBoolean,
65
+ CBoolean,
56
66
  ) => number,
57
67
  ('hermesParseResult_free', 'void', ['number']): number => void,
58
68
  ('hermesParseResult_getError', 'string', ['number']): number => string,
@@ -14,5 +14,5 @@ exports.ParserOptionsKeys = void 0;
14
14
  *
15
15
  * @format
16
16
  */
17
- const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens', 'transformOptions']);
17
+ const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'enableExperimentalFlowRecordSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens', 'transformOptions']);
18
18
  exports.ParserOptionsKeys = ParserOptionsKeys;
@@ -16,6 +16,7 @@ export type ParserOptions = {
16
16
  flow?: 'all' | 'detect',
17
17
  enableExperimentalComponentSyntax?: boolean,
18
18
  enableExperimentalFlowMatchSyntax?: boolean,
19
+ enableExperimentalFlowRecordSyntax?: boolean,
19
20
  reactRuntimeTarget?: '18' | '19',
20
21
  sourceFilename?: string,
21
22
  sourceType?: 'module' | 'script' | 'unambiguous',
@@ -33,6 +34,7 @@ export const ParserOptionsKeys: $ReadOnlySet<$Keys<ParserOptions>> = new Set([
33
34
  'flow',
34
35
  'enableExperimentalComponentSyntax',
35
36
  'enableExperimentalFlowMatchSyntax',
37
+ 'enableExperimentalFlowRecordSyntax',
36
38
  'reactRuntimeTarget',
37
39
  'sourceFilename',
38
40
  'sourceType',
@@ -30,6 +30,8 @@ const FlowESTreeAndBabelVisitorKeys = { ..._ESTreeVisitorKeys.default,
30
30
  BigIntLiteral: [],
31
31
  BlockStatement: ['directives', ..._ESTreeVisitorKeys.default.BlockStatement],
32
32
  BooleanLiteral: [],
33
+ ClassExpression: ['superTypeParameters', ..._ESTreeVisitorKeys.default.ClassExpression],
34
+ ClassDeclaration: ['superTypeParameters', ..._ESTreeVisitorKeys.default.ClassDeclaration],
33
35
  ClassMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
34
36
  ClassPrivateMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
35
37
  ClassProperty: ['key', 'value', 'typeAnnotation', 'variance'],
@@ -50,6 +52,7 @@ const FlowESTreeAndBabelVisitorKeys = { ..._ESTreeVisitorKeys.default,
50
52
  RegExpLiteral: [],
51
53
  RestElement: [..._ESTreeVisitorKeys.default.RestElement, 'typeAnnotation'],
52
54
  StringLiteral: [],
55
+ TupleTypeAnnotation: ['types'],
53
56
  CommentBlock: [],
54
57
  CommentLine: []
55
58
  };
@@ -821,10 +824,74 @@ function transformNode(node) {
821
824
  case 'TupleTypeAnnotation':
822
825
  {
823
826
  // $FlowExpectedError[cannot-write]
824
- delete node.inexact;
827
+ delete node.inexact; // $FlowExpectedError[prop-missing]
828
+
829
+ node.types = node.elementTypes; // $FlowExpectedError[cannot-write]
830
+
831
+ delete node.elementTypes;
825
832
  return node;
826
833
  }
827
834
 
835
+ case 'UnknownTypeAnnotation':
836
+ {
837
+ return {
838
+ type: 'GenericTypeAnnotation',
839
+ id: {
840
+ type: 'Identifier',
841
+ name: 'unknown',
842
+ optional: false,
843
+ typeAnnotation: null,
844
+ loc: node.loc,
845
+ range: node.range,
846
+ parent: EMPTY_PARENT
847
+ },
848
+ typeParameters: null,
849
+ loc: node.loc,
850
+ range: node.range,
851
+ parent: EMPTY_PARENT
852
+ };
853
+ }
854
+
855
+ case 'NeverTypeAnnotation':
856
+ {
857
+ return {
858
+ type: 'GenericTypeAnnotation',
859
+ id: {
860
+ type: 'Identifier',
861
+ name: 'never',
862
+ optional: false,
863
+ typeAnnotation: null,
864
+ loc: node.loc,
865
+ range: node.range,
866
+ parent: EMPTY_PARENT
867
+ },
868
+ typeParameters: null,
869
+ loc: node.loc,
870
+ range: node.range,
871
+ parent: EMPTY_PARENT
872
+ };
873
+ }
874
+
875
+ case 'UndefinedTypeAnnotation':
876
+ {
877
+ return {
878
+ type: 'GenericTypeAnnotation',
879
+ id: {
880
+ type: 'Identifier',
881
+ name: 'undefined',
882
+ optional: false,
883
+ typeAnnotation: null,
884
+ loc: node.loc,
885
+ range: node.range,
886
+ parent: EMPTY_PARENT
887
+ },
888
+ typeParameters: null,
889
+ loc: node.loc,
890
+ range: node.range,
891
+ parent: EMPTY_PARENT
892
+ };
893
+ }
894
+
828
895
  case 'JSXText':
829
896
  {
830
897
  // $FlowExpectedError[prop-missing]
@@ -987,9 +1054,15 @@ function transformNode(node) {
987
1054
  delete node.implements;
988
1055
  }
989
1056
 
990
- if (node.superTypeParameters == null) {
1057
+ if (node.superTypeArguments == null) {
1058
+ // $FlowExpectedError[cannot-write]
1059
+ delete node.superTypeArguments;
1060
+ } else {
991
1061
  // $FlowExpectedError[cannot-write]
992
- delete node.superTypeParameters;
1062
+ // $FlowExpectedError[prop-missing]
1063
+ node.superTypeParameters = node.superTypeArguments; // $FlowExpectedError[cannot-write]
1064
+
1065
+ delete node.superTypeArguments;
993
1066
  }
994
1067
 
995
1068
  if (node.typeParameters == null) {
@@ -59,6 +59,11 @@ const FlowESTreeAndBabelVisitorKeys: VisitorKeys = {
59
59
  BigIntLiteral: [],
60
60
  BlockStatement: ['directives', ...FlowVisitorKeys.BlockStatement],
61
61
  BooleanLiteral: [],
62
+ ClassExpression: ['superTypeParameters', ...FlowVisitorKeys.ClassExpression],
63
+ ClassDeclaration: [
64
+ 'superTypeParameters',
65
+ ...FlowVisitorKeys.ClassDeclaration,
66
+ ],
62
67
  ClassMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
63
68
  ClassPrivateMethod: ['key', 'params', 'body', 'returnType', 'typeParameters'],
64
69
  ClassProperty: ['key', 'value', 'typeAnnotation', 'variance'],
@@ -85,6 +90,7 @@ const FlowESTreeAndBabelVisitorKeys: VisitorKeys = {
85
90
  RegExpLiteral: [],
86
91
  RestElement: [...FlowVisitorKeys.RestElement, 'typeAnnotation'],
87
92
  StringLiteral: [],
93
+ TupleTypeAnnotation: ['types'],
88
94
  CommentBlock: [],
89
95
  CommentLine: [],
90
96
  };
@@ -1040,9 +1046,68 @@ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
1040
1046
  case 'TupleTypeAnnotation': {
1041
1047
  // $FlowExpectedError[cannot-write]
1042
1048
  delete node.inexact;
1049
+ // $FlowExpectedError[prop-missing]
1050
+ node.types = node.elementTypes;
1051
+ // $FlowExpectedError[cannot-write]
1052
+ delete node.elementTypes;
1043
1053
  return node;
1044
1054
  }
1045
1055
 
1056
+ case 'UnknownTypeAnnotation': {
1057
+ return {
1058
+ type: 'GenericTypeAnnotation',
1059
+ id: {
1060
+ type: 'Identifier',
1061
+ name: 'unknown',
1062
+ optional: false,
1063
+ typeAnnotation: null,
1064
+ loc: node.loc,
1065
+ range: node.range,
1066
+ parent: EMPTY_PARENT,
1067
+ },
1068
+ typeParameters: null,
1069
+ loc: node.loc,
1070
+ range: node.range,
1071
+ parent: EMPTY_PARENT,
1072
+ };
1073
+ }
1074
+ case 'NeverTypeAnnotation': {
1075
+ return {
1076
+ type: 'GenericTypeAnnotation',
1077
+ id: {
1078
+ type: 'Identifier',
1079
+ name: 'never',
1080
+ optional: false,
1081
+ typeAnnotation: null,
1082
+ loc: node.loc,
1083
+ range: node.range,
1084
+ parent: EMPTY_PARENT,
1085
+ },
1086
+ typeParameters: null,
1087
+ loc: node.loc,
1088
+ range: node.range,
1089
+ parent: EMPTY_PARENT,
1090
+ };
1091
+ }
1092
+ case 'UndefinedTypeAnnotation': {
1093
+ return {
1094
+ type: 'GenericTypeAnnotation',
1095
+ id: {
1096
+ type: 'Identifier',
1097
+ name: 'undefined',
1098
+ optional: false,
1099
+ typeAnnotation: null,
1100
+ loc: node.loc,
1101
+ range: node.range,
1102
+ parent: EMPTY_PARENT,
1103
+ },
1104
+ typeParameters: null,
1105
+ loc: node.loc,
1106
+ range: node.range,
1107
+ parent: EMPTY_PARENT,
1108
+ };
1109
+ }
1110
+
1046
1111
  case 'JSXText': {
1047
1112
  // $FlowExpectedError[prop-missing]
1048
1113
  node.extra = {
@@ -1167,9 +1232,15 @@ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
1167
1232
  // $FlowExpectedError[cannot-write]
1168
1233
  delete node.implements;
1169
1234
  }
1170
- if (node.superTypeParameters == null) {
1235
+ if (node.superTypeArguments == null) {
1236
+ // $FlowExpectedError[cannot-write]
1237
+ delete node.superTypeArguments;
1238
+ } else {
1239
+ // $FlowExpectedError[cannot-write]
1240
+ // $FlowExpectedError[prop-missing]
1241
+ node.superTypeParameters = node.superTypeArguments;
1171
1242
  // $FlowExpectedError[cannot-write]
1172
- delete node.superTypeParameters;
1243
+ delete node.superTypeArguments;
1173
1244
  }
1174
1245
  if (node.typeParameters == null) {
1175
1246
  // $FlowExpectedError[cannot-write]
@@ -117,7 +117,7 @@ function transformProgram(program, _options) {
117
117
  {
118
118
  return nodeWith(node, {
119
119
  typeParameters: null,
120
- superTypeParameters: null,
120
+ superTypeArguments: null,
121
121
  implements: [],
122
122
  decorators: []
123
123
  });
@@ -105,7 +105,7 @@ export function transformProgram(
105
105
  case 'ClassExpression': {
106
106
  return nodeWith(node, {
107
107
  typeParameters: null,
108
- superTypeParameters: null,
108
+ superTypeArguments: null,
109
109
  implements: [],
110
110
  decorators: [],
111
111
  });
@@ -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