hermes-transform 0.28.1 → 0.29.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 -0
- package/dist/detachedNode.js.flow +1 -0
- package/dist/generated/node-types.js +1 -12
- package/dist/generated/node-types.js.flow +2 -19
- package/dist/generated/special-case-node-types/misc.js +11 -0
- package/dist/generated/special-case-node-types/misc.js.flow +18 -0
- package/dist/src/detachedNode.js +178 -0
- package/dist/src/generated/node-types.js +2212 -0
- package/dist/src/generated/special-case-node-types/Comment.js +36 -0
- package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +58 -0
- package/dist/src/generated/special-case-node-types/DeclareHook.js +33 -0
- package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +44 -0
- package/dist/src/generated/special-case-node-types/Literal.js +97 -0
- package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +74 -0
- package/dist/src/generated/special-case-node-types/Property.js +136 -0
- package/dist/src/generated/special-case-node-types/misc.js +158 -0
- package/dist/src/generated/special-case-node-types.js +69 -0
- package/dist/src/index.js +53 -0
- package/dist/src/transform/Errors.js +43 -0
- package/dist/src/transform/MutationContext.js +81 -0
- package/dist/src/transform/TransformContext.js +213 -0
- package/dist/src/transform/comments/comments.js +308 -0
- package/dist/src/transform/comments/prettier/common/util.js +364 -0
- package/dist/src/transform/comments/prettier/language-js/comments.js +788 -0
- package/dist/src/transform/comments/prettier/language-js/loc.js +42 -0
- package/dist/src/transform/comments/prettier/language-js/printer-estree.js +32 -0
- package/dist/src/transform/comments/prettier/language-js/utils.js +119 -0
- package/dist/src/transform/comments/prettier/main/comments.js +440 -0
- package/dist/src/transform/comments/prettier/utils/get-last.js +13 -0
- package/dist/src/transform/mutations/AddComments.js +43 -0
- package/dist/src/transform/mutations/CloneCommentsTo.js +31 -0
- package/dist/src/transform/mutations/InsertStatement.js +91 -0
- package/dist/src/transform/mutations/ModifyNodeInPlace.js +59 -0
- package/dist/src/transform/mutations/RemoveComment.js +78 -0
- package/dist/src/transform/mutations/RemoveNode.js +205 -0
- package/dist/src/transform/mutations/RemoveStatement.js +59 -0
- package/dist/src/transform/mutations/ReplaceNode.js +92 -0
- package/dist/src/transform/mutations/ReplaceStatementWithMany.js +79 -0
- package/dist/src/transform/mutations/utils/getStatementParent.js +143 -0
- package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +43 -0
- package/dist/src/transform/parse.js +56 -0
- package/dist/src/transform/print.js +134 -0
- package/dist/src/transform/transform.js +36 -0
- package/dist/src/transform/transformAST.js +134 -0
- package/dist/src/traverse/NodeEventGenerator.js +353 -0
- package/dist/src/traverse/SafeEmitter.js +52 -0
- package/dist/src/traverse/esquery.js +37 -0
- package/dist/src/traverse/traverse.js +150 -0
- package/dist/transform/comments/prettier/language-js/comments.js +29 -1
- package/dist/transform/mutations/utils/getStatementParent.js.flow +7 -5
- package/package.json +5 -5
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _Comment = require("./special-case-node-types/Comment");
|
|
8
|
+
|
|
9
|
+
Object.keys(_Comment).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _Comment[key]) return;
|
|
12
|
+
exports[key] = _Comment[key];
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
var _DeclareExportDeclaration = require("./special-case-node-types/DeclareExportDeclaration");
|
|
16
|
+
|
|
17
|
+
Object.keys(_DeclareExportDeclaration).forEach(function (key) {
|
|
18
|
+
if (key === "default" || key === "__esModule") return;
|
|
19
|
+
if (key in exports && exports[key] === _DeclareExportDeclaration[key]) return;
|
|
20
|
+
exports[key] = _DeclareExportDeclaration[key];
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
var _ExportNamedDeclaration = require("./special-case-node-types/ExportNamedDeclaration");
|
|
24
|
+
|
|
25
|
+
Object.keys(_ExportNamedDeclaration).forEach(function (key) {
|
|
26
|
+
if (key === "default" || key === "__esModule") return;
|
|
27
|
+
if (key in exports && exports[key] === _ExportNamedDeclaration[key]) return;
|
|
28
|
+
exports[key] = _ExportNamedDeclaration[key];
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
var _Literal = require("./special-case-node-types/Literal");
|
|
32
|
+
|
|
33
|
+
Object.keys(_Literal).forEach(function (key) {
|
|
34
|
+
if (key === "default" || key === "__esModule") return;
|
|
35
|
+
if (key in exports && exports[key] === _Literal[key]) return;
|
|
36
|
+
exports[key] = _Literal[key];
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
var _ObjectTypeProperty = require("./special-case-node-types/ObjectTypeProperty");
|
|
40
|
+
|
|
41
|
+
Object.keys(_ObjectTypeProperty).forEach(function (key) {
|
|
42
|
+
if (key === "default" || key === "__esModule") return;
|
|
43
|
+
if (key in exports && exports[key] === _ObjectTypeProperty[key]) return;
|
|
44
|
+
exports[key] = _ObjectTypeProperty[key];
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
var _misc = require("./special-case-node-types/misc");
|
|
48
|
+
|
|
49
|
+
Object.keys(_misc).forEach(function (key) {
|
|
50
|
+
if (key === "default" || key === "__esModule") return;
|
|
51
|
+
if (key in exports && exports[key] === _misc[key]) return;
|
|
52
|
+
exports[key] = _misc[key];
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
var _Property = require("./special-case-node-types/Property");
|
|
56
|
+
|
|
57
|
+
Object.keys(_Property).forEach(function (key) {
|
|
58
|
+
if (key === "default" || key === "__esModule") return;
|
|
59
|
+
if (key in exports && exports[key] === _Property[key]) return;
|
|
60
|
+
exports[key] = _Property[key];
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
var _DeclareHook = require("./special-case-node-types/DeclareHook");
|
|
64
|
+
|
|
65
|
+
Object.keys(_DeclareHook).forEach(function (key) {
|
|
66
|
+
if (key === "default" || key === "__esModule") return;
|
|
67
|
+
if (key in exports && exports[key] === _DeclareHook[key]) return;
|
|
68
|
+
exports[key] = _DeclareHook[key];
|
|
69
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
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.traverseWithContext = exports.traverse = exports.transform = exports.t = exports.print = exports.parse = exports.makeCommentOwnLine = exports.cloneJSDocCommentsToNewNode = exports.asDetachedNode = exports.SimpleTraverser = void 0;
|
|
16
|
+
|
|
17
|
+
var _hermesParser = require("hermes-parser");
|
|
18
|
+
|
|
19
|
+
exports.SimpleTraverser = _hermesParser.SimpleTraverser;
|
|
20
|
+
|
|
21
|
+
var _traverse = require("./traverse/traverse");
|
|
22
|
+
|
|
23
|
+
exports.traverse = _traverse.traverse;
|
|
24
|
+
exports.traverseWithContext = _traverse.traverseWithContext;
|
|
25
|
+
|
|
26
|
+
var _transform = require("./transform/transform");
|
|
27
|
+
|
|
28
|
+
exports.transform = _transform.transform;
|
|
29
|
+
|
|
30
|
+
var _parse = require("./transform/parse");
|
|
31
|
+
|
|
32
|
+
exports.parse = _parse.parse;
|
|
33
|
+
|
|
34
|
+
var _print = require("./transform/print");
|
|
35
|
+
|
|
36
|
+
exports.print = _print.print;
|
|
37
|
+
|
|
38
|
+
var _t = _interopRequireWildcard(require("./generated/node-types"));
|
|
39
|
+
|
|
40
|
+
exports.t = _t;
|
|
41
|
+
|
|
42
|
+
var _detachedNode = require("./detachedNode");
|
|
43
|
+
|
|
44
|
+
exports.asDetachedNode = _detachedNode.asDetachedNode;
|
|
45
|
+
|
|
46
|
+
var _comments = require("./transform/comments/comments");
|
|
47
|
+
|
|
48
|
+
exports.cloneJSDocCommentsToNewNode = _comments.cloneJSDocCommentsToNewNode;
|
|
49
|
+
exports.makeCommentOwnLine = _comments.makeCommentOwnLine;
|
|
50
|
+
|
|
51
|
+
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); }
|
|
52
|
+
|
|
53
|
+
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; }
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.UnexpectedTransformationState = exports.NodeIsMutatedError = exports.NodeIsDeletedError = exports.InvalidStatementError = exports.InvalidReplacementError = exports.InvalidRemovalError = exports.InvalidInsertionError = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
10
|
+
*
|
|
11
|
+
* This source code is licensed under the MIT license found in the
|
|
12
|
+
* LICENSE file in the root directory of this source tree.
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
* @format
|
|
16
|
+
*/
|
|
17
|
+
class NodeIsDeletedError extends Error {}
|
|
18
|
+
|
|
19
|
+
exports.NodeIsDeletedError = NodeIsDeletedError;
|
|
20
|
+
|
|
21
|
+
class NodeIsMutatedError extends Error {}
|
|
22
|
+
|
|
23
|
+
exports.NodeIsMutatedError = NodeIsMutatedError;
|
|
24
|
+
|
|
25
|
+
class InvalidInsertionError extends Error {}
|
|
26
|
+
|
|
27
|
+
exports.InvalidInsertionError = InvalidInsertionError;
|
|
28
|
+
|
|
29
|
+
class UnexpectedTransformationState extends Error {}
|
|
30
|
+
|
|
31
|
+
exports.UnexpectedTransformationState = UnexpectedTransformationState;
|
|
32
|
+
|
|
33
|
+
class InvalidStatementError extends Error {}
|
|
34
|
+
|
|
35
|
+
exports.InvalidStatementError = InvalidStatementError;
|
|
36
|
+
|
|
37
|
+
class InvalidRemovalError extends Error {}
|
|
38
|
+
|
|
39
|
+
exports.InvalidRemovalError = InvalidRemovalError;
|
|
40
|
+
|
|
41
|
+
class InvalidReplacementError extends Error {}
|
|
42
|
+
|
|
43
|
+
exports.InvalidReplacementError = InvalidReplacementError;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MutationContext = void 0;
|
|
7
|
+
|
|
8
|
+
var _Errors = require("./Errors");
|
|
9
|
+
|
|
10
|
+
var _comments = require("./comments/comments");
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* @format
|
|
20
|
+
*/
|
|
21
|
+
class MutationContext {
|
|
22
|
+
// TODO - do we care about this? Arrays are pretty safe to concurrently mutate
|
|
23
|
+
constructor(code) {
|
|
24
|
+
this._deletedNodes = new Set();
|
|
25
|
+
this._mutatedKeys = new Map();
|
|
26
|
+
this._mutatedArrays = new Map();
|
|
27
|
+
this.code = void 0;
|
|
28
|
+
this.code = code;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Marks a node and its entire subtree as deleted.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
markDeletion(node) {
|
|
36
|
+
this._deletedNodes.add(node);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Marks the key of the node as having been mutated.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
markMutation(node, key) {
|
|
44
|
+
var _map$get$add, _map$get;
|
|
45
|
+
|
|
46
|
+
this.assertNotDeleted(node, `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`);
|
|
47
|
+
this.assertNotMutated(node, key, `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`);
|
|
48
|
+
const map = Array.isArray( // $FlowExpectedError[prop-missing]
|
|
49
|
+
node[key]) ? this._mutatedArrays : this._mutatedKeys;
|
|
50
|
+
map.set(node, (_map$get$add = (_map$get = map.get(node)) == null ? void 0 : _map$get.add(key)) != null ? _map$get$add : new Set([key]));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Throws if the node has been deleted
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
assertNotDeleted(node, message) {
|
|
58
|
+
if (this._deletedNodes.has(node)) {
|
|
59
|
+
throw new _Errors.NodeIsDeletedError(message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Throws if the key of the node has been mutated
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
assertNotMutated(node, key, message) {
|
|
68
|
+
var _this$_mutatedKeys$ge;
|
|
69
|
+
|
|
70
|
+
if (((_this$_mutatedKeys$ge = this._mutatedKeys.get(node)) == null ? void 0 : _this$_mutatedKeys$ge.has(key)) === true) {
|
|
71
|
+
throw new _Errors.NodeIsMutatedError(message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
appendCommentToSource(comment, placement) {
|
|
76
|
+
this.code = (0, _comments.appendCommentToSource)(this.code, comment, placement);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.MutationContext = MutationContext;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getTransformContext = getTransformContext;
|
|
7
|
+
|
|
8
|
+
var _detachedNode = require("../detachedNode");
|
|
9
|
+
|
|
10
|
+
var _hermesParser = require("hermes-parser");
|
|
11
|
+
|
|
12
|
+
var _comments = require("./comments/comments");
|
|
13
|
+
|
|
14
|
+
var _AddComments = require("./mutations/AddComments");
|
|
15
|
+
|
|
16
|
+
var _CloneCommentsTo = require("./mutations/CloneCommentsTo");
|
|
17
|
+
|
|
18
|
+
var _InsertStatement = require("./mutations/InsertStatement");
|
|
19
|
+
|
|
20
|
+
var _RemoveComment = require("./mutations/RemoveComment");
|
|
21
|
+
|
|
22
|
+
var _RemoveNode = require("./mutations/RemoveNode");
|
|
23
|
+
|
|
24
|
+
var _RemoveStatement = require("./mutations/RemoveStatement");
|
|
25
|
+
|
|
26
|
+
var _ReplaceNode = require("./mutations/ReplaceNode");
|
|
27
|
+
|
|
28
|
+
var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
|
|
29
|
+
|
|
30
|
+
var _ModifyNodeInPlace = require("./mutations/ModifyNodeInPlace");
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
34
|
+
*
|
|
35
|
+
* This source code is licensed under the MIT license found in the
|
|
36
|
+
* LICENSE file in the root directory of this source tree.
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
* @format
|
|
40
|
+
*/
|
|
41
|
+
function getTransformContext() {
|
|
42
|
+
/**
|
|
43
|
+
* The mutations in order of collection.
|
|
44
|
+
*/
|
|
45
|
+
const mutations = [];
|
|
46
|
+
|
|
47
|
+
function pushMutation(mutation) {
|
|
48
|
+
if (mutation != null) {
|
|
49
|
+
mutations.push(mutation);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const cloneAPIs = {
|
|
54
|
+
// $FlowFixMe[incompatible-exact]
|
|
55
|
+
shallowCloneNode: node => {
|
|
56
|
+
if (node == null) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (0, _detachedNode.shallowCloneNode)(node, {});
|
|
61
|
+
},
|
|
62
|
+
shallowCloneNodeWithOverrides: (node, newProps = {}) => {
|
|
63
|
+
if (node == null) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return (0, _detachedNode.shallowCloneNode)(node, newProps);
|
|
68
|
+
},
|
|
69
|
+
// $FlowFixMe[incompatible-exact]
|
|
70
|
+
shallowCloneArray: nodes => {
|
|
71
|
+
if (nodes == null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return nodes.map(node_ => {
|
|
76
|
+
const node = node_;
|
|
77
|
+
|
|
78
|
+
if (node == null) {
|
|
79
|
+
// $FlowExpectedError[incompatible-return]
|
|
80
|
+
return node;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return (0, _detachedNode.shallowCloneNode)(node, {});
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
// $FlowFixMe[incompatible-exact]
|
|
87
|
+
deepCloneNode: node => {
|
|
88
|
+
if (node == null) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return (0, _detachedNode.deepCloneNode)(node, {});
|
|
93
|
+
},
|
|
94
|
+
deepCloneNodeWithOverrides: (node, newProps = {}) => {
|
|
95
|
+
if (node == null) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return (0, _detachedNode.deepCloneNode)(node, newProps);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const commentAPIs = {
|
|
103
|
+
getComments: node => {
|
|
104
|
+
return [...(0, _comments.getCommentsForNode)(node)];
|
|
105
|
+
},
|
|
106
|
+
getLeadingComments: node => {
|
|
107
|
+
return (0, _comments.getCommentsForNode)(node).filter(_comments.isLeadingComment);
|
|
108
|
+
},
|
|
109
|
+
getTrailingComments: node => {
|
|
110
|
+
return (0, _comments.getCommentsForNode)(node).filter(_comments.isTrailingComment);
|
|
111
|
+
},
|
|
112
|
+
cloneCommentsTo: (target, destination) => {
|
|
113
|
+
pushMutation((0, _CloneCommentsTo.createCloneCommentsToMutation)(target, destination));
|
|
114
|
+
},
|
|
115
|
+
addLeadingComments: (node, comments) => {
|
|
116
|
+
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
|
|
117
|
+
comment,
|
|
118
|
+
placement: _comments.CommentPlacement.LEADING_OWN_LINE
|
|
119
|
+
}))));
|
|
120
|
+
},
|
|
121
|
+
addLeadingInlineComments: (node, comments) => {
|
|
122
|
+
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
|
|
123
|
+
comment,
|
|
124
|
+
placement: _comments.CommentPlacement.LEADING_INLINE
|
|
125
|
+
}))));
|
|
126
|
+
},
|
|
127
|
+
addTrailingComments: (node, comments) => {
|
|
128
|
+
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
|
|
129
|
+
comment,
|
|
130
|
+
placement: _comments.CommentPlacement.TRAILING_OWN_LINE
|
|
131
|
+
}))));
|
|
132
|
+
},
|
|
133
|
+
addTrailingInlineComments: (node, comments) => {
|
|
134
|
+
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
|
|
135
|
+
comment,
|
|
136
|
+
placement: _comments.CommentPlacement.TRAILING_INLINE
|
|
137
|
+
}))));
|
|
138
|
+
},
|
|
139
|
+
removeComments: comments => {
|
|
140
|
+
toArray(comments).forEach(comment => {
|
|
141
|
+
pushMutation((0, _RemoveComment.createRemoveCommentMutation)(comment));
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const insertAPIs = {
|
|
146
|
+
insertAfterStatement: (target, nodesToInsert) => {
|
|
147
|
+
pushMutation((0, _InsertStatement.createInsertStatementMutation)('after', target, toArray(nodesToInsert).map(n => (0, _detachedNode.asDetachedNode)(n, {
|
|
148
|
+
useDeepClone: true
|
|
149
|
+
}))));
|
|
150
|
+
},
|
|
151
|
+
insertBeforeStatement: (target, nodesToInsert) => {
|
|
152
|
+
pushMutation((0, _InsertStatement.createInsertStatementMutation)('before', target, toArray(nodesToInsert).map(n => (0, _detachedNode.asDetachedNode)(n, {
|
|
153
|
+
useDeepClone: true
|
|
154
|
+
}))));
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const removeAPIs = {
|
|
158
|
+
removeNode: node => {
|
|
159
|
+
pushMutation((0, _RemoveNode.createRemoveNodeMutation)(node));
|
|
160
|
+
},
|
|
161
|
+
removeStatement: node => {
|
|
162
|
+
pushMutation((0, _RemoveStatement.createRemoveStatementMutation)(node));
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const replaceAPIs = {
|
|
166
|
+
replaceNode: (target, nodeToReplaceWith, options) => {
|
|
167
|
+
pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, (0, _detachedNode.asDetachedNode)(nodeToReplaceWith), options));
|
|
168
|
+
},
|
|
169
|
+
replaceStatementWithMany: (target, nodesToReplaceWith, options) => {
|
|
170
|
+
pushMutation((0, _ReplaceStatementWithMany.createReplaceStatementWithManyMutation)(target, nodesToReplaceWith.map(n => (0, _detachedNode.asDetachedNode)(n)), options));
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const modifyAPIs = {
|
|
174
|
+
modifyNodeInPlace: (target, newProps) => {
|
|
175
|
+
const detachedProps = {};
|
|
176
|
+
|
|
177
|
+
for (const [key, value] of Object.entries(newProps)) {
|
|
178
|
+
if ((0, _hermesParser.isNode)(value)) {
|
|
179
|
+
// $FlowFixMe[incompatible-type]
|
|
180
|
+
const node = value;
|
|
181
|
+
detachedProps[key] = (0, _detachedNode.asDetachedNode)(node);
|
|
182
|
+
} else {
|
|
183
|
+
detachedProps[key] = value;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
pushMutation((0, _ModifyNodeInPlace.createModifyNodeInPlaceMutation)(target, detachedProps));
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
mutations,
|
|
192
|
+
|
|
193
|
+
// $FlowExpectedError[unsafe-getters-setters]
|
|
194
|
+
get astWasMutated() {
|
|
195
|
+
return mutations.length > 0;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
...cloneAPIs,
|
|
199
|
+
...commentAPIs,
|
|
200
|
+
...insertAPIs,
|
|
201
|
+
...modifyAPIs,
|
|
202
|
+
...removeAPIs,
|
|
203
|
+
...replaceAPIs
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function toArray(thing) {
|
|
208
|
+
if (Array.isArray(thing)) {
|
|
209
|
+
return thing;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return [thing];
|
|
213
|
+
}
|