Skip to content

Commit e0ba851

Browse files
committed
v1.0.4 repo/tests with the new plugin:
emilecantin#2
1 parent 1c6c186 commit e0ba851

File tree

1 file changed

+98
-23
lines changed

1 file changed

+98
-23
lines changed

index.js

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,116 @@
1-
'use strict';
1+
const OPTIONS = {
2+
comment: false,
3+
label: false,
4+
prefix: "__INLINE__",
5+
remove: true,
6+
};
27

38
const argumentsInliningVisitor = {
49
Identifier(path) {
5-
for(let i = 0; i < this.params.length; i++) {
10+
for (let i = 0; i < this.params.length; i++) {
611
if (path.node.name === this.params[i].name) {
7-
if(this.args[i]) {
12+
if (this.args[i]) {
813
path.replaceWith(this.args[i]);
14+
path.skip(); // don't recurse
915
} else {
10-
path.replaceWithSourceString('undefined');
16+
path.replaceWithSourceString("undefined");
1117
}
1218
}
13-
};
14-
}
15-
}
16-
const inlineFnVisitor = {
19+
}
20+
},
21+
};
22+
23+
const inlineFunctionVisitor = {
1724
CallExpression(path) {
18-
if (path.node.callee.name === this.fn.id.name) {
19-
const params = this.fn.params;
20-
const args = path.node.arguments;
21-
// console.log(this.fn);
22-
path.replaceWith(this.opts.types.cloneDeep(this.fn.body));
23-
path.traverse(argumentsInliningVisitor, {params, args});
24-
path.replaceWith(path.node.callee.body.body[0].body[0].argument);
25+
if (path.node.callee.name === this.name) {
26+
const { params } = this;
27+
const args = path.node.arguments; // grab these before we replace the node
28+
const returnStatement = this.types.cloneDeep(this.returnStatement);
29+
30+
path.replaceWith(returnStatement);
31+
path.traverse(argumentsInliningVisitor, { args, params });
32+
path.replaceWith(returnStatement.argument);
2533
}
26-
}
34+
},
2735
};
2836

29-
module.exports = function (opts) {
37+
function findComment(node, want) {
38+
const comments = node.leadingComments || [];
39+
40+
for (let i = comments.length - 1; i >= 0; --i) {
41+
const comment = comments[i];
42+
43+
if (comment.type !== "CommentBlock") {
44+
break;
45+
}
46+
47+
if (comment.value.trim() === want) {
48+
return `leadingComments.${i}`;
49+
}
50+
}
51+
}
52+
53+
function hasSingleStatement(node) {
54+
return node.body.body.length === 1;
55+
}
56+
57+
function matchLabel(types, statement, label) {
58+
return types.isLabeledStatement(statement) && statement.label.name === label;
59+
}
60+
61+
module.exports = function ({ types }, options) {
62+
const { comment, label, prefix, remove } = Object.assign(
63+
{},
64+
OPTIONS,
65+
options
66+
);
67+
68+
if (!(comment || label || prefix)) {
69+
return {};
70+
}
71+
3072
return {
3173
visitor: {
3274
FunctionDeclaration(path) {
33-
if (path.node.id.name.startsWith('__INLINE__')) {
34-
const fn = path.node
35-
path.parentPath.traverse(inlineFnVisitor, {fn, opts});
75+
const { node } = path;
76+
77+
let returnStatement;
78+
79+
if (hasSingleStatement(node)) {
80+
returnStatement = node.body.body[0];
81+
} else {
82+
return;
83+
}
84+
85+
const { name } = node.id;
86+
87+
let commentPath;
88+
89+
if (prefix && name.startsWith(prefix)) {
90+
// do nothing
91+
} else if (label && matchLabel(types, returnStatement, label)) {
92+
returnStatement = returnStatement.body;
93+
} else if (comment && (commentPath = findComment(node, comment))) {
94+
if (remove) {
95+
// remove the comment so it doesn't get attached to the
96+
// next declaration
97+
path.get(commentPath).remove();
98+
}
99+
} else {
100+
return;
101+
}
102+
103+
path.parentPath.traverse(inlineFunctionVisitor, {
104+
name,
105+
params: node.params,
106+
returnStatement,
107+
types,
108+
});
109+
110+
if (remove) {
36111
path.remove();
37112
}
38-
}
39-
}
113+
},
114+
},
40115
};
41-
}
116+
};

0 commit comments

Comments
 (0)