hermes-transform 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -0
- package/dist/detachedNode.js +128 -0
- package/dist/detachedNode.js.flow +113 -0
- package/dist/generated/TransformCloneSignatures.js.flow +19 -0
- package/dist/generated/TransformReplaceSignatures.js.flow +943 -0
- package/dist/generated/node-types.js +2071 -0
- package/dist/generated/node-types.js.flow +3149 -0
- package/dist/generated/special-case-node-types.js +178 -0
- package/dist/generated/special-case-node-types.js.flow +248 -0
- package/dist/getVisitorKeys.js +35 -0
- package/dist/getVisitorKeys.js.flow +31 -0
- package/dist/index.js +41 -0
- package/dist/index.js.flow +15 -0
- package/dist/transform/Errors.js +151 -0
- package/dist/transform/Errors.js.flow +17 -0
- package/dist/transform/MutationContext.js +94 -0
- package/dist/transform/MutationContext.js.flow +80 -0
- package/dist/transform/TransformContext.js +136 -0
- package/dist/transform/TransformContext.js.flow +378 -0
- package/dist/transform/comments/comments.js +140 -0
- package/dist/transform/comments/comments.js.flow +145 -0
- package/dist/transform/comments/prettier/README.md +6 -0
- package/dist/transform/comments/prettier/common/util.js +365 -0
- package/dist/transform/comments/prettier/common/util.js.flow +349 -0
- package/dist/transform/comments/prettier/language-js/comments.js +777 -0
- package/dist/transform/comments/prettier/language-js/comments.js.flow +950 -0
- package/dist/transform/comments/prettier/language-js/loc.js +41 -0
- package/dist/transform/comments/prettier/language-js/loc.js.flow +41 -0
- package/dist/transform/comments/prettier/language-js/printer-estree.js +31 -0
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +37 -0
- package/dist/transform/comments/prettier/language-js/utils.js +131 -0
- package/dist/transform/comments/prettier/language-js/utils.js.flow +135 -0
- package/dist/transform/comments/prettier/main/comments.js +513 -0
- package/dist/transform/comments/prettier/main/comments.js.flow +436 -0
- package/dist/transform/comments/prettier/utils/get-last.js +15 -0
- package/dist/transform/comments/prettier/utils/get-last.js.flow +14 -0
- package/dist/transform/getTransformedAST.js +159 -0
- package/dist/transform/getTransformedAST.js.flow +128 -0
- package/dist/transform/mutations/AddLeadingComments.js +47 -0
- package/dist/transform/mutations/AddLeadingComments.js.flow +49 -0
- package/dist/transform/mutations/AddTrailingComments.js +47 -0
- package/dist/transform/mutations/AddTrailingComments.js.flow +49 -0
- package/dist/transform/mutations/CloneCommentsTo.js +46 -0
- package/dist/transform/mutations/CloneCommentsTo.js.flow +51 -0
- package/dist/transform/mutations/InsertStatement.js +92 -0
- package/dist/transform/mutations/InsertStatement.js.flow +113 -0
- package/dist/transform/mutations/RemoveComment.js +96 -0
- package/dist/transform/mutations/RemoveComment.js.flow +80 -0
- package/dist/transform/mutations/RemoveStatement.js +61 -0
- package/dist/transform/mutations/RemoveStatement.js.flow +68 -0
- package/dist/transform/mutations/ReplaceNode.js +96 -0
- package/dist/transform/mutations/ReplaceNode.js.flow +113 -0
- package/dist/transform/mutations/ReplaceStatementWithMany.js +81 -0
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +102 -0
- package/dist/transform/mutations/utils/arrayUtils.js +41 -0
- package/dist/transform/mutations/utils/arrayUtils.js.flow +35 -0
- package/dist/transform/mutations/utils/getStatementParent.js +147 -0
- package/dist/transform/mutations/utils/getStatementParent.js.flow +143 -0
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +53 -0
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +50 -0
- package/dist/transform/transform.js +69 -0
- package/dist/transform/transform.js.flow +60 -0
- package/dist/traverse/NodeEventGenerator.js +427 -0
- package/dist/traverse/NodeEventGenerator.js.flow +406 -0
- package/dist/traverse/SafeEmitter.js +70 -0
- package/dist/traverse/SafeEmitter.js.flow +46 -0
- package/dist/traverse/SimpleTraverser.js +149 -0
- package/dist/traverse/SimpleTraverser.js.flow +109 -0
- package/dist/traverse/esquery.js +37 -0
- package/dist/traverse/esquery.js.flow +173 -0
- package/dist/traverse/traverse.js +139 -0
- package/dist/traverse/traverse.js.flow +149 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# hermes-transform
|
|
2
|
+
|
|
3
|
+
A package for traversing and transforming a hermes AST.
|
|
4
|
+
|
|
5
|
+
This package is pieced together and inspired by code from:
|
|
6
|
+
- [`@babel/traverse`](https://github.com/babel/babel/tree/35ec4394a72e6fc88553ce7dcf0fb1c91d9505a9/packages/babel-traverse)
|
|
7
|
+
- [`eslint`](https://github.com/eslint/eslint/tree/e926b1735c77bf55abc1150b060a535a6c4e2778)
|
|
8
|
+
- [`prettier`](https://github.com/prettier/prettier/tree/7054df719e1789a71ce9e28b5a5452bc60fd42e7)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.deepCloneNode = deepCloneNode;
|
|
7
|
+
exports.detachedProps = detachedProps;
|
|
8
|
+
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
|
9
|
+
exports.shallowCloneNode = shallowCloneNode;
|
|
10
|
+
exports.updateAllParentPointers = updateAllParentPointers;
|
|
11
|
+
|
|
12
|
+
var _getVisitorKeys = require("./getVisitorKeys");
|
|
13
|
+
|
|
14
|
+
var _SimpleTraverser = require("./traverse/SimpleTraverser");
|
|
15
|
+
|
|
16
|
+
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; } } }; }
|
|
17
|
+
|
|
18
|
+
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); }
|
|
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
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
// used by the node type function codegen
|
|
29
|
+
function detachedProps(parent, props) {
|
|
30
|
+
// $FlowExpectedError[incompatible-return]
|
|
31
|
+
return _objectSpread(_objectSpread({}, props), {}, {
|
|
32
|
+
// if not provided, then we purposely don't set this here
|
|
33
|
+
// and will rely on the tooling to update it as appropriate.
|
|
34
|
+
// nothing should be reading from this before it's set anyway.
|
|
35
|
+
parent: parent,
|
|
36
|
+
range: [0, 0],
|
|
37
|
+
loc: {
|
|
38
|
+
start: {
|
|
39
|
+
line: 1,
|
|
40
|
+
column: 0
|
|
41
|
+
},
|
|
42
|
+
end: {
|
|
43
|
+
line: 1,
|
|
44
|
+
column: 0
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Shallowly clones the node, but not its children.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
function shallowCloneNode(node) {
|
|
55
|
+
var newProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
56
|
+
return detachedProps(null, Object.assign({}, node, newProps));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Deeply clones node and its entire tree.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function deepCloneNode(node) {
|
|
64
|
+
var newProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
65
|
+
var clone = Object.assign(JSON.parse(JSON.stringify(node, function (key, value) {
|
|
66
|
+
// null out parent pointers
|
|
67
|
+
if (key === 'parent') {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return value;
|
|
72
|
+
})), newProps);
|
|
73
|
+
updateAllParentPointers(clone); // $FlowExpectedError[class-object-subtyping]
|
|
74
|
+
|
|
75
|
+
return detachedProps(null, clone);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Corrects the parent pointers in direct children of the given node
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
function setParentPointersInDirectChildren(node) {
|
|
83
|
+
var _iterator = _createForOfIteratorHelper((0, _getVisitorKeys.getVisitorKeys)(node)),
|
|
84
|
+
_step;
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
88
|
+
var key = _step.value;
|
|
89
|
+
|
|
90
|
+
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
91
|
+
node[key])) {
|
|
92
|
+
node[key].parent = node;
|
|
93
|
+
} else if (Array.isArray(node[key])) {
|
|
94
|
+
var _iterator2 = _createForOfIteratorHelper(node[key]),
|
|
95
|
+
_step2;
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
99
|
+
var child = _step2.value;
|
|
100
|
+
child.parent = node;
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
_iterator2.e(err);
|
|
104
|
+
} finally {
|
|
105
|
+
_iterator2.f();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} catch (err) {
|
|
110
|
+
_iterator.e(err);
|
|
111
|
+
} finally {
|
|
112
|
+
_iterator.f();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Traverses the entire subtree to ensure the parent pointers are set correctly
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
function updateAllParentPointers(node) {
|
|
121
|
+
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
|
122
|
+
enter: function enter(node, parent) {
|
|
123
|
+
// $FlowExpectedError[cannot-write]
|
|
124
|
+
node.parent = parent;
|
|
125
|
+
},
|
|
126
|
+
leave: function leave() {}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {BaseNode, ESNode} from 'hermes-estree';
|
|
12
|
+
|
|
13
|
+
import {getVisitorKeys, isNode} from './getVisitorKeys';
|
|
14
|
+
import {SimpleTraverser} from './traverse/SimpleTraverser';
|
|
15
|
+
|
|
16
|
+
export opaque type DetachedNode<+T> = T;
|
|
17
|
+
|
|
18
|
+
// used by the node type function codegen
|
|
19
|
+
export function detachedProps<T: BaseNode>(
|
|
20
|
+
parent: ?ESNode,
|
|
21
|
+
props: {...},
|
|
22
|
+
): DetachedNode<T> {
|
|
23
|
+
// $FlowExpectedError[incompatible-return]
|
|
24
|
+
return {
|
|
25
|
+
...props,
|
|
26
|
+
// if not provided, then we purposely don't set this here
|
|
27
|
+
// and will rely on the tooling to update it as appropriate.
|
|
28
|
+
// nothing should be reading from this before it's set anyway.
|
|
29
|
+
parent: (parent: $FlowFixMe),
|
|
30
|
+
range: [0, 0],
|
|
31
|
+
loc: {
|
|
32
|
+
start: {
|
|
33
|
+
line: 1,
|
|
34
|
+
column: 0,
|
|
35
|
+
},
|
|
36
|
+
end: {
|
|
37
|
+
line: 1,
|
|
38
|
+
column: 0,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Shallowly clones the node, but not its children.
|
|
46
|
+
*/
|
|
47
|
+
export function shallowCloneNode<T: ESNode>(
|
|
48
|
+
node: T,
|
|
49
|
+
newProps: $Shape<{...}> = {},
|
|
50
|
+
): DetachedNode<T> {
|
|
51
|
+
return detachedProps(null, (Object.assign({}, node, newProps): $FlowFixMe));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Deeply clones node and its entire tree.
|
|
56
|
+
*/
|
|
57
|
+
export function deepCloneNode<T: ESNode>(
|
|
58
|
+
node: T,
|
|
59
|
+
newProps: $Shape<{...}> = {},
|
|
60
|
+
): DetachedNode<T> {
|
|
61
|
+
const clone: DetachedNode<T> = Object.assign(
|
|
62
|
+
JSON.parse(
|
|
63
|
+
JSON.stringify(node, (key, value) => {
|
|
64
|
+
// null out parent pointers
|
|
65
|
+
if (key === 'parent') {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}),
|
|
70
|
+
),
|
|
71
|
+
newProps,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
updateAllParentPointers(clone);
|
|
75
|
+
|
|
76
|
+
// $FlowExpectedError[class-object-subtyping]
|
|
77
|
+
return detachedProps(null, clone);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Corrects the parent pointers in direct children of the given node
|
|
82
|
+
*/
|
|
83
|
+
export function setParentPointersInDirectChildren(
|
|
84
|
+
node: DetachedNode<ESNode>,
|
|
85
|
+
): void {
|
|
86
|
+
for (const key of getVisitorKeys(node)) {
|
|
87
|
+
if (
|
|
88
|
+
isNode(
|
|
89
|
+
// $FlowExpectedError[prop-missing]
|
|
90
|
+
node[key],
|
|
91
|
+
)
|
|
92
|
+
) {
|
|
93
|
+
node[key].parent = node;
|
|
94
|
+
} else if (Array.isArray(node[key])) {
|
|
95
|
+
for (const child of node[key]) {
|
|
96
|
+
child.parent = node;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Traverses the entire subtree to ensure the parent pointers are set correctly
|
|
104
|
+
*/
|
|
105
|
+
export function updateAllParentPointers(node: ESNode | DetachedNode<ESNode>) {
|
|
106
|
+
SimpleTraverser.traverse(node, {
|
|
107
|
+
enter(node, parent) {
|
|
108
|
+
// $FlowExpectedError[cannot-write]
|
|
109
|
+
node.parent = parent;
|
|
110
|
+
},
|
|
111
|
+
leave() {},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* !!! THIS FILE WILL BE OVERWRITTEN BY CODEGEN !!!
|
|
13
|
+
*
|
|
14
|
+
* Statically it should only contain the minimal set of
|
|
15
|
+
* definitions required to typecheck the code and execute
|
|
16
|
+
* the transformation tests.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type TransformCloneSignatures = {};
|