hermes-transform 0.5.0 → 0.6.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/LICENSE +1 -1
- package/dist/detachedNode.js +25 -49
- package/dist/detachedNode.js.flow +3 -3
- package/dist/generated/TransformCloneSignatures.js.flow +1761 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +936 -774
- package/dist/generated/node-types.js +1117 -1132
- package/dist/generated/node-types.js.flow +26 -1
- package/dist/generated/special-case-node-types.js +106 -85
- package/dist/generated/special-case-node-types.js.flow +28 -2
- package/dist/getVisitorKeys.js +4 -6
- package/dist/getVisitorKeys.js.flow +1 -1
- package/dist/index.js +13 -18
- package/dist/index.js.flow +7 -2
- package/dist/transform/Errors.js +8 -116
- package/dist/transform/Errors.js.flow +1 -1
- package/dist/transform/MutationContext.js +54 -67
- package/dist/transform/MutationContext.js.flow +1 -1
- package/dist/transform/TransformContext.js +106 -54
- package/dist/transform/TransformContext.js.flow +255 -121
- package/dist/transform/comments/comments.js +25 -25
- package/dist/transform/comments/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/common/util.js +46 -47
- package/dist/transform/comments/prettier/common/util.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/comments.js +199 -216
- package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/loc.js +10 -9
- package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/utils.js +18 -30
- package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
- package/dist/transform/comments/prettier/main/comments.js +225 -298
- package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/utils/get-last.js +2 -4
- package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
- package/dist/transform/getTransformedAST.js +85 -101
- package/dist/transform/getTransformedAST.js.flow +7 -2
- package/dist/transform/mutations/AddLeadingComments.js +15 -22
- package/dist/transform/mutations/AddLeadingComments.js.flow +1 -1
- package/dist/transform/mutations/AddTrailingComments.js +15 -22
- package/dist/transform/mutations/AddTrailingComments.js.flow +1 -1
- package/dist/transform/mutations/CloneCommentsTo.js +16 -23
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +27 -30
- package/dist/transform/mutations/InsertStatement.js.flow +1 -1
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +185 -0
- package/dist/transform/mutations/RemoveNode.js.flow +279 -0
- package/dist/transform/mutations/RemoveStatement.js +7 -9
- package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
- package/dist/transform/mutations/ReplaceNode.js +36 -44
- package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
- package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
- package/dist/transform/mutations/utils/arrayUtils.js +2 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +1 -1
- package/dist/transform/mutations/utils/getStatementParent.js +23 -28
- package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
- package/dist/transform/transform.js +20 -24
- package/dist/transform/transform.js.flow +12 -8
- package/dist/traverse/NodeEventGenerator.js +91 -165
- package/dist/traverse/NodeEventGenerator.js.flow +1 -1
- package/dist/traverse/SafeEmitter.js +20 -38
- package/dist/traverse/SafeEmitter.js.flow +1 -1
- package/dist/traverse/SimpleTraverser.js +67 -98
- package/dist/traverse/SimpleTraverser.js.flow +4 -1
- package/dist/traverse/esquery.js +6 -6
- package/dist/traverse/esquery.js.flow +1 -1
- package/dist/traverse/traverse.js +39 -51
- package/dist/traverse/traverse.js.flow +1 -1
- package/package.json +4 -3
|
@@ -14,25 +14,28 @@ var _Errors = require("../Errors");
|
|
|
14
14
|
|
|
15
15
|
var _getVisitorKeys = require("../../getVisitorKeys");
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
*/
|
|
23
26
|
function createReplaceNodeMutation(target, nodeToReplaceWith, options) {
|
|
24
27
|
var _options$keepComments;
|
|
25
28
|
|
|
26
29
|
return {
|
|
27
30
|
type: 'replaceNode',
|
|
28
|
-
target
|
|
29
|
-
nodeToReplaceWith
|
|
30
|
-
keepComments: (_options$keepComments = options
|
|
31
|
+
target,
|
|
32
|
+
nodeToReplaceWith,
|
|
33
|
+
keepComments: (_options$keepComments = options == null ? void 0 : options.keepComments) != null ? _options$keepComments : false
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
function performReplaceNodeMutation(mutationContext, mutation) {
|
|
35
|
-
|
|
38
|
+
const replacementParent = getParentKey(mutation.target);
|
|
36
39
|
mutationContext.markDeletion(mutation.target);
|
|
37
40
|
mutationContext.markMutation(replacementParent.parent, replacementParent.key); // NOTE: currently this mutation assumes you're doing the right thing.
|
|
38
41
|
// it does no runtime checks and provides no guarantees about the
|
|
@@ -40,7 +43,7 @@ function performReplaceNodeMutation(mutationContext, mutation) {
|
|
|
40
43
|
// TODO: maybe add some runtime checks based on codegenned predicates?
|
|
41
44
|
|
|
42
45
|
if (replacementParent.type === 'array') {
|
|
43
|
-
|
|
46
|
+
const parent = replacementParent.parent;
|
|
44
47
|
parent[replacementParent.key] = (0, _arrayUtils.replaceInArray)(parent[replacementParent.key], replacementParent.targetIndex, [mutation.nodeToReplaceWith]);
|
|
45
48
|
} else {
|
|
46
49
|
replacementParent.parent[replacementParent.key] = mutation.nodeToReplaceWith;
|
|
@@ -54,43 +57,32 @@ function performReplaceNodeMutation(mutationContext, mutation) {
|
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
function getParentKey(target) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
const parent = target.parent;
|
|
61
|
+
|
|
62
|
+
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent)) {
|
|
63
|
+
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
64
|
+
parent[key])) {
|
|
65
|
+
if (parent[key] === target) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'single',
|
|
68
|
+
parent,
|
|
69
|
+
key
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} else if (Array.isArray(parent[key])) {
|
|
73
|
+
for (let i = 0; i < parent[key].length; i += 1) {
|
|
74
|
+
if (parent[key][i] === target) {
|
|
69
75
|
return {
|
|
70
|
-
type: '
|
|
71
|
-
parent
|
|
72
|
-
key
|
|
76
|
+
type: 'array',
|
|
77
|
+
parent,
|
|
78
|
+
key,
|
|
79
|
+
targetIndex: i
|
|
73
80
|
};
|
|
74
81
|
}
|
|
75
|
-
} else if (Array.isArray(parent[key])) {
|
|
76
|
-
for (var i = 0; i < parent[key].length; i += 1) {
|
|
77
|
-
if (parent[key][i] === target) {
|
|
78
|
-
return {
|
|
79
|
-
type: 'array',
|
|
80
|
-
parent: parent,
|
|
81
|
-
key: key,
|
|
82
|
-
targetIndex: i
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
82
|
}
|
|
87
|
-
}
|
|
83
|
+
}
|
|
84
|
+
} // this shouldn't happen ever
|
|
88
85
|
|
|
89
|
-
} catch (err) {
|
|
90
|
-
_iterator.e(err);
|
|
91
|
-
} finally {
|
|
92
|
-
_iterator.f();
|
|
93
|
-
}
|
|
94
86
|
|
|
95
|
-
throw new _Errors.InvalidReplacementError(
|
|
87
|
+
throw new _Errors.InvalidReplacementError(`Expected to find the ${target.type} as a direct child of the ${target.type}.`);
|
|
96
88
|
}
|
|
@@ -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
|
});
|
|
@@ -20,12 +18,12 @@ var _Errors = require("../Errors");
|
|
|
20
18
|
|
|
21
19
|
var t = _interopRequireWildcard(require("../../generated/node-types"));
|
|
22
20
|
|
|
23
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function
|
|
21
|
+
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); }
|
|
24
22
|
|
|
25
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null ||
|
|
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; }
|
|
26
24
|
|
|
27
25
|
/**
|
|
28
|
-
* Copyright (c)
|
|
26
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
29
27
|
*
|
|
30
28
|
* This source code is licensed under the MIT license found in the
|
|
31
29
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -42,17 +40,17 @@ function createReplaceStatementWithManyMutation(target, nodesToReplaceWith, opti
|
|
|
42
40
|
|
|
43
41
|
return {
|
|
44
42
|
type: 'replaceStatementWithMany',
|
|
45
|
-
target
|
|
46
|
-
nodesToReplaceWith
|
|
47
|
-
keepComments: (_options$keepComments = options
|
|
43
|
+
target,
|
|
44
|
+
nodesToReplaceWith,
|
|
45
|
+
keepComments: (_options$keepComments = options == null ? void 0 : options.keepComments) != null ? _options$keepComments : false
|
|
48
46
|
};
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
52
|
-
|
|
50
|
+
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are replacing with module declarations - they are being inserted in a valid location
|
|
53
51
|
|
|
54
52
|
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(replacementParent.parent, mutation.nodesToReplaceWith)) {
|
|
55
|
-
throw new _Errors.InvalidReplacementError(
|
|
53
|
+
throw new _Errors.InvalidReplacementError(`import/export cannot be replaced into a ${replacementParent.parent.type}.`);
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
mutationContext.markDeletion(mutation.target);
|
|
@@ -64,15 +62,15 @@ function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
if (replacementParent.type === 'array') {
|
|
67
|
-
|
|
65
|
+
const parent = replacementParent.parent;
|
|
68
66
|
parent[replacementParent.key] = (0, _arrayUtils.replaceInArray)(parent[replacementParent.key], replacementParent.targetIndex, mutation.nodesToReplaceWith);
|
|
69
67
|
return replacementParent.parent;
|
|
70
68
|
}
|
|
71
69
|
|
|
72
|
-
|
|
70
|
+
const statementsToReplaceWith = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
|
|
73
71
|
mutation.nodesToReplaceWith; // we need to wrap the nodes in a BlockStatement as before there was only 1 node
|
|
74
72
|
|
|
75
|
-
|
|
73
|
+
const blockStatement = t.BlockStatement({
|
|
76
74
|
body: statementsToReplaceWith,
|
|
77
75
|
parent: replacementParent.parent
|
|
78
76
|
});
|
|
@@ -7,20 +7,8 @@ exports.insertInArray = insertInArray;
|
|
|
7
7
|
exports.removeFromArray = removeFromArray;
|
|
8
8
|
exports.replaceInArray = replaceInArray;
|
|
9
9
|
|
|
10
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
11
|
-
|
|
12
|
-
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."); }
|
|
13
|
-
|
|
14
|
-
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); }
|
|
15
|
-
|
|
16
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
17
|
-
|
|
18
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
19
|
-
|
|
20
|
-
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
|
-
|
|
22
10
|
/**
|
|
23
|
-
* Copyright (c)
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
24
12
|
*
|
|
25
13
|
* This source code is licensed under the MIT license found in the
|
|
26
14
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -33,7 +21,7 @@ function insertInArray(array, index, elements) {
|
|
|
33
21
|
}
|
|
34
22
|
|
|
35
23
|
function removeFromArray(array, index) {
|
|
36
|
-
return [
|
|
24
|
+
return [...array.slice(0, index), ...array.slice(index + 1)];
|
|
37
25
|
}
|
|
38
26
|
|
|
39
27
|
function replaceInArray(array, index, elements) {
|
|
@@ -8,7 +8,7 @@ exports.getStatementParent = getStatementParent;
|
|
|
8
8
|
var _Errors = require("../../Errors");
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Copyright (c)
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
12
|
*
|
|
13
13
|
* This source code is licensed under the MIT license found in the
|
|
14
14
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -17,44 +17,39 @@ var _Errors = require("../../Errors");
|
|
|
17
17
|
* @format
|
|
18
18
|
*/
|
|
19
19
|
function getStatementParent(target) {
|
|
20
|
-
function assertValidStatementLocation(parentWithType) {
|
|
21
|
-
for (
|
|
22
|
-
invalidKeys[_key - 1] = arguments[_key];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
for (var _i = 0, _invalidKeys = invalidKeys; _i < _invalidKeys.length; _i++) {
|
|
26
|
-
var key = _invalidKeys[_i];
|
|
20
|
+
function assertValidStatementLocation(parentWithType, ...invalidKeys) {
|
|
21
|
+
for (const key of invalidKeys) {
|
|
27
22
|
// $FlowExpectedError[prop-missing]
|
|
28
|
-
|
|
23
|
+
const value = parentWithType[key];
|
|
29
24
|
|
|
30
25
|
if (value === target || Array.isArray(value) && value.includes(target)) {
|
|
31
|
-
throw new _Errors.InvalidStatementError(
|
|
26
|
+
throw new _Errors.InvalidStatementError(`Attempted to insert a statement into \`${parentWithType.type}.${key}\`.`);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
function getAssertedIndex(key, arr) {
|
|
37
|
-
|
|
32
|
+
const idx = arr.indexOf(target);
|
|
38
33
|
|
|
39
|
-
if (idx
|
|
40
|
-
throw new _Errors.InvalidStatementError(
|
|
34
|
+
if (idx === -1) {
|
|
35
|
+
throw new _Errors.InvalidStatementError(`Could not find target in array of \`${parent.type}.${key}\`.`);
|
|
41
36
|
}
|
|
42
37
|
|
|
43
38
|
return idx;
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
|
|
41
|
+
const parent = target.parent;
|
|
47
42
|
|
|
48
|
-
|
|
43
|
+
const result = (() => {
|
|
49
44
|
switch (parent.type) {
|
|
50
45
|
case 'IfStatement':
|
|
51
46
|
{
|
|
52
47
|
assertValidStatementLocation(parent, 'test');
|
|
53
|
-
|
|
48
|
+
const key = parent.consequent === target ? 'consequent' : 'alternate';
|
|
54
49
|
return {
|
|
55
50
|
type: 'single',
|
|
56
|
-
parent
|
|
57
|
-
key
|
|
51
|
+
parent,
|
|
52
|
+
key
|
|
58
53
|
};
|
|
59
54
|
}
|
|
60
55
|
|
|
@@ -63,7 +58,7 @@ function getStatementParent(target) {
|
|
|
63
58
|
assertValidStatementLocation(parent, 'label');
|
|
64
59
|
return {
|
|
65
60
|
type: 'single',
|
|
66
|
-
parent
|
|
61
|
+
parent,
|
|
67
62
|
key: 'body'
|
|
68
63
|
};
|
|
69
64
|
}
|
|
@@ -73,7 +68,7 @@ function getStatementParent(target) {
|
|
|
73
68
|
assertValidStatementLocation(parent, 'object');
|
|
74
69
|
return {
|
|
75
70
|
type: 'single',
|
|
76
|
-
parent
|
|
71
|
+
parent,
|
|
77
72
|
key: 'body'
|
|
78
73
|
};
|
|
79
74
|
}
|
|
@@ -84,7 +79,7 @@ function getStatementParent(target) {
|
|
|
84
79
|
assertValidStatementLocation(parent, 'test');
|
|
85
80
|
return {
|
|
86
81
|
type: 'single',
|
|
87
|
-
parent
|
|
82
|
+
parent,
|
|
88
83
|
key: 'body'
|
|
89
84
|
};
|
|
90
85
|
}
|
|
@@ -94,7 +89,7 @@ function getStatementParent(target) {
|
|
|
94
89
|
assertValidStatementLocation(parent, 'init', 'test', 'update');
|
|
95
90
|
return {
|
|
96
91
|
type: 'single',
|
|
97
|
-
parent
|
|
92
|
+
parent,
|
|
98
93
|
key: 'body'
|
|
99
94
|
};
|
|
100
95
|
}
|
|
@@ -106,7 +101,7 @@ function getStatementParent(target) {
|
|
|
106
101
|
parent, 'left', 'right');
|
|
107
102
|
return {
|
|
108
103
|
type: 'single',
|
|
109
|
-
parent
|
|
104
|
+
parent,
|
|
110
105
|
key: 'body'
|
|
111
106
|
};
|
|
112
107
|
}
|
|
@@ -116,7 +111,7 @@ function getStatementParent(target) {
|
|
|
116
111
|
assertValidStatementLocation(parent, 'test');
|
|
117
112
|
return {
|
|
118
113
|
type: 'array',
|
|
119
|
-
parent
|
|
114
|
+
parent,
|
|
120
115
|
key: 'consequent',
|
|
121
116
|
targetIndex: getAssertedIndex('consequent', parent.consequent)
|
|
122
117
|
};
|
|
@@ -127,20 +122,20 @@ function getStatementParent(target) {
|
|
|
127
122
|
{
|
|
128
123
|
return {
|
|
129
124
|
type: 'array',
|
|
130
|
-
parent
|
|
125
|
+
parent,
|
|
131
126
|
key: 'body',
|
|
132
127
|
targetIndex: getAssertedIndex('body', parent.body)
|
|
133
128
|
};
|
|
134
129
|
}
|
|
135
130
|
}
|
|
136
131
|
|
|
137
|
-
throw new _Errors.InvalidStatementError(
|
|
138
|
-
}();
|
|
132
|
+
throw new _Errors.InvalidStatementError(`Expected to find a valid statement parent, but found a parent of type "${parent.type}".`);
|
|
133
|
+
})();
|
|
139
134
|
|
|
140
135
|
if ( // array insertions are already validated by the getAssertedIndex function
|
|
141
136
|
result.targetIndex == null && // $FlowExpectedError[prop-missing]
|
|
142
137
|
result.parent[result.key] !== target) {
|
|
143
|
-
throw new _Errors.InvalidStatementError(
|
|
138
|
+
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.');
|
|
144
139
|
}
|
|
145
140
|
|
|
146
141
|
return result;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
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.
|
|
@@ -52,7 +52,7 @@ export function getStatementParent(
|
|
|
52
52
|
}
|
|
53
53
|
function getAssertedIndex(key: string, arr: $ReadOnlyArray<mixed>): number {
|
|
54
54
|
const idx = arr.indexOf(target);
|
|
55
|
-
if (idx
|
|
55
|
+
if (idx === -1) {
|
|
56
56
|
throw new InvalidStatementError(
|
|
57
57
|
`Could not find target in array of \`${parent.type}.${key}\`.`,
|
|
58
58
|
);
|
|
@@ -5,14 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.isValidModuleDeclarationParent = isValidModuleDeclarationParent;
|
|
7
7
|
|
|
8
|
-
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; } } }; }
|
|
9
|
-
|
|
10
|
-
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); }
|
|
11
|
-
|
|
12
|
-
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; }
|
|
13
|
-
|
|
14
8
|
/**
|
|
15
|
-
* Copyright (c)
|
|
9
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
16
10
|
*
|
|
17
11
|
* This source code is licensed under the MIT license found in the
|
|
18
12
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -29,24 +23,13 @@ function isValidModuleDeclarationParent(target, nodesToInsertOrReplace) {
|
|
|
29
23
|
return true;
|
|
30
24
|
}
|
|
31
25
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
37
|
-
var node = _step.value;
|
|
38
|
-
|
|
39
|
-
if (!isModuleDeclaration( // $FlowExpectedError[incompatible-cast]
|
|
40
|
-
node)) {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return false;
|
|
26
|
+
for (const node of nodesToInsertOrReplace) {
|
|
27
|
+
if (!isModuleDeclaration( // $FlowExpectedError[incompatible-cast]
|
|
28
|
+
node)) {
|
|
29
|
+
continue;
|
|
45
30
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
} finally {
|
|
49
|
-
_iterator.f();
|
|
31
|
+
|
|
32
|
+
return false;
|
|
50
33
|
}
|
|
51
34
|
|
|
52
35
|
return true;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
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.
|
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
*/
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
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); }
|
|
13
|
-
|
|
14
12
|
Object.defineProperty(exports, "__esModule", {
|
|
15
13
|
value: true
|
|
16
14
|
});
|
|
@@ -18,27 +16,20 @@ exports.transform = transform;
|
|
|
18
16
|
|
|
19
17
|
var prettier = _interopRequireWildcard(require("prettier"));
|
|
20
18
|
|
|
21
|
-
var
|
|
19
|
+
var _getTransformedAST = require("./getTransformedAST");
|
|
22
20
|
|
|
23
21
|
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
24
22
|
|
|
25
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function
|
|
26
|
-
|
|
27
|
-
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; }
|
|
28
|
-
|
|
29
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
30
|
-
|
|
31
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
32
|
-
|
|
33
|
-
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; }
|
|
23
|
+
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
24
|
|
|
35
|
-
function
|
|
36
|
-
var prettierOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
25
|
+
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; }
|
|
37
26
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
27
|
+
function transform(originalCode, visitors, prettierOptions = {}) {
|
|
28
|
+
const {
|
|
29
|
+
ast,
|
|
30
|
+
astWasMutated,
|
|
31
|
+
mutatedCode
|
|
32
|
+
} = (0, _getTransformedAST.getTransformedAST)(originalCode, visitors);
|
|
42
33
|
|
|
43
34
|
if (!astWasMutated) {
|
|
44
35
|
return originalCode;
|
|
@@ -49,11 +40,13 @@ function transform(originalCode, visitors) {
|
|
|
49
40
|
|
|
50
41
|
|
|
51
42
|
_SimpleTraverser.SimpleTraverser.traverse(ast, {
|
|
52
|
-
enter
|
|
43
|
+
enter(node) {
|
|
53
44
|
// $FlowExpectedError[cannot-write]
|
|
54
45
|
delete node.parent;
|
|
55
46
|
},
|
|
56
|
-
|
|
47
|
+
|
|
48
|
+
leave() {}
|
|
49
|
+
|
|
57
50
|
}); // we need to delete the comments prop or else prettier will do
|
|
58
51
|
// its own attachment pass after the mutation and duplicate the
|
|
59
52
|
// comments on each node, borking the output
|
|
@@ -61,9 +54,12 @@ function transform(originalCode, visitors) {
|
|
|
61
54
|
|
|
62
55
|
|
|
63
56
|
delete ast.comments;
|
|
64
|
-
return prettier.format(mutatedCode,
|
|
65
|
-
|
|
57
|
+
return prettier.format(mutatedCode, // $FlowExpectedError[incompatible-exact] - we don't want to create a dependency on the prettier types
|
|
58
|
+
{ ...prettierOptions,
|
|
59
|
+
|
|
60
|
+
parser() {
|
|
66
61
|
return ast;
|
|
67
62
|
}
|
|
68
|
-
|
|
63
|
+
|
|
64
|
+
});
|
|
69
65
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
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.
|
|
@@ -17,11 +17,11 @@ import * as prettier from 'prettier';
|
|
|
17
17
|
import {getTransformedAST} from './getTransformedAST';
|
|
18
18
|
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
|
19
19
|
|
|
20
|
-
export type
|
|
20
|
+
export type TransformVisitor = Visitor<TransformContext>;
|
|
21
21
|
|
|
22
22
|
export function transform(
|
|
23
23
|
originalCode: string,
|
|
24
|
-
visitors:
|
|
24
|
+
visitors: TransformVisitor,
|
|
25
25
|
prettierOptions: {...} = {},
|
|
26
26
|
): string {
|
|
27
27
|
const {ast, astWasMutated, mutatedCode} = getTransformedAST(
|
|
@@ -51,10 +51,14 @@ export function transform(
|
|
|
51
51
|
// $FlowExpectedError[cannot-write]
|
|
52
52
|
delete ast.comments;
|
|
53
53
|
|
|
54
|
-
return prettier.format(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
return prettier.format(
|
|
55
|
+
mutatedCode,
|
|
56
|
+
// $FlowExpectedError[incompatible-exact] - we don't want to create a dependency on the prettier types
|
|
57
|
+
{
|
|
58
|
+
...prettierOptions,
|
|
59
|
+
parser() {
|
|
60
|
+
return ast;
|
|
61
|
+
},
|
|
58
62
|
},
|
|
59
|
-
|
|
63
|
+
);
|
|
60
64
|
}
|