hermes-transform 0.20.0 → 0.21.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.
- package/dist/detachedNode.js +1 -2
- package/dist/detachedNode.js.flow +7 -8
- package/dist/generated/TransformCloneSignatures.js.flow +34 -1
- package/dist/generated/TransformModifySignatures.js.flow +22 -1
- package/dist/generated/TransformReplaceSignatures.js.flow +22 -1
- package/dist/generated/node-types.js +40 -3
- package/dist/generated/node-types.js.flow +1037 -761
- package/dist/transform/TransformContext.js +4 -5
- package/dist/transform/TransformContext.js.flow +24 -31
- package/dist/transform/mutations/RemoveNode.js +5 -3
- package/dist/transform/mutations/RemoveNode.js.flow +5 -2
- package/dist/transform/mutations/ReplaceNode.js +7 -6
- package/dist/transform/mutations/ReplaceNode.js.flow +6 -10
- package/dist/traverse/traverse.js +0 -2
- package/dist/traverse/traverse.js.flow +1 -3
- package/package.json +6 -5
|
@@ -68,7 +68,9 @@ function getTransformContext() {
|
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
return nodes.map(
|
|
71
|
+
return nodes.map(node_ => {
|
|
72
|
+
const node = node_;
|
|
73
|
+
|
|
72
74
|
if (node == null) {
|
|
73
75
|
// $FlowExpectedError[incompatible-return]
|
|
74
76
|
return node;
|
|
@@ -157,7 +159,6 @@ function getTransformContext() {
|
|
|
157
159
|
}
|
|
158
160
|
};
|
|
159
161
|
const replaceAPIs = {
|
|
160
|
-
// $FlowFixMe[incompatible-exact]
|
|
161
162
|
replaceNode: (target, nodeToReplaceWith, options) => {
|
|
162
163
|
pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, (0, _detachedNode.asDetachedNode)(nodeToReplaceWith), options));
|
|
163
164
|
},
|
|
@@ -173,8 +174,7 @@ function getTransformContext() {
|
|
|
173
174
|
|
|
174
175
|
const cloned = (0, _detachedNode.shallowCloneNode)(node, newProps, {
|
|
175
176
|
preserveLocation: true
|
|
176
|
-
});
|
|
177
|
-
|
|
177
|
+
});
|
|
178
178
|
replaceAPIs.replaceNode(node, cloned, options);
|
|
179
179
|
}
|
|
180
180
|
};
|
|
@@ -197,7 +197,6 @@ function getTransformContext() {
|
|
|
197
197
|
|
|
198
198
|
function toArray(thing) {
|
|
199
199
|
if (Array.isArray(thing)) {
|
|
200
|
-
// $FlowExpectedError[incompatible-return]
|
|
201
200
|
return thing;
|
|
202
201
|
}
|
|
203
202
|
|
|
@@ -331,72 +331,64 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
331
331
|
|
|
332
332
|
const cloneAPIs: TransformCloneAPIs = {
|
|
333
333
|
// $FlowFixMe[incompatible-exact]
|
|
334
|
-
shallowCloneNode: ((
|
|
335
|
-
node: ?ESNode,
|
|
336
|
-
): // $FlowExpectedError[incompatible-cast]
|
|
337
|
-
?DetachedNode<ESNode> => {
|
|
334
|
+
shallowCloneNode: (((node: ?ESNode): ?DetachedNode<ESNode> => {
|
|
338
335
|
if (node == null) {
|
|
339
336
|
return null;
|
|
340
337
|
}
|
|
341
338
|
|
|
342
339
|
return shallowCloneNode(node, {});
|
|
343
|
-
}: TransformCloneAPIs['shallowCloneNode']),
|
|
340
|
+
}: $FlowFixMe): TransformCloneAPIs['shallowCloneNode']),
|
|
344
341
|
|
|
345
|
-
shallowCloneNodeWithOverrides: ((
|
|
342
|
+
shallowCloneNodeWithOverrides: (((
|
|
346
343
|
node: ?ESNode,
|
|
347
344
|
newProps?: $ReadOnly<{...}> = {},
|
|
348
|
-
): // $FlowExpectedError[
|
|
349
|
-
// $FlowExpectedError[prop-missing]
|
|
345
|
+
): // $FlowExpectedError[prop-missing]
|
|
350
346
|
?DetachedNode<ESNode> => {
|
|
351
347
|
if (node == null) {
|
|
352
348
|
return null;
|
|
353
349
|
}
|
|
354
350
|
|
|
355
351
|
return shallowCloneNode(node, newProps);
|
|
356
|
-
}: TransformCloneAPIs['shallowCloneNodeWithOverrides']),
|
|
352
|
+
}: $FlowFixMe): TransformCloneAPIs['shallowCloneNodeWithOverrides']),
|
|
357
353
|
|
|
358
354
|
// $FlowFixMe[incompatible-exact]
|
|
359
|
-
shallowCloneArray: (<T: ESNode>(
|
|
355
|
+
shallowCloneArray: ((<T: ESNode>(
|
|
360
356
|
nodes: ?$ReadOnlyArray<?T>,
|
|
361
|
-
):
|
|
362
|
-
?$ReadOnlyArray<DetachedNode<?ESNode>> => {
|
|
357
|
+
): ?$ReadOnlyArray<DetachedNode<?ESNode>> => {
|
|
363
358
|
if (nodes == null) {
|
|
364
359
|
return null;
|
|
365
360
|
}
|
|
366
361
|
|
|
367
|
-
return nodes.map((
|
|
362
|
+
return nodes.map((node_: ?T): DetachedNode<?ESNode> => {
|
|
363
|
+
const node: ?ESNode = node_;
|
|
368
364
|
if (node == null) {
|
|
369
365
|
// $FlowExpectedError[incompatible-return]
|
|
370
366
|
return node;
|
|
371
367
|
}
|
|
372
|
-
return shallowCloneNode<
|
|
368
|
+
return shallowCloneNode<ESNode>(node, {});
|
|
373
369
|
});
|
|
374
|
-
}: TransformCloneAPIs['shallowCloneArray']),
|
|
370
|
+
}: $FlowFixMe): TransformCloneAPIs['shallowCloneArray']),
|
|
375
371
|
|
|
376
372
|
// $FlowFixMe[incompatible-exact]
|
|
377
|
-
deepCloneNode: ((
|
|
378
|
-
node: ?ESNode,
|
|
379
|
-
): // $FlowExpectedError[incompatible-cast]
|
|
380
|
-
?DetachedNode<ESNode> => {
|
|
373
|
+
deepCloneNode: (((node: ?ESNode): ?DetachedNode<ESNode> => {
|
|
381
374
|
if (node == null) {
|
|
382
375
|
return null;
|
|
383
376
|
}
|
|
384
377
|
|
|
385
378
|
return deepCloneNode(node, {});
|
|
386
|
-
}: TransformCloneAPIs['deepCloneNode']),
|
|
379
|
+
}: $FlowFixMe): TransformCloneAPIs['deepCloneNode']),
|
|
387
380
|
|
|
388
|
-
deepCloneNodeWithOverrides: ((
|
|
381
|
+
deepCloneNodeWithOverrides: (((
|
|
389
382
|
node: ?ESNode,
|
|
390
383
|
newProps?: $ReadOnly<{...}> = {},
|
|
391
|
-
): // $FlowExpectedError[
|
|
392
|
-
// $FlowExpectedError[prop-missing]
|
|
384
|
+
): // $FlowExpectedError[prop-missing]
|
|
393
385
|
?DetachedNode<ESNode> => {
|
|
394
386
|
if (node == null) {
|
|
395
387
|
return null;
|
|
396
388
|
}
|
|
397
389
|
|
|
398
390
|
return deepCloneNode(node, newProps);
|
|
399
|
-
}: TransformCloneAPIs['deepCloneNodeWithOverrides']),
|
|
391
|
+
}: $FlowFixMe): TransformCloneAPIs['deepCloneNodeWithOverrides']),
|
|
400
392
|
};
|
|
401
393
|
const commentAPIs: TransformCommentAPIs = {
|
|
402
394
|
getComments: ((node): Array<Comment> => {
|
|
@@ -504,8 +496,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
504
496
|
}: TransformRemoveAPIs['removeStatement']),
|
|
505
497
|
};
|
|
506
498
|
const replaceAPIs: TransformReplaceAPIs = {
|
|
507
|
-
|
|
508
|
-
replaceNode: ((
|
|
499
|
+
replaceNode: (((
|
|
509
500
|
target: ESNode,
|
|
510
501
|
nodeToReplaceWith: MaybeDetachedNode<ESNode>,
|
|
511
502
|
options?: ReplaceNodeOptions,
|
|
@@ -517,7 +508,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
517
508
|
options,
|
|
518
509
|
),
|
|
519
510
|
);
|
|
520
|
-
}: TransformReplaceAPIs['replaceNode']),
|
|
511
|
+
}: $FlowFixMe): TransformReplaceAPIs['replaceNode']),
|
|
521
512
|
|
|
522
513
|
replaceStatementWithMany: ((
|
|
523
514
|
target,
|
|
@@ -544,8 +535,11 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
544
535
|
}
|
|
545
536
|
|
|
546
537
|
const cloned = shallowCloneNode(node, newProps, {preserveLocation: true});
|
|
547
|
-
|
|
548
|
-
|
|
538
|
+
replaceAPIs.replaceNode(
|
|
539
|
+
(node: $FlowFixMe),
|
|
540
|
+
(cloned: $FlowFixMe),
|
|
541
|
+
options,
|
|
542
|
+
);
|
|
549
543
|
}: TransformModifyAPIs['modifyNodeInPlace']),
|
|
550
544
|
};
|
|
551
545
|
|
|
@@ -568,8 +562,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
568
562
|
|
|
569
563
|
function toArray<T>(thing: SingleOrArray<T>): $ReadOnlyArray<T> {
|
|
570
564
|
if (Array.isArray(thing)) {
|
|
571
|
-
|
|
572
|
-
return thing;
|
|
565
|
+
return (thing: $FlowFixMe);
|
|
573
566
|
}
|
|
574
567
|
return [thing];
|
|
575
568
|
}
|
|
@@ -26,7 +26,7 @@ function createRemoveNodeMutation(node) {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const VALID_ENUM_MEMBER_PARENTS = ['EnumBooleanBody', 'EnumNumberBody', 'EnumStringBody', 'EnumSymbolBody'];
|
|
29
|
+
const VALID_ENUM_MEMBER_PARENTS = ['EnumBooleanBody', 'EnumNumberBody', 'EnumBigIntBody', 'EnumStringBody', 'EnumSymbolBody'];
|
|
30
30
|
const VALID_FUNCTION_PARAMETER_PARENTS = ['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression'];
|
|
31
31
|
const VALID_PROPERTY_PARENTS = ['ObjectExpression', 'ObjectPattern'];
|
|
32
32
|
const VALID_COMPONENT_TYPE_PARAMETER_PARENTS = ['DeclareComponent', 'ComponentTypeAnnotation'];
|
|
@@ -139,9 +139,10 @@ function getRemovalParent(node) {
|
|
|
139
139
|
|
|
140
140
|
case 'ObjectPattern':
|
|
141
141
|
return 'properties';
|
|
142
|
+
// $FlowFixMe[incompatible-type]
|
|
142
143
|
|
|
143
|
-
case 'CallExpression':
|
|
144
144
|
case 'OptionalCallExpression':
|
|
145
|
+
case 'CallExpression':
|
|
145
146
|
case 'NewExpression':
|
|
146
147
|
return 'arguments';
|
|
147
148
|
|
|
@@ -158,9 +159,10 @@ function getRemovalParent(node) {
|
|
|
158
159
|
|
|
159
160
|
case 'ObjectExpression':
|
|
160
161
|
return 'properties';
|
|
162
|
+
// $FlowFixMe[incompatible-type]
|
|
161
163
|
|
|
162
|
-
case 'CallExpression':
|
|
163
164
|
case 'OptionalCallExpression':
|
|
165
|
+
case 'CallExpression':
|
|
164
166
|
case 'NewExpression':
|
|
165
167
|
return 'arguments';
|
|
166
168
|
|
|
@@ -70,6 +70,7 @@ export function createRemoveNodeMutation(
|
|
|
70
70
|
const VALID_ENUM_MEMBER_PARENTS: $ReadOnlyArray<string> = [
|
|
71
71
|
'EnumBooleanBody',
|
|
72
72
|
'EnumNumberBody',
|
|
73
|
+
'EnumBigIntBody',
|
|
73
74
|
'EnumStringBody',
|
|
74
75
|
'EnumSymbolBody',
|
|
75
76
|
];
|
|
@@ -214,8 +215,9 @@ function getRemovalParent(node: RemoveNodeMutation['node']): $ReadOnly<{
|
|
|
214
215
|
case 'ObjectPattern':
|
|
215
216
|
return 'properties';
|
|
216
217
|
|
|
217
|
-
|
|
218
|
+
// $FlowFixMe[incompatible-type]
|
|
218
219
|
case 'OptionalCallExpression':
|
|
220
|
+
case 'CallExpression':
|
|
219
221
|
case 'NewExpression':
|
|
220
222
|
return 'arguments';
|
|
221
223
|
|
|
@@ -244,8 +246,9 @@ function getRemovalParent(node: RemoveNodeMutation['node']): $ReadOnly<{
|
|
|
244
246
|
case 'ObjectExpression':
|
|
245
247
|
return 'properties';
|
|
246
248
|
|
|
247
|
-
|
|
249
|
+
// $FlowFixMe[incompatible-type]
|
|
248
250
|
case 'OptionalCallExpression':
|
|
251
|
+
case 'CallExpression':
|
|
249
252
|
case 'NewExpression':
|
|
250
253
|
return 'arguments';
|
|
251
254
|
|
|
@@ -60,18 +60,19 @@ function getParentKey(target) {
|
|
|
60
60
|
const parent = target.parent;
|
|
61
61
|
|
|
62
62
|
for (const key of (0, _hermesParser.getVisitorKeys)(parent)) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
const child = parent[key];
|
|
64
|
+
|
|
65
|
+
if ((0, _hermesParser.isNode)(child)) {
|
|
66
|
+
if (child === target) {
|
|
66
67
|
return {
|
|
67
68
|
type: 'single',
|
|
68
69
|
parent,
|
|
69
70
|
key
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
|
-
} else if (Array.isArray(
|
|
73
|
-
for (let i = 0; i <
|
|
74
|
-
const current =
|
|
73
|
+
} else if (Array.isArray(child)) {
|
|
74
|
+
for (let i = 0; i < child.length; i += 1) {
|
|
75
|
+
const current = child[i];
|
|
75
76
|
const originalNode = (0, _detachedNode.getOriginalNode)(current);
|
|
76
77
|
|
|
77
78
|
if (current === target || originalNode === target) {
|
|
@@ -88,18 +88,14 @@ function getParentKey(target: ESNode): $ReadOnly<
|
|
|
88
88
|
> {
|
|
89
89
|
const parent = target.parent;
|
|
90
90
|
for (const key of getVisitorKeys(parent)) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
parent[key],
|
|
95
|
-
)
|
|
96
|
-
) {
|
|
97
|
-
if (parent[key] === target) {
|
|
91
|
+
const child = (parent: $FlowFixMe)[key];
|
|
92
|
+
if (isNode(child)) {
|
|
93
|
+
if (child === target) {
|
|
98
94
|
return {type: 'single', parent, key};
|
|
99
95
|
}
|
|
100
|
-
} else if (Array.isArray(
|
|
101
|
-
for (let i = 0; i <
|
|
102
|
-
const current =
|
|
96
|
+
} else if (Array.isArray(child)) {
|
|
97
|
+
for (let i = 0; i < child.length; i += 1) {
|
|
98
|
+
const current = child[i];
|
|
103
99
|
const originalNode = getOriginalNode(current);
|
|
104
100
|
if (current === target || originalNode === target) {
|
|
105
101
|
return {type: 'array', parent, key, targetIndex: i};
|
|
@@ -109,8 +109,6 @@ function traverseWithContext(code, ast, scopeManager, additionalContext, visitor
|
|
|
109
109
|
|
|
110
110
|
Object.keys(selectors).forEach(selector => {
|
|
111
111
|
// flow doesn't want us to be general here - but it's safe
|
|
112
|
-
// $FlowExpectedError[incompatible-type]
|
|
113
|
-
// $FlowExpectedError[prop-missing]
|
|
114
112
|
const listener = selectors[selector];
|
|
115
113
|
|
|
116
114
|
if (listener) {
|
|
@@ -175,9 +175,7 @@ export function traverseWithContext<T = TraversalContextBase>(
|
|
|
175
175
|
// add all the selectors from the visitor as listeners
|
|
176
176
|
Object.keys(selectors).forEach(selector => {
|
|
177
177
|
// flow doesn't want us to be general here - but it's safe
|
|
178
|
-
|
|
179
|
-
// $FlowExpectedError[prop-missing]
|
|
180
|
-
const listener: ?EmitterListener = selectors[selector];
|
|
178
|
+
const listener: ?EmitterListener = (selectors: $FlowFixMe)[selector];
|
|
181
179
|
if (listener) {
|
|
182
180
|
emitter.on(selector, listener);
|
|
183
181
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-transform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Tools built on top of Hermes-ESTree to enable codebase transformation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,13 +12,14 @@
|
|
|
12
12
|
"@babel/code-frame": "^7.16.0",
|
|
13
13
|
"esquery": "^1.4.0",
|
|
14
14
|
"flow-enums-runtime": "^0.0.6",
|
|
15
|
-
"hermes-eslint": "0.
|
|
16
|
-
"hermes-estree": "0.
|
|
17
|
-
"hermes-parser": "0.
|
|
15
|
+
"hermes-eslint": "0.21.0",
|
|
16
|
+
"hermes-estree": "0.21.0",
|
|
17
|
+
"hermes-parser": "0.21.0",
|
|
18
|
+
"string-width": "4.2.3"
|
|
18
19
|
},
|
|
19
20
|
"peerDependencies": {
|
|
20
21
|
"prettier": "^3.0.0 || ^2.7.1",
|
|
21
|
-
"prettier-plugin-hermes-parser": "0.
|
|
22
|
+
"prettier-plugin-hermes-parser": "0.21.0"
|
|
22
23
|
},
|
|
23
24
|
"files": [
|
|
24
25
|
"dist",
|