hermes-transform 0.8.0 → 0.9.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/ESLINT_LICENCE +19 -0
- package/PRETTIER_LICENCE +7 -0
- package/dist/detachedNode.js +96 -20
- package/dist/detachedNode.js.flow +105 -17
- package/dist/generated/TransformModifySignatures.js.flow +1113 -0
- package/dist/generated/node-types.js +627 -852
- package/dist/generated/node-types.js.flow +969 -1154
- package/dist/generated/special-case-node-types.js +71 -77
- package/dist/generated/special-case-node-types.js.flow +104 -88
- package/dist/transform/TransformContext.js +34 -11
- package/dist/transform/TransformContext.js.flow +90 -33
- package/dist/transform/comments/comments.js +23 -5
- package/dist/transform/comments/comments.js.flow +23 -3
- package/dist/transform/getTransformedAST.js +15 -0
- package/dist/transform/getTransformedAST.js.flow +19 -1
- package/dist/transform/mutations/InsertStatement.js +1 -0
- package/dist/transform/mutations/InsertStatement.js.flow +1 -0
- package/dist/transform/mutations/ReplaceNode.js +7 -2
- package/dist/transform/mutations/ReplaceNode.js.flow +5 -2
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +5 -2
- package/dist/traverse/NodeEventGenerator.js.flow +1 -1
- package/dist/traverse/traverse.js.flow +1 -1
- package/package.json +8 -4
package/ESLINT_LICENCE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/PRETTIER_LICENCE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright © James Long and contributors
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/detachedNode.js
CHANGED
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.asDetachedNode = void 0;
|
|
6
7
|
exports.deepCloneNode = deepCloneNode;
|
|
7
8
|
exports.detachedProps = detachedProps;
|
|
9
|
+
exports.getOriginalNode = getOriginalNode;
|
|
10
|
+
exports.isDetachedNode = isDetachedNode;
|
|
8
11
|
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
|
9
12
|
exports.shallowCloneNode = shallowCloneNode;
|
|
10
13
|
exports.updateAllParentPointers = updateAllParentPointers;
|
|
@@ -22,41 +25,114 @@ var _SimpleTraverser = require("./traverse/SimpleTraverser");
|
|
|
22
25
|
*
|
|
23
26
|
* @format
|
|
24
27
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
const DETACHED_MARKER = Symbol.for('hermes-transform - Detached AST Node');
|
|
29
|
+
const ORIGINAL_NODE = Symbol.for('hermes-transform - Original Node');
|
|
30
|
+
|
|
31
|
+
function isDetachedNode(node) {
|
|
32
|
+
// $FlowExpectedError[invalid-in-lhs] flow doesn't support symbols as keys
|
|
33
|
+
return DETACHED_MARKER in node;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getOriginalNode(node) {
|
|
37
|
+
// $FlowExpectedError[prop-missing]
|
|
38
|
+
return node[ORIGINAL_NODE];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const asDetachedNode = (node, {
|
|
42
|
+
useDeepClone
|
|
43
|
+
} = {
|
|
44
|
+
useDeepClone: false
|
|
45
|
+
}) => {
|
|
46
|
+
if (node == null) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isDetachedNode(node)) {
|
|
51
|
+
return node;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return useDeepClone ? deepCloneNode(node, {}) : shallowCloneNode(node, {});
|
|
55
|
+
}; // used by the node type function codegen
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
exports.asDetachedNode = asDetachedNode;
|
|
59
|
+
|
|
60
|
+
function detachedProps(parent, props, config = { ...null
|
|
61
|
+
}) {
|
|
62
|
+
// $FlowExpectedError[incompatible-type]
|
|
63
|
+
const detachedNode = { ...props,
|
|
64
|
+
...((config == null ? void 0 : config.preserveLocation) !== true ? {
|
|
65
|
+
// if this is [0,0] AND the file has a docblock then prettier will insert newlines between
|
|
66
|
+
// certain detached nodes. Because of "intended" formatting behaviour (https://github.com/prettier/prettier/issues/12078)
|
|
67
|
+
// this can cause us to output weirdly formatted code that should have been collapsed.
|
|
68
|
+
//
|
|
69
|
+
// prettier works backwards from the position you give it to find newlines or non whitespace
|
|
70
|
+
// tokens and uses this to determine if newlines should be inserted between nodes.
|
|
71
|
+
// By placing the range at [1, 1] we can ensure a token is always found before a newline
|
|
72
|
+
// and therefore no newlines will be placed between nodes.
|
|
73
|
+
//
|
|
74
|
+
// NOTE: we will still run into the bug if there is weird code like a docblock with whitespace
|
|
75
|
+
// characters before it. However we assume this isn't going to happen because any file
|
|
76
|
+
// already formatted by prettier will have that whitespace removed.
|
|
77
|
+
// We considered a more complex solution involving traversing the AST and manually updating
|
|
78
|
+
// detached node ranges after mutations are applied - however this is a lot heavier and will
|
|
79
|
+
// probably never be needed.
|
|
80
|
+
range: [1, 1],
|
|
81
|
+
loc: {
|
|
82
|
+
start: {
|
|
83
|
+
line: 1,
|
|
84
|
+
column: 0
|
|
85
|
+
},
|
|
86
|
+
end: {
|
|
87
|
+
line: 1,
|
|
88
|
+
column: 0
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
} : {}),
|
|
29
92
|
// if not provided, then we purposely don't set this here
|
|
30
93
|
// and will rely on the tooling to update it as appropriate.
|
|
31
94
|
// nothing should be reading from this before it's set anyway.
|
|
32
|
-
parent: parent
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
95
|
+
parent: parent
|
|
96
|
+
}; // mark the node as detached
|
|
97
|
+
|
|
98
|
+
Object.defineProperty(detachedNode, DETACHED_MARKER, {
|
|
99
|
+
configurable: false,
|
|
100
|
+
enumerable: false,
|
|
101
|
+
value: true,
|
|
102
|
+
writable: false
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
if (config.originalNode) {
|
|
106
|
+
Object.defineProperty(detachedNode, ORIGINAL_NODE, {
|
|
107
|
+
configurable: false,
|
|
108
|
+
enumerable: false,
|
|
109
|
+
value: config.originalNode,
|
|
110
|
+
writable: false
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return detachedNode;
|
|
45
115
|
}
|
|
46
116
|
/**
|
|
47
117
|
* Shallowly clones the node, but not its children.
|
|
48
118
|
*/
|
|
49
119
|
|
|
50
120
|
|
|
51
|
-
function shallowCloneNode(node, newProps = {
|
|
52
|
-
|
|
121
|
+
function shallowCloneNode(node, newProps, config = { ...null
|
|
122
|
+
}) {
|
|
123
|
+
var _config$preserveLocat, _config$originalNode;
|
|
124
|
+
|
|
125
|
+
return detachedProps(null, Object.assign({}, node, newProps), {
|
|
126
|
+
preserveLocation: (_config$preserveLocat = config.preserveLocation) != null ? _config$preserveLocat : true,
|
|
127
|
+
originalNode: (_config$originalNode = config.originalNode) != null ? _config$originalNode : node
|
|
128
|
+
});
|
|
53
129
|
}
|
|
54
130
|
/**
|
|
55
131
|
* Deeply clones node and its entire tree.
|
|
56
132
|
*/
|
|
57
133
|
|
|
58
134
|
|
|
59
|
-
function deepCloneNode(node, newProps
|
|
135
|
+
function deepCloneNode(node, newProps) {
|
|
60
136
|
const clone = Object.assign(JSON.parse(JSON.stringify(node, (key, value) => {
|
|
61
137
|
// null out parent pointers
|
|
62
138
|
if (key === 'parent') {
|
|
@@ -14,31 +14,115 @@ import {getVisitorKeys, isNode} from './getVisitorKeys';
|
|
|
14
14
|
import {SimpleTraverser} from './traverse/SimpleTraverser';
|
|
15
15
|
|
|
16
16
|
export opaque type DetachedNode<+T> = T;
|
|
17
|
+
export type MaybeDetachedNode<+T> = T | DetachedNode<T>;
|
|
18
|
+
|
|
19
|
+
type DetachConfig = $ReadOnly<{
|
|
20
|
+
preserveLocation?: boolean,
|
|
21
|
+
originalNode?: ESNode,
|
|
22
|
+
}>;
|
|
23
|
+
|
|
24
|
+
const DETACHED_MARKER = Symbol.for('hermes-transform - Detached AST Node');
|
|
25
|
+
const ORIGINAL_NODE = Symbol.for('hermes-transform - Original Node');
|
|
26
|
+
|
|
27
|
+
export function isDetachedNode(node: MaybeDetachedNode<ESNode>): boolean {
|
|
28
|
+
// $FlowExpectedError[invalid-in-lhs] flow doesn't support symbols as keys
|
|
29
|
+
return DETACHED_MARKER in node;
|
|
30
|
+
}
|
|
31
|
+
export function getOriginalNode(node: MaybeDetachedNode<ESNode>): ?ESNode {
|
|
32
|
+
// $FlowExpectedError[prop-missing]
|
|
33
|
+
return node[ORIGINAL_NODE];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const asDetachedNode: {
|
|
37
|
+
<T: ESNode>(
|
|
38
|
+
node: MaybeDetachedNode<T>,
|
|
39
|
+
config?: {useDeepClone: boolean},
|
|
40
|
+
): DetachedNode<T>,
|
|
41
|
+
<T: ESNode>(
|
|
42
|
+
node: ?MaybeDetachedNode<T>,
|
|
43
|
+
config?: {useDeepClone: boolean},
|
|
44
|
+
): ?DetachedNode<T>,
|
|
45
|
+
} = <T: ESNode>(
|
|
46
|
+
node: ?MaybeDetachedNode<T>,
|
|
47
|
+
{useDeepClone}: {useDeepClone: boolean} = {useDeepClone: false},
|
|
48
|
+
): // $FlowExpectedError[incompatible-type]
|
|
49
|
+
?DetachedNode<T> => {
|
|
50
|
+
if (node == null) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (isDetachedNode(node)) {
|
|
55
|
+
return node;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return useDeepClone
|
|
59
|
+
? deepCloneNode<T>(node, {})
|
|
60
|
+
: shallowCloneNode<T>(node, {});
|
|
61
|
+
};
|
|
17
62
|
|
|
18
63
|
// used by the node type function codegen
|
|
19
64
|
export function detachedProps<T: BaseNode>(
|
|
20
65
|
parent: ?ESNode,
|
|
21
|
-
props: {...}
|
|
66
|
+
props: $ReadOnly<$Partial<{...}>>,
|
|
67
|
+
config: DetachConfig = {...null},
|
|
22
68
|
): DetachedNode<T> {
|
|
23
|
-
// $FlowExpectedError[incompatible-
|
|
24
|
-
|
|
69
|
+
// $FlowExpectedError[incompatible-type]
|
|
70
|
+
const detachedNode: DetachedNode<T> = {
|
|
25
71
|
...props,
|
|
72
|
+
...(config?.preserveLocation !== true
|
|
73
|
+
? {
|
|
74
|
+
// if this is [0,0] AND the file has a docblock then prettier will insert newlines between
|
|
75
|
+
// certain detached nodes. Because of "intended" formatting behaviour (https://github.com/prettier/prettier/issues/12078)
|
|
76
|
+
// this can cause us to output weirdly formatted code that should have been collapsed.
|
|
77
|
+
//
|
|
78
|
+
// prettier works backwards from the position you give it to find newlines or non whitespace
|
|
79
|
+
// tokens and uses this to determine if newlines should be inserted between nodes.
|
|
80
|
+
// By placing the range at [1, 1] we can ensure a token is always found before a newline
|
|
81
|
+
// and therefore no newlines will be placed between nodes.
|
|
82
|
+
//
|
|
83
|
+
// NOTE: we will still run into the bug if there is weird code like a docblock with whitespace
|
|
84
|
+
// characters before it. However we assume this isn't going to happen because any file
|
|
85
|
+
// already formatted by prettier will have that whitespace removed.
|
|
86
|
+
// We considered a more complex solution involving traversing the AST and manually updating
|
|
87
|
+
// detached node ranges after mutations are applied - however this is a lot heavier and will
|
|
88
|
+
// probably never be needed.
|
|
89
|
+
range: [1, 1],
|
|
90
|
+
loc: {
|
|
91
|
+
start: {
|
|
92
|
+
line: 1,
|
|
93
|
+
column: 0,
|
|
94
|
+
},
|
|
95
|
+
end: {
|
|
96
|
+
line: 1,
|
|
97
|
+
column: 0,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
: {}),
|
|
26
102
|
// if not provided, then we purposely don't set this here
|
|
27
103
|
// and will rely on the tooling to update it as appropriate.
|
|
28
104
|
// nothing should be reading from this before it's set anyway.
|
|
29
105
|
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
106
|
};
|
|
107
|
+
|
|
108
|
+
// mark the node as detached
|
|
109
|
+
Object.defineProperty(detachedNode, DETACHED_MARKER, {
|
|
110
|
+
configurable: false,
|
|
111
|
+
enumerable: false,
|
|
112
|
+
value: true,
|
|
113
|
+
writable: false,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
if (config.originalNode) {
|
|
117
|
+
Object.defineProperty(detachedNode, ORIGINAL_NODE, {
|
|
118
|
+
configurable: false,
|
|
119
|
+
enumerable: false,
|
|
120
|
+
value: config.originalNode,
|
|
121
|
+
writable: false,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return detachedNode;
|
|
42
126
|
}
|
|
43
127
|
|
|
44
128
|
/**
|
|
@@ -46,9 +130,13 @@ export function detachedProps<T: BaseNode>(
|
|
|
46
130
|
*/
|
|
47
131
|
export function shallowCloneNode<T: ESNode>(
|
|
48
132
|
node: T,
|
|
49
|
-
newProps: $Partial<{...}
|
|
133
|
+
newProps: $ReadOnly<$Partial<{...}>>,
|
|
134
|
+
config?: DetachConfig = {...null},
|
|
50
135
|
): DetachedNode<T> {
|
|
51
|
-
return detachedProps(null, (Object.assign({}, node, newProps): $FlowFixMe)
|
|
136
|
+
return detachedProps(null, (Object.assign({}, node, newProps): $FlowFixMe), {
|
|
137
|
+
preserveLocation: config.preserveLocation ?? true,
|
|
138
|
+
originalNode: config.originalNode ?? node,
|
|
139
|
+
});
|
|
52
140
|
}
|
|
53
141
|
|
|
54
142
|
/**
|
|
@@ -56,7 +144,7 @@ export function shallowCloneNode<T: ESNode>(
|
|
|
56
144
|
*/
|
|
57
145
|
export function deepCloneNode<T: ESNode>(
|
|
58
146
|
node: T,
|
|
59
|
-
newProps: $Partial<{...}
|
|
147
|
+
newProps: $ReadOnly<$Partial<{...}>>,
|
|
60
148
|
): DetachedNode<T> {
|
|
61
149
|
const clone: DetachedNode<T> = Object.assign(
|
|
62
150
|
JSON.parse(
|