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.
- package/LICENSE +1 -1
- package/dist/detachedNode.js +25 -49
- package/dist/detachedNode.js.flow +3 -3
- package/dist/generated/TransformCloneSignatures.js.flow +1728 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +921 -780
- package/dist/generated/node-types.js +1099 -1156
- package/dist/generated/node-types.js.flow +77 -125
- 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 +8 -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 +4 -3
- package/dist/transform/TransformContext.js +100 -58
- package/dist/transform/TransformContext.js.flow +277 -141
- package/dist/transform/comments/comments.js +127 -40
- package/dist/transform/comments/comments.js.flow +106 -29
- 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 +81 -104
- package/dist/transform/getTransformedAST.js.flow +18 -13
- package/dist/transform/mutations/AddComments.js +43 -0
- package/dist/transform/mutations/AddComments.js.flow +50 -0
- package/dist/transform/mutations/CloneCommentsTo.js +16 -24
- package/dist/transform/mutations/CloneCommentsTo.js.flow +2 -3
- package/dist/transform/mutations/InsertStatement.js +28 -30
- package/dist/transform/mutations/InsertStatement.js.flow +7 -2
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +192 -0
- package/dist/transform/mutations/RemoveNode.js.flow +290 -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 +16 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +16 -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 +58 -30
- package/dist/transform/transform.js.flow +51 -13
- 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 +64 -52
- package/dist/traverse/traverse.js.flow +63 -10
- package/package.json +5 -3
- package/dist/transform/mutations/AddLeadingComments.js +0 -47
- package/dist/transform/mutations/AddLeadingComments.js.flow +0 -49
- package/dist/transform/mutations/AddTrailingComments.js +0 -47
- package/dist/transform/mutations/AddTrailingComments.js.flow +0 -49
|
@@ -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.
|
|
@@ -16,134 +16,103 @@ exports.SimpleTraverserSkip = exports.SimpleTraverserBreak = exports.SimpleTrave
|
|
|
16
16
|
|
|
17
17
|
var _getVisitorKeys = require("../getVisitorKeys");
|
|
18
18
|
|
|
19
|
-
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; } } }; }
|
|
20
|
-
|
|
21
|
-
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); }
|
|
22
|
-
|
|
23
|
-
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; }
|
|
24
|
-
|
|
25
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
26
|
-
|
|
27
|
-
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); } }
|
|
28
|
-
|
|
29
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
30
|
-
|
|
31
19
|
/**
|
|
32
20
|
* Can be thrown within the traversal "enter" function to prevent the traverser
|
|
33
21
|
* from traversing the node any further, essentially culling the remainder of the
|
|
34
22
|
* AST branch
|
|
35
23
|
*/
|
|
36
|
-
|
|
24
|
+
const SimpleTraverserSkip = new Error();
|
|
37
25
|
/**
|
|
38
26
|
* Can be thrown at any point during the traversal to immediately stop traversal
|
|
39
27
|
* entirely.
|
|
40
28
|
*/
|
|
41
29
|
|
|
42
30
|
exports.SimpleTraverserSkip = SimpleTraverserSkip;
|
|
43
|
-
|
|
31
|
+
const SimpleTraverserBreak = new Error();
|
|
44
32
|
/**
|
|
45
33
|
* A very simple traverser class to traverse AST trees.
|
|
46
34
|
*/
|
|
47
35
|
|
|
48
36
|
exports.SimpleTraverserBreak = SimpleTraverserBreak;
|
|
49
37
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
class SimpleTraverser {
|
|
39
|
+
/**
|
|
40
|
+
* Traverse the given AST tree.
|
|
41
|
+
* @param node The root node to traverse.
|
|
42
|
+
* @param options The option object.
|
|
43
|
+
*/
|
|
44
|
+
traverse(node, options) {
|
|
45
|
+
try {
|
|
46
|
+
this._traverse(node, null, options);
|
|
47
|
+
} catch (ex) {
|
|
48
|
+
if (ex === SimpleTraverserBreak) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw ex;
|
|
53
|
+
}
|
|
53
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Traverse the given AST tree recursively.
|
|
57
|
+
* @param node The current node.
|
|
58
|
+
* @param parent The parent node.
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
54
61
|
|
|
55
|
-
_createClass(SimpleTraverser, [{
|
|
56
|
-
key: "traverse",
|
|
57
|
-
value:
|
|
58
|
-
/**
|
|
59
|
-
* Traverse the given AST tree.
|
|
60
|
-
* @param node The root node to traverse.
|
|
61
|
-
* @param options The option object.
|
|
62
|
-
*/
|
|
63
|
-
function traverse(node, options) {
|
|
64
|
-
try {
|
|
65
|
-
this._traverse(node, null, options);
|
|
66
|
-
} catch (ex) {
|
|
67
|
-
if (ex === SimpleTraverserBreak) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
62
|
|
|
71
|
-
|
|
72
|
-
|
|
63
|
+
_traverse(node, parent, options) {
|
|
64
|
+
if (!(0, _getVisitorKeys.isNode)(node)) {
|
|
65
|
+
return;
|
|
73
66
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Traverse the given AST tree recursively.
|
|
76
|
-
* @param node The current node.
|
|
77
|
-
* @param parent The parent node.
|
|
78
|
-
* @private
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
}, {
|
|
82
|
-
key: "_traverse",
|
|
83
|
-
value: function _traverse(node, parent, options) {
|
|
84
|
-
if (!(0, _getVisitorKeys.isNode)(node)) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
67
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
throw ex;
|
|
68
|
+
try {
|
|
69
|
+
options.enter(node, parent);
|
|
70
|
+
} catch (ex) {
|
|
71
|
+
if (ex === SimpleTraverserSkip) {
|
|
72
|
+
return;
|
|
96
73
|
}
|
|
97
74
|
|
|
98
|
-
|
|
75
|
+
throw ex;
|
|
76
|
+
}
|
|
99
77
|
|
|
100
|
-
|
|
101
|
-
_step;
|
|
78
|
+
const keys = (0, _getVisitorKeys.getVisitorKeys)(node);
|
|
102
79
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// $FlowExpectedError[prop-missing]
|
|
107
|
-
var child = node[key];
|
|
80
|
+
for (const key of keys) {
|
|
81
|
+
// $FlowExpectedError[prop-missing]
|
|
82
|
+
const child = node[key];
|
|
108
83
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
this._traverse(child, node, options);
|
|
115
|
-
}
|
|
84
|
+
if (Array.isArray(child)) {
|
|
85
|
+
for (let j = 0; j < child.length; ++j) {
|
|
86
|
+
this._traverse(child[j], node, options);
|
|
116
87
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
} finally {
|
|
120
|
-
_iterator.f();
|
|
88
|
+
} else {
|
|
89
|
+
this._traverse(child, node, options);
|
|
121
90
|
}
|
|
91
|
+
}
|
|
122
92
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
throw ex;
|
|
93
|
+
try {
|
|
94
|
+
options.leave(node, parent);
|
|
95
|
+
} catch (ex) {
|
|
96
|
+
if (ex === SimpleTraverserSkip) {
|
|
97
|
+
return;
|
|
131
98
|
}
|
|
99
|
+
|
|
100
|
+
throw ex;
|
|
132
101
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}]);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Traverse the given AST tree.
|
|
105
|
+
* @param node The root node to traverse.
|
|
106
|
+
* @param options The option object.
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
static traverse(node, options) {
|
|
111
|
+
new SimpleTraverser().traverse(node, options);
|
|
112
|
+
}
|
|
145
113
|
|
|
146
|
-
|
|
147
|
-
}();
|
|
114
|
+
}
|
|
148
115
|
|
|
149
|
-
exports.SimpleTraverser = SimpleTraverser;
|
|
116
|
+
exports.SimpleTraverser = SimpleTraverser;
|
|
117
|
+
SimpleTraverser.Break = SimpleTraverserBreak;
|
|
118
|
+
SimpleTraverser.Skip = SimpleTraverserSkip;
|
|
@@ -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.
|
|
@@ -38,6 +38,9 @@ export const SimpleTraverserBreak: Error = new Error();
|
|
|
38
38
|
* A very simple traverser class to traverse AST trees.
|
|
39
39
|
*/
|
|
40
40
|
export class SimpleTraverser {
|
|
41
|
+
static Break: Error = SimpleTraverserBreak;
|
|
42
|
+
static Skip: Error = SimpleTraverserSkip;
|
|
43
|
+
|
|
41
44
|
/**
|
|
42
45
|
* Traverse the given AST tree.
|
|
43
46
|
* @param node The root node to traverse.
|
package/dist/traverse/esquery.js
CHANGED
|
@@ -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.
|
|
@@ -16,22 +16,22 @@ exports.query = exports.parse = exports.matches = exports.match = void 0;
|
|
|
16
16
|
|
|
17
17
|
var _esquery = _interopRequireDefault(require("esquery"));
|
|
18
18
|
|
|
19
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
20
|
|
|
21
21
|
// $FlowExpectedError[cannot-resolve-module]
|
|
22
22
|
|
|
23
23
|
/** Parse a selector and return its AST. */
|
|
24
|
-
|
|
24
|
+
const parse = _esquery.default.parse;
|
|
25
25
|
/** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
|
|
26
26
|
|
|
27
27
|
exports.parse = parse;
|
|
28
|
-
|
|
28
|
+
const match = _esquery.default.match;
|
|
29
29
|
/** Given a `node` and its ancestors, determine if `node` is matched by `selector`. */
|
|
30
30
|
|
|
31
31
|
exports.match = match;
|
|
32
|
-
|
|
32
|
+
const matches = _esquery.default.matches;
|
|
33
33
|
/** Query the code AST using the selector string. */
|
|
34
34
|
|
|
35
35
|
exports.matches = matches;
|
|
36
|
-
|
|
36
|
+
const query = _esquery.default.query;
|
|
37
37
|
exports.query = query;
|
|
@@ -6,23 +6,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.traverse = traverse;
|
|
7
7
|
exports.traverseWithContext = traverseWithContext;
|
|
8
8
|
|
|
9
|
+
var _codeFrame = require("@babel/code-frame");
|
|
10
|
+
|
|
9
11
|
var _NodeEventGenerator = require("./NodeEventGenerator");
|
|
10
12
|
|
|
11
13
|
var _SafeEmitter = require("./SafeEmitter");
|
|
12
14
|
|
|
13
15
|
var _SimpleTraverser = require("./SimpleTraverser");
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
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; }
|
|
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
|
+
*/
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Traverse the AST with additional context members provided by `additionalContext`.
|
|
@@ -30,35 +30,36 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
30
30
|
* @param scopeManager the eslint-scope compatible scope manager instance calculated using the ast
|
|
31
31
|
* @param additionalContext a callback function which returns additional context members to add to the context provided to the visitor
|
|
32
32
|
*/
|
|
33
|
-
function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
function traverseWithContext(code, ast, scopeManager, additionalContext, visitor) {
|
|
34
|
+
const emitter = new _SafeEmitter.SafeEmitter();
|
|
35
|
+
const nodeQueue = [];
|
|
36
|
+
let currentNode = ast; // set parent pointers and build up the traversal queue
|
|
37
37
|
|
|
38
38
|
_SimpleTraverser.SimpleTraverser.traverse(ast, {
|
|
39
|
-
enter
|
|
39
|
+
enter(node, parent) {
|
|
40
40
|
// $FlowExpectedError[cannot-write] - hermes doesn't set this
|
|
41
41
|
node.parent = parent;
|
|
42
42
|
nodeQueue.push({
|
|
43
43
|
isEntering: true,
|
|
44
|
-
node
|
|
44
|
+
node
|
|
45
45
|
});
|
|
46
46
|
},
|
|
47
|
-
|
|
47
|
+
|
|
48
|
+
leave(node) {
|
|
48
49
|
nodeQueue.push({
|
|
49
50
|
isEntering: false,
|
|
50
|
-
node
|
|
51
|
+
node
|
|
51
52
|
});
|
|
52
53
|
}
|
|
54
|
+
|
|
53
55
|
});
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
var givenNode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : currentNode;
|
|
57
|
+
const getScope = (givenNode = currentNode) => {
|
|
57
58
|
// On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
|
|
58
|
-
|
|
59
|
+
const inner = givenNode.type !== 'Program';
|
|
59
60
|
|
|
60
|
-
for (
|
|
61
|
-
|
|
61
|
+
for (let node = givenNode; node; node = node.parent) {
|
|
62
|
+
const scope = scopeManager.acquire(node, inner);
|
|
62
63
|
|
|
63
64
|
if (scope) {
|
|
64
65
|
if (scope.type === 'function-expression-name') {
|
|
@@ -72,29 +73,38 @@ function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
|
|
|
72
73
|
return scopeManager.scopes[0];
|
|
73
74
|
};
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
const traversalContextBase = Object.freeze({
|
|
77
|
+
buildCodeFrame: (node, message) => {
|
|
78
|
+
// babel uses 1-indexed columns
|
|
79
|
+
const locForBabel = {
|
|
80
|
+
start: {
|
|
81
|
+
line: node.loc.start.line,
|
|
82
|
+
column: node.loc.start.column + 1
|
|
83
|
+
},
|
|
84
|
+
end: {
|
|
85
|
+
line: node.loc.end.line,
|
|
86
|
+
column: node.loc.end.column + 1
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return (0, _codeFrame.codeFrameColumns)(code, locForBabel, {
|
|
90
|
+
linesAbove: 0,
|
|
91
|
+
linesBelow: 0,
|
|
92
|
+
highlightCode: process.env.NODE_ENV !== 'test',
|
|
93
|
+
message: message
|
|
94
|
+
});
|
|
78
95
|
},
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
buildSimpleCodeFrame: (node, message) => {
|
|
97
|
+
return `[${node.type}:${node.loc.start.line}:${node.loc.start.column}] ${message}`;
|
|
98
|
+
},
|
|
99
|
+
getDeclaredVariables: node => scopeManager.getDeclaredVariables(node),
|
|
100
|
+
getBinding: name => {
|
|
101
|
+
let currentScope = getScope();
|
|
81
102
|
|
|
82
103
|
while (currentScope != null) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
88
|
-
var variable = _step.value;
|
|
89
|
-
|
|
90
|
-
if (variable.defs.length && variable.name === name) {
|
|
91
|
-
return variable;
|
|
92
|
-
}
|
|
104
|
+
for (const variable of currentScope.variables) {
|
|
105
|
+
if (variable.defs.length && variable.name === name) {
|
|
106
|
+
return variable;
|
|
93
107
|
}
|
|
94
|
-
} catch (err) {
|
|
95
|
-
_iterator.e(err);
|
|
96
|
-
} finally {
|
|
97
|
-
_iterator.f();
|
|
98
108
|
}
|
|
99
109
|
|
|
100
110
|
currentScope = currentScope.upper;
|
|
@@ -102,23 +112,25 @@ function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
|
|
|
102
112
|
|
|
103
113
|
return null;
|
|
104
114
|
},
|
|
105
|
-
getScope
|
|
115
|
+
getScope
|
|
116
|
+
});
|
|
117
|
+
const traversalContext = Object.freeze({ ...traversalContextBase,
|
|
118
|
+
...additionalContext(traversalContextBase)
|
|
106
119
|
});
|
|
107
|
-
|
|
108
|
-
var selectors = visitor(traversalContext); // add all the selectors from the visitor as listeners
|
|
120
|
+
const selectors = visitor(traversalContext); // add all the selectors from the visitor as listeners
|
|
109
121
|
|
|
110
|
-
Object.keys(selectors).forEach(
|
|
122
|
+
Object.keys(selectors).forEach(selector => {
|
|
111
123
|
// flow doesn't want us to be general here - but it's safe
|
|
112
124
|
// $FlowExpectedError[incompatible-type]
|
|
113
125
|
// $FlowExpectedError[prop-missing]
|
|
114
|
-
|
|
126
|
+
const listener = selectors[selector];
|
|
115
127
|
|
|
116
128
|
if (listener) {
|
|
117
129
|
emitter.on(selector, listener);
|
|
118
130
|
}
|
|
119
131
|
});
|
|
120
|
-
|
|
121
|
-
nodeQueue.forEach(
|
|
132
|
+
const eventGenerator = new _NodeEventGenerator.NodeEventGenerator(emitter);
|
|
133
|
+
nodeQueue.forEach(traversalInfo => {
|
|
122
134
|
currentNode = traversalInfo.node;
|
|
123
135
|
|
|
124
136
|
try {
|
|
@@ -134,6 +146,6 @@ function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
|
|
|
134
146
|
});
|
|
135
147
|
}
|
|
136
148
|
|
|
137
|
-
function traverse(ast, scopeManager, visitor) {
|
|
138
|
-
traverseWithContext(ast, scopeManager,
|
|
149
|
+
function traverse(code, ast, scopeManager, visitor) {
|
|
150
|
+
traverseWithContext(code, ast, scopeManager, () => {}, visitor);
|
|
139
151
|
}
|
|
@@ -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.
|
|
@@ -12,11 +12,12 @@ import type {ESNode, ESQueryNodeSelectors, Program} from 'hermes-estree';
|
|
|
12
12
|
import type {ScopeManager, Scope, Variable} from 'hermes-eslint';
|
|
13
13
|
import type {EmitterListener} from './SafeEmitter';
|
|
14
14
|
|
|
15
|
+
import {codeFrameColumns} from '@babel/code-frame';
|
|
15
16
|
import {NodeEventGenerator} from './NodeEventGenerator';
|
|
16
17
|
import {SafeEmitter} from './SafeEmitter';
|
|
17
18
|
import {SimpleTraverser} from './SimpleTraverser';
|
|
18
19
|
|
|
19
|
-
export type
|
|
20
|
+
export type TraversalContextBase = $ReadOnly<{
|
|
20
21
|
/**
|
|
21
22
|
* Gets the variables that were declared by the given node.
|
|
22
23
|
*/
|
|
@@ -31,13 +32,36 @@ export type TraversalContext<T = void> = $ReadOnly<{
|
|
|
31
32
|
* Defaults to the currently traversed node.
|
|
32
33
|
*/
|
|
33
34
|
getScope: (node?: ESNode) => Scope,
|
|
34
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Creates a full code frame for the node along with the message.
|
|
37
|
+
*
|
|
38
|
+
* i.e. `context.buildCodeFrame(node, 'foo')` will create a string like:
|
|
39
|
+
* ```
|
|
40
|
+
* 56 | function () {
|
|
41
|
+
* | ^^^^^^^^^^^^^
|
|
42
|
+
* 57 | }.bind(this)
|
|
43
|
+
* | ^^ foo
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
buildCodeFrame: (node: ESNode, message: string) => string,
|
|
47
|
+
/**
|
|
48
|
+
* Creates a simple code frame for the node along with the message.
|
|
49
|
+
* Use this if you want a condensed marker for your message.
|
|
50
|
+
*
|
|
51
|
+
* i.e. `context.logWithNode(node, 'foo')` will create a string like:
|
|
52
|
+
* ```
|
|
53
|
+
* [FunctionExpression:56:44] foo
|
|
54
|
+
* ```
|
|
55
|
+
* (where 56:44 represents L56, Col44)
|
|
56
|
+
*/
|
|
57
|
+
buildSimpleCodeFrame: (node: ESNode, message: string) => string,
|
|
58
|
+
}>;
|
|
59
|
+
export type TraversalContext<T> = $ReadOnly<{
|
|
60
|
+
...TraversalContextBase,
|
|
35
61
|
...T,
|
|
36
62
|
}>;
|
|
37
63
|
|
|
38
|
-
export type Visitor<T =
|
|
39
|
-
context: TraversalContext<T>,
|
|
40
|
-
) => ESQueryNodeSelectors;
|
|
64
|
+
export type Visitor<T> = (context: TraversalContext<T>) => ESQueryNodeSelectors;
|
|
41
65
|
|
|
42
66
|
/**
|
|
43
67
|
* Traverse the AST with additional context members provided by `additionalContext`.
|
|
@@ -45,10 +69,11 @@ export type Visitor<T = void> = (
|
|
|
45
69
|
* @param scopeManager the eslint-scope compatible scope manager instance calculated using the ast
|
|
46
70
|
* @param additionalContext a callback function which returns additional context members to add to the context provided to the visitor
|
|
47
71
|
*/
|
|
48
|
-
export function traverseWithContext<T =
|
|
72
|
+
export function traverseWithContext<T = TraversalContextBase>(
|
|
73
|
+
code: string,
|
|
49
74
|
ast: Program,
|
|
50
75
|
scopeManager: ScopeManager,
|
|
51
|
-
additionalContext:
|
|
76
|
+
additionalContext: TraversalContextBase => T,
|
|
52
77
|
visitor: Visitor<T>,
|
|
53
78
|
): void {
|
|
54
79
|
const emitter = new SafeEmitter();
|
|
@@ -86,9 +111,34 @@ export function traverseWithContext<T = void>(
|
|
|
86
111
|
return scopeManager.scopes[0];
|
|
87
112
|
};
|
|
88
113
|
|
|
89
|
-
const traversalContextBase:
|
|
114
|
+
const traversalContextBase: TraversalContextBase = Object.freeze({
|
|
115
|
+
buildCodeFrame: (node: ESNode, message: string): string => {
|
|
116
|
+
// babel uses 1-indexed columns
|
|
117
|
+
const locForBabel = {
|
|
118
|
+
start: {
|
|
119
|
+
line: node.loc.start.line,
|
|
120
|
+
column: node.loc.start.column + 1,
|
|
121
|
+
},
|
|
122
|
+
end: {
|
|
123
|
+
line: node.loc.end.line,
|
|
124
|
+
column: node.loc.end.column + 1,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
return codeFrameColumns(code, locForBabel, {
|
|
128
|
+
linesAbove: 0,
|
|
129
|
+
linesBelow: 0,
|
|
130
|
+
highlightCode: process.env.NODE_ENV !== 'test',
|
|
131
|
+
message: message,
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
buildSimpleCodeFrame: (node: ESNode, message: string): string => {
|
|
136
|
+
return `[${node.type}:${node.loc.start.line}:${node.loc.start.column}] ${message}`;
|
|
137
|
+
},
|
|
138
|
+
|
|
90
139
|
getDeclaredVariables: (node: ESNode) =>
|
|
91
140
|
scopeManager.getDeclaredVariables(node),
|
|
141
|
+
|
|
92
142
|
getBinding: (name: string) => {
|
|
93
143
|
let currentScope = getScope();
|
|
94
144
|
|
|
@@ -103,8 +153,10 @@ export function traverseWithContext<T = void>(
|
|
|
103
153
|
|
|
104
154
|
return null;
|
|
105
155
|
},
|
|
156
|
+
|
|
106
157
|
getScope,
|
|
107
158
|
});
|
|
159
|
+
|
|
108
160
|
const traversalContext: TraversalContext<T> = Object.freeze({
|
|
109
161
|
...traversalContextBase,
|
|
110
162
|
...additionalContext(traversalContextBase),
|
|
@@ -141,9 +193,10 @@ export function traverseWithContext<T = void>(
|
|
|
141
193
|
}
|
|
142
194
|
|
|
143
195
|
export function traverse(
|
|
196
|
+
code: string,
|
|
144
197
|
ast: Program,
|
|
145
198
|
scopeManager: ScopeManager,
|
|
146
199
|
visitor: Visitor<void>,
|
|
147
200
|
): void {
|
|
148
|
-
traverseWithContext(ast, scopeManager, () => {}, visitor);
|
|
201
|
+
traverseWithContext(code, ast, scopeManager, () => {}, visitor);
|
|
149
202
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-transform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Tools built on top of Hermes-ESTree to enable codebase transformation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,9 +9,11 @@
|
|
|
9
9
|
"url": "git@github.com:facebook/hermes.git"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
+
"@babel/code-frame": "^7.16.0",
|
|
12
13
|
"esquery": "^1.4.0",
|
|
13
|
-
"
|
|
14
|
-
"hermes-
|
|
14
|
+
"flow-enums-runtime": "^0.0.6",
|
|
15
|
+
"hermes-eslint": "0.8.0",
|
|
16
|
+
"hermes-estree": "0.8.0"
|
|
15
17
|
},
|
|
16
18
|
"peerDependencies": {
|
|
17
19
|
"prettier": "^2.4.1"
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createAddLeadingCommentsMutation = createAddLeadingCommentsMutation;
|
|
7
|
-
exports.performAddLeadingCommentsMutation = performAddLeadingCommentsMutation;
|
|
8
|
-
|
|
9
|
-
var _comments = require("../comments/comments");
|
|
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
|
-
|
|
17
|
-
function createAddLeadingCommentsMutation(node, comments) {
|
|
18
|
-
if (comments.length === 0) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
type: 'addLeadingComments',
|
|
24
|
-
comments: comments,
|
|
25
|
-
node: node
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function performAddLeadingCommentsMutation(mutationContext, mutation) {
|
|
30
|
-
var _iterator = _createForOfIteratorHelper(mutation.comments),
|
|
31
|
-
_step;
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
35
|
-
var originalComment = _step.value;
|
|
36
|
-
var comment = (0, _comments.cloneComment)(originalComment);
|
|
37
|
-
mutationContext.appendCommentToSource(comment);
|
|
38
|
-
(0, _comments.addLeadingComment)(mutation.node, comment);
|
|
39
|
-
}
|
|
40
|
-
} catch (err) {
|
|
41
|
-
_iterator.e(err);
|
|
42
|
-
} finally {
|
|
43
|
-
_iterator.f();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return null;
|
|
47
|
-
}
|