hermes-estree 0.32.1 → 0.33.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/dist/generated/HermesESTreeSelectorTypes.js.flow +262 -27
- package/dist/generated/predicates.js +120 -0
- package/dist/generated/predicates.js.flow +72 -0
- package/dist/predicates.js +2 -4
- package/dist/predicates.js.flow +5 -4
- package/dist/src/generated/predicates.js +120 -0
- package/dist/src/predicates.js +2 -4
- package/dist/types.js.flow +103 -11
- package/package.json +1 -1
- package/dist/HermesAST.js.flow +0 -57
- package/dist/HermesASTAdapter.js +0 -192
- package/dist/HermesASTAdapter.js.flow +0 -189
- package/dist/HermesParser.js +0 -108
- package/dist/HermesParser.js.flow +0 -161
- package/dist/HermesParserDecodeUTF8String.js +0 -68
- package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
- package/dist/HermesParserDeserializer.js +0 -242
- package/dist/HermesParserDeserializer.js.flow +0 -260
- package/dist/HermesParserNodeDeserializers.js +0 -2477
- package/dist/HermesParserNodeDeserializers.js.flow +0 -16
- package/dist/HermesParserWASM.js +0 -6
- package/dist/HermesParserWASM.js.flow +0 -87
- package/dist/HermesToESTreeAdapter.js +0 -439
- package/dist/HermesToESTreeAdapter.js.flow +0 -421
- package/dist/ParserOptions.js +0 -18
- package/dist/ParserOptions.js.flow +0 -41
- package/dist/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
- package/dist/estree/StripFlowTypes.js +0 -175
- package/dist/estree/StripFlowTypes.js.flow +0 -158
- package/dist/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
- package/dist/estree/TransformComponentSyntax.js +0 -788
- package/dist/estree/TransformComponentSyntax.js.flow +0 -864
- package/dist/estree/TransformEnumSyntax.js +0 -106
- package/dist/estree/TransformEnumSyntax.js.flow +0 -125
- package/dist/estree/TransformMatchSyntax.js +0 -1006
- package/dist/estree/TransformMatchSyntax.js.flow +0 -912
- package/dist/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
- package/dist/generated/ParserVisitorKeys.js +0 -794
- package/dist/generated/ParserVisitorKeys.js.flow +0 -17
- package/dist/getModuleDocblock.js +0 -112
- package/dist/getModuleDocblock.js.flow +0 -118
- package/dist/src/HermesASTAdapter.js +0 -192
- package/dist/src/HermesParser.js +0 -108
- package/dist/src/HermesParserDecodeUTF8String.js +0 -68
- package/dist/src/HermesParserDeserializer.js +0 -242
- package/dist/src/HermesParserNodeDeserializers.js +0 -2477
- package/dist/src/HermesToESTreeAdapter.js +0 -439
- package/dist/src/ParserOptions.js +0 -18
- package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/src/estree/StripFlowTypes.js +0 -175
- package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/src/estree/TransformComponentSyntax.js +0 -788
- package/dist/src/estree/TransformEnumSyntax.js +0 -106
- package/dist/src/estree/TransformMatchSyntax.js +0 -1006
- package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/src/generated/ParserVisitorKeys.js +0 -794
- package/dist/src/getModuleDocblock.js +0 -112
- package/dist/src/transform/SimpleTransform.js +0 -136
- package/dist/src/transform/astArrayMutationHelpers.js +0 -62
- package/dist/src/transform/astNodeMutationHelpers.js +0 -200
- package/dist/src/traverse/SimpleTraverser.js +0 -137
- package/dist/src/traverse/getVisitorKeys.js +0 -37
- package/dist/src/utils/Builders.js +0 -191
- package/dist/src/utils/GenID.js +0 -41
- package/dist/src/utils/createSyntaxError.js +0 -25
- package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/transform/SimpleTransform.js +0 -136
- package/dist/transform/SimpleTransform.js.flow +0 -169
- package/dist/transform/astArrayMutationHelpers.js +0 -62
- package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
- package/dist/transform/astNodeMutationHelpers.js +0 -200
- package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
- package/dist/traverse/SimpleTraverser.js +0 -137
- package/dist/traverse/SimpleTraverser.js.flow +0 -133
- package/dist/traverse/getVisitorKeys.js +0 -37
- package/dist/traverse/getVisitorKeys.js.flow +0 -36
- package/dist/utils/Builders.js +0 -191
- package/dist/utils/Builders.js.flow +0 -218
- package/dist/utils/GenID.js +0 -41
- package/dist/utils/GenID.js.flow +0 -38
- package/dist/utils/createSyntaxError.js +0 -25
- package/dist/utils/createSyntaxError.js.flow +0 -24
- package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.EMPTY_PARENT = void 0;
|
|
16
|
-
exports.callExpression = callExpression;
|
|
17
|
-
exports.conjunction = conjunction;
|
|
18
|
-
exports.createDefaultPosition = createDefaultPosition;
|
|
19
|
-
exports.disjunction = disjunction;
|
|
20
|
-
exports.etc = etc;
|
|
21
|
-
exports.ident = ident;
|
|
22
|
-
exports.iife = iife;
|
|
23
|
-
exports.nullLiteral = nullLiteral;
|
|
24
|
-
exports.numberLiteral = numberLiteral;
|
|
25
|
-
exports.stringLiteral = stringLiteral;
|
|
26
|
-
exports.throwStatement = throwStatement;
|
|
27
|
-
exports.typeofExpression = typeofExpression;
|
|
28
|
-
exports.variableDeclaration = variableDeclaration;
|
|
29
|
-
// Rely on the mapper to fix up parent relationships.
|
|
30
|
-
const EMPTY_PARENT = null;
|
|
31
|
-
exports.EMPTY_PARENT = EMPTY_PARENT;
|
|
32
|
-
|
|
33
|
-
function createDefaultPosition() {
|
|
34
|
-
return {
|
|
35
|
-
line: 1,
|
|
36
|
-
column: 0
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function etc({
|
|
41
|
-
loc,
|
|
42
|
-
range,
|
|
43
|
-
parent
|
|
44
|
-
} = {}) {
|
|
45
|
-
return {
|
|
46
|
-
loc: {
|
|
47
|
-
start: (loc == null ? void 0 : loc.start) != null ? loc.start : createDefaultPosition(),
|
|
48
|
-
end: (loc == null ? void 0 : loc.end) != null ? loc.end : createDefaultPosition()
|
|
49
|
-
},
|
|
50
|
-
range: range != null ? range : [0, 0],
|
|
51
|
-
parent: parent != null ? parent : EMPTY_PARENT
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function ident(name, info) {
|
|
56
|
-
return {
|
|
57
|
-
type: 'Identifier',
|
|
58
|
-
name,
|
|
59
|
-
optional: false,
|
|
60
|
-
typeAnnotation: null,
|
|
61
|
-
...etc(info)
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function stringLiteral(value, info) {
|
|
66
|
-
return {
|
|
67
|
-
type: 'Literal',
|
|
68
|
-
value,
|
|
69
|
-
raw: `"${value}"`,
|
|
70
|
-
literalType: 'string',
|
|
71
|
-
...etc(info)
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function numberLiteral(value, info) {
|
|
76
|
-
return {
|
|
77
|
-
type: 'Literal',
|
|
78
|
-
value,
|
|
79
|
-
raw: String(value),
|
|
80
|
-
literalType: 'numeric',
|
|
81
|
-
...etc(info)
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function nullLiteral(info) {
|
|
86
|
-
return {
|
|
87
|
-
type: 'Literal',
|
|
88
|
-
value: null,
|
|
89
|
-
raw: 'null',
|
|
90
|
-
literalType: 'null',
|
|
91
|
-
...etc(info)
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function conjunction(tests) {
|
|
96
|
-
if (tests.length === 0) {
|
|
97
|
-
throw new Error('Must have at least one test.');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return tests.reduce((acc, test) => ({
|
|
101
|
-
type: 'LogicalExpression',
|
|
102
|
-
left: acc,
|
|
103
|
-
right: test,
|
|
104
|
-
operator: '&&',
|
|
105
|
-
...etc()
|
|
106
|
-
}));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function disjunction(tests) {
|
|
110
|
-
if (tests.length === 0) {
|
|
111
|
-
throw new Error('Must have at least one test.');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return tests.reduce((acc, test) => ({
|
|
115
|
-
type: 'LogicalExpression',
|
|
116
|
-
left: acc,
|
|
117
|
-
right: test,
|
|
118
|
-
operator: '||',
|
|
119
|
-
...etc()
|
|
120
|
-
}));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function variableDeclaration(kind, id, init, info) {
|
|
124
|
-
return {
|
|
125
|
-
type: 'VariableDeclaration',
|
|
126
|
-
kind,
|
|
127
|
-
declarations: [{
|
|
128
|
-
type: 'VariableDeclarator',
|
|
129
|
-
init,
|
|
130
|
-
id,
|
|
131
|
-
...etc(),
|
|
132
|
-
parent: EMPTY_PARENT
|
|
133
|
-
}],
|
|
134
|
-
...etc(info)
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function callExpression(callee, args, info) {
|
|
139
|
-
return {
|
|
140
|
-
type: 'CallExpression',
|
|
141
|
-
callee,
|
|
142
|
-
arguments: args,
|
|
143
|
-
typeArguments: null,
|
|
144
|
-
optional: false,
|
|
145
|
-
...etc(info)
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function throwStatement(arg, info) {
|
|
150
|
-
return {
|
|
151
|
-
type: 'ThrowStatement',
|
|
152
|
-
argument: callExpression(ident('Error'), [arg]),
|
|
153
|
-
...etc(info)
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function iife(statements, params = [], args = []) {
|
|
158
|
-
const callee = {
|
|
159
|
-
type: 'ArrowFunctionExpression',
|
|
160
|
-
params,
|
|
161
|
-
expression: false,
|
|
162
|
-
async: false,
|
|
163
|
-
predicate: null,
|
|
164
|
-
returnType: null,
|
|
165
|
-
typeParameters: null,
|
|
166
|
-
id: null,
|
|
167
|
-
body: {
|
|
168
|
-
type: 'BlockStatement',
|
|
169
|
-
body: statements,
|
|
170
|
-
...etc()
|
|
171
|
-
},
|
|
172
|
-
...etc()
|
|
173
|
-
};
|
|
174
|
-
return callExpression(callee, args);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function typeofExpression(arg, kind) {
|
|
178
|
-
return {
|
|
179
|
-
type: 'BinaryExpression',
|
|
180
|
-
left: {
|
|
181
|
-
type: 'UnaryExpression',
|
|
182
|
-
operator: 'typeof',
|
|
183
|
-
argument: arg,
|
|
184
|
-
prefix: true,
|
|
185
|
-
...etc()
|
|
186
|
-
},
|
|
187
|
-
right: stringLiteral(kind),
|
|
188
|
-
operator: '===',
|
|
189
|
-
...etc()
|
|
190
|
-
};
|
|
191
|
-
}
|
package/dist/src/utils/GenID.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.createGenID = createGenID;
|
|
16
|
-
const genPrefix = '$$gen$';
|
|
17
|
-
|
|
18
|
-
function createGenID(uniqueTransformPrefix) {
|
|
19
|
-
let genN = 0;
|
|
20
|
-
const used = new Set();
|
|
21
|
-
return {
|
|
22
|
-
genID() {
|
|
23
|
-
let name;
|
|
24
|
-
|
|
25
|
-
do {
|
|
26
|
-
name = `${genPrefix}${uniqueTransformPrefix}${genN}`;
|
|
27
|
-
genN++;
|
|
28
|
-
} while (used.has(name));
|
|
29
|
-
|
|
30
|
-
used.add(name);
|
|
31
|
-
return name;
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
addUsage(name) {
|
|
35
|
-
if (name.startsWith(genPrefix)) {
|
|
36
|
-
used.add(name);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
};
|
|
41
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.createSyntaxError = createSyntaxError;
|
|
16
|
-
|
|
17
|
-
function createSyntaxError(node, err) {
|
|
18
|
-
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
19
|
-
|
|
20
|
-
syntaxError.loc = {
|
|
21
|
-
line: node.loc.start.line,
|
|
22
|
-
column: node.loc.start.column
|
|
23
|
-
};
|
|
24
|
-
return syntaxError;
|
|
25
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.default = mutate;
|
|
16
|
-
|
|
17
|
-
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
18
|
-
|
|
19
|
-
// https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
|
|
20
|
-
function transformChainExpression(node, comments) {
|
|
21
|
-
if (comments != null) {
|
|
22
|
-
var _node$comments;
|
|
23
|
-
|
|
24
|
-
// $FlowExpectedError[prop-missing]
|
|
25
|
-
const joinedComments = comments.concat((_node$comments = node.comments) != null ? _node$comments : []); // $FlowExpectedError[prop-missing]
|
|
26
|
-
// $FlowFixMe[cannot-write]
|
|
27
|
-
|
|
28
|
-
node.comments = joinedComments;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
switch (node.type) {
|
|
32
|
-
case 'CallExpression':
|
|
33
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
34
|
-
return { ...node,
|
|
35
|
-
type: 'OptionalCallExpression',
|
|
36
|
-
callee: transformChainExpression(node.callee)
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
case 'MemberExpression':
|
|
40
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
41
|
-
return { ...node,
|
|
42
|
-
type: 'OptionalMemberExpression',
|
|
43
|
-
object: transformChainExpression(node.object)
|
|
44
|
-
};
|
|
45
|
-
// No default
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return node;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function mutate(rootNode, visitorKeys) {
|
|
52
|
-
// Since we don't return the result of `transform` we need to be careful not to replace the Program root node.
|
|
53
|
-
_SimpleTransform.SimpleTransform.transform(rootNode, {
|
|
54
|
-
transform(node) {
|
|
55
|
-
// prettier fully expects the parent pointers are NOT set and
|
|
56
|
-
// certain cases can crash due to prettier infinite-looping
|
|
57
|
-
// whilst naively traversing the parent property
|
|
58
|
-
// https://github.com/prettier/prettier/issues/11793
|
|
59
|
-
// Note: Only needed for prettier V2, this is supported in V3
|
|
60
|
-
if (node.parent) {
|
|
61
|
-
// $FlowExpectedError[cannot-write]
|
|
62
|
-
delete node.parent;
|
|
63
|
-
} // prettier currently relies on the AST being in the old-school, deprecated AST format for optional chaining
|
|
64
|
-
// so we have to apply their transform to our AST so it can actually format it.
|
|
65
|
-
// Note: Only needed for prettier V2, this is supported in V3
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (node.type === 'ChainExpression') {
|
|
69
|
-
// $FlowFixMe[prop-missing]
|
|
70
|
-
return transformChainExpression(node.expression, node == null ? void 0 : node.comments);
|
|
71
|
-
} // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
|
|
72
|
-
// `ObjectTypeProperty` is a method or not (instead of using the `node.method` boolean). To correctly print
|
|
73
|
-
// the node when its not a method we need the start position to be different from the `node.value`s start
|
|
74
|
-
// position.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (node.type === 'ObjectTypeProperty') {
|
|
78
|
-
if (node.method === false && node.kind === 'init' && node.range[0] === 1 && node.value.range[0] === 1) {
|
|
79
|
-
// $FlowExpectedError[cannot-write]
|
|
80
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
81
|
-
node.value = { ...node.value,
|
|
82
|
-
range: [2, node.value.range[1]]
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return node;
|
|
87
|
-
} // Prettier currently relies on comparing the the start positions to know if the import/export specifier should have a
|
|
88
|
-
// rename (eg `Name` vs `Name as Name`) when the name is exactly the same
|
|
89
|
-
// So we need to ensure that the range is always the same to avoid the useless code printing
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (node.type === 'ImportSpecifier') {
|
|
93
|
-
if (node.local.name === node.imported.name) {
|
|
94
|
-
if (node.local.range == null) {
|
|
95
|
-
// for our TS-ast printing which has no locs
|
|
96
|
-
// $FlowExpectedError[cannot-write]
|
|
97
|
-
node.local.range = [0, 0];
|
|
98
|
-
} // $FlowExpectedError[cannot-write]
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
node.imported.range = [...node.local.range];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return node;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (node.type === 'ExportSpecifier') {
|
|
108
|
-
if (node.local.name === node.exported.name) {
|
|
109
|
-
if (node.local.range == null) {
|
|
110
|
-
// for our TS-ast printing which has no locs
|
|
111
|
-
// $FlowExpectedError[cannot-write]
|
|
112
|
-
node.local.range = [0, 0];
|
|
113
|
-
} // $FlowExpectedError[cannot-write]
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
node.exported.range = [...node.local.range];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return node;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return node;
|
|
123
|
-
},
|
|
124
|
-
|
|
125
|
-
visitorKeys
|
|
126
|
-
});
|
|
127
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.SimpleTransform = void 0;
|
|
16
|
-
|
|
17
|
-
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
18
|
-
|
|
19
|
-
var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
|
20
|
-
|
|
21
|
-
function setParentPointer(node, parent) {
|
|
22
|
-
if (parent != null) {
|
|
23
|
-
if (Array.isArray(node)) {
|
|
24
|
-
for (const item of node) {
|
|
25
|
-
// $FlowExpectedError[cannot-write]
|
|
26
|
-
item.parent = parent;
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
29
|
-
// $FlowExpectedError[cannot-write]
|
|
30
|
-
node.parent = parent;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* A simple class to recursively tranform AST trees.
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class SimpleTransform {
|
|
40
|
-
/**
|
|
41
|
-
* Transform the given AST tree.
|
|
42
|
-
* @param rootNode The root node to traverse.
|
|
43
|
-
* @param options The option object.
|
|
44
|
-
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
|
45
|
-
*/
|
|
46
|
-
transform(rootNode, options) {
|
|
47
|
-
let resultRootNode = rootNode;
|
|
48
|
-
|
|
49
|
-
_SimpleTraverser.SimpleTraverser.traverse(rootNode, {
|
|
50
|
-
enter: (node, parent) => {
|
|
51
|
-
// Ensure the parent pointers are correctly set before entering the node.
|
|
52
|
-
setParentPointer(node, parent);
|
|
53
|
-
const resultNode = options.transform(node);
|
|
54
|
-
|
|
55
|
-
if (resultNode !== node) {
|
|
56
|
-
let traversedResultNode = null;
|
|
57
|
-
|
|
58
|
-
if (resultNode != null) {
|
|
59
|
-
// Ensure the new node has the correct parent pointers before recursing again.
|
|
60
|
-
setParentPointer(resultNode, parent);
|
|
61
|
-
|
|
62
|
-
if (Array.isArray(resultNode)) {
|
|
63
|
-
traversedResultNode = resultNode.map(item => this.transform(item, options)).filter(item => item != null);
|
|
64
|
-
} else {
|
|
65
|
-
traversedResultNode = this.transform(resultNode, options);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (parent == null) {
|
|
70
|
-
if (node !== rootNode) {
|
|
71
|
-
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (Array.isArray(traversedResultNode)) {
|
|
75
|
-
throw new Error('SimpleTransform: invalid array result for root node');
|
|
76
|
-
} else {
|
|
77
|
-
resultRootNode = traversedResultNode;
|
|
78
|
-
}
|
|
79
|
-
} else if (traversedResultNode == null) {
|
|
80
|
-
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
|
81
|
-
} else {
|
|
82
|
-
(0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode, options.visitorKeys);
|
|
83
|
-
setParentPointer(traversedResultNode, parent);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
throw _SimpleTraverser.SimpleTraverser.Skip;
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
leave(_node) {},
|
|
91
|
-
|
|
92
|
-
visitorKeys: options.visitorKeys
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
return resultRootNode;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Transform the given AST tree.
|
|
99
|
-
* @param node The root node to traverse.
|
|
100
|
-
* @param options The option object.
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
static transform(node, options) {
|
|
105
|
-
return new SimpleTransform().transform(node, options);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
static transformProgram(program, options) {
|
|
109
|
-
const result = SimpleTransform.transform(program, options);
|
|
110
|
-
|
|
111
|
-
if ((result == null ? void 0 : result.type) === 'Program') {
|
|
112
|
-
return result;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
throw new Error('SimpleTransform.transformProgram: Expected program node.');
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Return a new AST node with the given properties overrided if needed.
|
|
119
|
-
*
|
|
120
|
-
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
|
121
|
-
* is important as its used to know if a node should be re-traversed.
|
|
122
|
-
*
|
|
123
|
-
* @param node The base AST node.
|
|
124
|
-
* @param overrideProps New properties to apply to the node.
|
|
125
|
-
* @return Either the orginal node if the properties matched the existing node or a new node with
|
|
126
|
-
* the new properties.
|
|
127
|
-
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
static nodeWith(node, overrideProps, visitorKeys) {
|
|
131
|
-
return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps, visitorKeys);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
exports.SimpleTransform = SimpleTransform;
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and 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
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
'use strict';
|
|
12
|
-
|
|
13
|
-
import type {VisitorKeysType} from '../traverse/getVisitorKeys';
|
|
14
|
-
import type {ESNode, Program} from 'hermes-estree';
|
|
15
|
-
|
|
16
|
-
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
|
17
|
-
import {
|
|
18
|
-
nodeWith,
|
|
19
|
-
removeNodeOnParent,
|
|
20
|
-
replaceNodeOnParent,
|
|
21
|
-
} from './astNodeMutationHelpers';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Transform callback
|
|
25
|
-
* @param node The node we are visiting
|
|
26
|
-
* @returns
|
|
27
|
-
* - return input node, signals no changes were made will continue to the next node.
|
|
28
|
-
* - return new node, the old node will be replaced in the AST. The new node and its
|
|
29
|
-
* children are then traversed.
|
|
30
|
-
* - return null, signals the node should be deleted from the AST.
|
|
31
|
-
*/
|
|
32
|
-
export type TransformCallback = (
|
|
33
|
-
node: ESNode,
|
|
34
|
-
) => ESNode | $ReadOnlyArray<ESNode> | null;
|
|
35
|
-
|
|
36
|
-
export type TransformOptions = $ReadOnly<{
|
|
37
|
-
/** The callback function which is called on entering each node. */
|
|
38
|
-
transform: TransformCallback,
|
|
39
|
-
|
|
40
|
-
/** The set of visitor keys to use for traversal. Defaults to the Flow ESTree visitor keys */
|
|
41
|
-
visitorKeys?: ?VisitorKeysType,
|
|
42
|
-
}>;
|
|
43
|
-
|
|
44
|
-
function setParentPointer(
|
|
45
|
-
node: ESNode | $ReadOnlyArray<ESNode>,
|
|
46
|
-
parent: ?ESNode,
|
|
47
|
-
): void {
|
|
48
|
-
if (parent != null) {
|
|
49
|
-
if (Array.isArray(node)) {
|
|
50
|
-
for (const item of node) {
|
|
51
|
-
// $FlowExpectedError[cannot-write]
|
|
52
|
-
item.parent = parent;
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
// $FlowExpectedError[cannot-write]
|
|
56
|
-
node.parent = parent;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A simple class to recursively tranform AST trees.
|
|
63
|
-
*/
|
|
64
|
-
export class SimpleTransform {
|
|
65
|
-
/**
|
|
66
|
-
* Transform the given AST tree.
|
|
67
|
-
* @param rootNode The root node to traverse.
|
|
68
|
-
* @param options The option object.
|
|
69
|
-
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
|
70
|
-
*/
|
|
71
|
-
transform(rootNode: ESNode, options: TransformOptions): ESNode | null {
|
|
72
|
-
let resultRootNode: ESNode | null = rootNode;
|
|
73
|
-
SimpleTraverser.traverse(rootNode, {
|
|
74
|
-
enter: (node: ESNode, parent: ?ESNode) => {
|
|
75
|
-
// Ensure the parent pointers are correctly set before entering the node.
|
|
76
|
-
setParentPointer(node, parent);
|
|
77
|
-
|
|
78
|
-
const resultNode: ESNode | $ReadOnlyArray<ESNode> | null =
|
|
79
|
-
options.transform(node);
|
|
80
|
-
if (resultNode !== node) {
|
|
81
|
-
let traversedResultNode: ESNode | $ReadOnlyArray<ESNode> | null =
|
|
82
|
-
null;
|
|
83
|
-
|
|
84
|
-
if (resultNode != null) {
|
|
85
|
-
// Ensure the new node has the correct parent pointers before recursing again.
|
|
86
|
-
setParentPointer(resultNode, parent);
|
|
87
|
-
|
|
88
|
-
if (Array.isArray(resultNode)) {
|
|
89
|
-
traversedResultNode = resultNode
|
|
90
|
-
.map(item => this.transform(item, options))
|
|
91
|
-
.filter(item => item != null);
|
|
92
|
-
} else {
|
|
93
|
-
traversedResultNode = this.transform(resultNode, options);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (parent == null) {
|
|
98
|
-
if (node !== rootNode) {
|
|
99
|
-
throw new Error(
|
|
100
|
-
'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
if (Array.isArray(traversedResultNode)) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
'SimpleTransform: invalid array result for root node',
|
|
106
|
-
);
|
|
107
|
-
} else {
|
|
108
|
-
resultRootNode = traversedResultNode;
|
|
109
|
-
}
|
|
110
|
-
} else if (traversedResultNode == null) {
|
|
111
|
-
removeNodeOnParent(node, parent, options.visitorKeys);
|
|
112
|
-
} else {
|
|
113
|
-
replaceNodeOnParent(
|
|
114
|
-
node,
|
|
115
|
-
parent,
|
|
116
|
-
traversedResultNode,
|
|
117
|
-
options.visitorKeys,
|
|
118
|
-
);
|
|
119
|
-
setParentPointer(traversedResultNode, parent);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
throw SimpleTraverser.Skip;
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
leave(_node: ESNode) {},
|
|
126
|
-
visitorKeys: options.visitorKeys,
|
|
127
|
-
});
|
|
128
|
-
return resultRootNode;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Transform the given AST tree.
|
|
133
|
-
* @param node The root node to traverse.
|
|
134
|
-
* @param options The option object.
|
|
135
|
-
*/
|
|
136
|
-
static transform(node: ESNode, options: TransformOptions): ESNode | null {
|
|
137
|
-
return new SimpleTransform().transform(node, options);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
static transformProgram(
|
|
141
|
-
program: Program,
|
|
142
|
-
options: TransformOptions,
|
|
143
|
-
): Program {
|
|
144
|
-
const result = SimpleTransform.transform(program, options);
|
|
145
|
-
if (result?.type === 'Program') {
|
|
146
|
-
return result;
|
|
147
|
-
}
|
|
148
|
-
throw new Error('SimpleTransform.transformProgram: Expected program node.');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Return a new AST node with the given properties overrided if needed.
|
|
153
|
-
*
|
|
154
|
-
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
|
155
|
-
* is important as its used to know if a node should be re-traversed.
|
|
156
|
-
*
|
|
157
|
-
* @param node The base AST node.
|
|
158
|
-
* @param overrideProps New properties to apply to the node.
|
|
159
|
-
* @return Either the orginal node if the properties matched the existing node or a new node with
|
|
160
|
-
* the new properties.
|
|
161
|
-
*/
|
|
162
|
-
static nodeWith<T: ESNode>(
|
|
163
|
-
node: T,
|
|
164
|
-
overrideProps: Partial<T>,
|
|
165
|
-
visitorKeys?: VisitorKeysType,
|
|
166
|
-
): T {
|
|
167
|
-
return nodeWith<T>(node, overrideProps, visitorKeys);
|
|
168
|
-
}
|
|
169
|
-
}
|