hermes-parser 0.28.1 → 0.29.1

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 (41) hide show
  1. package/dist/HermesASTAdapter.js +1 -2
  2. package/dist/HermesASTAdapter.js.flow +2 -5
  3. package/dist/HermesParserNodeDeserializers.js +3 -2
  4. package/dist/HermesParserWASM.js +1 -1
  5. package/dist/HermesToESTreeAdapter.js +12 -0
  6. package/dist/HermesToESTreeAdapter.js.flow +10 -0
  7. package/dist/babel/TransformESTreeToBabel.js.flow +21 -19
  8. package/dist/estree/StripComponentSyntax.js.flow +6 -5
  9. package/dist/estree/TransformMatchSyntax.js +8 -18
  10. package/dist/estree/TransformMatchSyntax.js.flow +22 -23
  11. package/dist/generated/ESTreeVisitorKeys.js +1 -1
  12. package/dist/generated/ParserVisitorKeys.js +1 -1
  13. package/dist/index.js.flow +1 -1
  14. package/dist/src/HermesASTAdapter.js +192 -0
  15. package/dist/src/HermesParser.js +108 -0
  16. package/dist/src/HermesParserDecodeUTF8String.js +68 -0
  17. package/dist/src/HermesParserDeserializer.js +243 -0
  18. package/dist/src/HermesParserNodeDeserializers.js +2473 -0
  19. package/dist/src/HermesToESTreeAdapter.js +453 -0
  20. package/dist/src/ParserOptions.js +18 -0
  21. package/dist/src/babel/TransformESTreeToBabel.js +1104 -0
  22. package/dist/src/estree/StripComponentSyntax.js +788 -0
  23. package/dist/src/estree/StripFlowTypes.js +175 -0
  24. package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
  25. package/dist/src/estree/TransformMatchSyntax.js +1006 -0
  26. package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
  27. package/dist/src/generated/ParserVisitorKeys.js +790 -0
  28. package/dist/src/getModuleDocblock.js +112 -0
  29. package/dist/src/index.js +153 -0
  30. package/dist/src/transform/SimpleTransform.js +120 -0
  31. package/dist/src/transform/astArrayMutationHelpers.js +62 -0
  32. package/dist/src/transform/astNodeMutationHelpers.js +195 -0
  33. package/dist/src/traverse/SimpleTraverser.js +137 -0
  34. package/dist/src/traverse/getVisitorKeys.js +37 -0
  35. package/dist/src/utils/Builders.js +191 -0
  36. package/dist/src/utils/GenID.js +41 -0
  37. package/dist/src/utils/createSyntaxError.js +25 -0
  38. package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
  39. package/dist/utils/Builders.js +17 -0
  40. package/dist/utils/Builders.js.flow +20 -0
  41. package/package.json +2 -2
@@ -0,0 +1,453 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _HermesASTAdapter = _interopRequireDefault(require("./HermesASTAdapter"));
9
+
10
+ var _getModuleDocblock = require("./getModuleDocblock");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ *
21
+ * @format
22
+ */
23
+ class HermesToESTreeAdapter extends _HermesASTAdapter.default {
24
+ constructor(options, code) {
25
+ super(options);
26
+ this.code = void 0;
27
+ this.code = code;
28
+ }
29
+
30
+ fixSourceLocation(node) {
31
+ var _this$sourceFilename;
32
+
33
+ const loc = node.loc;
34
+
35
+ if (loc == null) {
36
+ return;
37
+ }
38
+
39
+ node.loc = {
40
+ source: (_this$sourceFilename = this.sourceFilename) != null ? _this$sourceFilename : null,
41
+ start: loc.start,
42
+ end: loc.end
43
+ };
44
+ node.range = [loc.rangeStart, loc.rangeEnd];
45
+ delete node.start;
46
+ delete node.end;
47
+ }
48
+
49
+ mapNode(node) {
50
+ this.fixSourceLocation(node);
51
+
52
+ switch (node.type) {
53
+ case 'Program':
54
+ return this.mapProgram(node);
55
+
56
+ case 'NullLiteral':
57
+ return this.mapNullLiteral(node);
58
+
59
+ case 'BooleanLiteral':
60
+ case 'StringLiteral':
61
+ case 'NumericLiteral':
62
+ case 'JSXStringLiteral':
63
+ return this.mapSimpleLiteral(node);
64
+
65
+ case 'BigIntLiteral':
66
+ return this.mapBigIntLiteral(node);
67
+
68
+ case 'RegExpLiteral':
69
+ return this.mapRegExpLiteral(node);
70
+
71
+ case 'Empty':
72
+ return this.mapEmpty(node);
73
+
74
+ case 'TemplateElement':
75
+ return this.mapTemplateElement(node);
76
+
77
+ case 'BigIntLiteralTypeAnnotation':
78
+ return this.mapBigIntLiteralTypeAnnotation(node);
79
+
80
+ case 'GenericTypeAnnotation':
81
+ return this.mapGenericTypeAnnotation(node);
82
+
83
+ case 'ImportDeclaration':
84
+ return this.mapImportDeclaration(node);
85
+
86
+ case 'ImportSpecifier':
87
+ return this.mapImportSpecifier(node);
88
+
89
+ case 'ExportDefaultDeclaration':
90
+ return this.mapExportDefaultDeclaration(node);
91
+
92
+ case 'ExportNamedDeclaration':
93
+ return this.mapExportNamedDeclaration(node);
94
+
95
+ case 'ExportAllDeclaration':
96
+ return this.mapExportAllDeclaration(node);
97
+
98
+ case 'Property':
99
+ return this.mapProperty(node);
100
+
101
+ case 'FunctionDeclaration':
102
+ case 'FunctionExpression':
103
+ case 'ArrowFunctionExpression':
104
+ return this.mapFunction(node);
105
+
106
+ case 'PrivateName':
107
+ return this.mapPrivateName(node);
108
+
109
+ case 'ClassProperty':
110
+ case 'ClassPrivateProperty':
111
+ return this.mapClassProperty(node);
112
+
113
+ case 'MemberExpression':
114
+ case 'OptionalMemberExpression':
115
+ case 'CallExpression':
116
+ case 'OptionalCallExpression':
117
+ return this.mapChainExpression(node);
118
+
119
+ case 'BlockStatement':
120
+ return this.mapBlockStatement(node);
121
+
122
+ default:
123
+ return this.mapNodeDefault(node);
124
+ }
125
+ }
126
+
127
+ mapProgram(node) {
128
+ const nodeDefault = this.mapNodeDefault(node);
129
+ node.sourceType = this.getSourceType();
130
+ node.docblock = (0, _getModuleDocblock.getModuleDocblock)(nodeDefault);
131
+ return nodeDefault;
132
+ }
133
+
134
+ mapSimpleLiteral(node) {
135
+ return {
136
+ type: 'Literal',
137
+ loc: node.loc,
138
+ range: node.range,
139
+ value: node.value,
140
+ raw: this.code.slice(node.range[0], node.range[1]),
141
+ literalType: (() => {
142
+ switch (node.type) {
143
+ case 'NullLiteral':
144
+ return 'null';
145
+
146
+ case 'BooleanLiteral':
147
+ return 'boolean';
148
+
149
+ case 'StringLiteral':
150
+ case 'JSXStringLiteral':
151
+ return 'string';
152
+
153
+ case 'NumericLiteral':
154
+ return 'numeric';
155
+
156
+ case 'BigIntLiteral':
157
+ return 'bigint';
158
+
159
+ case 'RegExpLiteral':
160
+ return 'regexp';
161
+ }
162
+
163
+ return null;
164
+ })()
165
+ };
166
+ }
167
+
168
+ mapBigIntLiteral(node) {
169
+ const newNode = this.mapSimpleLiteral(node);
170
+ return { ...newNode,
171
+ ...this.getBigIntLiteralValue(node.bigint)
172
+ };
173
+ }
174
+
175
+ mapNullLiteral(node) {
176
+ return { ...this.mapSimpleLiteral(node),
177
+ value: null
178
+ };
179
+ }
180
+
181
+ mapRegExpLiteral(node) {
182
+ const {
183
+ pattern,
184
+ flags
185
+ } = node; // Create RegExp value if possible. This can fail when the flags are invalid.
186
+
187
+ let value;
188
+
189
+ try {
190
+ value = new RegExp(pattern, flags);
191
+ } catch (e) {
192
+ value = null;
193
+ }
194
+
195
+ return { ...this.mapSimpleLiteral(node),
196
+ value,
197
+ regex: {
198
+ pattern,
199
+ flags
200
+ }
201
+ };
202
+ }
203
+
204
+ mapBigIntLiteralTypeAnnotation(node) {
205
+ return { ...node,
206
+ ...this.getBigIntLiteralValue(node.raw)
207
+ };
208
+ }
209
+
210
+ mapTemplateElement(node) {
211
+ return {
212
+ type: 'TemplateElement',
213
+ loc: node.loc,
214
+ range: node.range,
215
+ tail: node.tail,
216
+ value: {
217
+ cooked: node.cooked,
218
+ raw: node.raw
219
+ }
220
+ };
221
+ }
222
+
223
+ mapGenericTypeAnnotation(node) {
224
+ // Convert simple `this` generic type to ThisTypeAnnotation
225
+ if (node.typeParameters == null && node.id.type === 'Identifier' && node.id.name === 'this') {
226
+ return {
227
+ type: 'ThisTypeAnnotation',
228
+ loc: node.loc,
229
+ range: node.range
230
+ };
231
+ }
232
+
233
+ return this.mapNodeDefault(node);
234
+ }
235
+
236
+ mapProperty(nodeUnprocessed) {
237
+ const node = this.mapNodeDefault(nodeUnprocessed);
238
+
239
+ if (node.value.type === 'FunctionExpression' && (node.method || node.kind !== 'init')) {
240
+ node.value.loc.start = node.key.loc.end;
241
+ node.value.range[0] = node.key.range[1];
242
+ }
243
+
244
+ return node;
245
+ }
246
+
247
+ mapComment(node) {
248
+ if (node.type === 'CommentBlock') {
249
+ node.type = 'Block';
250
+ } else if (node.type === 'CommentLine') {
251
+ node.type = 'Line';
252
+ }
253
+
254
+ return node;
255
+ }
256
+
257
+ mapFunction(nodeUnprocessed) {
258
+ const node = this.mapNodeDefault(nodeUnprocessed);
259
+
260
+ switch (node.type) {
261
+ // This prop should ideally only live on `ArrowFunctionExpression` but to
262
+ // match espree output we place it on all functions types.
263
+ case 'FunctionDeclaration':
264
+ case 'FunctionExpression':
265
+ node.expression = false;
266
+ return node;
267
+
268
+ case 'ArrowFunctionExpression':
269
+ node.expression = node.body.type !== 'BlockStatement';
270
+ return node;
271
+ }
272
+
273
+ return node;
274
+ }
275
+
276
+ mapChainExpression(nodeUnprocessed) {
277
+ /*
278
+ NOTE - In the below comments `MemberExpression` and `CallExpression`
279
+ are completely interchangable. For terseness we just reference
280
+ one each time.
281
+ */
282
+
283
+ /*
284
+ Hermes uses the old babel-style AST:
285
+ ```
286
+ (one?.two).three?.four;
287
+ ^^^^^^^^^^^^^^^^^^^^^^ OptionalMemberExpression
288
+ ^^^^^^^^^^^^^^^^ MemberExpression
289
+ ^^^^^^^^ OptionalMemberExpression
290
+ ```
291
+ We need to convert it to the ESTree representation:
292
+ ```
293
+ (one?.two).three?.four;
294
+ ^^^^^^^^^^^^^^^^^^^^^^ ChainExpression
295
+ ^^^^^^^^^^^^^^^^^^^^^^ MemberExpression[optional = true]
296
+ ^^^^^^^^^^^^^^^^ MemberExpression[optional = false]
297
+ ^^^^^^^^ ChainExpression
298
+ ^^^^^^^^ MemberExpression[optional = true]
299
+ ```
300
+ We do this by converting the AST and its children (depth first), and then unwrapping
301
+ the resulting AST as appropriate.
302
+ Put another way:
303
+ 1) traverse to the leaf
304
+ 2) if the current node is an `OptionalMemberExpression`:
305
+ a) if the `.object` is a `ChainExpression`:
306
+ i) unwrap the child (`node.object = child.expression`)
307
+ b) convert this node to a `MemberExpression[optional = true]`
308
+ c) wrap this node (`node = ChainExpression[expression = node]`)
309
+ 3) if the current node is a `MemberExpression`:
310
+ a) convert this node to a `MemberExpression[optional = true]`
311
+ */
312
+ const node = this.mapNodeDefault(nodeUnprocessed);
313
+
314
+ const {
315
+ child,
316
+ childKey,
317
+ isOptional
318
+ } = (() => {
319
+ const isOptional = node.optional === true;
320
+
321
+ if (node.type.endsWith('MemberExpression')) {
322
+ return {
323
+ child: node.object,
324
+ childKey: 'object',
325
+ isOptional
326
+ };
327
+ } else if (node.type.endsWith('CallExpression')) {
328
+ return {
329
+ child: node.callee,
330
+ childKey: 'callee',
331
+ isOptional
332
+ };
333
+ } else {
334
+ return {
335
+ child: node.expression,
336
+ childKey: 'expression',
337
+ isOptional: false
338
+ };
339
+ }
340
+ })();
341
+
342
+ const isChildUnwrappable = child.type === 'ChainExpression' && // (x?.y).z is semantically different to `x?.y.z`.
343
+ // In the un-parenthesised case `.z` is only executed if and only if `x?.y` returns a non-nullish value.
344
+ // In the parenthesised case, `.z` is **always** executed, regardless of the return of `x?.y`.
345
+ // As such the AST is different between the two cases.
346
+ //
347
+ // In the hermes AST - any member part of a non-short-circuited optional chain is represented with `OptionalMemberExpression`
348
+ // so if we see a `MemberExpression`, then we know we've hit a parenthesis boundary.
349
+ node.type !== 'MemberExpression' && node.type !== 'CallExpression';
350
+
351
+ if (node.type.startsWith('Optional')) {
352
+ node.type = node.type.replace('Optional', '');
353
+ node.optional = isOptional;
354
+ } else {
355
+ node.optional = false;
356
+ }
357
+
358
+ if (!isChildUnwrappable && !isOptional) {
359
+ return node;
360
+ }
361
+
362
+ if (isChildUnwrappable) {
363
+ const newChild = child.expression;
364
+ node[childKey] = newChild;
365
+ }
366
+
367
+ return {
368
+ type: 'ChainExpression',
369
+ expression: node,
370
+ loc: node.loc,
371
+ range: node.range
372
+ };
373
+ }
374
+
375
+ mapClassProperty(nodeUnprocessed) {
376
+ const node = this.mapNodeDefault(nodeUnprocessed);
377
+
378
+ const key = (() => {
379
+ if (node.type === 'ClassPrivateProperty') {
380
+ const key = this.mapNodeDefault(node.key);
381
+ return {
382
+ type: 'PrivateIdentifier',
383
+ name: key.name,
384
+ range: key.range,
385
+ loc: key.loc
386
+ };
387
+ }
388
+
389
+ return node.key;
390
+ })();
391
+
392
+ return { ...node,
393
+ computed: node.type === 'ClassPrivateProperty' ? false : node.computed,
394
+ key,
395
+ type: 'PropertyDefinition'
396
+ };
397
+ }
398
+
399
+ mapPrivateName(node) {
400
+ return {
401
+ type: 'PrivateIdentifier',
402
+ name: node.id.name,
403
+ // estree the location refers to the entire string including the hash token
404
+ range: node.range,
405
+ loc: node.loc
406
+ };
407
+ }
408
+
409
+ mapExportNamedDeclaration(nodeUnprocessed) {
410
+ const node = super.mapExportNamedDeclaration(nodeUnprocessed);
411
+ const namespaceSpecifier = node.specifiers.find(spec => spec.type === 'ExportNamespaceSpecifier');
412
+
413
+ if (namespaceSpecifier != null) {
414
+ var _node$exportKind;
415
+
416
+ if (node.specifiers.length !== 1) {
417
+ // this should already a hermes parser error - but let's be absolutely sure we're aligned with the spec
418
+ throw new Error('Cannot use an export all with any other specifiers');
419
+ }
420
+
421
+ return {
422
+ type: 'ExportAllDeclaration',
423
+ source: node.source,
424
+ exportKind: (_node$exportKind = node.exportKind) != null ? _node$exportKind : 'value',
425
+ exported: namespaceSpecifier.exported,
426
+ range: node.range,
427
+ loc: node.loc
428
+ };
429
+ }
430
+
431
+ return node;
432
+ }
433
+
434
+ mapExportAllDeclaration(nodeUnprocessed) {
435
+ var _node$exported;
436
+
437
+ const node = super.mapExportAllDeclaration(nodeUnprocessed);
438
+ node.exported = (_node$exported = node.exported) != null ? _node$exported : null;
439
+ return node;
440
+ }
441
+
442
+ mapBlockStatement(node) {
443
+ if (node.implicit && node.body.length) {
444
+ return this.mapNode(node.body[0]);
445
+ }
446
+
447
+ delete node.implicit;
448
+ return this.mapNodeDefault(node);
449
+ }
450
+
451
+ }
452
+
453
+ exports.default = HermesToESTreeAdapter;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ParserOptionsKeys = void 0;
7
+
8
+ /**
9
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
10
+ *
11
+ * This source code is licensed under the MIT license found in the
12
+ * LICENSE file in the root directory of this source tree.
13
+ *
14
+ *
15
+ * @format
16
+ */
17
+ const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens']);
18
+ exports.ParserOptionsKeys = ParserOptionsKeys;