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
|
@@ -9,86 +9,73 @@ var _Errors = require("./Errors");
|
|
|
9
9
|
|
|
10
10
|
var _comments = require("./comments/comments");
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* @format
|
|
20
|
+
*/
|
|
21
|
+
class MutationContext {
|
|
21
22
|
// TODO - do we care about this? Arrays are pretty safe to concurrently mutate
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
constructor(code) {
|
|
24
|
+
this._deletedNodes = new Set();
|
|
25
|
+
this._mutatedKeys = new Map();
|
|
26
|
+
this._mutatedArrays = new Map();
|
|
27
|
+
this.code = void 0;
|
|
28
|
+
this.code = code;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Marks a node and its entire subtree as deleted.
|
|
32
|
+
*/
|
|
24
33
|
|
|
25
|
-
_defineProperty(this, "_deletedNodes", new Set());
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
markDeletion(node) {
|
|
36
|
+
this._deletedNodes.add(node);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Marks the key of the node as having been mutated.
|
|
40
|
+
*/
|
|
28
41
|
|
|
29
|
-
_defineProperty(this, "_mutatedArrays", new Map());
|
|
30
42
|
|
|
31
|
-
|
|
43
|
+
markMutation(node, key) {
|
|
44
|
+
var _map$get$add, _map$get;
|
|
32
45
|
|
|
33
|
-
this.
|
|
46
|
+
this.assertNotDeleted(node, `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`);
|
|
47
|
+
this.assertNotMutated(node, key, `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`);
|
|
48
|
+
const map = Array.isArray( // $FlowExpectedError[prop-missing]
|
|
49
|
+
node[key]) ? this._mutatedArrays : this._mutatedKeys;
|
|
50
|
+
map.set(node, (_map$get$add = (_map$get = map.get(node)) == null ? void 0 : _map$get.add(key)) != null ? _map$get$add : new Set([key]));
|
|
34
51
|
}
|
|
35
52
|
/**
|
|
36
|
-
*
|
|
53
|
+
* Throws if the node has been deleted
|
|
37
54
|
*/
|
|
38
55
|
|
|
39
56
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
}
|
|
57
|
+
assertNotDeleted(node, message) {
|
|
58
|
+
if (this._deletedNodes.has(node)) {
|
|
59
|
+
throw new _Errors.NodeIsDeletedError(message);
|
|
83
60
|
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Throws if the key of the node has been mutated
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
assertNotMutated(node, key, message) {
|
|
68
|
+
var _this$_mutatedKeys$ge;
|
|
69
|
+
|
|
70
|
+
if (((_this$_mutatedKeys$ge = this._mutatedKeys.get(node)) == null ? void 0 : _this$_mutatedKeys$ge.has(key)) === true) {
|
|
71
|
+
throw new _Errors.NodeIsMutatedError(message);
|
|
88
72
|
}
|
|
89
|
-
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
appendCommentToSource(comment) {
|
|
76
|
+
this.code = (0, _comments.appendCommentToSource)(this.code, comment);
|
|
77
|
+
}
|
|
90
78
|
|
|
91
|
-
|
|
92
|
-
}();
|
|
79
|
+
}
|
|
93
80
|
|
|
94
81
|
exports.MutationContext = MutationContext;
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getTransformContext = getTransformContext;
|
|
7
7
|
|
|
8
|
+
var _codeFrame = require("@babel/code-frame");
|
|
9
|
+
|
|
8
10
|
var _detachedNode = require("../detachedNode");
|
|
9
11
|
|
|
10
12
|
var _comments = require("./comments/comments");
|
|
@@ -19,29 +21,28 @@ var _InsertStatement = require("./mutations/InsertStatement");
|
|
|
19
21
|
|
|
20
22
|
var _RemoveComment = require("./mutations/RemoveComment");
|
|
21
23
|
|
|
24
|
+
var _RemoveNode = require("./mutations/RemoveNode");
|
|
25
|
+
|
|
22
26
|
var _RemoveStatement = require("./mutations/RemoveStatement");
|
|
23
27
|
|
|
24
28
|
var _ReplaceNode = require("./mutations/ReplaceNode");
|
|
25
29
|
|
|
26
30
|
var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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() {
|
|
32
|
+
/**
|
|
33
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
34
|
+
*
|
|
35
|
+
* This source code is licensed under the MIT license found in the
|
|
36
|
+
* LICENSE file in the root directory of this source tree.
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
* @format
|
|
40
|
+
*/
|
|
41
|
+
function getTransformContext(code) {
|
|
41
42
|
/**
|
|
42
43
|
* The mutations in order of collection.
|
|
43
44
|
*/
|
|
44
|
-
|
|
45
|
+
const mutations = [];
|
|
45
46
|
|
|
46
47
|
function pushMutation(mutation) {
|
|
47
48
|
if (mutation != null) {
|
|
@@ -49,81 +50,132 @@ function getTransformContext() {
|
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
const cloneAPIs = {
|
|
54
|
+
// $FlowExpectedError[incompatible-exact]
|
|
55
|
+
shallowCloneNode: node => {
|
|
56
|
+
if (node == null) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
get astWasMutated() {
|
|
57
|
-
return mutations.length > 0;
|
|
60
|
+
return (0, _detachedNode.shallowCloneNode)(node);
|
|
58
61
|
},
|
|
59
|
-
|
|
60
62
|
// $FlowExpectedError[incompatible-exact]
|
|
61
|
-
|
|
63
|
+
shallowCloneNodeWithOverrides: (node, newProps) => {
|
|
62
64
|
if (node == null) {
|
|
63
65
|
return null;
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
return (0, _detachedNode.shallowCloneNode)(node, newProps);
|
|
67
69
|
},
|
|
68
|
-
shallowCloneArray:
|
|
70
|
+
shallowCloneArray: nodes => {
|
|
69
71
|
if (nodes == null) {
|
|
70
72
|
return null;
|
|
71
73
|
}
|
|
72
74
|
|
|
73
|
-
return nodes.map(
|
|
74
|
-
return (0, _detachedNode.shallowCloneNode)(node);
|
|
75
|
-
});
|
|
75
|
+
return nodes.map(node => (0, _detachedNode.shallowCloneNode)(node));
|
|
76
76
|
},
|
|
77
77
|
// $FlowExpectedError[incompatible-exact]
|
|
78
|
-
deepCloneNode:
|
|
78
|
+
deepCloneNode: node => {
|
|
79
79
|
if (node == null) {
|
|
80
80
|
return null;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
return (0, _detachedNode.deepCloneNode)(node
|
|
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)));
|
|
83
|
+
return (0, _detachedNode.deepCloneNode)(node);
|
|
90
84
|
},
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
getComments: function (node) {
|
|
104
|
-
return _toConsumableArray((0, _comments.getCommentsForNode)(node));
|
|
85
|
+
// $FlowExpectedError[incompatible-exact]
|
|
86
|
+
deepCloneNodeWithOverrides: (node, newProps) => {
|
|
87
|
+
if (node == null) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return (0, _detachedNode.deepCloneNode)(node, newProps);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const commentAPIs = {
|
|
95
|
+
getComments: node => {
|
|
96
|
+
return [...(0, _comments.getCommentsForNode)(node)];
|
|
105
97
|
},
|
|
106
|
-
getLeadingComments:
|
|
98
|
+
getLeadingComments: node => {
|
|
107
99
|
return (0, _comments.getCommentsForNode)(node).filter(_comments.isLeadingComment);
|
|
108
100
|
},
|
|
109
|
-
getTrailingComments:
|
|
101
|
+
getTrailingComments: node => {
|
|
110
102
|
return (0, _comments.getCommentsForNode)(node).filter(_comments.isTrailingComment);
|
|
111
103
|
},
|
|
112
|
-
cloneCommentsTo:
|
|
104
|
+
cloneCommentsTo: (target, destination) => {
|
|
113
105
|
pushMutation((0, _CloneCommentsTo.createCloneCommentsToMutation)(target, destination));
|
|
114
106
|
},
|
|
115
|
-
addLeadingComments:
|
|
107
|
+
addLeadingComments: (node, comments) => {
|
|
116
108
|
pushMutation((0, _AddLeadingComments.createAddLeadingCommentsMutation)(node, toArray(comments)));
|
|
117
109
|
},
|
|
118
|
-
addTrailingComments:
|
|
110
|
+
addTrailingComments: (node, comments) => {
|
|
119
111
|
pushMutation((0, _AddTrailingComments.createAddTrailingCommentsMutation)(node, toArray(comments)));
|
|
120
112
|
},
|
|
121
|
-
removeComments:
|
|
122
|
-
toArray(comments).forEach(
|
|
113
|
+
removeComments: comments => {
|
|
114
|
+
toArray(comments).forEach(comment => {
|
|
123
115
|
pushMutation((0, _RemoveComment.createRemoveCommentMutation)(comment));
|
|
124
116
|
});
|
|
125
117
|
}
|
|
126
118
|
};
|
|
119
|
+
const insertAPIs = {
|
|
120
|
+
insertAfterStatement: (target, nodesToInsert) => {
|
|
121
|
+
pushMutation((0, _InsertStatement.createInsertStatementMutation)('after', target, toArray(nodesToInsert)));
|
|
122
|
+
},
|
|
123
|
+
insertBeforeStatement: (target, nodesToInsert) => {
|
|
124
|
+
pushMutation((0, _InsertStatement.createInsertStatementMutation)('before', target, toArray(nodesToInsert)));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const removeAPIs = {
|
|
128
|
+
removeNode: node => {
|
|
129
|
+
pushMutation((0, _RemoveNode.createRemoveNodeMutation)(node));
|
|
130
|
+
},
|
|
131
|
+
removeStatement: node => {
|
|
132
|
+
pushMutation((0, _RemoveStatement.createRemoveStatementMutation)(node));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const replaceAPIs = {
|
|
136
|
+
replaceNode: (target, nodeToReplaceWith, options) => {
|
|
137
|
+
pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, nodeToReplaceWith, options));
|
|
138
|
+
},
|
|
139
|
+
replaceStatementWithMany: (target, nodesToReplaceWith, options) => {
|
|
140
|
+
pushMutation((0, _ReplaceStatementWithMany.createReplaceStatementWithManyMutation)(target, nodesToReplaceWith, options));
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
mutations,
|
|
145
|
+
|
|
146
|
+
// $FlowExpectedError[unsafe-getters-setters]
|
|
147
|
+
get astWasMutated() {
|
|
148
|
+
return mutations.length > 0;
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
buildCodeFrame: (node, message) => {
|
|
152
|
+
// babel uses 1-indexed columns
|
|
153
|
+
const locForBabel = {
|
|
154
|
+
start: {
|
|
155
|
+
line: node.loc.start.line,
|
|
156
|
+
column: node.loc.start.column + 1
|
|
157
|
+
},
|
|
158
|
+
end: {
|
|
159
|
+
line: node.loc.end.line,
|
|
160
|
+
column: node.loc.end.column + 1
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
return (0, _codeFrame.codeFrameColumns)(code, locForBabel, {
|
|
164
|
+
linesAbove: 0,
|
|
165
|
+
linesBelow: 0,
|
|
166
|
+
highlightCode: process.env.NODE_ENV !== 'test',
|
|
167
|
+
message: message
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
buildSimpleCodeFrame: (node, message) => {
|
|
171
|
+
return `[${node.type}:${node.loc.start.line}:${node.loc.start.column}] ${message}`;
|
|
172
|
+
},
|
|
173
|
+
...cloneAPIs,
|
|
174
|
+
...commentAPIs,
|
|
175
|
+
...insertAPIs,
|
|
176
|
+
...removeAPIs,
|
|
177
|
+
...replaceAPIs
|
|
178
|
+
};
|
|
127
179
|
}
|
|
128
180
|
|
|
129
181
|
function toArray(thing) {
|