hermes-estree 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 (87) hide show
  1. package/dist/generated/HermesESTreeSelectorTypes.js.flow +262 -27
  2. package/dist/generated/predicates.js +120 -0
  3. package/dist/generated/predicates.js.flow +72 -0
  4. package/dist/predicates.js +2 -4
  5. package/dist/predicates.js.flow +5 -4
  6. package/dist/src/generated/predicates.js +120 -0
  7. package/dist/src/predicates.js +2 -4
  8. package/dist/types.js.flow +103 -11
  9. package/package.json +1 -1
  10. package/dist/HermesAST.js.flow +0 -57
  11. package/dist/HermesASTAdapter.js +0 -192
  12. package/dist/HermesASTAdapter.js.flow +0 -189
  13. package/dist/HermesParser.js +0 -108
  14. package/dist/HermesParser.js.flow +0 -161
  15. package/dist/HermesParserDecodeUTF8String.js +0 -68
  16. package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
  17. package/dist/HermesParserDeserializer.js +0 -242
  18. package/dist/HermesParserDeserializer.js.flow +0 -260
  19. package/dist/HermesParserNodeDeserializers.js +0 -2477
  20. package/dist/HermesParserNodeDeserializers.js.flow +0 -16
  21. package/dist/HermesParserWASM.js +0 -6
  22. package/dist/HermesParserWASM.js.flow +0 -87
  23. package/dist/HermesToESTreeAdapter.js +0 -439
  24. package/dist/HermesToESTreeAdapter.js.flow +0 -421
  25. package/dist/ParserOptions.js +0 -18
  26. package/dist/ParserOptions.js.flow +0 -41
  27. package/dist/babel/TransformESTreeToBabel.js +0 -1123
  28. package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
  29. package/dist/estree/StripFlowTypes.js +0 -175
  30. package/dist/estree/StripFlowTypes.js.flow +0 -158
  31. package/dist/estree/StripFlowTypesForBabel.js +0 -215
  32. package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
  33. package/dist/estree/TransformComponentSyntax.js +0 -788
  34. package/dist/estree/TransformComponentSyntax.js.flow +0 -864
  35. package/dist/estree/TransformEnumSyntax.js +0 -106
  36. package/dist/estree/TransformEnumSyntax.js.flow +0 -125
  37. package/dist/estree/TransformMatchSyntax.js +0 -1006
  38. package/dist/estree/TransformMatchSyntax.js.flow +0 -912
  39. package/dist/generated/ESTreeVisitorKeys.js +0 -220
  40. package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
  41. package/dist/generated/ParserVisitorKeys.js +0 -794
  42. package/dist/generated/ParserVisitorKeys.js.flow +0 -17
  43. package/dist/getModuleDocblock.js +0 -112
  44. package/dist/getModuleDocblock.js.flow +0 -118
  45. package/dist/src/HermesASTAdapter.js +0 -192
  46. package/dist/src/HermesParser.js +0 -108
  47. package/dist/src/HermesParserDecodeUTF8String.js +0 -68
  48. package/dist/src/HermesParserDeserializer.js +0 -242
  49. package/dist/src/HermesParserNodeDeserializers.js +0 -2477
  50. package/dist/src/HermesToESTreeAdapter.js +0 -439
  51. package/dist/src/ParserOptions.js +0 -18
  52. package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
  53. package/dist/src/estree/StripFlowTypes.js +0 -175
  54. package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
  55. package/dist/src/estree/TransformComponentSyntax.js +0 -788
  56. package/dist/src/estree/TransformEnumSyntax.js +0 -106
  57. package/dist/src/estree/TransformMatchSyntax.js +0 -1006
  58. package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
  59. package/dist/src/generated/ParserVisitorKeys.js +0 -794
  60. package/dist/src/getModuleDocblock.js +0 -112
  61. package/dist/src/transform/SimpleTransform.js +0 -136
  62. package/dist/src/transform/astArrayMutationHelpers.js +0 -62
  63. package/dist/src/transform/astNodeMutationHelpers.js +0 -200
  64. package/dist/src/traverse/SimpleTraverser.js +0 -137
  65. package/dist/src/traverse/getVisitorKeys.js +0 -37
  66. package/dist/src/utils/Builders.js +0 -191
  67. package/dist/src/utils/GenID.js +0 -41
  68. package/dist/src/utils/createSyntaxError.js +0 -25
  69. package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
  70. package/dist/transform/SimpleTransform.js +0 -136
  71. package/dist/transform/SimpleTransform.js.flow +0 -169
  72. package/dist/transform/astArrayMutationHelpers.js +0 -62
  73. package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
  74. package/dist/transform/astNodeMutationHelpers.js +0 -200
  75. package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
  76. package/dist/traverse/SimpleTraverser.js +0 -137
  77. package/dist/traverse/SimpleTraverser.js.flow +0 -133
  78. package/dist/traverse/getVisitorKeys.js +0 -37
  79. package/dist/traverse/getVisitorKeys.js.flow +0 -36
  80. package/dist/utils/Builders.js +0 -191
  81. package/dist/utils/Builders.js.flow +0 -218
  82. package/dist/utils/GenID.js +0 -41
  83. package/dist/utils/GenID.js.flow +0 -38
  84. package/dist/utils/createSyntaxError.js +0 -25
  85. package/dist/utils/createSyntaxError.js.flow +0 -24
  86. package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
  87. package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
@@ -1,112 +0,0 @@
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.getModuleDocblock = getModuleDocblock;
16
- exports.parseDocblockString = parseDocblockString;
17
- const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
18
-
19
- function parseDocblockString(docblock) {
20
- const directiveLines = docblock.split('\n') // remove the leading " *" from each line
21
- .map(line => line.trimStart().replace(/^\* ?/, '').trim()).filter(line => line.startsWith('@'));
22
- const directives = // $FlowExpectedError[incompatible-type] - flow types this return as {...}
23
- Object.create(null);
24
-
25
- for (const line of directiveLines) {
26
- var _match$;
27
-
28
- const match = DIRECTIVE_REGEX.exec(line);
29
-
30
- if (match == null) {
31
- continue;
32
- }
33
-
34
- const name = match[1]; // explicitly use an empty string if there's no value
35
- // this way the array length tracks how many instances of the directive there was
36
-
37
- const value = ((_match$ = match[2]) != null ? _match$ : '').trim();
38
-
39
- if (directives[name]) {
40
- directives[name].push(value);
41
- } else {
42
- directives[name] = [value];
43
- }
44
- }
45
-
46
- return directives;
47
- }
48
-
49
- function getModuleDocblock(hermesProgram) {
50
- const docblockNode = (() => {
51
- if (hermesProgram.type !== 'Program') {
52
- return null;
53
- } // $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
54
-
55
-
56
- const program = hermesProgram;
57
-
58
- if (program.comments.length === 0) {
59
- return null;
60
- }
61
-
62
- const firstComment = (() => {
63
- const first = program.comments[0];
64
-
65
- if (first.type === 'Block') {
66
- return first;
67
- }
68
-
69
- if (program.comments.length === 1) {
70
- return null;
71
- } // ESLint will always strip out the shebang comment from the code before passing it to the parser
72
- // https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
73
- // this means that we're forced to parse it as a line comment :(
74
- // this hacks around it by selecting the second comment in this case
75
-
76
-
77
- const second = program.comments[1];
78
-
79
- if (first.type === 'Line' && first.range[0] === 0 && second.type === 'Block') {
80
- return second;
81
- }
82
-
83
- return null;
84
- })();
85
-
86
- if (firstComment == null) {
87
- return null;
88
- }
89
- /*
90
- Handle cases like this where the comment isn't actually the first thing in the code:
91
- ```
92
- const x = 1; /* docblock *./
93
- ```
94
- */
95
-
96
-
97
- if (program.body.length > 0 && program.body[0].range[0] < firstComment.range[0]) {
98
- return null;
99
- }
100
-
101
- return firstComment;
102
- })();
103
-
104
- if (docblockNode == null) {
105
- return null;
106
- }
107
-
108
- return {
109
- directives: parseDocblockString(docblockNode.value),
110
- comment: docblockNode
111
- };
112
- }
@@ -1,136 +0,0 @@
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
- function setParentPointer(node, parent) {
22
- if (parent != null) {
23
- if (Array.isArray(node)) {
24
- for (const item of node) {
25
- // $FlowExpectedError[cannot-write]
26
- item.parent = parent;
27
- }
28
- } else {
29
- // $FlowExpectedError[cannot-write]
30
- node.parent = parent;
31
- }
32
- }
33
- }
34
- /**
35
- * A simple class to recursively tranform AST trees.
36
- */
37
-
38
-
39
- class SimpleTransform {
40
- /**
41
- * Transform the given AST tree.
42
- * @param rootNode The root node to traverse.
43
- * @param options The option object.
44
- * @return The modified rootNode or a new node if the rootNode was transformed directly.
45
- */
46
- transform(rootNode, options) {
47
- let resultRootNode = rootNode;
48
-
49
- _SimpleTraverser.SimpleTraverser.traverse(rootNode, {
50
- enter: (node, parent) => {
51
- // Ensure the parent pointers are correctly set before entering the node.
52
- setParentPointer(node, parent);
53
- const resultNode = options.transform(node);
54
-
55
- if (resultNode !== node) {
56
- let traversedResultNode = null;
57
-
58
- if (resultNode != null) {
59
- // Ensure the new node has the correct parent pointers before recursing again.
60
- setParentPointer(resultNode, parent);
61
-
62
- if (Array.isArray(resultNode)) {
63
- traversedResultNode = resultNode.map(item => this.transform(item, options)).filter(item => item != null);
64
- } else {
65
- traversedResultNode = this.transform(resultNode, options);
66
- }
67
- }
68
-
69
- if (parent == null) {
70
- if (node !== rootNode) {
71
- throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
72
- }
73
-
74
- if (Array.isArray(traversedResultNode)) {
75
- throw new Error('SimpleTransform: invalid array result for root node');
76
- } else {
77
- resultRootNode = traversedResultNode;
78
- }
79
- } else if (traversedResultNode == null) {
80
- (0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
81
- } else {
82
- (0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode, options.visitorKeys);
83
- setParentPointer(traversedResultNode, parent);
84
- }
85
-
86
- throw _SimpleTraverser.SimpleTraverser.Skip;
87
- }
88
- },
89
-
90
- leave(_node) {},
91
-
92
- visitorKeys: options.visitorKeys
93
- });
94
-
95
- return resultRootNode;
96
- }
97
- /**
98
- * Transform the given AST tree.
99
- * @param node The root node to traverse.
100
- * @param options The option object.
101
- */
102
-
103
-
104
- static transform(node, options) {
105
- return new SimpleTransform().transform(node, options);
106
- }
107
-
108
- static transformProgram(program, options) {
109
- const result = SimpleTransform.transform(program, options);
110
-
111
- if ((result == null ? void 0 : result.type) === 'Program') {
112
- return result;
113
- }
114
-
115
- throw new Error('SimpleTransform.transformProgram: Expected program node.');
116
- }
117
- /**
118
- * Return a new AST node with the given properties overrided if needed.
119
- *
120
- * This function takes care to only create new nodes when needed. Referential equality of nodes
121
- * is important as its used to know if a node should be re-traversed.
122
- *
123
- * @param node The base AST node.
124
- * @param overrideProps New properties to apply to the node.
125
- * @return Either the orginal node if the properties matched the existing node or a new node with
126
- * the new properties.
127
- */
128
-
129
-
130
- static nodeWith(node, overrideProps, visitorKeys) {
131
- return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps, visitorKeys);
132
- }
133
-
134
- }
135
-
136
- exports.SimpleTransform = SimpleTransform;
@@ -1,62 +0,0 @@
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
- }
@@ -1,200 +0,0 @@
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.deepCloneNode = deepCloneNode;
16
- exports.nodeWith = nodeWith;
17
- exports.removeNodeOnParent = removeNodeOnParent;
18
- exports.replaceNodeOnParent = replaceNodeOnParent;
19
- exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
20
- exports.shallowCloneNode = shallowCloneNode;
21
- exports.updateAllParentPointers = updateAllParentPointers;
22
-
23
- var _astArrayMutationHelpers = require("./astArrayMutationHelpers");
24
-
25
- var _getVisitorKeys = require("../traverse/getVisitorKeys");
26
-
27
- var _SimpleTraverser = require("../traverse/SimpleTraverser");
28
-
29
- function getParentKey(target, parent, visitorKeys) {
30
- if (parent == null) {
31
- throw new Error(`Expected parent node to be set on "${target.type}"`);
32
- }
33
-
34
- for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
35
- if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
36
- parent[key])) {
37
- // $FlowFixMe[invalid-compare]
38
- if (parent[key] === target) {
39
- return {
40
- type: 'single',
41
- node: parent,
42
- key
43
- };
44
- }
45
- } else if (Array.isArray(parent[key])) {
46
- for (let i = 0; i < parent[key].length; i += 1) {
47
- // $FlowExpectedError[invalid-tuple-index]
48
- const current = parent[key][i];
49
-
50
- if (current === target) {
51
- return {
52
- type: 'array',
53
- node: parent,
54
- key,
55
- targetIndex: i
56
- };
57
- }
58
- }
59
- }
60
- } // this shouldn't happen ever
61
-
62
-
63
- throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
64
- }
65
- /**
66
- * Replace a node with a new node within an AST (via the parent pointer).
67
- */
68
-
69
-
70
- function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith, visitorKeys) {
71
- const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
72
- const parent = replacementParent.node;
73
-
74
- if (replacementParent.type === 'array') {
75
- // $FlowExpectedError[prop-missing]
76
- parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
77
- parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
78
- } else {
79
- if (Array.isArray(nodeToReplaceWith)) {
80
- throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
81
- } // $FlowExpectedError[prop-missing]
82
-
83
-
84
- parent[replacementParent.key] = nodeToReplaceWith;
85
- }
86
- }
87
- /**
88
- * Remove a node from the AST its connected to (via the parent pointer).
89
- */
90
-
91
-
92
- function removeNodeOnParent(originalNode, originalNodeParent, visitorKeys) {
93
- const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
94
- const parent = replacementParent.node;
95
-
96
- if (replacementParent.type === 'array') {
97
- // $FlowExpectedError[prop-missing]
98
- parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)( // $FlowExpectedError[prop-missing]
99
- parent[replacementParent.key], replacementParent.targetIndex);
100
- } else {
101
- // $FlowExpectedError[prop-missing]
102
- parent[replacementParent.key] = null;
103
- }
104
- }
105
- /**
106
- * Corrects the parent pointers in direct children of the given node.
107
- */
108
-
109
-
110
- function setParentPointersInDirectChildren(node, visitorKeys) {
111
- for (const key of (0, _getVisitorKeys.getVisitorKeys)(node, visitorKeys)) {
112
- if ((0, _getVisitorKeys.isNode)(node[key])) {
113
- // $FlowExpectedError[cannot-write]
114
- node[key].parent = node;
115
- } else if (Array.isArray(node[key])) {
116
- for (const child of node[key]) {
117
- child.parent = node;
118
- }
119
- }
120
- }
121
- }
122
- /**
123
- * Traverses the entire subtree to ensure the parent pointers are set correctly.
124
- */
125
-
126
-
127
- function updateAllParentPointers(node, visitorKeys) {
128
- _SimpleTraverser.SimpleTraverser.traverse(node, {
129
- enter(node, parent) {
130
- // $FlowExpectedError[cannot-write]
131
- node.parent = parent;
132
- },
133
-
134
- leave() {},
135
-
136
- visitorKeys
137
- });
138
- }
139
- /**
140
- * Clone node and add new props.
141
- *
142
- * This will only create a new object if the overrides actually result in a change.
143
- */
144
-
145
-
146
- function nodeWith(node, overrideProps, visitorKeys) {
147
- // Check if this will actually result in a change, maintaining referential equality is important.
148
- const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => {
149
- const node_ = node;
150
-
151
- if (Array.isArray(value)) {
152
- return Array.isArray(node_[key]) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node_[key], value) : false;
153
- }
154
-
155
- return node_[key] === value;
156
- });
157
-
158
- if (willBeUnchanged) {
159
- return node;
160
- } // Create new node.
161
- // $FlowExpectedError[cannot-spread-interface]
162
-
163
-
164
- const newNode = { ...node,
165
- ...overrideProps
166
- }; // Ensure parent pointers are correctly set within this nodes children.
167
-
168
- setParentPointersInDirectChildren(newNode, visitorKeys);
169
- return newNode;
170
- }
171
- /**
172
- * Shallow clones node, providing a new reference for an existing node.
173
- */
174
-
175
-
176
- function shallowCloneNode(node, visitorKeys) {
177
- // $FlowExpectedError[cannot-spread-interface]
178
- const newNode = { ...node
179
- }; // Ensure parent pointers are correctly set within this nodes children.
180
-
181
- setParentPointersInDirectChildren(newNode, visitorKeys);
182
- return newNode;
183
- }
184
- /**
185
- * Deeply clones node and its entire tree.
186
- */
187
-
188
-
189
- function deepCloneNode(node, visitorKeys) {
190
- const clone = JSON.parse(JSON.stringify(node, (key, value) => {
191
- // null out parent pointers
192
- if (key === 'parent') {
193
- return undefined;
194
- }
195
-
196
- return value;
197
- }));
198
- updateAllParentPointers(clone, visitorKeys);
199
- return clone;
200
- }
@@ -1,137 +0,0 @@
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.SimpleTraverserSkip = exports.SimpleTraverserBreak = exports.SimpleTraverser = void 0;
16
-
17
- var _getVisitorKeys = require("./getVisitorKeys");
18
-
19
- /**
20
- * Can be thrown within the traversal "enter" function to prevent the traverser
21
- * from traversing the node any further, essentially culling the remainder of the
22
- * AST branch
23
- */
24
- const SimpleTraverserSkip = new Error();
25
- /**
26
- * Can be thrown at any point during the traversal to immediately stop traversal
27
- * entirely.
28
- */
29
-
30
- exports.SimpleTraverserSkip = SimpleTraverserSkip;
31
- const SimpleTraverserBreak = new Error();
32
- /**
33
- * A very simple traverser class to traverse AST trees.
34
- */
35
-
36
- exports.SimpleTraverserBreak = SimpleTraverserBreak;
37
-
38
- class SimpleTraverser {
39
- /**
40
- * Traverse the given AST tree.
41
- * @param node The root node to traverse.
42
- * @param options The option object.
43
- */
44
- traverse(node, options) {
45
- try {
46
- this._traverse(node, null, options);
47
- } catch (ex) {
48
- if (ex === SimpleTraverserBreak) {
49
- return;
50
- }
51
-
52
- throw ex;
53
- }
54
- }
55
- /**
56
- * Traverse the given AST tree recursively.
57
- * @param node The current node.
58
- * @param parent The parent node.
59
- * @private
60
- */
61
-
62
-
63
- _traverse(node, parent, options) {
64
- if (!(0, _getVisitorKeys.isNode)(node)) {
65
- return;
66
- }
67
-
68
- try {
69
- options.enter(node, parent);
70
- } catch (ex) {
71
- if (ex === SimpleTraverserSkip) {
72
- return;
73
- }
74
-
75
- this._setErrorContext(ex, node);
76
-
77
- throw ex;
78
- }
79
-
80
- const keys = (0, _getVisitorKeys.getVisitorKeys)(node, options.visitorKeys);
81
-
82
- for (const key of keys) {
83
- const child = node[key];
84
-
85
- if (Array.isArray(child)) {
86
- for (let j = 0; j < child.length; ++j) {
87
- this._traverse(child[j], node, options);
88
- }
89
- } else {
90
- this._traverse(child, node, options);
91
- }
92
- }
93
-
94
- try {
95
- options.leave(node, parent);
96
- } catch (ex) {
97
- if (ex === SimpleTraverserSkip) {
98
- return;
99
- }
100
-
101
- this._setErrorContext(ex, node);
102
-
103
- throw ex;
104
- }
105
- }
106
- /**
107
- * Set useful contextual information onto the error object.
108
- * @param ex The error object.
109
- * @param node The current node.
110
- * @private
111
- */
112
-
113
-
114
- _setErrorContext(ex, node) {
115
- // $FlowFixMe[prop-missing]
116
- ex.currentNode = {
117
- type: node.type,
118
- range: node.range,
119
- loc: node.loc
120
- };
121
- }
122
- /**
123
- * Traverse the given AST tree.
124
- * @param node The root node to traverse.
125
- * @param options The option object.
126
- */
127
-
128
-
129
- static traverse(node, options) {
130
- new SimpleTraverser().traverse(node, options);
131
- }
132
-
133
- }
134
-
135
- exports.SimpleTraverser = SimpleTraverser;
136
- SimpleTraverser.Break = SimpleTraverserBreak;
137
- SimpleTraverser.Skip = SimpleTraverserSkip;
@@ -1,37 +0,0 @@
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
- * @noformat
9
- */
10
- 'use strict';
11
-
12
- Object.defineProperty(exports, "__esModule", {
13
- value: true
14
- });
15
- exports.getVisitorKeys = getVisitorKeys;
16
- exports.isNode = isNode;
17
-
18
- var _ESTreeVisitorKeys = _interopRequireDefault(require("../generated/ESTreeVisitorKeys"));
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function isNode(thing)
23
- /*: implies thing is {+[string]: mixed} */
24
- {
25
- return typeof thing === 'object' && thing != null && typeof thing.type === 'string';
26
- }
27
-
28
- function getVisitorKeys(node, visitorKeys) {
29
- const keys = (visitorKeys != null ? visitorKeys : _ESTreeVisitorKeys.default)[node.type];
30
-
31
- if (keys == null) {
32
- throw new Error(`No visitor keys found for node type "${node.type}".`);
33
- } // $FlowExpectedError[prop-missing]
34
-
35
-
36
- return keys;
37
- }