Skip to content

Commit

Permalink
Make each reference to a helper function a unique node so that babel …
Browse files Browse the repository at this point in the history
…7's module import rewriting visitor won't skip rewriting some helpers
  • Loading branch information
rpetrich committed Mar 24, 2019
1 parent 1c7a154 commit 0d10ebc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions async-to-promises.ts
Expand Up @@ -139,6 +139,14 @@ export default function({ types, template, traverse, transformFromAst, version }

const isNewBabel = !/^6\./.test(version);

function cloneNode<T extends Node>(node: T): T {
const result = (types as any).cloneDeep(node) as T;
if ((node.type == "Identifier" || node.type == "MemberExpression") && node.hasOwnProperty("_helperName")) {
(result as any as Identifier)._helperName = (node as any as Identifier)._helperName;
}
return result;
}

// Helper to wrap a node in a statement so it can be used by functions that require a statement
function wrapNodeInStatement(node: Node): Statement {
if (types.isStatement(node)) {
Expand Down Expand Up @@ -516,7 +524,7 @@ export default function({ types, template, traverse, transformFromAst, version }
// Check if an expression is a function that returns undefined and has no side effects or is a reference to the _empty helper
function isEmptyContinuation(continuation: Expression, path: NodePath): boolean {
if (types.isIdentifier(continuation)) {
return continuation === path.hub.file.declarations["_empty"];
return continuation._helperName === "_empty";
}
if (isContinuation(continuation)) {
const body = continuation.body;
Expand Down Expand Up @@ -2645,7 +2653,9 @@ export default function({ types, template, traverse, transformFromAst, version }
function helperReference(state: PluginState, path: NodePath, name: string): Identifier {
const file = path.scope.hub.file;
let result = file.declarations[name];
if (!result) {
if (result) {
result = cloneNode(result);
} else {
result = file.declarations[name] = usesIdentifier(file.path, name) ? file.path.scope.generateUidIdentifier(name) : types.identifier(name);
result._helperName = name;
if (readConfigKey(state.opts, "externalHelpers")) {
Expand Down Expand Up @@ -2691,7 +2701,7 @@ export default function({ types, template, traverse, transformFromAst, version }
for (const dependency of helper.dependencies) {
helperReference(state, path, dependency);
}
const value = (types as any).cloneDeep(helper.value) as typeof helper.value;
const value = cloneNode(helper.value) as typeof helper.value;
let traversePath = file.path.get("body")[0];
if (types.isVariableDeclaration(value) && traversePath.isVariableDeclaration()) {
// TODO: Support variable declaration that references another variable declaration (this case doesn't exist yet in our helpers, but may in the future)
Expand Down

0 comments on commit 0d10ebc

Please sign in to comment.