Skip to content

Commit

Permalink
Properly dodge import statements when inserting helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rpetrich committed May 25, 2019
1 parent 85c3f7c commit 58855e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions async-to-promises.ts
Expand Up @@ -2842,7 +2842,7 @@ export default function({ types, template, traverse, transformFromAst, version }
}

function insertHelper(programPath: NodePath<File>, 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);
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/helper names/hoisted.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/helper names/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58855e8

Please sign in to comment.