|
1 |
| -'use strict'; |
| 1 | +const OPTIONS = { |
| 2 | + comment: false, |
| 3 | + label: false, |
| 4 | + prefix: "__INLINE__", |
| 5 | + remove: true, |
| 6 | +}; |
2 | 7 |
|
3 | 8 | const argumentsInliningVisitor = {
|
4 | 9 | Identifier(path) {
|
5 |
| - for(let i = 0; i < this.params.length; i++) { |
| 10 | + for (let i = 0; i < this.params.length; i++) { |
6 | 11 | if (path.node.name === this.params[i].name) {
|
7 |
| - if(this.args[i]) { |
| 12 | + if (this.args[i]) { |
8 | 13 | path.replaceWith(this.args[i]);
|
| 14 | + path.skip(); // don't recurse |
9 | 15 | } else {
|
10 |
| - path.replaceWithSourceString('undefined'); |
| 16 | + path.replaceWithSourceString("undefined"); |
11 | 17 | }
|
12 | 18 | }
|
13 |
| - }; |
14 |
| - } |
15 |
| -} |
16 |
| -const inlineFnVisitor = { |
| 19 | + } |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +const inlineFunctionVisitor = { |
17 | 24 | 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); |
25 | 33 | }
|
26 |
| - } |
| 34 | + }, |
27 | 35 | };
|
28 | 36 |
|
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 | + |
30 | 72 | return {
|
31 | 73 | visitor: {
|
32 | 74 | 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) { |
36 | 111 | path.remove();
|
37 | 112 | }
|
38 |
| - } |
39 |
| - } |
| 113 | + }, |
| 114 | + }, |
40 | 115 | };
|
41 |
| -} |
| 116 | +}; |
0 commit comments