hermes-transform 0.36.1 → 0.37.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 +3 -57
- package/dist/detachedNode.js.flow +12 -12
- package/dist/generated/TransformCloneSignatures.js.flow +1 -1
- package/dist/generated/TransformModifySignatures.js.flow +1 -1
- package/dist/generated/TransformReplaceSignatures.js.flow +207 -207
- package/dist/generated/node-types.js +0 -21
- package/dist/generated/node-types.js.flow +1086 -978
- package/dist/generated/special-case-node-types/Comment.js +0 -13
- package/dist/generated/special-case-node-types/Comment.js.flow +2 -2
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +10 -10
- package/dist/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/generated/special-case-node-types/DeclareHook.js.flow +4 -4
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +9 -9
- package/dist/generated/special-case-node-types/Literal.js +1 -12
- package/dist/generated/special-case-node-types/Literal.js.flow +20 -20
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +23 -21
- package/dist/generated/special-case-node-types/Property.js +0 -9
- package/dist/generated/special-case-node-types/Property.js.flow +50 -42
- package/dist/generated/special-case-node-types/misc.js +6 -35
- package/dist/generated/special-case-node-types/misc.js.flow +56 -48
- package/dist/index.js +0 -9
- package/dist/src/detachedNode.js +3 -57
- package/dist/src/generated/node-types.js +0 -21
- package/dist/src/generated/special-case-node-types/Comment.js +0 -13
- package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/src/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/src/generated/special-case-node-types/Literal.js +1 -12
- package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/src/generated/special-case-node-types/Property.js +0 -9
- package/dist/src/generated/special-case-node-types/misc.js +6 -35
- package/dist/src/index.js +0 -9
- package/dist/src/transform/Errors.js +0 -9
- package/dist/src/transform/MutationContext.js +1 -28
- package/dist/src/transform/TransformContext.js +0 -18
- package/dist/src/transform/comments/comments.js +12 -78
- package/dist/src/transform/comments/prettier/common/util.js +4 -133
- package/dist/src/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/src/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/src/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/src/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/src/transform/comments/prettier/main/comments.js +11 -77
- package/dist/src/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/src/transform/mutations/AddComments.js +0 -9
- package/dist/src/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/src/transform/mutations/InsertStatement.js +4 -16
- package/dist/src/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/src/transform/mutations/RemoveComment.js +0 -10
- package/dist/src/transform/mutations/RemoveNode.js +1 -23
- package/dist/src/transform/mutations/RemoveStatement.js +0 -16
- package/dist/src/transform/mutations/ReplaceNode.js +2 -16
- package/dist/src/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/src/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/src/transform/parse.js +6 -25
- package/dist/src/transform/print.js +9 -35
- package/dist/src/transform/transform.js +0 -9
- package/dist/src/transform/transformAST.js +3 -31
- package/dist/src/traverse/NodeEventGenerator.js +2 -108
- package/dist/src/traverse/SafeEmitter.js +0 -19
- package/dist/src/traverse/esquery.js +0 -18
- package/dist/src/traverse/traverse.js +1 -22
- package/dist/transform/Errors.js +0 -9
- package/dist/transform/MutationContext.js +1 -28
- package/dist/transform/MutationContext.js.flow +3 -3
- package/dist/transform/TransformContext.js +0 -18
- package/dist/transform/TransformContext.js.flow +60 -54
- package/dist/transform/comments/comments.js +12 -78
- package/dist/transform/comments/comments.js.flow +8 -8
- package/dist/transform/comments/prettier/common/util.js +4 -133
- package/dist/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/transform/comments/prettier/main/comments.js +11 -77
- package/dist/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/transform/mutations/AddComments.js +0 -9
- package/dist/transform/mutations/AddComments.js.flow +2 -2
- package/dist/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +4 -16
- package/dist/transform/mutations/InsertStatement.js.flow +7 -6
- package/dist/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/transform/mutations/ModifyNodeInPlace.js.flow +2 -2
- package/dist/transform/mutations/RemoveComment.js +0 -10
- package/dist/transform/mutations/RemoveComment.js.flow +4 -4
- package/dist/transform/mutations/RemoveNode.js +1 -23
- package/dist/transform/mutations/RemoveNode.js.flow +9 -9
- package/dist/transform/mutations/RemoveStatement.js +0 -16
- package/dist/transform/mutations/RemoveStatement.js.flow +3 -3
- package/dist/transform/mutations/ReplaceNode.js +2 -16
- package/dist/transform/mutations/ReplaceNode.js.flow +6 -6
- package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +7 -8
- package/dist/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -6
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +4 -3
- package/dist/transform/parse.js +6 -25
- package/dist/transform/print.js +9 -35
- package/dist/transform/print.js.flow +7 -15
- package/dist/transform/transform.js +0 -9
- package/dist/transform/transformAST.js +3 -31
- package/dist/traverse/NodeEventGenerator.js +2 -108
- package/dist/traverse/NodeEventGenerator.js.flow +13 -9
- package/dist/traverse/SafeEmitter.js +0 -19
- package/dist/traverse/SafeEmitter.js.flow +2 -2
- package/dist/traverse/esquery.js +0 -18
- package/dist/traverse/esquery.js.flow +10 -10
- package/dist/traverse/traverse.js +1 -22
- package/dist/traverse/traverse.js.flow +4 -4
- package/package.json +7 -12
|
@@ -35,7 +35,7 @@ import type {DetachedNode} from '../../detachedNode';
|
|
|
35
35
|
import {astArrayMutationHelpers} from 'hermes-parser';
|
|
36
36
|
import {InvalidRemovalError} from '../Errors';
|
|
37
37
|
|
|
38
|
-
export type RemoveNodeMutation =
|
|
38
|
+
export type RemoveNodeMutation = Readonly<{
|
|
39
39
|
type: 'removeNode',
|
|
40
40
|
node:
|
|
41
41
|
| ClassMember
|
|
@@ -67,34 +67,34 @@ export function createRemoveNodeMutation(
|
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
const VALID_ENUM_MEMBER_PARENTS:
|
|
70
|
+
const VALID_ENUM_MEMBER_PARENTS: ReadonlyArray<string> = [
|
|
71
71
|
'EnumBooleanBody',
|
|
72
72
|
'EnumNumberBody',
|
|
73
73
|
'EnumBigIntBody',
|
|
74
74
|
'EnumStringBody',
|
|
75
75
|
'EnumSymbolBody',
|
|
76
76
|
];
|
|
77
|
-
const VALID_FUNCTION_PARAMETER_PARENTS:
|
|
77
|
+
const VALID_FUNCTION_PARAMETER_PARENTS: ReadonlyArray<string> = [
|
|
78
78
|
'ArrowFunctionExpression',
|
|
79
79
|
'FunctionDeclaration',
|
|
80
80
|
'FunctionExpression',
|
|
81
81
|
];
|
|
82
|
-
const VALID_PROPERTY_PARENTS:
|
|
82
|
+
const VALID_PROPERTY_PARENTS: ReadonlyArray<string> = [
|
|
83
83
|
'ObjectExpression',
|
|
84
84
|
'ObjectPattern',
|
|
85
85
|
];
|
|
86
|
-
const VALID_COMPONENT_TYPE_PARAMETER_PARENTS:
|
|
86
|
+
const VALID_COMPONENT_TYPE_PARAMETER_PARENTS: ReadonlyArray<string> = [
|
|
87
87
|
'DeclareComponent',
|
|
88
88
|
'ComponentTypeAnnotation',
|
|
89
89
|
];
|
|
90
|
-
function getRemovalParent(node: RemoveNodeMutation['node']):
|
|
90
|
+
function getRemovalParent(node: RemoveNodeMutation['node']): Readonly<{
|
|
91
91
|
type: 'array',
|
|
92
92
|
parent: ESNode,
|
|
93
93
|
key: string,
|
|
94
94
|
targetIndex: number,
|
|
95
95
|
}> {
|
|
96
96
|
const key = ((): string => {
|
|
97
|
-
function getErrorMessage(expectedParents:
|
|
97
|
+
function getErrorMessage(expectedParents: ReadonlyArray<string>): string {
|
|
98
98
|
return `Tried to remove ${node.type} from parent of type ${
|
|
99
99
|
node.parent.type
|
|
100
100
|
}.\nHowever ${
|
|
@@ -103,7 +103,7 @@ function getRemovalParent(node: RemoveNodeMutation['node']): $ReadOnly<{
|
|
|
103
103
|
' | ',
|
|
104
104
|
)}.`;
|
|
105
105
|
}
|
|
106
|
-
function assertParent(expectedParents_: string |
|
|
106
|
+
function assertParent(expectedParents_: string | ReadonlyArray<string>) {
|
|
107
107
|
const expectedParents =
|
|
108
108
|
typeof expectedParents_ === 'string'
|
|
109
109
|
? [expectedParents_]
|
|
@@ -304,7 +304,7 @@ export function performRemoveNodeMutation(
|
|
|
304
304
|
mutationContext.markMutation(removalParent.parent, removalParent.key);
|
|
305
305
|
|
|
306
306
|
const parent: interface {
|
|
307
|
-
[string]:
|
|
307
|
+
[string]: ReadonlyArray<DetachedNode<RemoveNodeMutation['node']>>,
|
|
308
308
|
} = removalParent.parent;
|
|
309
309
|
parent[removalParent.key] = astArrayMutationHelpers.removeFromArray(
|
|
310
310
|
parent[removalParent.key],
|
|
@@ -16,15 +16,6 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
16
16
|
|
|
17
17
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
21
|
-
*
|
|
22
|
-
* This source code is licensed under the MIT license found in the
|
|
23
|
-
* LICENSE file in the root directory of this source tree.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @format
|
|
27
|
-
*/
|
|
28
19
|
function createRemoveStatementMutation(node) {
|
|
29
20
|
return {
|
|
30
21
|
type: 'removeStatement',
|
|
@@ -41,13 +32,6 @@ function performRemoveStatementMutation(mutationContext, mutation) {
|
|
|
41
32
|
const parent = removalParent.parent;
|
|
42
33
|
parent[removalParent.key] = _hermesParser.astArrayMutationHelpers.removeFromArray(parent[removalParent.key], removalParent.targetIndex);
|
|
43
34
|
} else {
|
|
44
|
-
// The parent has a 1:1 relationship on this key, so we can't just
|
|
45
|
-
// remove the node. Instead we replace it with an empty block statement.
|
|
46
|
-
// We COULD throw an error here and make the codemodder write a stricter
|
|
47
|
-
// codemod - but we decided to add this bit of magic to make it easier
|
|
48
|
-
// to write codemods.
|
|
49
|
-
// Worst case it creates some dead code that can be easily detected
|
|
50
|
-
// and cleaned up later.
|
|
51
35
|
const blockStatement = t.BlockStatement({
|
|
52
36
|
body: [],
|
|
53
37
|
parent: removalParent.parent
|
|
@@ -16,7 +16,7 @@ import {astArrayMutationHelpers} from 'hermes-parser';
|
|
|
16
16
|
import {getStatementParent} from './utils/getStatementParent';
|
|
17
17
|
import * as t from '../../generated/node-types';
|
|
18
18
|
|
|
19
|
-
export type RemoveStatementMutation =
|
|
19
|
+
export type RemoveStatementMutation = Readonly<{
|
|
20
20
|
type: 'removeStatement',
|
|
21
21
|
node: ModuleDeclaration | Statement,
|
|
22
22
|
}>;
|
|
@@ -41,7 +41,7 @@ export function performRemoveStatementMutation(
|
|
|
41
41
|
|
|
42
42
|
if (removalParent.type === 'array') {
|
|
43
43
|
const parent: interface {
|
|
44
|
-
[string]:
|
|
44
|
+
[string]: ReadonlyArray<DetachedNode<Statement | ModuleDeclaration>>,
|
|
45
45
|
} = removalParent.parent;
|
|
46
46
|
parent[removalParent.key] = astArrayMutationHelpers.removeFromArray(
|
|
47
47
|
parent[removalParent.key],
|
|
@@ -60,7 +60,7 @@ export function performRemoveStatementMutation(
|
|
|
60
60
|
parent: removalParent.parent,
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
-
(removalParent.parent
|
|
63
|
+
(removalParent.parent as interface {[string]: unknown})[removalParent.key] =
|
|
64
64
|
blockStatement;
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -14,15 +14,6 @@ var _Errors = require("../Errors");
|
|
|
14
14
|
|
|
15
15
|
var _detachedNode = require("../../detachedNode");
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
19
|
-
*
|
|
20
|
-
* This source code is licensed under the MIT license found in the
|
|
21
|
-
* LICENSE file in the root directory of this source tree.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @format
|
|
25
|
-
*/
|
|
26
17
|
function createReplaceNodeMutation(target, nodeToReplaceWith, options) {
|
|
27
18
|
var _options$keepComments;
|
|
28
19
|
|
|
@@ -37,10 +28,7 @@ function createReplaceNodeMutation(target, nodeToReplaceWith, options) {
|
|
|
37
28
|
function performReplaceNodeMutation(mutationContext, mutation) {
|
|
38
29
|
const replacementParent = getParentKey(mutation.target);
|
|
39
30
|
mutationContext.markDeletion(mutation.target);
|
|
40
|
-
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
41
|
-
// it does no runtime checks and provides no guarantees about the
|
|
42
|
-
// correctness of the resulting code.
|
|
43
|
-
// TODO: maybe add some runtime checks based on codegenned predicates?
|
|
31
|
+
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
44
32
|
|
|
45
33
|
if (replacementParent.type === 'array') {
|
|
46
34
|
const parent = replacementParent.parent;
|
|
@@ -63,7 +51,6 @@ function getParentKey(target) {
|
|
|
63
51
|
const child = parent[key];
|
|
64
52
|
|
|
65
53
|
if ((0, _hermesParser.isNode)(child)) {
|
|
66
|
-
// $FlowFixMe[invalid-compare]
|
|
67
54
|
if (child === target) {
|
|
68
55
|
return {
|
|
69
56
|
type: 'single',
|
|
@@ -86,8 +73,7 @@ function getParentKey(target) {
|
|
|
86
73
|
}
|
|
87
74
|
}
|
|
88
75
|
}
|
|
89
|
-
}
|
|
90
|
-
|
|
76
|
+
}
|
|
91
77
|
|
|
92
78
|
throw new _Errors.InvalidReplacementError(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
93
79
|
}
|
|
@@ -17,7 +17,7 @@ import {moveCommentsToNewNode} from '../comments/comments';
|
|
|
17
17
|
import {InvalidReplacementError} from '../Errors';
|
|
18
18
|
import {getOriginalNode} from '../../detachedNode';
|
|
19
19
|
|
|
20
|
-
export type ReplaceNodeMutation =
|
|
20
|
+
export type ReplaceNodeMutation = Readonly<{
|
|
21
21
|
type: 'replaceNode',
|
|
22
22
|
target: ESNode,
|
|
23
23
|
nodeToReplaceWith: DetachedNode<ESNode>,
|
|
@@ -27,7 +27,7 @@ export type ReplaceNodeMutation = $ReadOnly<{
|
|
|
27
27
|
export function createReplaceNodeMutation(
|
|
28
28
|
target: ReplaceNodeMutation['target'],
|
|
29
29
|
nodeToReplaceWith: ReplaceNodeMutation['nodeToReplaceWith'],
|
|
30
|
-
options?:
|
|
30
|
+
options?: Readonly<{keepComments?: boolean}>,
|
|
31
31
|
): ReplaceNodeMutation {
|
|
32
32
|
return {
|
|
33
33
|
type: 'replaceNode',
|
|
@@ -53,7 +53,7 @@ export function performReplaceNodeMutation(
|
|
|
53
53
|
|
|
54
54
|
if (replacementParent.type === 'array') {
|
|
55
55
|
const parent: interface {
|
|
56
|
-
[string]:
|
|
56
|
+
[string]: ReadonlyArray<DetachedNode<ESNode>>,
|
|
57
57
|
} = replacementParent.parent;
|
|
58
58
|
parent[replacementParent.key] = astArrayMutationHelpers.replaceInArray(
|
|
59
59
|
parent[replacementParent.key],
|
|
@@ -61,7 +61,7 @@ export function performReplaceNodeMutation(
|
|
|
61
61
|
[mutation.nodeToReplaceWith],
|
|
62
62
|
);
|
|
63
63
|
} else {
|
|
64
|
-
(replacementParent.parent
|
|
64
|
+
(replacementParent.parent as interface {[string]: unknown})[
|
|
65
65
|
replacementParent.key
|
|
66
66
|
] = mutation.nodeToReplaceWith;
|
|
67
67
|
}
|
|
@@ -73,7 +73,7 @@ export function performReplaceNodeMutation(
|
|
|
73
73
|
return replacementParent.parent;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
function getParentKey(target: ESNode):
|
|
76
|
+
function getParentKey(target: ESNode): Readonly<
|
|
77
77
|
| {
|
|
78
78
|
type: 'single',
|
|
79
79
|
parent: ESNode,
|
|
@@ -88,7 +88,7 @@ function getParentKey(target: ESNode): $ReadOnly<
|
|
|
88
88
|
> {
|
|
89
89
|
const parent = target.parent;
|
|
90
90
|
for (const key of getVisitorKeys(parent)) {
|
|
91
|
-
const child = (parent
|
|
91
|
+
const child = (parent as $FlowFixMe)[key];
|
|
92
92
|
if (isNode(child)) {
|
|
93
93
|
// $FlowFixMe[invalid-compare]
|
|
94
94
|
if (child === target) {
|
|
@@ -22,15 +22,6 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
22
22
|
|
|
23
23
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
24
24
|
|
|
25
|
-
/**
|
|
26
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
27
|
-
*
|
|
28
|
-
* This source code is licensed under the MIT license found in the
|
|
29
|
-
* LICENSE file in the root directory of this source tree.
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @format
|
|
33
|
-
*/
|
|
34
25
|
function createReplaceStatementWithManyMutation(target, nodesToReplaceWith, options) {
|
|
35
26
|
var _options$keepComments;
|
|
36
27
|
|
|
@@ -47,7 +38,7 @@ function createReplaceStatementWithManyMutation(target, nodesToReplaceWith, opti
|
|
|
47
38
|
}
|
|
48
39
|
|
|
49
40
|
function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
50
|
-
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
41
|
+
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
51
42
|
|
|
52
43
|
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(replacementParent.parent, mutation.nodesToReplaceWith)) {
|
|
53
44
|
throw new _Errors.InvalidReplacementError(`import/export cannot be replaced into a ${replacementParent.parent.type}.`);
|
|
@@ -57,7 +48,6 @@ function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
|
57
48
|
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
58
49
|
|
|
59
50
|
if (mutation.keepComments) {
|
|
60
|
-
// attach comments to the very first replacement node
|
|
61
51
|
(0, _comments.moveCommentsToNewNode)(mutation.target, mutation.nodesToReplaceWith[0]);
|
|
62
52
|
}
|
|
63
53
|
|
|
@@ -67,9 +57,7 @@ function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
|
67
57
|
return replacementParent.parent;
|
|
68
58
|
}
|
|
69
59
|
|
|
70
|
-
const statementsToReplaceWith =
|
|
71
|
-
mutation.nodesToReplaceWith; // we need to wrap the nodes in a BlockStatement as before there was only 1 node
|
|
72
|
-
|
|
60
|
+
const statementsToReplaceWith = mutation.nodesToReplaceWith;
|
|
73
61
|
const blockStatement = t.BlockStatement({
|
|
74
62
|
body: statementsToReplaceWith,
|
|
75
63
|
parent: replacementParent.parent
|
|
@@ -20,12 +20,11 @@ import {InvalidReplacementError} from '../Errors';
|
|
|
20
20
|
import * as t from '../../generated/node-types';
|
|
21
21
|
|
|
22
22
|
export type ReplaceStatementWithManyMutationNodes =
|
|
23
|
-
|
|
|
24
|
-
|
|
25
|
-
export type ReplaceStatementWithManyMutation = $ReadOnly<{
|
|
23
|
+
ModuleDeclaration | Statement;
|
|
24
|
+
export type ReplaceStatementWithManyMutation = Readonly<{
|
|
26
25
|
type: 'replaceStatementWithMany',
|
|
27
26
|
target: ReplaceStatementWithManyMutationNodes,
|
|
28
|
-
nodesToReplaceWith:
|
|
27
|
+
nodesToReplaceWith: ReadonlyArray<
|
|
29
28
|
DetachedNode<ReplaceStatementWithManyMutationNodes>,
|
|
30
29
|
>,
|
|
31
30
|
keepComments: boolean,
|
|
@@ -34,7 +33,7 @@ export type ReplaceStatementWithManyMutation = $ReadOnly<{
|
|
|
34
33
|
export function createReplaceStatementWithManyMutation(
|
|
35
34
|
target: ReplaceStatementWithManyMutation['target'],
|
|
36
35
|
nodesToReplaceWith: ReplaceStatementWithManyMutation['nodesToReplaceWith'],
|
|
37
|
-
options?:
|
|
36
|
+
options?: Readonly<{keepComments?: boolean}>,
|
|
38
37
|
): ?ReplaceStatementWithManyMutation {
|
|
39
38
|
if (nodesToReplaceWith.length === 0) {
|
|
40
39
|
return null;
|
|
@@ -76,7 +75,7 @@ export function performReplaceStatementWithManyMutation(
|
|
|
76
75
|
|
|
77
76
|
if (replacementParent.type === 'array') {
|
|
78
77
|
const parent: interface {
|
|
79
|
-
[string]:
|
|
78
|
+
[string]: ReadonlyArray<DetachedNode<Statement | ModuleDeclaration>>,
|
|
80
79
|
} = replacementParent.parent;
|
|
81
80
|
parent[replacementParent.key] = astArrayMutationHelpers.replaceInArray(
|
|
82
81
|
parent[replacementParent.key],
|
|
@@ -89,7 +88,7 @@ export function performReplaceStatementWithManyMutation(
|
|
|
89
88
|
|
|
90
89
|
const statementsToReplaceWith =
|
|
91
90
|
// $FlowExpectedError[incompatible-type] -- this is enforced by isValidModuleDeclarationParent above
|
|
92
|
-
|
|
91
|
+
mutation.nodesToReplaceWith as ReadonlyArray<DetachedNode<Statement>>;
|
|
93
92
|
|
|
94
93
|
// we need to wrap the nodes in a BlockStatement as before there was only 1 node
|
|
95
94
|
const blockStatement = t.BlockStatement({
|
|
@@ -97,7 +96,7 @@ export function performReplaceStatementWithManyMutation(
|
|
|
97
96
|
parent: replacementParent.parent,
|
|
98
97
|
});
|
|
99
98
|
|
|
100
|
-
(replacementParent.parent
|
|
99
|
+
(replacementParent.parent as interface {[string]: unknown})[
|
|
101
100
|
replacementParent.key
|
|
102
101
|
] = blockStatement;
|
|
103
102
|
|
|
@@ -7,23 +7,12 @@ exports.getStatementParent = getStatementParent;
|
|
|
7
7
|
|
|
8
8
|
var _Errors = require("../../Errors");
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
-
*
|
|
13
|
-
* This source code is licensed under the MIT license found in the
|
|
14
|
-
* LICENSE file in the root directory of this source tree.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @format
|
|
18
|
-
*/
|
|
19
10
|
function getStatementParent(target) {
|
|
20
11
|
function assertValidStatementLocation(parentWithType, ...invalidKeys) {
|
|
21
12
|
for (const key of invalidKeys) {
|
|
22
|
-
// $FlowExpectedError[prop-missing]
|
|
23
13
|
const value = parentWithType[key];
|
|
24
14
|
|
|
25
|
-
if (
|
|
26
|
-
value === target || Array.isArray(value) && value.includes(target)) {
|
|
15
|
+
if (value === target || Array.isArray(value) && value.includes(target)) {
|
|
27
16
|
throw new _Errors.InvalidStatementError(`Attempted to insert a statement into \`${parentWithType.type}.${key}\`.`);
|
|
28
17
|
}
|
|
29
18
|
}
|
|
@@ -98,9 +87,7 @@ function getStatementParent(target) {
|
|
|
98
87
|
case 'ForInStatement':
|
|
99
88
|
case 'ForOfStatement':
|
|
100
89
|
{
|
|
101
|
-
assertValidStatementLocation(parent,
|
|
102
|
-
'left', // $FlowExpectedError[prop-missing] - flow does not track properties from parent interface
|
|
103
|
-
'right');
|
|
90
|
+
assertValidStatementLocation(parent, 'left', 'right');
|
|
104
91
|
return {
|
|
105
92
|
type: 'single',
|
|
106
93
|
parent,
|
|
@@ -134,10 +121,7 @@ function getStatementParent(target) {
|
|
|
134
121
|
throw new _Errors.InvalidStatementError(`Expected to find a valid statement parent, but found a parent of type "${parent.type}".`);
|
|
135
122
|
})();
|
|
136
123
|
|
|
137
|
-
if (
|
|
138
|
-
result.targetIndex == null && // $FlowExpectedError[prop-missing]
|
|
139
|
-
// $FlowFixMe[invalid-compare]
|
|
140
|
-
result.parent[result.key] !== target) {
|
|
124
|
+
if (result.targetIndex == null && result.parent[result.key] !== target) {
|
|
141
125
|
throw new _Errors.InvalidStatementError(`Expected to find the target "${target.type}" on the "${result.parent.type}.${result.key}", but found a different node. ` + 'This likely means that you attempted to mutate around the target after it was deleted/replaced.');
|
|
142
126
|
}
|
|
143
127
|
|
|
@@ -19,7 +19,7 @@ import type {
|
|
|
19
19
|
|
|
20
20
|
import {InvalidStatementError} from '../../Errors';
|
|
21
21
|
|
|
22
|
-
type StatementParent =
|
|
22
|
+
type StatementParent = Readonly<
|
|
23
23
|
| {
|
|
24
24
|
type: 'single',
|
|
25
25
|
parent: StatementParentSingle,
|
|
@@ -36,10 +36,9 @@ type StatementParent = $ReadOnly<
|
|
|
36
36
|
export function getStatementParent(
|
|
37
37
|
target: ModuleDeclaration | Statement,
|
|
38
38
|
): StatementParent {
|
|
39
|
-
function assertValidStatementLocation<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
): void {
|
|
39
|
+
function assertValidStatementLocation<
|
|
40
|
+
T extends Readonly<interface {type: string}>,
|
|
41
|
+
>(parentWithType: T, ...invalidKeys: ReadonlyArray<keyof T>): void {
|
|
43
42
|
for (const key of invalidKeys) {
|
|
44
43
|
// $FlowExpectedError[prop-missing]
|
|
45
44
|
const value = parentWithType[key];
|
|
@@ -55,7 +54,7 @@ export function getStatementParent(
|
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
|
-
function getAssertedIndex(key: string, arr:
|
|
57
|
+
function getAssertedIndex(key: string, arr: ReadonlyArray<unknown>): number {
|
|
59
58
|
const idx = arr.indexOf(target);
|
|
60
59
|
if (idx === -1) {
|
|
61
60
|
throw new InvalidStatementError(
|
|
@@ -5,34 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.isValidModuleDeclarationParent = isValidModuleDeclarationParent;
|
|
7
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
|
-
* @noformat
|
|
16
|
-
*/
|
|
17
|
-
function isModuleDeclaration(node)
|
|
18
|
-
/*: node is (
|
|
19
|
-
| ImportDeclaration
|
|
20
|
-
| ExportNamedDeclaration
|
|
21
|
-
| ExportDefaultDeclaration
|
|
22
|
-
| ExportAllDeclaration
|
|
23
|
-
) */
|
|
24
|
-
{
|
|
8
|
+
function isModuleDeclaration(node) {
|
|
25
9
|
return node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration' || node.type === 'ExportAllDeclaration';
|
|
26
10
|
}
|
|
27
11
|
|
|
28
12
|
function isValidModuleDeclarationParent(target, nodesToInsertOrReplace) {
|
|
29
|
-
|
|
13
|
+
var _target$parent;
|
|
14
|
+
|
|
15
|
+
if (target.type === 'Program' || target.type === 'BlockStatement' && ((_target$parent = target.parent) == null ? void 0 : _target$parent.type) === 'DeclareModule') {
|
|
30
16
|
return true;
|
|
31
17
|
}
|
|
32
18
|
|
|
33
19
|
for (const node of nodesToInsertOrReplace) {
|
|
34
|
-
if (!isModuleDeclaration(
|
|
35
|
-
node)) {
|
|
20
|
+
if (!isModuleDeclaration(node)) {
|
|
36
21
|
continue;
|
|
37
22
|
}
|
|
38
23
|
|
|
@@ -43,13 +43,14 @@ function isModuleDeclaration(node: ESNode) /*: node is (
|
|
|
43
43
|
|
|
44
44
|
export function isValidModuleDeclarationParent(
|
|
45
45
|
target: ESNode,
|
|
46
|
-
nodesToInsertOrReplace:
|
|
46
|
+
nodesToInsertOrReplace: ReadonlyArray<
|
|
47
47
|
DetachedNode<ModuleDeclaration | Statement>,
|
|
48
48
|
>,
|
|
49
49
|
): boolean {
|
|
50
50
|
if (
|
|
51
51
|
target.type === 'Program' ||
|
|
52
|
-
(target.type === 'BlockStatement' &&
|
|
52
|
+
(target.type === 'BlockStatement' &&
|
|
53
|
+
target.parent?.type === 'DeclareModule')
|
|
53
54
|
) {
|
|
54
55
|
return true;
|
|
55
56
|
}
|
|
@@ -58,7 +59,7 @@ export function isValidModuleDeclarationParent(
|
|
|
58
59
|
if (
|
|
59
60
|
!isModuleDeclaration(
|
|
60
61
|
// $FlowExpectedError[incompatible-type]
|
|
61
|
-
|
|
62
|
+
node as ESNode,
|
|
62
63
|
)
|
|
63
64
|
) {
|
|
64
65
|
continue;
|
package/dist/transform/parse.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
'use strict';
|
|
11
2
|
|
|
12
3
|
Object.defineProperty(exports, "__esModule", {
|
|
@@ -25,27 +16,17 @@ function parse(code) {
|
|
|
25
16
|
} = (0, _hermesEslint.parseForESLint)(code, {
|
|
26
17
|
sourceType: 'module',
|
|
27
18
|
enableExperimentalComponentSyntax: true
|
|
28
|
-
});
|
|
29
|
-
// as it should be maintained at the top of the file as nodes are moved around.
|
|
30
|
-
|
|
19
|
+
});
|
|
31
20
|
let comments = ast.comments;
|
|
32
21
|
|
|
33
22
|
if (ast.docblock != null && ast.docblock.comment === comments[0]) {
|
|
34
|
-
const [first, ...nonDocblockComments] = comments;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
first.placement = 'endOfLine'; // $FlowExpectedError[prop-missing]
|
|
39
|
-
|
|
40
|
-
first.leading = true; // $FlowExpectedError[prop-missing]
|
|
41
|
-
|
|
42
|
-
first.trailing = false; // $FlowExpectedError[prop-missing]
|
|
43
|
-
|
|
23
|
+
const [first, ...nonDocblockComments] = comments;
|
|
24
|
+
first.placement = 'endOfLine';
|
|
25
|
+
first.leading = true;
|
|
26
|
+
first.trailing = false;
|
|
44
27
|
first.printed = false;
|
|
45
28
|
comments = nonDocblockComments;
|
|
46
|
-
}
|
|
47
|
-
// cloned / moved around - comments remain in the correct place with respect to the node
|
|
48
|
-
|
|
29
|
+
}
|
|
49
30
|
|
|
50
31
|
(0, _comments.attachComments)(comments, ast, code);
|
|
51
32
|
return {
|
package/dist/transform/print.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
'use strict';
|
|
11
2
|
|
|
12
3
|
Object.defineProperty(exports, "__esModule", {
|
|
@@ -25,13 +16,11 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
25
16
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
26
17
|
|
|
27
18
|
async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
28
|
-
|
|
29
|
-
const program = ast; // If the AST body is empty, we can skip the cost of prettier by returning a static string of the contents.
|
|
19
|
+
const program = ast;
|
|
30
20
|
|
|
31
21
|
if (program.body.length === 0) {
|
|
32
22
|
var _program$docblock;
|
|
33
23
|
|
|
34
|
-
// If the program had a docblock comment, we need to create the string manually.
|
|
35
24
|
const docblockComment = (_program$docblock = program.docblock) == null ? void 0 : _program$docblock.comment;
|
|
36
25
|
|
|
37
26
|
if (docblockComment != null) {
|
|
@@ -39,31 +28,16 @@ async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
|
39
28
|
}
|
|
40
29
|
|
|
41
30
|
return '';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode); // Fix up the AST to match what prettier expects.
|
|
31
|
+
}
|
|
46
32
|
|
|
33
|
+
const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode);
|
|
47
34
|
(0, _hermesParser.mutateESTreeASTForPrettier)(program, visitorKeys);
|
|
48
|
-
let pluginParserName = 'flow';
|
|
49
|
-
let pluginParser;
|
|
50
|
-
let pluginPrinter;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
// Use prettier-plugin-hermes-parser if we can. It has latest Flow syntax support.
|
|
54
|
-
// $FlowExpectedError[untyped-import]
|
|
55
|
-
const prettierHermesPlugin = await Promise.resolve().then(() => _interopRequireWildcard(require('prettier-plugin-hermes-parser')));
|
|
56
|
-
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
57
|
-
pluginPrinter = prettierHermesPlugin.printers;
|
|
58
|
-
pluginParserName = 'hermes';
|
|
59
|
-
} catch {
|
|
60
|
-
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
61
|
-
|
|
62
|
-
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
63
|
-
}
|
|
64
35
|
|
|
65
|
-
|
|
66
|
-
|
|
36
|
+
const prettierEstreePlugin = require('prettier/plugins/estree');
|
|
37
|
+
|
|
38
|
+
const pluginParserName = visitorKeys != null ? 'typescript' : 'flow';
|
|
39
|
+
const pluginParser = visitorKeys != null ? require('prettier/plugins/typescript').parsers.typescript : require('prettier/plugins/flow').parsers.flow;
|
|
40
|
+
return prettier.format(codeForPrinting, { ...prettierOptions,
|
|
67
41
|
parser: pluginParserName,
|
|
68
42
|
requirePragma: false,
|
|
69
43
|
plugins: [{
|
|
@@ -76,7 +50,7 @@ async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
|
76
50
|
|
|
77
51
|
}
|
|
78
52
|
},
|
|
79
|
-
printers:
|
|
53
|
+
printers: prettierEstreePlugin.printers
|
|
80
54
|
}]
|
|
81
55
|
});
|
|
82
56
|
}
|
|
@@ -47,20 +47,12 @@ export async function print(
|
|
|
47
47
|
// Fix up the AST to match what prettier expects.
|
|
48
48
|
mutateESTreeASTForPrettier(program, visitorKeys);
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const prettierHermesPlugin = await import('prettier-plugin-hermes-parser');
|
|
57
|
-
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
58
|
-
pluginPrinter = prettierHermesPlugin.printers;
|
|
59
|
-
pluginParserName = 'hermes';
|
|
60
|
-
} catch {
|
|
61
|
-
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
62
|
-
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
63
|
-
}
|
|
50
|
+
const prettierEstreePlugin = require('prettier/plugins/estree');
|
|
51
|
+
const pluginParserName = visitorKeys != null ? 'typescript' : 'flow';
|
|
52
|
+
const pluginParser =
|
|
53
|
+
visitorKeys != null
|
|
54
|
+
? require('prettier/plugins/typescript').parsers.typescript
|
|
55
|
+
: require('prettier/plugins/flow').parsers.flow;
|
|
64
56
|
|
|
65
57
|
return prettier.format(
|
|
66
58
|
codeForPrinting,
|
|
@@ -79,7 +71,7 @@ export async function print(
|
|
|
79
71
|
},
|
|
80
72
|
},
|
|
81
73
|
},
|
|
82
|
-
printers:
|
|
74
|
+
printers: prettierEstreePlugin.printers,
|
|
83
75
|
},
|
|
84
76
|
],
|
|
85
77
|
},
|
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
'use strict';
|
|
11
2
|
|
|
12
3
|
Object.defineProperty(exports, "__esModule", {
|