diff --git a/async-to-promises.ts b/async-to-promises.ts index 6d258dd..c2e977f 100644 --- a/async-to-promises.ts +++ b/async-to-promises.ts @@ -2842,7 +2842,7 @@ export default function({ types, template, traverse, transformFromAst, version } } function insertHelper(programPath: NodePath, value: Node): NodePath { - const destinationPath = programPath.get("body").find((path: NodePath) => !path.node._isHelperDefinition)!; + const destinationPath = programPath.get("body").find((path: NodePath) => !path.node._isHelperDefinition && !path.isImportDeclaration())!; if (destinationPath.isVariableDeclaration()) { const before = destinationPath.get("declarations").filter((path: NodePath) => path.node._isHelperDefinition); const after = destinationPath.get("declarations").filter((path: NodePath) => !path.node._isHelperDefinition); @@ -2884,8 +2884,10 @@ export default function({ types, template, traverse, transformFromAst, version } } else { value._isHelperDefinition = true; } - destinationPath.insertBefore(value); - return getPreviousSibling(destinationPath)!; + const oldNode = destinationPath.node; + destinationPath.replaceWith(value); + destinationPath.insertAfter(oldNode); + return destinationPath; } } @@ -2943,8 +2945,7 @@ export default function({ types, template, traverse, transformFromAst, version } } // Insert the new node const value = cloneNode(helper.value) as typeof helper.value; - const destinationPath = file.path.get("body").find((path: NodePath) => !path.node._isHelperDefinition)!; - const newPath = insertHelper(destinationPath, value); + const newPath = insertHelper(file.path, value); // Rename references to other helpers due to name conflicts newPath.traverse({ Identifier(path) { diff --git a/tests/helper names/hoisted.js b/tests/helper names/hoisted.js index 38f96eb..e010b19 100644 --- a/tests/helper names/hoisted.js +++ b/tests/helper names/hoisted.js @@ -1 +1 @@ -function _async3(f){return function(){for(var args=[],i=0;i_await2(_async(0),(_async2)=>_async2&&_await())) \ No newline at end of file +function _await2(value,then,direct){if(direct){return then?then(value):value;}if(!value||!value.then){value=Promise.resolve(value);}return then?value.then(then):value;}function _async3(f){return function(){for(var args=[],i=0;i_await2(_async(0),(_async2)=>_async2&&_await())) \ No newline at end of file