Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
astexplorer authored Jun 29, 2021
1 parent 5f3a7d8 commit d2e848c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function transformer(file, { jscodeshift: j }) {
const { isFnNode, hasCallback, awaitFn, filterImmediateFns, removeWrapperFn, convertParentFnAsync, createBlockStatement } = getFns(j);
const { isFnNode, hasCallback, awaitFn, filterImmediateFns, removeWrapperFn, convertParentFnAsync, createBlockStatement, removeWrappingParenthesis } = getFns(j);

return j(file.source)
.find(j.CallExpression, {
Expand All @@ -16,6 +16,7 @@ export default function transformer(file, { jscodeshift: j }) {
.find(j.CallExpression)
.filter(hasCallback)
.replaceWith(awaitFn({ tryCatch: false }));
return;
wfFns.find(j.FunctionExpression).filter(filterImmediateFns).replaceWith(removeWrapperFn);
wfFns.find(j.ArrowFunctionExpression).filter(filterImmediateFns).replaceWith(removeWrapperFn);
j(wf)
Expand All @@ -29,10 +30,8 @@ export default function transformer(file, { jscodeshift: j }) {
});

// remove the async.waterfall() contents from block statement
// and
const wfParentBody = wf.parent.parent.node.body;
const asyncWfIndex = wfParentBody.indexOf(wf.parent.node);
wfParentBody.splice(asyncWfIndex, 1, ...wf.node.body);
// and insert it in parent's body
removeWrappingParenthesis(wf, wf.node.body);
})
.toSource();
}
Expand Down Expand Up @@ -72,6 +71,15 @@ function getFns(j) {
return parent;
};

const removeWrappingParenthesis = (p, replacementContents) => {
const parentNode = p.parent.node;
const grandparentNodeBody = p.parent.parent.node.body;
const parentNodePosition = grandparentNodeBody.indexOf(parentNode);

// remove parentNode and replace it with `removeWrappingParenthesis`
grandparentNodeBody.splice(parentNodePosition, 1, ...replacementContents);
};

const awaitFn = ({ tryCatch }) => (p) => {
// ensure parent function is converted to an `await` fn
const parent = convertParentFnAsync(p);
Expand Down Expand Up @@ -125,7 +133,7 @@ function getFns(j) {
// there is no way to do it so attaching it to parent fn's body
if (tryCatch) return tryStatement;
else {
parent.node.body = tryContents;
removeWrappingParenthesis(p, tryContents.body);
}
};

Expand Down Expand Up @@ -161,6 +169,7 @@ function getFns(j) {
createBlockStatement,
filterImmediateFns,
removeWrapperFn,
convertParentFnAsync
convertParentFnAsync,
removeWrappingParenthesis
};
}

0 comments on commit d2e848c

Please sign in to comment.