-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (39 loc) · 1.05 KB
/
index.js
File metadata and controls
41 lines (39 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const argumentsInliningVisitor = {
Identifier(path) {
for(let i = 0; i < this.params.length; i++) {
if (path.node.name === this.params[i].name) {
if(this.args[i]) {
path.replaceWith(this.args[i]);
} else {
path.replaceWithSourceString('undefined');
}
}
};
}
}
const inlineFnVisitor = {
CallExpression(path) {
if (path.node.callee.name === this.fn.id.name) {
const params = this.fn.params;
const args = path.node.arguments;
// console.log(this.fn);
path.replaceWith(this.opts.types.cloneDeep(this.fn.body));
path.traverse(argumentsInliningVisitor, {params, args});
path.replaceWith(path.node.callee.body.body[0].body[0].argument);
}
}
};
module.exports = function (opts) {
return {
visitor: {
FunctionDeclaration(path) {
if (path.node.id.name.startsWith('__INLINE__')) {
const fn = path.node
path.parentPath.traverse(inlineFnVisitor, {fn, opts});
path.remove();
}
}
}
};
}