hermes-transform 0.5.0 → 0.8.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 (76) hide show
  1. package/LICENSE +1 -1
  2. package/dist/detachedNode.js +25 -49
  3. package/dist/detachedNode.js.flow +3 -3
  4. package/dist/generated/TransformCloneSignatures.js.flow +1728 -9
  5. package/dist/generated/TransformReplaceSignatures.js.flow +921 -780
  6. package/dist/generated/node-types.js +1099 -1156
  7. package/dist/generated/node-types.js.flow +77 -125
  8. package/dist/generated/special-case-node-types.js +106 -85
  9. package/dist/generated/special-case-node-types.js.flow +28 -2
  10. package/dist/getVisitorKeys.js +4 -6
  11. package/dist/getVisitorKeys.js.flow +1 -1
  12. package/dist/index.js +13 -18
  13. package/dist/index.js.flow +8 -2
  14. package/dist/transform/Errors.js +8 -116
  15. package/dist/transform/Errors.js.flow +1 -1
  16. package/dist/transform/MutationContext.js +54 -67
  17. package/dist/transform/MutationContext.js.flow +4 -3
  18. package/dist/transform/TransformContext.js +100 -58
  19. package/dist/transform/TransformContext.js.flow +277 -141
  20. package/dist/transform/comments/comments.js +127 -40
  21. package/dist/transform/comments/comments.js.flow +106 -29
  22. package/dist/transform/comments/prettier/common/util.js +46 -47
  23. package/dist/transform/comments/prettier/common/util.js.flow +1 -1
  24. package/dist/transform/comments/prettier/language-js/comments.js +199 -216
  25. package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
  26. package/dist/transform/comments/prettier/language-js/loc.js +10 -9
  27. package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
  28. package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
  29. package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
  30. package/dist/transform/comments/prettier/language-js/utils.js +18 -30
  31. package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
  32. package/dist/transform/comments/prettier/main/comments.js +225 -298
  33. package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
  34. package/dist/transform/comments/prettier/utils/get-last.js +2 -4
  35. package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
  36. package/dist/transform/getTransformedAST.js +81 -104
  37. package/dist/transform/getTransformedAST.js.flow +18 -13
  38. package/dist/transform/mutations/AddComments.js +43 -0
  39. package/dist/transform/mutations/AddComments.js.flow +50 -0
  40. package/dist/transform/mutations/CloneCommentsTo.js +16 -24
  41. package/dist/transform/mutations/CloneCommentsTo.js.flow +2 -3
  42. package/dist/transform/mutations/InsertStatement.js +28 -30
  43. package/dist/transform/mutations/InsertStatement.js.flow +7 -2
  44. package/dist/transform/mutations/RemoveComment.js +25 -43
  45. package/dist/transform/mutations/RemoveComment.js.flow +1 -1
  46. package/dist/transform/mutations/RemoveNode.js +192 -0
  47. package/dist/transform/mutations/RemoveNode.js.flow +290 -0
  48. package/dist/transform/mutations/RemoveStatement.js +7 -9
  49. package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
  50. package/dist/transform/mutations/ReplaceNode.js +36 -44
  51. package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
  52. package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
  53. package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
  54. package/dist/transform/mutations/utils/arrayUtils.js +16 -14
  55. package/dist/transform/mutations/utils/arrayUtils.js.flow +16 -1
  56. package/dist/transform/mutations/utils/getStatementParent.js +23 -28
  57. package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
  58. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
  59. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
  60. package/dist/transform/transform.js +58 -30
  61. package/dist/transform/transform.js.flow +51 -13
  62. package/dist/traverse/NodeEventGenerator.js +91 -165
  63. package/dist/traverse/NodeEventGenerator.js.flow +1 -1
  64. package/dist/traverse/SafeEmitter.js +20 -38
  65. package/dist/traverse/SafeEmitter.js.flow +1 -1
  66. package/dist/traverse/SimpleTraverser.js +67 -98
  67. package/dist/traverse/SimpleTraverser.js.flow +4 -1
  68. package/dist/traverse/esquery.js +6 -6
  69. package/dist/traverse/esquery.js.flow +1 -1
  70. package/dist/traverse/traverse.js +64 -52
  71. package/dist/traverse/traverse.js.flow +63 -10
  72. package/package.json +5 -3
  73. package/dist/transform/mutations/AddLeadingComments.js +0 -47
  74. package/dist/transform/mutations/AddLeadingComments.js.flow +0 -49
  75. package/dist/transform/mutations/AddTrailingComments.js +0 -47
  76. package/dist/transform/mutations/AddTrailingComments.js.flow +0 -49
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -8,8 +8,6 @@
8
8
  */
9
9
  'use strict';
10
10
 
11
- var getLast = function getLast(arr) {
12
- return arr[arr.length - 1];
13
- };
11
+ const getLast = arr => arr[arr.length - 1];
14
12
 
15
13
  module.exports = getLast;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -26,9 +26,7 @@ var _TransformContext = require("./TransformContext");
26
26
 
27
27
  var _comments = require("./comments/comments");
28
28
 
29
- var _AddLeadingComments = require("./mutations/AddLeadingComments");
30
-
31
- var _AddTrailingComments = require("./mutations/AddTrailingComments");
29
+ var _AddComments = require("./mutations/AddComments");
32
30
 
33
31
  var _CloneCommentsTo = require("./mutations/CloneCommentsTo");
34
32
 
@@ -36,123 +34,102 @@ var _InsertStatement = require("./mutations/InsertStatement");
36
34
 
37
35
  var _RemoveComment = require("./mutations/RemoveComment");
38
36
 
37
+ var _RemoveNode = require("./mutations/RemoveNode");
38
+
39
39
  var _RemoveStatement = require("./mutations/RemoveStatement");
40
40
 
41
41
  var _ReplaceNode = require("./mutations/ReplaceNode");
42
42
 
43
43
  var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
44
44
 
45
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
46
-
47
- 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); }
48
-
49
- 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; }
50
-
51
45
  function getTransformedAST(code, visitors) {
52
- var _parseForESLint = (0, _hermesEslint.parseForESLint)(code, {
46
+ const {
47
+ ast,
48
+ scopeManager
49
+ } = (0, _hermesEslint.parseForESLint)(code, {
53
50
  sourceType: 'module'
54
- }),
55
- ast = _parseForESLint.ast,
56
- scopeManager = _parseForESLint.scopeManager; // attach comments before mutation. this will ensure that as nodes are
51
+ }); // attach comments before mutation. this will ensure that as nodes are
57
52
  // cloned / moved around - comments remain in the correct place with respect to the node
58
53
 
59
-
60
54
  (0, _comments.attachComments)(ast.comments, ast, code); // traverse the AST and colllect the mutations
61
55
 
62
- var transformContext = (0, _TransformContext.getTransformContext)();
63
- (0, _traverse.traverseWithContext)(ast, scopeManager, function () {
64
- return transformContext;
65
- }, visitors); // apply the mutations to the AST
66
-
67
- var mutationContext = new _MutationContext.MutationContext(code);
68
- var removeCommentMutations = [];
69
-
70
- var _iterator = _createForOfIteratorHelper(transformContext.mutations),
71
- _step;
72
-
73
- try {
74
- var _loop = function _loop() {
75
- var mutation = _step.value;
76
-
77
- var mutationRoot = function () {
78
- switch (mutation.type) {
79
- case 'insertStatement':
80
- {
81
- return (0, _InsertStatement.performInsertStatementMutation)(mutationContext, mutation);
82
- }
83
-
84
- case 'replaceNode':
85
- {
86
- return (0, _ReplaceNode.performReplaceNodeMutation)(mutationContext, mutation);
87
- }
88
-
89
- case 'replaceStatementWithMany':
90
- {
91
- return (0, _ReplaceStatementWithMany.performReplaceStatementWithManyMutation)(mutationContext, mutation);
92
- }
93
-
94
- case 'removeStatement':
95
- {
96
- return (0, _RemoveStatement.performRemoveStatementMutation)(mutationContext, mutation);
97
- }
98
-
99
- case 'removeComment':
100
- {
101
- // these are handled later
102
- removeCommentMutations.push(mutation);
103
- return null;
104
- }
105
-
106
- case 'addLeadingComments':
107
- {
108
- return (0, _AddLeadingComments.performAddLeadingCommentsMutation)(mutationContext, mutation);
109
- }
110
-
111
- case 'addTrailingComments':
112
- {
113
- return (0, _AddTrailingComments.performAddTrailingCommentsMutation)(mutationContext, mutation);
114
- }
115
-
116
- case 'cloneCommentsTo':
117
- {
118
- return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
119
- }
120
- }
121
- }(); // ensure the subtree's parent pointers are correct
122
- // this is required for two reasons:
123
- // 1) The userland transform is just JS - so there's nothing stopping them
124
- // from doing anything dodgy. The flow types have some enforcement, but
125
- // ofc that can just be ignored with a suppression.
126
- // 2) Shallow clones are a necessary evil in the transform because they
127
- // allow codemods to do simple changes to just one node without the
128
- // weight that comes with deeply cloning the entire AST.
129
- // However we can't update the parent pointers of the cloned node's
130
- // children until the mutation step or else we would be mutating
131
- // real AST nodes and potentially break the traverse step.
132
- //
133
- // Being strict here just helps us ensure we keep everything in sync
134
-
135
-
136
- if (mutationRoot) {
137
- (0, _detachedNode.updateAllParentPointers)(mutationRoot);
56
+ const transformContext = (0, _TransformContext.getTransformContext)();
57
+ (0, _traverse.traverseWithContext)(code, ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST
58
+
59
+ const mutationContext = new _MutationContext.MutationContext(code);
60
+ const removeCommentMutations = [];
61
+
62
+ for (const mutation of transformContext.mutations) {
63
+ const mutationRoot = (() => {
64
+ switch (mutation.type) {
65
+ case 'insertStatement':
66
+ {
67
+ return (0, _InsertStatement.performInsertStatementMutation)(mutationContext, mutation);
68
+ }
69
+
70
+ case 'replaceNode':
71
+ {
72
+ return (0, _ReplaceNode.performReplaceNodeMutation)(mutationContext, mutation);
73
+ }
74
+
75
+ case 'replaceStatementWithMany':
76
+ {
77
+ return (0, _ReplaceStatementWithMany.performReplaceStatementWithManyMutation)(mutationContext, mutation);
78
+ }
79
+
80
+ case 'removeNode':
81
+ {
82
+ return (0, _RemoveNode.performRemoveNodeMutation)(mutationContext, mutation);
83
+ }
84
+
85
+ case 'removeStatement':
86
+ {
87
+ return (0, _RemoveStatement.performRemoveStatementMutation)(mutationContext, mutation);
88
+ }
89
+
90
+ case 'removeComment':
91
+ {
92
+ // these are handled later
93
+ removeCommentMutations.push(mutation);
94
+ return null;
95
+ }
96
+
97
+ case 'addComments':
98
+ {
99
+ return (0, _AddComments.performAddCommentsMutation)(mutationContext, mutation);
100
+ }
101
+
102
+ case 'cloneCommentsTo':
103
+ {
104
+ return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
105
+ }
138
106
  }
139
- };
140
-
141
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
142
- _loop();
143
- } // remove the comments
144
- // this is done at the end because it requires a complete traversal of the AST
145
- // so that we can find relevant node's attachment array
107
+ })(); // ensure the subtree's parent pointers are correct
108
+ // this is required for two reasons:
109
+ // 1) The userland transform is just JS - so there's nothing stopping them
110
+ // from doing anything dodgy. The flow types have some enforcement, but
111
+ // ofc that can just be ignored with a suppression.
112
+ // 2) Shallow clones are a necessary evil in the transform because they
113
+ // allow codemods to do simple changes to just one node without the
114
+ // weight that comes with deeply cloning the entire AST.
115
+ // However we can't update the parent pointers of the cloned node's
116
+ // children until the mutation step or else we would be mutating
117
+ // real AST nodes and potentially break the traverse step.
118
+ //
119
+ // Being strict here just helps us ensure we keep everything in sync
120
+
121
+
122
+ if (mutationRoot) {
123
+ (0, _detachedNode.updateAllParentPointers)(mutationRoot);
124
+ }
125
+ } // remove the comments
126
+ // this is done at the end because it requires a complete traversal of the AST
127
+ // so that we can find relevant node's attachment array
146
128
 
147
- } catch (err) {
148
- _iterator.e(err);
149
- } finally {
150
- _iterator.f();
151
- }
152
129
 
153
130
  (0, _RemoveComment.performRemoveCommentMutations)(ast, removeCommentMutations);
154
131
  return {
155
- ast: ast,
132
+ ast,
156
133
  astWasMutated: transformContext.astWasMutated,
157
134
  mutatedCode: mutationContext.code
158
135
  };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -11,8 +11,7 @@
11
11
  'use strict';
12
12
 
13
13
  import type {ESNode, Program} from 'hermes-estree';
14
- import type {Visitor} from '../traverse/traverse';
15
- import type {TransformContext} from './TransformContext';
14
+ import type {TransformVisitor} from './transform';
16
15
  import type {RemoveCommentMutation} from './mutations/RemoveComment';
17
16
 
18
17
  import {parseForESLint} from 'hermes-eslint';
@@ -21,18 +20,18 @@ import {traverseWithContext} from '../traverse/traverse';
21
20
  import {MutationContext} from './MutationContext';
22
21
  import {getTransformContext} from './TransformContext';
23
22
  import {attachComments} from './comments/comments';
24
- import {performAddLeadingCommentsMutation} from './mutations/AddLeadingComments';
25
- import {performAddTrailingCommentsMutation} from './mutations/AddTrailingComments';
23
+ import {performAddCommentsMutation} from './mutations/AddComments';
26
24
  import {performCloneCommentsToMutation} from './mutations/CloneCommentsTo';
27
25
  import {performInsertStatementMutation} from './mutations/InsertStatement';
28
26
  import {performRemoveCommentMutations} from './mutations/RemoveComment';
27
+ import {performRemoveNodeMutation} from './mutations/RemoveNode';
29
28
  import {performRemoveStatementMutation} from './mutations/RemoveStatement';
30
29
  import {performReplaceNodeMutation} from './mutations/ReplaceNode';
31
30
  import {performReplaceStatementWithManyMutation} from './mutations/ReplaceStatementWithMany';
32
31
 
33
32
  export function getTransformedAST(
34
33
  code: string,
35
- visitors: Visitor<TransformContext>,
34
+ visitors: TransformVisitor,
36
35
  ): {
37
36
  ast: Program,
38
37
  astWasMutated: boolean,
@@ -48,7 +47,13 @@ export function getTransformedAST(
48
47
 
49
48
  // traverse the AST and colllect the mutations
50
49
  const transformContext = getTransformContext();
51
- traverseWithContext(ast, scopeManager, () => transformContext, visitors);
50
+ traverseWithContext(
51
+ code,
52
+ ast,
53
+ scopeManager,
54
+ () => transformContext,
55
+ visitors,
56
+ );
52
57
 
53
58
  // apply the mutations to the AST
54
59
  const mutationContext = new MutationContext(code);
@@ -73,6 +78,10 @@ export function getTransformedAST(
73
78
  );
74
79
  }
75
80
 
81
+ case 'removeNode': {
82
+ return performRemoveNodeMutation(mutationContext, mutation);
83
+ }
84
+
76
85
  case 'removeStatement': {
77
86
  return performRemoveStatementMutation(mutationContext, mutation);
78
87
  }
@@ -83,12 +92,8 @@ export function getTransformedAST(
83
92
  return null;
84
93
  }
85
94
 
86
- case 'addLeadingComments': {
87
- return performAddLeadingCommentsMutation(mutationContext, mutation);
88
- }
89
-
90
- case 'addTrailingComments': {
91
- return performAddTrailingCommentsMutation(mutationContext, mutation);
95
+ case 'addComments': {
96
+ return performAddCommentsMutation(mutationContext, mutation);
92
97
  }
93
98
 
94
99
  case 'cloneCommentsTo': {
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAddCommentsMutation = createAddCommentsMutation;
7
+ exports.performAddCommentsMutation = performAddCommentsMutation;
8
+
9
+ var _comments = require("../comments/comments");
10
+
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ *
18
+ * @format
19
+ */
20
+ function createAddCommentsMutation(node, comments) {
21
+ if (comments.length === 0) {
22
+ return null;
23
+ }
24
+
25
+ return {
26
+ type: 'addComments',
27
+ comments,
28
+ node
29
+ };
30
+ }
31
+
32
+ function performAddCommentsMutation(mutationContext, mutation) {
33
+ for (const {
34
+ comment: originalComment,
35
+ placement
36
+ } of mutation.comments) {
37
+ const comment = (0, _comments.cloneComment)(originalComment);
38
+ mutationContext.appendCommentToSource(comment, placement);
39
+ (0, _comments.addComment)(mutation.node, comment, placement);
40
+ }
41
+
42
+ return null;
43
+ }
@@ -0,0 +1,50 @@
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
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ import type {Comment, ESNode} from 'hermes-estree';
12
+ import type {DetachedNode} from '../../detachedNode';
13
+ import type {MutationContext} from '../MutationContext';
14
+ import type {CommentPlacement} from '../comments/comments';
15
+
16
+ import {addComment, cloneComment} from '../comments/comments';
17
+
18
+ export type AddCommentsMutation = $ReadOnly<{
19
+ type: 'addComments',
20
+ comments: $ReadOnlyArray<{comment: Comment, placement: CommentPlacement}>,
21
+ node: ESNode | DetachedNode<ESNode>,
22
+ }>;
23
+
24
+ export function createAddCommentsMutation(
25
+ node: AddCommentsMutation['node'],
26
+ comments: AddCommentsMutation['comments'],
27
+ ): ?AddCommentsMutation {
28
+ if (comments.length === 0) {
29
+ return null;
30
+ }
31
+
32
+ return {
33
+ type: 'addComments',
34
+ comments,
35
+ node,
36
+ };
37
+ }
38
+
39
+ export function performAddCommentsMutation(
40
+ mutationContext: MutationContext,
41
+ mutation: AddCommentsMutation,
42
+ ): null {
43
+ for (const {comment: originalComment, placement} of mutation.comments) {
44
+ const comment = cloneComment(originalComment);
45
+ mutationContext.appendCommentToSource(comment, placement);
46
+ addComment(mutation.node, comment, placement);
47
+ }
48
+
49
+ return null;
50
+ }
@@ -8,37 +8,29 @@ exports.performCloneCommentsToMutation = performCloneCommentsToMutation;
8
8
 
9
9
  var _comments = require("../comments/comments");
10
10
 
11
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
12
-
13
- 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); }
14
-
15
- 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; }
16
-
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ *
18
+ * @format
19
+ */
17
20
  function createCloneCommentsToMutation(target, destination) {
18
21
  return {
19
22
  type: 'cloneCommentsTo',
20
- target: target,
21
- destination: destination
23
+ target,
24
+ destination
22
25
  };
23
26
  }
24
27
 
25
- function performCloneCommentsToMutation(mutationContext, mutation) {
26
- var newComments = [];
27
-
28
- var _iterator = _createForOfIteratorHelper((0, _comments.getCommentsForNode)(mutation.target)),
29
- _step;
28
+ function performCloneCommentsToMutation(_mutationContext, mutation) {
29
+ const newComments = [];
30
30
 
31
- try {
32
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
33
- var originalComment = _step.value;
34
- var comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
35
- mutationContext.appendCommentToSource(comment);
36
- newComments.push(comment);
37
- }
38
- } catch (err) {
39
- _iterator.e(err);
40
- } finally {
41
- _iterator.f();
31
+ for (const originalComment of (0, _comments.getCommentsForNode)(mutation.target)) {
32
+ const comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
33
+ newComments.push(comment);
42
34
  }
43
35
 
44
36
  (0, _comments.addCommentsToNode)(mutation.destination, newComments);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -36,13 +36,12 @@ export function createCloneCommentsToMutation(
36
36
  }
37
37
 
38
38
  export function performCloneCommentsToMutation(
39
- mutationContext: MutationContext,
39
+ _mutationContext: MutationContext,
40
40
  mutation: CloneCommentsToMutation,
41
41
  ): null {
42
42
  const newComments = [];
43
43
  for (const originalComment of getCommentsForNode(mutation.target)) {
44
44
  const comment = cloneCommentWithMarkers(originalComment);
45
- mutationContext.appendCommentToSource(comment);
46
45
  newComments.push(comment);
47
46
  }
48
47
  addCommentsToNode(mutation.destination, newComments);
@@ -1,7 +1,5 @@
1
1
  "use strict";
2
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
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
@@ -18,22 +16,19 @@ var _Errors = require("../Errors");
18
16
 
19
17
  var t = _interopRequireWildcard(require("../../generated/node-types"));
20
18
 
21
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
22
-
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
-
25
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
26
-
27
- 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."); }
28
-
29
- 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); }
30
-
31
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
32
-
33
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
19
+ 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); }
34
20
 
35
- 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; }
21
+ 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; }
36
22
 
23
+ /**
24
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
25
+ *
26
+ * This source code is licensed under the MIT license found in the
27
+ * LICENSE file in the root directory of this source tree.
28
+ *
29
+ *
30
+ * @format
31
+ */
37
32
  function createInsertStatementMutation(side, target, nodesToInsert) {
38
33
  if (nodesToInsert.length === 0) {
39
34
  return null;
@@ -41,34 +36,35 @@ function createInsertStatementMutation(side, target, nodesToInsert) {
41
36
 
42
37
  return {
43
38
  type: 'insertStatement',
44
- side: side,
45
- target: target,
46
- nodesToInsert: nodesToInsert
39
+ side,
40
+ target,
41
+ nodesToInsert
47
42
  };
48
43
  }
49
44
 
50
45
  function performInsertStatementMutation(mutationContext, mutation) {
51
- var insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are inserting module declarations - they are being inserted in a valid location
46
+ mutationContext.assertNotDeleted(mutation.target, `Attempted to insert ${mutation.side} a deleted ${mutation.target.type} node. This likely means that you attempted to mutate around the target after it was deleted/replaced.`);
47
+ const insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are inserting module declarations - they are being inserted in a valid location
52
48
 
53
49
  if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(insertionParent.parent, mutation.nodesToInsert)) {
54
- throw new _Errors.InvalidInsertionError("import/export cannot be inserted into a ".concat(insertionParent.parent.type, "."));
50
+ throw new _Errors.InvalidInsertionError(`import/export cannot be inserted into a ${insertionParent.parent.type}.`);
55
51
  }
56
52
 
57
53
  mutationContext.markMutation(insertionParent.parent, insertionParent.key);
58
54
 
59
55
  if (insertionParent.type === 'array') {
60
- var _parent = insertionParent.parent;
56
+ const parent = insertionParent.parent;
61
57
 
62
58
  switch (mutation.side) {
63
59
  case 'before':
64
60
  {
65
- _parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(_parent[insertionParent.key], insertionParent.targetIndex - 1, mutation.nodesToInsert);
61
+ parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex, mutation.nodesToInsert);
66
62
  break;
67
63
  }
68
64
 
69
65
  case 'after':
70
66
  {
71
- _parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(_parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
67
+ parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
72
68
  break;
73
69
  }
74
70
  }
@@ -76,15 +72,17 @@ function performInsertStatementMutation(mutationContext, mutation) {
76
72
  return insertionParent.parent;
77
73
  }
78
74
 
79
- var statementsToInsert = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
75
+ const statementsToInsert = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
80
76
  mutation.nodesToInsert;
81
- var parent = insertionParent.parent,
82
- key = insertionParent.key; // $FlowExpectedError[prop-missing]
77
+ const {
78
+ parent,
79
+ key
80
+ } = insertionParent; // $FlowExpectedError[prop-missing]
83
81
 
84
- var statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
82
+ const statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
85
83
 
86
- var blockStatement = t.BlockStatement({
87
- body: mutation.side === 'before' ? [].concat(_toConsumableArray(statementsToInsert), [statementToWrap]) : [statementToWrap].concat(_toConsumableArray(statementsToInsert)),
84
+ const blockStatement = t.BlockStatement({
85
+ body: mutation.side === 'before' ? [...statementsToInsert, statementToWrap] : [statementToWrap, ...statementsToInsert],
88
86
  parent: insertionParent.parent
89
87
  });
90
88
  insertionParent.parent[insertionParent.key] = blockStatement;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -46,6 +46,11 @@ export function performInsertStatementMutation(
46
46
  mutationContext: MutationContext,
47
47
  mutation: InsertStatementMutation,
48
48
  ): ESNode {
49
+ mutationContext.assertNotDeleted(
50
+ mutation.target,
51
+ `Attempted to insert ${mutation.side} a deleted ${mutation.target.type} node. This likely means that you attempted to mutate around the target after it was deleted/replaced.`,
52
+ );
53
+
49
54
  const insertionParent = getStatementParent(mutation.target);
50
55
 
51
56
  // enforce that if we are inserting module declarations - they are being inserted in a valid location
@@ -70,7 +75,7 @@ export function performInsertStatementMutation(
70
75
  case 'before': {
71
76
  parent[insertionParent.key] = insertInArray(
72
77
  parent[insertionParent.key],
73
- insertionParent.targetIndex - 1,
78
+ insertionParent.targetIndex,
74
79
  mutation.nodesToInsert,
75
80
  );
76
81
  break;