hermes-parser 0.9.0 → 0.10.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.
@@ -121,6 +121,12 @@ class HermesToBabelAdapter extends _HermesASTAdapter.default {
121
121
  case 'BigIntLiteral':
122
122
  return this.mapBigIntLiteral(node);
123
123
 
124
+ case 'BigIntLiteralTypeAnnotation':
125
+ return this.mapBigIntLiteralTypeAnnotation(node);
126
+
127
+ case 'BigIntTypeAnnotation':
128
+ return this.mapBigIntTypeAnnotation(node);
129
+
124
130
  default:
125
131
  return this.mapNodeDefault(node);
126
132
  }
@@ -436,10 +442,36 @@ class HermesToBabelAdapter extends _HermesASTAdapter.default {
436
442
  }
437
443
 
438
444
  mapBigIntLiteral(node) {
439
- const bigint = node.bigint.replace(/n$/, '').replace(/_/, '');
440
- node.value = typeof BigInt === 'function' ? BigInt(bigint) : null;
445
+ node.value = this.getBigIntLiteralValue(node.bigint).value;
446
+ return node;
447
+ }
448
+
449
+ mapBigIntLiteralTypeAnnotation(node) {
450
+ node.value = this.getBigIntLiteralValue(node.raw).value;
441
451
  return node;
442
452
  }
453
+ /**
454
+ * Babel does not parse the bigint keyword type as the keyword node.
455
+ * So we need to down-level the AST to a plain GenericTypeAnnotation
456
+ */
457
+
458
+
459
+ mapBigIntTypeAnnotation(node) {
460
+ return {
461
+ type: 'GenericTypeAnnotation',
462
+ id: {
463
+ type: 'Identifier',
464
+ name: 'bigint',
465
+ loc: node.loc,
466
+ start: node.start,
467
+ end: node.end
468
+ },
469
+ typeParameters: null,
470
+ loc: node.loc,
471
+ start: node.start,
472
+ end: node.end
473
+ };
474
+ }
443
475
 
444
476
  mapPrivateProperty(nodeUnprocessed) {
445
477
  const node = this.mapNodeDefault(nodeUnprocessed);
@@ -91,6 +91,10 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
91
91
  return this.mapUnsupportedTypeAnnotation(node);
92
92
  case 'BigIntLiteral':
93
93
  return this.mapBigIntLiteral(node);
94
+ case 'BigIntLiteralTypeAnnotation':
95
+ return this.mapBigIntLiteralTypeAnnotation(node);
96
+ case 'BigIntTypeAnnotation':
97
+ return this.mapBigIntTypeAnnotation(node);
94
98
  default:
95
99
  return this.mapNodeDefault(node);
96
100
  }
@@ -413,10 +417,33 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
413
417
  }
414
418
 
415
419
  mapBigIntLiteral(node: HermesNode): HermesNode {
416
- const bigint = node.bigint.replace(/n$/, '').replace(/_/, '');
417
- node.value = typeof BigInt === 'function' ? BigInt(bigint) : null;
420
+ node.value = this.getBigIntLiteralValue(node.bigint).value;
418
421
  return node;
419
422
  }
423
+ mapBigIntLiteralTypeAnnotation(node: HermesNode): HermesNode {
424
+ node.value = this.getBigIntLiteralValue(node.raw).value;
425
+ return node;
426
+ }
427
+ /**
428
+ * Babel does not parse the bigint keyword type as the keyword node.
429
+ * So we need to down-level the AST to a plain GenericTypeAnnotation
430
+ */
431
+ mapBigIntTypeAnnotation(node: HermesNode): HermesNode {
432
+ return {
433
+ type: 'GenericTypeAnnotation',
434
+ id: {
435
+ type: 'Identifier',
436
+ name: 'bigint',
437
+ loc: node.loc,
438
+ start: node.start,
439
+ end: node.end,
440
+ },
441
+ typeParameters: null,
442
+ loc: node.loc,
443
+ start: node.start,
444
+ end: node.end,
445
+ };
446
+ }
420
447
 
421
448
  mapPrivateProperty(nodeUnprocessed: HermesNode): HermesNode {
422
449
  const node = this.mapNodeDefault(nodeUnprocessed);
@@ -173,14 +173,8 @@ class HermesToESTreeAdapter extends _HermesASTAdapter.default {
173
173
 
174
174
  mapBigIntLiteral(node) {
175
175
  const newNode = this.mapSimpleLiteral(node);
176
- const bigint = node.bigint // estree spec is to not have a trailing `n` on this property
177
- // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
178
- .replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
179
- .replace(/_/, '');
180
176
  return { ...newNode,
181
- // coerce the string to a bigint value if supported by the environment
182
- value: typeof BigInt === 'function' ? BigInt(bigint) : null,
183
- bigint
177
+ ...this.getBigIntLiteralValue(node.bigint)
184
178
  };
185
179
  }
186
180
 
@@ -214,8 +208,9 @@ class HermesToESTreeAdapter extends _HermesASTAdapter.default {
214
208
  }
215
209
 
216
210
  mapBigIntLiteralTypeAnnotation(node) {
217
- node.value = null;
218
- return node;
211
+ return { ...node,
212
+ ...this.getBigIntLiteralValue(node.raw)
213
+ };
219
214
  }
220
215
 
221
216
  mapTemplateElement(node) {
@@ -150,17 +150,9 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
150
150
 
151
151
  mapBigIntLiteral(node: HermesNode): HermesNode {
152
152
  const newNode = this.mapSimpleLiteral(node);
153
- const bigint = node.bigint
154
- // estree spec is to not have a trailing `n` on this property
155
- // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
156
- .replace(/n$/, '')
157
- // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
158
- .replace(/_/, '');
159
153
  return {
160
154
  ...newNode,
161
- // coerce the string to a bigint value if supported by the environment
162
- value: typeof BigInt === 'function' ? BigInt(bigint) : null,
163
- bigint,
155
+ ...this.getBigIntLiteralValue(node.bigint),
164
156
  };
165
157
  }
166
158
 
@@ -193,8 +185,10 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
193
185
  }
194
186
 
195
187
  mapBigIntLiteralTypeAnnotation(node: HermesNode): HermesNode {
196
- node.value = null;
197
- return node;
188
+ return {
189
+ ...node,
190
+ ...this.getBigIntLiteralValue(node.raw),
191
+ };
198
192
  }
199
193
 
200
194
  mapTemplateElement(node: HermesNode): HermesNode {
@@ -6,6 +6,13 @@
6
6
  *
7
7
  *
8
8
  * @format
9
+ * @generated
10
+ */
11
+
12
+ /*
13
+ * !!! GENERATED FILE !!!
14
+ *
15
+ * Any manual changes to this file will be overwritten. To regenerate run `yarn build`.
9
16
  */
10
17
  // lint directives to let us do some basic validation of generated files
11
18
 
@@ -55,6 +62,7 @@ const HERMES_AST_VISITOR_KEYS = {
55
62
  },
56
63
  BigIntLiteral: {},
57
64
  BigIntLiteralTypeAnnotation: {},
65
+ BigIntTypeAnnotation: {},
58
66
  BinaryExpression: {
59
67
  left: 'Node',
60
68
  right: 'Node'
@@ -454,7 +462,8 @@ const HERMES_AST_VISITOR_KEYS = {
454
462
  key: 'Node',
455
463
  value: 'Node',
456
464
  variance: 'Node',
457
- typeAnnotation: 'Node'
465
+ typeAnnotation: 'Node',
466
+ tsModifiers: 'Node'
458
467
  },
459
468
  QualifiedTypeIdentifier: {
460
469
  qualification: 'Node',
@@ -19,7 +19,8 @@ const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
19
19
  function parseDocblockString(docblock) {
20
20
  const directiveLines = docblock.split('\n') // remove the leading " *" from each line
21
21
  .map(line => line.trimStart().replace(/^\* ?/, '').trim()).filter(line => line.startsWith('@'));
22
- const directives = {};
22
+ const directives = // $FlowExpectedError[incompatible-type] - flow types this return as {...}
23
+ Object.create(null);
23
24
 
24
25
  for (const line of directiveLines) {
25
26
  var _match$;
@@ -24,7 +24,9 @@ export function parseDocblockString(docblock: string): DocblockDirectives {
24
24
 
25
25
  const directives: {
26
26
  [string]: Array<string>,
27
- } = {};
27
+ } =
28
+ // $FlowExpectedError[incompatible-type] - flow types this return as {...}
29
+ Object.create(null);
28
30
 
29
31
  for (const line of directiveLines) {
30
32
  const match = DIRECTIVE_REGEX.exec(line);
@@ -0,0 +1 @@
1
+ "use strict";
File without changes
package/dist/index.js CHANGED
@@ -12,6 +12,12 @@
12
12
  Object.defineProperty(exports, "__esModule", {
13
13
  value: true
14
14
  });
15
+ var _exportNames = {
16
+ parse: true,
17
+ astArrayMutationHelpers: true,
18
+ astNodeMutationHelpers: true
19
+ };
20
+ exports.astNodeMutationHelpers = exports.astArrayMutationHelpers = void 0;
15
21
  exports.parse = parse;
16
22
 
17
23
  var HermesParser = _interopRequireWildcard(require("./HermesParser"));
@@ -20,6 +26,41 @@ var _HermesToBabelAdapter = _interopRequireDefault(require("./HermesToBabelAdapt
20
26
 
21
27
  var _HermesToESTreeAdapter = _interopRequireDefault(require("./HermesToESTreeAdapter"));
22
28
 
29
+ var _SimpleTraverser = require("./traverse/SimpleTraverser");
30
+
31
+ Object.keys(_SimpleTraverser).forEach(function (key) {
32
+ if (key === "default" || key === "__esModule") return;
33
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
34
+ if (key in exports && exports[key] === _SimpleTraverser[key]) return;
35
+ exports[key] = _SimpleTraverser[key];
36
+ });
37
+
38
+ var _SimpleTransform = require("./transform/SimpleTransform");
39
+
40
+ Object.keys(_SimpleTransform).forEach(function (key) {
41
+ if (key === "default" || key === "__esModule") return;
42
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
43
+ if (key in exports && exports[key] === _SimpleTransform[key]) return;
44
+ exports[key] = _SimpleTransform[key];
45
+ });
46
+
47
+ var _getVisitorKeys = require("./traverse/getVisitorKeys");
48
+
49
+ Object.keys(_getVisitorKeys).forEach(function (key) {
50
+ if (key === "default" || key === "__esModule") return;
51
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
52
+ if (key in exports && exports[key] === _getVisitorKeys[key]) return;
53
+ exports[key] = _getVisitorKeys[key];
54
+ });
55
+
56
+ var _astArrayMutationHelpers = _interopRequireWildcard(require("./transform/astArrayMutationHelpers"));
57
+
58
+ exports.astArrayMutationHelpers = _astArrayMutationHelpers;
59
+
60
+ var _astNodeMutationHelpers = _interopRequireWildcard(require("./transform/astNodeMutationHelpers"));
61
+
62
+ exports.astNodeMutationHelpers = _astNodeMutationHelpers;
63
+
23
64
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
65
 
25
66
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
@@ -83,3 +83,8 @@ export function parse(
83
83
  }
84
84
 
85
85
  export type {ParserOptions} from './ParserOptions';
86
+ export * from './traverse/SimpleTraverser';
87
+ export * from './transform/SimpleTransform';
88
+ export * from './traverse/getVisitorKeys';
89
+ export * as astArrayMutationHelpers from './transform/astArrayMutationHelpers';
90
+ export * as astNodeMutationHelpers from './transform/astNodeMutationHelpers';
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @format
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.SimpleTransform = void 0;
16
+
17
+ var _SimpleTraverser = require("../traverse/SimpleTraverser");
18
+
19
+ var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
20
+
21
+ /**
22
+ * A simple class to recursively tranform AST trees.
23
+ */
24
+ class SimpleTransform {
25
+ /**
26
+ * Transform the given AST tree.
27
+ * @param rootNode The root node to traverse.
28
+ * @param options The option object.
29
+ * @return The modified rootNode or a new node if the rootNode was transformed directly.
30
+ */
31
+ transform(rootNode, options) {
32
+ let resultRootNode = rootNode;
33
+
34
+ _SimpleTraverser.SimpleTraverser.traverse(rootNode, {
35
+ enter: (node, parent) => {
36
+ const resultNode = options.transform(node);
37
+
38
+ if (resultNode !== node) {
39
+ const traversedResultNode = resultNode != null ? this.transform(resultNode, options) : null;
40
+
41
+ if (parent == null) {
42
+ if (node !== rootNode) {
43
+ throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
44
+ }
45
+
46
+ resultRootNode = traversedResultNode;
47
+ } else if (traversedResultNode == null) {
48
+ (0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent);
49
+ } else {
50
+ (0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode);
51
+ }
52
+
53
+ throw _SimpleTraverser.SimpleTraverser.Skip;
54
+ }
55
+ },
56
+
57
+ leave(_node) {}
58
+
59
+ });
60
+
61
+ return resultRootNode;
62
+ }
63
+ /**
64
+ * Transform the given AST tree.
65
+ * @param node The root node to traverse.
66
+ * @param options The option object.
67
+ */
68
+
69
+
70
+ static transform(node, options) {
71
+ return new SimpleTransform().transform(node, options);
72
+ }
73
+ /**
74
+ * Return a new AST node with the given properties overrided if needed.
75
+ *
76
+ * This function takes care to only create new nodes when needed. Referential equality of nodes
77
+ * is important as its used to know if a node should be re-traversed.
78
+ *
79
+ * @param node The base AST node.
80
+ * @param overrideProps New properties to apply to the node.
81
+ * @return Either the orginal node if the properties matched the existing node or a new node with
82
+ * the new properties.
83
+ */
84
+
85
+
86
+ static nodeWith(node, overrideProps) {
87
+ return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps);
88
+ }
89
+
90
+ }
91
+
92
+ exports.SimpleTransform = SimpleTransform;
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {VisitorKeysType} from '../traverse/getVisitorKeys';
14
+ import type {ESNode} from 'hermes-estree';
15
+
16
+ import {SimpleTraverser} from '../traverse/SimpleTraverser';
17
+ import {
18
+ nodeWith,
19
+ removeNodeOnParent,
20
+ replaceNodeOnParent,
21
+ } from './astNodeMutationHelpers';
22
+
23
+ /**
24
+ * Transform callback
25
+ * @param node The node we are visiting
26
+ * @returns
27
+ * - return input node, signals no changes were made will continue to the next node.
28
+ * - return new node, the old node will be replaced in the AST. The new node and its
29
+ * children are then traversed.
30
+ * - return null, signals the node should be deleted from the AST.
31
+ */
32
+ export type TransformCallback = (node: ESNode) => ESNode | null;
33
+
34
+ export type TransformOptions = $ReadOnly<{
35
+ /** The callback function which is called on entering each node. */
36
+ transform: TransformCallback,
37
+
38
+ /** The set of visitor keys to use for traversal. Defaults to the `hermes-eslint` visitor keys */
39
+ visitorKeys?: ?VisitorKeysType,
40
+ }>;
41
+
42
+ /**
43
+ * A simple class to recursively tranform AST trees.
44
+ */
45
+ export class SimpleTransform {
46
+ /**
47
+ * Transform the given AST tree.
48
+ * @param rootNode The root node to traverse.
49
+ * @param options The option object.
50
+ * @return The modified rootNode or a new node if the rootNode was transformed directly.
51
+ */
52
+ transform(rootNode: ESNode, options: TransformOptions): ESNode | null {
53
+ let resultRootNode: ESNode | null = rootNode;
54
+ SimpleTraverser.traverse(rootNode, {
55
+ enter: (node: ESNode, parent: ?ESNode) => {
56
+ const resultNode = options.transform(node);
57
+ if (resultNode !== node) {
58
+ const traversedResultNode =
59
+ resultNode != null ? this.transform(resultNode, options) : null;
60
+ if (parent == null) {
61
+ if (node !== rootNode) {
62
+ throw new Error(
63
+ 'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
64
+ );
65
+ }
66
+ resultRootNode = traversedResultNode;
67
+ } else if (traversedResultNode == null) {
68
+ removeNodeOnParent(node, parent);
69
+ } else {
70
+ replaceNodeOnParent(node, parent, traversedResultNode);
71
+ }
72
+
73
+ throw SimpleTraverser.Skip;
74
+ }
75
+ },
76
+ leave(_node: ESNode) {},
77
+ });
78
+ return resultRootNode;
79
+ }
80
+
81
+ /**
82
+ * Transform the given AST tree.
83
+ * @param node The root node to traverse.
84
+ * @param options The option object.
85
+ */
86
+ static transform(node: ESNode, options: TransformOptions): ESNode | null {
87
+ return new SimpleTransform().transform(node, options);
88
+ }
89
+
90
+ /**
91
+ * Return a new AST node with the given properties overrided if needed.
92
+ *
93
+ * This function takes care to only create new nodes when needed. Referential equality of nodes
94
+ * is important as its used to know if a node should be re-traversed.
95
+ *
96
+ * @param node The base AST node.
97
+ * @param overrideProps New properties to apply to the node.
98
+ * @return Either the orginal node if the properties matched the existing node or a new node with
99
+ * the new properties.
100
+ */
101
+ static nodeWith<T: ESNode>(node: T, overrideProps: $Partial<T>): T {
102
+ return nodeWith<T>(node, overrideProps);
103
+ }
104
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.arrayIsEqual = arrayIsEqual;
7
+ exports.insertInArray = insertInArray;
8
+ exports.removeFromArray = removeFromArray;
9
+ exports.replaceInArray = replaceInArray;
10
+
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ *
18
+ * @format
19
+ */
20
+ function assertArrayBounds(array, index) {
21
+ if (index < 0 || index >= array.length) {
22
+ throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
23
+ }
24
+ }
25
+
26
+ function arrayIsEqual(a1, a2) {
27
+ if (a1 === a2) {
28
+ return true;
29
+ }
30
+
31
+ if (a1.length !== a2.length) {
32
+ return false;
33
+ }
34
+
35
+ for (let i = 0; i < a1.length; i++) {
36
+ if (a1[i] !== a2[i]) {
37
+ return false;
38
+ }
39
+ }
40
+
41
+ return true;
42
+ }
43
+
44
+ function insertInArray(array, index, elements) {
45
+ if (index === array.length) {
46
+ // Support the insert at end of array case.
47
+ return array.concat(elements);
48
+ }
49
+
50
+ assertArrayBounds(array, index);
51
+ return array.slice(0, index).concat(elements).concat(array.slice(index));
52
+ }
53
+
54
+ function removeFromArray(array, index) {
55
+ assertArrayBounds(array, index);
56
+ return [...array.slice(0, index), ...array.slice(index + 1)];
57
+ }
58
+
59
+ function replaceInArray(array, index, elements) {
60
+ assertArrayBounds(array, index);
61
+ return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
62
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ function assertArrayBounds<T>(array: $ReadOnlyArray<T>, index: number): void {
12
+ if (index < 0 || index >= array.length) {
13
+ throw new Error(
14
+ `Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`,
15
+ );
16
+ }
17
+ }
18
+
19
+ export function arrayIsEqual(
20
+ a1: $ReadOnlyArray<mixed>,
21
+ a2: $ReadOnlyArray<mixed>,
22
+ ): boolean {
23
+ if (a1 === a2) {
24
+ return true;
25
+ }
26
+
27
+ if (a1.length !== a2.length) {
28
+ return false;
29
+ }
30
+
31
+ for (let i = 0; i < a1.length; i++) {
32
+ if (a1[i] !== a2[i]) {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ return true;
38
+ }
39
+
40
+ export function insertInArray<T>(
41
+ array: $ReadOnlyArray<T>,
42
+ index: number,
43
+ elements: $ReadOnlyArray<T>,
44
+ ): Array<T> {
45
+ if (index === array.length) {
46
+ // Support the insert at end of array case.
47
+ return array.concat(elements);
48
+ }
49
+ assertArrayBounds(array, index);
50
+ return array.slice(0, index).concat(elements).concat(array.slice(index));
51
+ }
52
+
53
+ export function removeFromArray<T>(
54
+ array: $ReadOnlyArray<T>,
55
+ index: number,
56
+ ): Array<T> {
57
+ assertArrayBounds(array, index);
58
+ return [...array.slice(0, index), ...array.slice(index + 1)];
59
+ }
60
+
61
+ export function replaceInArray<T>(
62
+ array: $ReadOnlyArray<T>,
63
+ index: number,
64
+ elements: $ReadOnlyArray<T>,
65
+ ): Array<T> {
66
+ assertArrayBounds(array, index);
67
+ return array
68
+ .slice(0, index)
69
+ .concat(elements)
70
+ .concat(array.slice(index + 1));
71
+ }