hermes-transform 0.5.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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -0
  3. package/dist/detachedNode.js +128 -0
  4. package/dist/detachedNode.js.flow +113 -0
  5. package/dist/generated/TransformCloneSignatures.js.flow +19 -0
  6. package/dist/generated/TransformReplaceSignatures.js.flow +943 -0
  7. package/dist/generated/node-types.js +2071 -0
  8. package/dist/generated/node-types.js.flow +3149 -0
  9. package/dist/generated/special-case-node-types.js +178 -0
  10. package/dist/generated/special-case-node-types.js.flow +248 -0
  11. package/dist/getVisitorKeys.js +35 -0
  12. package/dist/getVisitorKeys.js.flow +31 -0
  13. package/dist/index.js +41 -0
  14. package/dist/index.js.flow +15 -0
  15. package/dist/transform/Errors.js +151 -0
  16. package/dist/transform/Errors.js.flow +17 -0
  17. package/dist/transform/MutationContext.js +94 -0
  18. package/dist/transform/MutationContext.js.flow +80 -0
  19. package/dist/transform/TransformContext.js +136 -0
  20. package/dist/transform/TransformContext.js.flow +378 -0
  21. package/dist/transform/comments/comments.js +140 -0
  22. package/dist/transform/comments/comments.js.flow +145 -0
  23. package/dist/transform/comments/prettier/README.md +6 -0
  24. package/dist/transform/comments/prettier/common/util.js +365 -0
  25. package/dist/transform/comments/prettier/common/util.js.flow +349 -0
  26. package/dist/transform/comments/prettier/language-js/comments.js +777 -0
  27. package/dist/transform/comments/prettier/language-js/comments.js.flow +950 -0
  28. package/dist/transform/comments/prettier/language-js/loc.js +41 -0
  29. package/dist/transform/comments/prettier/language-js/loc.js.flow +41 -0
  30. package/dist/transform/comments/prettier/language-js/printer-estree.js +31 -0
  31. package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +37 -0
  32. package/dist/transform/comments/prettier/language-js/utils.js +131 -0
  33. package/dist/transform/comments/prettier/language-js/utils.js.flow +135 -0
  34. package/dist/transform/comments/prettier/main/comments.js +513 -0
  35. package/dist/transform/comments/prettier/main/comments.js.flow +436 -0
  36. package/dist/transform/comments/prettier/utils/get-last.js +15 -0
  37. package/dist/transform/comments/prettier/utils/get-last.js.flow +14 -0
  38. package/dist/transform/getTransformedAST.js +159 -0
  39. package/dist/transform/getTransformedAST.js.flow +128 -0
  40. package/dist/transform/mutations/AddLeadingComments.js +47 -0
  41. package/dist/transform/mutations/AddLeadingComments.js.flow +49 -0
  42. package/dist/transform/mutations/AddTrailingComments.js +47 -0
  43. package/dist/transform/mutations/AddTrailingComments.js.flow +49 -0
  44. package/dist/transform/mutations/CloneCommentsTo.js +46 -0
  45. package/dist/transform/mutations/CloneCommentsTo.js.flow +51 -0
  46. package/dist/transform/mutations/InsertStatement.js +92 -0
  47. package/dist/transform/mutations/InsertStatement.js.flow +113 -0
  48. package/dist/transform/mutations/RemoveComment.js +96 -0
  49. package/dist/transform/mutations/RemoveComment.js.flow +80 -0
  50. package/dist/transform/mutations/RemoveStatement.js +61 -0
  51. package/dist/transform/mutations/RemoveStatement.js.flow +68 -0
  52. package/dist/transform/mutations/ReplaceNode.js +96 -0
  53. package/dist/transform/mutations/ReplaceNode.js.flow +113 -0
  54. package/dist/transform/mutations/ReplaceStatementWithMany.js +81 -0
  55. package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +102 -0
  56. package/dist/transform/mutations/utils/arrayUtils.js +41 -0
  57. package/dist/transform/mutations/utils/arrayUtils.js.flow +35 -0
  58. package/dist/transform/mutations/utils/getStatementParent.js +147 -0
  59. package/dist/transform/mutations/utils/getStatementParent.js.flow +143 -0
  60. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +53 -0
  61. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +50 -0
  62. package/dist/transform/transform.js +69 -0
  63. package/dist/transform/transform.js.flow +60 -0
  64. package/dist/traverse/NodeEventGenerator.js +427 -0
  65. package/dist/traverse/NodeEventGenerator.js.flow +406 -0
  66. package/dist/traverse/SafeEmitter.js +70 -0
  67. package/dist/traverse/SafeEmitter.js.flow +46 -0
  68. package/dist/traverse/SimpleTraverser.js +149 -0
  69. package/dist/traverse/SimpleTraverser.js.flow +109 -0
  70. package/dist/traverse/esquery.js +37 -0
  71. package/dist/traverse/esquery.js.flow +173 -0
  72. package/dist/traverse/traverse.js +139 -0
  73. package/dist/traverse/traverse.js.flow +149 -0
  74. package/package.json +22 -0
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.UnexpectedTransformationState = exports.NodeIsMutatedError = exports.NodeIsDeletedError = exports.InvalidStatementError = exports.InvalidReplacementError = exports.InvalidRemovalError = exports.InvalidInsertionError = void 0;
9
+
10
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
+
12
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
+
14
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
15
+
16
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
17
+
18
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
19
+
20
+ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
21
+
22
+ function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
23
+
24
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
25
+
26
+ function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
27
+
28
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
29
+
30
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
31
+
32
+ /**
33
+ * Copyright (c) Facebook, Inc. and its 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
+ var NodeIsDeletedError = /*#__PURE__*/function (_Error) {
42
+ _inherits(NodeIsDeletedError, _Error);
43
+
44
+ var _super = _createSuper(NodeIsDeletedError);
45
+
46
+ function NodeIsDeletedError() {
47
+ _classCallCheck(this, NodeIsDeletedError);
48
+
49
+ return _super.apply(this, arguments);
50
+ }
51
+
52
+ return NodeIsDeletedError;
53
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
54
+
55
+ exports.NodeIsDeletedError = NodeIsDeletedError;
56
+
57
+ var NodeIsMutatedError = /*#__PURE__*/function (_Error2) {
58
+ _inherits(NodeIsMutatedError, _Error2);
59
+
60
+ var _super2 = _createSuper(NodeIsMutatedError);
61
+
62
+ function NodeIsMutatedError() {
63
+ _classCallCheck(this, NodeIsMutatedError);
64
+
65
+ return _super2.apply(this, arguments);
66
+ }
67
+
68
+ return NodeIsMutatedError;
69
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
70
+
71
+ exports.NodeIsMutatedError = NodeIsMutatedError;
72
+
73
+ var InvalidInsertionError = /*#__PURE__*/function (_Error3) {
74
+ _inherits(InvalidInsertionError, _Error3);
75
+
76
+ var _super3 = _createSuper(InvalidInsertionError);
77
+
78
+ function InvalidInsertionError() {
79
+ _classCallCheck(this, InvalidInsertionError);
80
+
81
+ return _super3.apply(this, arguments);
82
+ }
83
+
84
+ return InvalidInsertionError;
85
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
86
+
87
+ exports.InvalidInsertionError = InvalidInsertionError;
88
+
89
+ var UnexpectedTransformationState = /*#__PURE__*/function (_Error4) {
90
+ _inherits(UnexpectedTransformationState, _Error4);
91
+
92
+ var _super4 = _createSuper(UnexpectedTransformationState);
93
+
94
+ function UnexpectedTransformationState() {
95
+ _classCallCheck(this, UnexpectedTransformationState);
96
+
97
+ return _super4.apply(this, arguments);
98
+ }
99
+
100
+ return UnexpectedTransformationState;
101
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
102
+
103
+ exports.UnexpectedTransformationState = UnexpectedTransformationState;
104
+
105
+ var InvalidStatementError = /*#__PURE__*/function (_Error5) {
106
+ _inherits(InvalidStatementError, _Error5);
107
+
108
+ var _super5 = _createSuper(InvalidStatementError);
109
+
110
+ function InvalidStatementError() {
111
+ _classCallCheck(this, InvalidStatementError);
112
+
113
+ return _super5.apply(this, arguments);
114
+ }
115
+
116
+ return InvalidStatementError;
117
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
118
+
119
+ exports.InvalidStatementError = InvalidStatementError;
120
+
121
+ var InvalidRemovalError = /*#__PURE__*/function (_Error6) {
122
+ _inherits(InvalidRemovalError, _Error6);
123
+
124
+ var _super6 = _createSuper(InvalidRemovalError);
125
+
126
+ function InvalidRemovalError() {
127
+ _classCallCheck(this, InvalidRemovalError);
128
+
129
+ return _super6.apply(this, arguments);
130
+ }
131
+
132
+ return InvalidRemovalError;
133
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
134
+
135
+ exports.InvalidRemovalError = InvalidRemovalError;
136
+
137
+ var InvalidReplacementError = /*#__PURE__*/function (_Error7) {
138
+ _inherits(InvalidReplacementError, _Error7);
139
+
140
+ var _super7 = _createSuper(InvalidReplacementError);
141
+
142
+ function InvalidReplacementError() {
143
+ _classCallCheck(this, InvalidReplacementError);
144
+
145
+ return _super7.apply(this, arguments);
146
+ }
147
+
148
+ return InvalidReplacementError;
149
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
150
+
151
+ exports.InvalidReplacementError = InvalidReplacementError;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ export class NodeIsDeletedError extends Error {}
12
+ export class NodeIsMutatedError extends Error {}
13
+ export class InvalidInsertionError extends Error {}
14
+ export class UnexpectedTransformationState extends Error {}
15
+ export class InvalidStatementError extends Error {}
16
+ export class InvalidRemovalError extends Error {}
17
+ export class InvalidReplacementError extends Error {}
@@ -0,0 +1,94 @@
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
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
+
14
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
15
+
16
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
17
+
18
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
+
20
+ var MutationContext = /*#__PURE__*/function () {
21
+ // TODO - do we care about this? Arrays are pretty safe to concurrently mutate
22
+ function MutationContext(code) {
23
+ _classCallCheck(this, MutationContext);
24
+
25
+ _defineProperty(this, "_deletedNodes", new Set());
26
+
27
+ _defineProperty(this, "_mutatedKeys", new Map());
28
+
29
+ _defineProperty(this, "_mutatedArrays", new Map());
30
+
31
+ _defineProperty(this, "code", void 0);
32
+
33
+ this.code = code;
34
+ }
35
+ /**
36
+ * Marks a node and its entire subtree as deleted.
37
+ */
38
+
39
+
40
+ _createClass(MutationContext, [{
41
+ key: "markDeletion",
42
+ value: function markDeletion(node) {
43
+ this._deletedNodes.add(node);
44
+ }
45
+ /**
46
+ * Marks the key of the node as having been mutated.
47
+ */
48
+
49
+ }, {
50
+ key: "markMutation",
51
+ value: function markMutation(node, key) {
52
+ var _map$get$add, _map$get;
53
+
54
+ this.assertNotDeleted(node, "Attempted to mutate a `".concat(node.type, ".").concat(key, "` on a deleted node."));
55
+ this.assertNotMutated(node, key, "Attempted to mutate a `".concat(node.type, ".").concat(key, "` when it has already been mutated."));
56
+ var map = Array.isArray( // $FlowExpectedError[prop-missing]
57
+ node[key]) ? this._mutatedArrays : this._mutatedKeys;
58
+ map.set(node, (_map$get$add = (_map$get = map.get(node)) === null || _map$get === void 0 ? void 0 : _map$get.add(key)) !== null && _map$get$add !== void 0 ? _map$get$add : new Set([key]));
59
+ }
60
+ /**
61
+ * Throws if the node has been deleted
62
+ */
63
+
64
+ }, {
65
+ key: "assertNotDeleted",
66
+ value: function assertNotDeleted(node, message) {
67
+ if (this._deletedNodes.has(node)) {
68
+ throw new _Errors.NodeIsDeletedError(message);
69
+ }
70
+ }
71
+ /**
72
+ * Throws if the key of the node has been mutated
73
+ */
74
+
75
+ }, {
76
+ key: "assertNotMutated",
77
+ value: function assertNotMutated(node, key, message) {
78
+ var _this$_mutatedKeys$ge;
79
+
80
+ if (((_this$_mutatedKeys$ge = this._mutatedKeys.get(node)) === null || _this$_mutatedKeys$ge === void 0 ? void 0 : _this$_mutatedKeys$ge.has(key)) === true) {
81
+ throw new _Errors.NodeIsMutatedError(message);
82
+ }
83
+ }
84
+ }, {
85
+ key: "appendCommentToSource",
86
+ value: function appendCommentToSource(comment) {
87
+ this.code = (0, _comments.appendCommentToSource)(this.code, comment);
88
+ }
89
+ }]);
90
+
91
+ return MutationContext;
92
+ }();
93
+
94
+ exports.MutationContext = MutationContext;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ import type {Comment, ESNode} from 'hermes-estree';
12
+
13
+ import {NodeIsDeletedError, NodeIsMutatedError} from './Errors';
14
+ import {appendCommentToSource} from './comments/comments';
15
+
16
+ export class MutationContext {
17
+ +_deletedNodes: Set<ESNode> = new Set();
18
+ +_mutatedKeys: Map<ESNode, Set<string>> = new Map();
19
+ // TODO - do we care about this? Arrays are pretty safe to concurrently mutate
20
+ +_mutatedArrays: Map<ESNode, Set<string>> = new Map();
21
+
22
+ code: string;
23
+
24
+ constructor(code: string) {
25
+ this.code = code;
26
+ }
27
+
28
+ /**
29
+ * Marks a node and its entire subtree as deleted.
30
+ */
31
+ markDeletion(node: ESNode): void {
32
+ this._deletedNodes.add(node);
33
+ }
34
+
35
+ /**
36
+ * Marks the key of the node as having been mutated.
37
+ */
38
+ markMutation(node: ESNode, key: string): void {
39
+ this.assertNotDeleted(
40
+ node,
41
+ `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`,
42
+ );
43
+ this.assertNotMutated(
44
+ node,
45
+ key,
46
+ `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`,
47
+ );
48
+
49
+ const map = Array.isArray(
50
+ // $FlowExpectedError[prop-missing]
51
+ node[key],
52
+ )
53
+ ? this._mutatedArrays
54
+ : this._mutatedKeys;
55
+
56
+ map.set(node, map.get(node)?.add(key) ?? new Set([key]));
57
+ }
58
+
59
+ /**
60
+ * Throws if the node has been deleted
61
+ */
62
+ assertNotDeleted(node: ESNode, message: string): void {
63
+ if (this._deletedNodes.has(node)) {
64
+ throw new NodeIsDeletedError(message);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Throws if the key of the node has been mutated
70
+ */
71
+ assertNotMutated(node: ESNode, key: string, message: string): void {
72
+ if (this._mutatedKeys.get(node)?.has(key) === true) {
73
+ throw new NodeIsMutatedError(message);
74
+ }
75
+ }
76
+
77
+ appendCommentToSource(comment: Comment): void {
78
+ this.code = appendCommentToSource(this.code, comment);
79
+ }
80
+ }
@@ -0,0 +1,136 @@
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 _comments = require("./comments/comments");
11
+
12
+ var _AddLeadingComments = require("./mutations/AddLeadingComments");
13
+
14
+ var _AddTrailingComments = require("./mutations/AddTrailingComments");
15
+
16
+ var _CloneCommentsTo = require("./mutations/CloneCommentsTo");
17
+
18
+ var _InsertStatement = require("./mutations/InsertStatement");
19
+
20
+ var _RemoveComment = require("./mutations/RemoveComment");
21
+
22
+ var _RemoveStatement = require("./mutations/RemoveStatement");
23
+
24
+ var _ReplaceNode = require("./mutations/ReplaceNode");
25
+
26
+ var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
27
+
28
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
29
+
30
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
31
+
32
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
33
+
34
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
35
+
36
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
37
+
38
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
39
+
40
+ function getTransformContext() {
41
+ /**
42
+ * The mutations in order of collection.
43
+ */
44
+ var mutations = [];
45
+
46
+ function pushMutation(mutation) {
47
+ if (mutation != null) {
48
+ mutations.push(mutation);
49
+ }
50
+ }
51
+
52
+ return {
53
+ mutations: mutations,
54
+
55
+ // $FlowExpectedError[unsafe-getters-setters]
56
+ get astWasMutated() {
57
+ return mutations.length > 0;
58
+ },
59
+
60
+ // $FlowExpectedError[incompatible-exact]
61
+ shallowCloneNode: function (node, newProps) {
62
+ if (node == null) {
63
+ return null;
64
+ }
65
+
66
+ return (0, _detachedNode.shallowCloneNode)(node, newProps);
67
+ },
68
+ shallowCloneArray: function (nodes) {
69
+ if (nodes == null) {
70
+ return null;
71
+ }
72
+
73
+ return nodes.map(function (node) {
74
+ return (0, _detachedNode.shallowCloneNode)(node);
75
+ });
76
+ },
77
+ // $FlowExpectedError[incompatible-exact]
78
+ deepCloneNode: function (node, newProps) {
79
+ if (node == null) {
80
+ return null;
81
+ }
82
+
83
+ return (0, _detachedNode.deepCloneNode)(node, newProps);
84
+ },
85
+ insertAfterStatement: function (target, nodesToInsert) {
86
+ pushMutation((0, _InsertStatement.createInsertStatementMutation)('after', target, toArray(nodesToInsert)));
87
+ },
88
+ insertBeforeStatement: function (target, nodesToInsert) {
89
+ pushMutation((0, _InsertStatement.createInsertStatementMutation)('before', target, toArray(nodesToInsert)));
90
+ },
91
+ replaceNode: function (target, nodeToReplaceWith, options) {
92
+ pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, nodeToReplaceWith, options));
93
+ },
94
+ replaceStatementWithMany: function (target, nodesToReplaceWith, options) {
95
+ pushMutation((0, _ReplaceStatementWithMany.createReplaceStatementWithManyMutation)(target, nodesToReplaceWith, options));
96
+ },
97
+ removeStatement: function (node) {
98
+ pushMutation((0, _RemoveStatement.createRemoveStatementMutation)(node));
99
+ },
100
+ //
101
+ // Comment APIs
102
+ //
103
+ getComments: function (node) {
104
+ return _toConsumableArray((0, _comments.getCommentsForNode)(node));
105
+ },
106
+ getLeadingComments: function (node) {
107
+ return (0, _comments.getCommentsForNode)(node).filter(_comments.isLeadingComment);
108
+ },
109
+ getTrailingComments: function (node) {
110
+ return (0, _comments.getCommentsForNode)(node).filter(_comments.isTrailingComment);
111
+ },
112
+ cloneCommentsTo: function (target, destination) {
113
+ pushMutation((0, _CloneCommentsTo.createCloneCommentsToMutation)(target, destination));
114
+ },
115
+ addLeadingComments: function (node, comments) {
116
+ pushMutation((0, _AddLeadingComments.createAddLeadingCommentsMutation)(node, toArray(comments)));
117
+ },
118
+ addTrailingComments: function (node, comments) {
119
+ pushMutation((0, _AddTrailingComments.createAddTrailingCommentsMutation)(node, toArray(comments)));
120
+ },
121
+ removeComments: function (comments) {
122
+ toArray(comments).forEach(function (comment) {
123
+ pushMutation((0, _RemoveComment.createRemoveCommentMutation)(comment));
124
+ });
125
+ }
126
+ };
127
+ }
128
+
129
+ function toArray(thing) {
130
+ if (Array.isArray(thing)) {
131
+ // $FlowExpectedError[incompatible-return]
132
+ return thing;
133
+ }
134
+
135
+ return [thing];
136
+ }