hermes-parser 0.28.1 → 0.29.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/HermesASTAdapter.js +1 -2
- package/dist/HermesASTAdapter.js.flow +2 -5
- package/dist/HermesParserNodeDeserializers.js +3 -2
- package/dist/HermesParserWASM.js +1 -1
- package/dist/HermesToESTreeAdapter.js +12 -0
- package/dist/HermesToESTreeAdapter.js.flow +10 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +21 -19
- package/dist/estree/StripComponentSyntax.js.flow +6 -5
- package/dist/estree/TransformMatchSyntax.js +4 -15
- package/dist/estree/TransformMatchSyntax.js.flow +18 -19
- package/dist/generated/ESTreeVisitorKeys.js +1 -1
- package/dist/generated/ParserVisitorKeys.js +1 -1
- package/dist/index.js.flow +1 -1
- package/dist/src/HermesASTAdapter.js +192 -0
- package/dist/src/HermesParser.js +108 -0
- package/dist/src/HermesParserDecodeUTF8String.js +68 -0
- package/dist/src/HermesParserDeserializer.js +243 -0
- package/dist/src/HermesParserNodeDeserializers.js +2473 -0
- package/dist/src/HermesToESTreeAdapter.js +453 -0
- package/dist/src/ParserOptions.js +18 -0
- package/dist/src/babel/TransformESTreeToBabel.js +1104 -0
- package/dist/src/estree/StripComponentSyntax.js +788 -0
- package/dist/src/estree/StripFlowTypes.js +175 -0
- package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/src/estree/TransformMatchSyntax.js +1005 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
- package/dist/src/generated/ParserVisitorKeys.js +790 -0
- package/dist/src/getModuleDocblock.js +112 -0
- package/dist/src/index.js +153 -0
- package/dist/src/transform/SimpleTransform.js +120 -0
- package/dist/src/transform/astArrayMutationHelpers.js +62 -0
- package/dist/src/transform/astNodeMutationHelpers.js +195 -0
- package/dist/src/traverse/SimpleTraverser.js +137 -0
- package/dist/src/traverse/getVisitorKeys.js +37 -0
- package/dist/src/utils/Builders.js +191 -0
- package/dist/src/utils/GenID.js +41 -0
- package/dist/src/utils/createSyntaxError.js +25 -0
- package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/utils/Builders.js +17 -0
- package/dist/utils/Builders.js.flow +20 -0
- package/package.json +2 -2
|
@@ -0,0 +1,112 @@
|
|
|
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.getModuleDocblock = getModuleDocblock;
|
|
16
|
+
exports.parseDocblockString = parseDocblockString;
|
|
17
|
+
const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
|
|
18
|
+
|
|
19
|
+
function parseDocblockString(docblock) {
|
|
20
|
+
const directiveLines = docblock.split('\n') // remove the leading " *" from each line
|
|
21
|
+
.map(line => line.trimStart().replace(/^\* ?/, '').trim()).filter(line => line.startsWith('@'));
|
|
22
|
+
const directives = // $FlowExpectedError[incompatible-type] - flow types this return as {...}
|
|
23
|
+
Object.create(null);
|
|
24
|
+
|
|
25
|
+
for (const line of directiveLines) {
|
|
26
|
+
var _match$;
|
|
27
|
+
|
|
28
|
+
const match = DIRECTIVE_REGEX.exec(line);
|
|
29
|
+
|
|
30
|
+
if (match == null) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const name = match[1]; // explicitly use an empty string if there's no value
|
|
35
|
+
// this way the array length tracks how many instances of the directive there was
|
|
36
|
+
|
|
37
|
+
const value = ((_match$ = match[2]) != null ? _match$ : '').trim();
|
|
38
|
+
|
|
39
|
+
if (directives[name]) {
|
|
40
|
+
directives[name].push(value);
|
|
41
|
+
} else {
|
|
42
|
+
directives[name] = [value];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return directives;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getModuleDocblock(hermesProgram) {
|
|
50
|
+
const docblockNode = (() => {
|
|
51
|
+
if (hermesProgram.type !== 'Program') {
|
|
52
|
+
return null;
|
|
53
|
+
} // $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const program = hermesProgram;
|
|
57
|
+
|
|
58
|
+
if (program.comments.length === 0) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const firstComment = (() => {
|
|
63
|
+
const first = program.comments[0];
|
|
64
|
+
|
|
65
|
+
if (first.type === 'Block') {
|
|
66
|
+
return first;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (program.comments.length === 1) {
|
|
70
|
+
return null;
|
|
71
|
+
} // ESLint will always strip out the shebang comment from the code before passing it to the parser
|
|
72
|
+
// https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
|
|
73
|
+
// this means that we're forced to parse it as a line comment :(
|
|
74
|
+
// this hacks around it by selecting the second comment in this case
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const second = program.comments[1];
|
|
78
|
+
|
|
79
|
+
if (first.type === 'Line' && first.range[0] === 0 && second.type === 'Block') {
|
|
80
|
+
return second;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
})();
|
|
85
|
+
|
|
86
|
+
if (firstComment == null) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/*
|
|
90
|
+
Handle cases like this where the comment isn't actually the first thing in the code:
|
|
91
|
+
```
|
|
92
|
+
const x = 1; /* docblock *./
|
|
93
|
+
```
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if (program.body.length > 0 && program.body[0].range[0] < firstComment.range[0]) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return firstComment;
|
|
102
|
+
})();
|
|
103
|
+
|
|
104
|
+
if (docblockNode == null) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
directives: parseDocblockString(docblockNode.value),
|
|
110
|
+
comment: docblockNode
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
var _exportNames = {
|
|
16
|
+
parse: true,
|
|
17
|
+
mutateESTreeASTForPrettier: true,
|
|
18
|
+
Transforms: true,
|
|
19
|
+
FlowVisitorKeys: true,
|
|
20
|
+
astArrayMutationHelpers: true,
|
|
21
|
+
astNodeMutationHelpers: true
|
|
22
|
+
};
|
|
23
|
+
exports.mutateESTreeASTForPrettier = exports.astNodeMutationHelpers = exports.astArrayMutationHelpers = exports.Transforms = void 0;
|
|
24
|
+
exports.parse = parse;
|
|
25
|
+
|
|
26
|
+
var HermesParser = _interopRequireWildcard(require("./HermesParser"));
|
|
27
|
+
|
|
28
|
+
var _HermesToESTreeAdapter = _interopRequireDefault(require("./HermesToESTreeAdapter"));
|
|
29
|
+
|
|
30
|
+
var _ESTreeVisitorKeys = _interopRequireDefault(require("./generated/ESTreeVisitorKeys"));
|
|
31
|
+
|
|
32
|
+
exports.FlowVisitorKeys = _ESTreeVisitorKeys.default;
|
|
33
|
+
|
|
34
|
+
var StripComponentSyntax = _interopRequireWildcard(require("./estree/StripComponentSyntax"));
|
|
35
|
+
|
|
36
|
+
var TransformMatchSyntax = _interopRequireWildcard(require("./estree/TransformMatchSyntax"));
|
|
37
|
+
|
|
38
|
+
var StripFlowTypesForBabel = _interopRequireWildcard(require("./estree/StripFlowTypesForBabel"));
|
|
39
|
+
|
|
40
|
+
var TransformESTreeToBabel = _interopRequireWildcard(require("./babel/TransformESTreeToBabel"));
|
|
41
|
+
|
|
42
|
+
var StripFlowTypes = _interopRequireWildcard(require("./estree/StripFlowTypes"));
|
|
43
|
+
|
|
44
|
+
var _ParserOptions = require("./ParserOptions");
|
|
45
|
+
|
|
46
|
+
Object.keys(_ParserOptions).forEach(function (key) {
|
|
47
|
+
if (key === "default" || key === "__esModule") return;
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
49
|
+
if (key in exports && exports[key] === _ParserOptions[key]) return;
|
|
50
|
+
exports[key] = _ParserOptions[key];
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
var _SimpleTraverser = require("./traverse/SimpleTraverser");
|
|
54
|
+
|
|
55
|
+
Object.keys(_SimpleTraverser).forEach(function (key) {
|
|
56
|
+
if (key === "default" || key === "__esModule") return;
|
|
57
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
58
|
+
if (key in exports && exports[key] === _SimpleTraverser[key]) return;
|
|
59
|
+
exports[key] = _SimpleTraverser[key];
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
var _SimpleTransform = require("./transform/SimpleTransform");
|
|
63
|
+
|
|
64
|
+
Object.keys(_SimpleTransform).forEach(function (key) {
|
|
65
|
+
if (key === "default" || key === "__esModule") return;
|
|
66
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
67
|
+
if (key in exports && exports[key] === _SimpleTransform[key]) return;
|
|
68
|
+
exports[key] = _SimpleTransform[key];
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
var _getVisitorKeys = require("./traverse/getVisitorKeys");
|
|
72
|
+
|
|
73
|
+
Object.keys(_getVisitorKeys).forEach(function (key) {
|
|
74
|
+
if (key === "default" || key === "__esModule") return;
|
|
75
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
76
|
+
if (key in exports && exports[key] === _getVisitorKeys[key]) return;
|
|
77
|
+
exports[key] = _getVisitorKeys[key];
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
var _astArrayMutationHelpers = _interopRequireWildcard(require("./transform/astArrayMutationHelpers"));
|
|
81
|
+
|
|
82
|
+
exports.astArrayMutationHelpers = _astArrayMutationHelpers;
|
|
83
|
+
|
|
84
|
+
var _astNodeMutationHelpers = _interopRequireWildcard(require("./transform/astNodeMutationHelpers"));
|
|
85
|
+
|
|
86
|
+
exports.astNodeMutationHelpers = _astNodeMutationHelpers;
|
|
87
|
+
|
|
88
|
+
var _mutateESTreeASTForPrettier = _interopRequireDefault(require("./utils/mutateESTreeASTForPrettier"));
|
|
89
|
+
|
|
90
|
+
exports.mutateESTreeASTForPrettier = _mutateESTreeASTForPrettier.default;
|
|
91
|
+
|
|
92
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
93
|
+
|
|
94
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
95
|
+
|
|
96
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
97
|
+
|
|
98
|
+
const DEFAULTS = {
|
|
99
|
+
flow: 'detect'
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
function getOptions(options = { ...DEFAULTS
|
|
103
|
+
}) {
|
|
104
|
+
// Default to detecting whether to parse Flow syntax by the presence
|
|
105
|
+
// of an pragma.
|
|
106
|
+
if (options.flow == null) {
|
|
107
|
+
options.flow = DEFAULTS.flow;
|
|
108
|
+
} else if (options.flow !== 'all' && options.flow !== 'detect') {
|
|
109
|
+
throw new Error('flow option must be "all" or "detect"');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (options.sourceType === 'unambiguous') {
|
|
113
|
+
// Clear source type so that it will be detected from the contents of the file
|
|
114
|
+
delete options.sourceType;
|
|
115
|
+
} else if (options.sourceType != null && options.sourceType !== 'script' && options.sourceType !== 'module') {
|
|
116
|
+
throw new Error('sourceType option must be "script", "module", or "unambiguous" if set');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (options.enableExperimentalComponentSyntax == null) {
|
|
120
|
+
options.enableExperimentalComponentSyntax = true; // Enable by default
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (options.enableExperimentalFlowMatchSyntax == null) {
|
|
124
|
+
options.enableExperimentalFlowMatchSyntax = true; // Enable by default
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
options.tokens = options.tokens === true;
|
|
128
|
+
options.allowReturnOutsideFunction = options.allowReturnOutsideFunction === true;
|
|
129
|
+
return options;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// eslint-disable-next-line no-redeclare
|
|
133
|
+
function parse(code, opts) {
|
|
134
|
+
const options = getOptions(opts);
|
|
135
|
+
const ast = HermesParser.parse(code, options);
|
|
136
|
+
const estreeAdapter = new _HermesToESTreeAdapter.default(options, code);
|
|
137
|
+
const estreeAST = estreeAdapter.transform(ast);
|
|
138
|
+
|
|
139
|
+
if (options.babel !== true) {
|
|
140
|
+
return estreeAST;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const loweredESTreeAST = [TransformMatchSyntax.transformProgram, StripComponentSyntax.transformProgram, StripFlowTypesForBabel.transformProgram].reduce((ast, transform) => transform(ast, options), estreeAST);
|
|
144
|
+
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const Transforms = {
|
|
148
|
+
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
149
|
+
stripComponentSyntax: StripComponentSyntax.transformProgram,
|
|
150
|
+
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
151
|
+
stripFlowTypes: StripFlowTypes.transformProgram
|
|
152
|
+
};
|
|
153
|
+
exports.Transforms = Transforms;
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
// $FlowExpectedError[cannot-write]
|
|
24
|
+
node.parent = parent;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A simple class to recursively tranform AST trees.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SimpleTransform {
|
|
33
|
+
/**
|
|
34
|
+
* Transform the given AST tree.
|
|
35
|
+
* @param rootNode The root node to traverse.
|
|
36
|
+
* @param options The option object.
|
|
37
|
+
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
|
38
|
+
*/
|
|
39
|
+
transform(rootNode, options) {
|
|
40
|
+
let resultRootNode = rootNode;
|
|
41
|
+
|
|
42
|
+
_SimpleTraverser.SimpleTraverser.traverse(rootNode, {
|
|
43
|
+
enter: (node, parent) => {
|
|
44
|
+
// Ensure the parent pointers are correctly set before entering the node.
|
|
45
|
+
setParentPointer(node, parent);
|
|
46
|
+
const resultNode = options.transform(node);
|
|
47
|
+
|
|
48
|
+
if (resultNode !== node) {
|
|
49
|
+
let traversedResultNode = null;
|
|
50
|
+
|
|
51
|
+
if (resultNode != null) {
|
|
52
|
+
// Ensure the new node has the correct parent pointers before recursing again.
|
|
53
|
+
setParentPointer(resultNode, parent);
|
|
54
|
+
traversedResultNode = this.transform(resultNode, options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (parent == null) {
|
|
58
|
+
if (node !== rootNode) {
|
|
59
|
+
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
resultRootNode = traversedResultNode;
|
|
63
|
+
} else if (traversedResultNode == null) {
|
|
64
|
+
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
|
65
|
+
} else {
|
|
66
|
+
(0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode, options.visitorKeys);
|
|
67
|
+
setParentPointer(traversedResultNode, parent);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
throw _SimpleTraverser.SimpleTraverser.Skip;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
leave(_node) {},
|
|
75
|
+
|
|
76
|
+
visitorKeys: options.visitorKeys
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return resultRootNode;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Transform the given AST tree.
|
|
83
|
+
* @param node The root node to traverse.
|
|
84
|
+
* @param options The option object.
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
static transform(node, options) {
|
|
89
|
+
return new SimpleTransform().transform(node, options);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static transformProgram(program, options) {
|
|
93
|
+
const result = SimpleTransform.transform(program, options);
|
|
94
|
+
|
|
95
|
+
if ((result == null ? void 0 : result.type) === 'Program') {
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
throw new Error('SimpleTransform.transformProgram: Expected program node.');
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Return a new AST node with the given properties overrided if needed.
|
|
103
|
+
*
|
|
104
|
+
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
|
105
|
+
* is important as its used to know if a node should be re-traversed.
|
|
106
|
+
*
|
|
107
|
+
* @param node The base AST node.
|
|
108
|
+
* @param overrideProps New properties to apply to the node.
|
|
109
|
+
* @return Either the orginal node if the properties matched the existing node or a new node with
|
|
110
|
+
* the new properties.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
static nodeWith(node, overrideProps, visitorKeys) {
|
|
115
|
+
return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps, visitorKeys);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.SimpleTransform = SimpleTransform;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.arrayIsEqual = arrayIsEqual;
|
|
7
|
+
exports.insertInArray = insertInArray;
|
|
8
|
+
exports.removeFromArray = removeFromArray;
|
|
9
|
+
exports.replaceInArray = replaceInArray;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
+
*
|
|
14
|
+
* This source code is licensed under the MIT license found in the
|
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
* @format
|
|
19
|
+
*/
|
|
20
|
+
function assertArrayBounds(array, index) {
|
|
21
|
+
if (index < 0 || index >= array.length) {
|
|
22
|
+
throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function arrayIsEqual(a1, a2) {
|
|
27
|
+
if (a1 === a2) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (a1.length !== a2.length) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < a1.length; i++) {
|
|
36
|
+
if (a1[i] !== a2[i]) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function insertInArray(array, index, elements) {
|
|
45
|
+
if (index === array.length) {
|
|
46
|
+
// Support the insert at end of array case.
|
|
47
|
+
return array.concat(elements);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
assertArrayBounds(array, index);
|
|
51
|
+
return array.slice(0, index).concat(elements).concat(array.slice(index));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function removeFromArray(array, index) {
|
|
55
|
+
assertArrayBounds(array, index);
|
|
56
|
+
return [...array.slice(0, index), ...array.slice(index + 1)];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function replaceInArray(array, index, elements) {
|
|
60
|
+
assertArrayBounds(array, index);
|
|
61
|
+
return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
|
|
62
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
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.deepCloneNode = deepCloneNode;
|
|
16
|
+
exports.nodeWith = nodeWith;
|
|
17
|
+
exports.removeNodeOnParent = removeNodeOnParent;
|
|
18
|
+
exports.replaceNodeOnParent = replaceNodeOnParent;
|
|
19
|
+
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
|
20
|
+
exports.shallowCloneNode = shallowCloneNode;
|
|
21
|
+
exports.updateAllParentPointers = updateAllParentPointers;
|
|
22
|
+
|
|
23
|
+
var _astArrayMutationHelpers = require("./astArrayMutationHelpers");
|
|
24
|
+
|
|
25
|
+
var _getVisitorKeys = require("../traverse/getVisitorKeys");
|
|
26
|
+
|
|
27
|
+
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
28
|
+
|
|
29
|
+
function getParentKey(target, parent, visitorKeys) {
|
|
30
|
+
if (parent == null) {
|
|
31
|
+
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
35
|
+
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
|
+
parent[key])) {
|
|
37
|
+
if (parent[key] === target) {
|
|
38
|
+
return {
|
|
39
|
+
type: 'single',
|
|
40
|
+
node: parent,
|
|
41
|
+
key
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
} else if (Array.isArray(parent[key])) {
|
|
45
|
+
for (let i = 0; i < parent[key].length; i += 1) {
|
|
46
|
+
// $FlowExpectedError[invalid-tuple-index]
|
|
47
|
+
const current = parent[key][i];
|
|
48
|
+
|
|
49
|
+
if (current === target) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'array',
|
|
52
|
+
node: parent,
|
|
53
|
+
key,
|
|
54
|
+
targetIndex: i
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} // this shouldn't happen ever
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Replace a node with a new node within an AST (via the parent pointer).
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith, visitorKeys) {
|
|
70
|
+
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
71
|
+
const parent = replacementParent.node;
|
|
72
|
+
|
|
73
|
+
if (replacementParent.type === 'array') {
|
|
74
|
+
// $FlowExpectedError[prop-missing]
|
|
75
|
+
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
76
|
+
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
|
77
|
+
} else {
|
|
78
|
+
// $FlowExpectedError[prop-missing]
|
|
79
|
+
parent[replacementParent.key] = nodeToReplaceWith;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Remove a node from the AST its connected to (via the parent pointer).
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
function removeNodeOnParent(originalNode, originalNodeParent, visitorKeys) {
|
|
88
|
+
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
89
|
+
const parent = replacementParent.node;
|
|
90
|
+
|
|
91
|
+
if (replacementParent.type === 'array') {
|
|
92
|
+
// $FlowExpectedError[prop-missing]
|
|
93
|
+
parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)( // $FlowExpectedError[prop-missing]
|
|
94
|
+
parent[replacementParent.key], replacementParent.targetIndex);
|
|
95
|
+
} else {
|
|
96
|
+
// $FlowExpectedError[prop-missing]
|
|
97
|
+
parent[replacementParent.key] = null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Corrects the parent pointers in direct children of the given node.
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
function setParentPointersInDirectChildren(node, visitorKeys) {
|
|
106
|
+
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node, visitorKeys)) {
|
|
107
|
+
if ((0, _getVisitorKeys.isNode)(node[key])) {
|
|
108
|
+
// $FlowExpectedError[cannot-write]
|
|
109
|
+
node[key].parent = node;
|
|
110
|
+
} else if (Array.isArray(node[key])) {
|
|
111
|
+
for (const child of node[key]) {
|
|
112
|
+
child.parent = node;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
function updateAllParentPointers(node, visitorKeys) {
|
|
123
|
+
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
|
124
|
+
enter(node, parent) {
|
|
125
|
+
// $FlowExpectedError[cannot-write]
|
|
126
|
+
node.parent = parent;
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
leave() {},
|
|
130
|
+
|
|
131
|
+
visitorKeys
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Clone node and add new props.
|
|
136
|
+
*
|
|
137
|
+
* This will only create a new object if the overrides actually result in a change.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
function nodeWith(node, overrideProps, visitorKeys) {
|
|
142
|
+
// Check if this will actually result in a change, maintaining referential equality is important.
|
|
143
|
+
const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => {
|
|
144
|
+
const node_ = node;
|
|
145
|
+
|
|
146
|
+
if (Array.isArray(value)) {
|
|
147
|
+
return Array.isArray(node_[key]) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node_[key], value) : false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return node_[key] === value;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (willBeUnchanged) {
|
|
154
|
+
return node;
|
|
155
|
+
} // Create new node.
|
|
156
|
+
// $FlowExpectedError[cannot-spread-interface]
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
const newNode = { ...node,
|
|
160
|
+
...overrideProps
|
|
161
|
+
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
162
|
+
|
|
163
|
+
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
164
|
+
return newNode;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Shallow clones node, providing a new reference for an existing node.
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
function shallowCloneNode(node, visitorKeys) {
|
|
172
|
+
// $FlowExpectedError[cannot-spread-interface]
|
|
173
|
+
const newNode = { ...node
|
|
174
|
+
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
175
|
+
|
|
176
|
+
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
177
|
+
return newNode;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Deeply clones node and its entire tree.
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
function deepCloneNode(node, visitorKeys) {
|
|
185
|
+
const clone = JSON.parse(JSON.stringify(node, (key, value) => {
|
|
186
|
+
// null out parent pointers
|
|
187
|
+
if (key === 'parent') {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return value;
|
|
192
|
+
}));
|
|
193
|
+
updateAllParentPointers(clone, visitorKeys);
|
|
194
|
+
return clone;
|
|
195
|
+
}
|