Skip to content

Commit

Permalink
Reregister relocated declarations to avoid warnings in later versions…
Browse files Browse the repository at this point in the history
… of Babel (closes #38)
  • Loading branch information
rpetrich committed Oct 19, 2019
1 parent dc89730 commit 3628002
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions async-to-promises.ts
Expand Up @@ -1337,7 +1337,7 @@ export default function({
this.originalScope.removeBinding(declarator.id.name);
}
}
path.scope.registerDeclaration(path);
reregisterDeclarations(path);
},
Function(path) {
path.skip();
Expand Down Expand Up @@ -1570,7 +1570,7 @@ export default function({
}
// Insert any new variable declarators the await call needed
if (replacement.declarators.length) {
target.insertBefore(types.variableDeclaration("const", replacement.declarators));
reregisterDeclarations(target.insertBefore(types.variableDeclaration("const", replacement.declarators)));
}
// Hoist the call arguments if configured to do so
if (readConfigKey(generatorState.state.opts, "hoist")) {
Expand Down Expand Up @@ -1800,10 +1800,8 @@ export default function({
for (const sibling of path.getAllPrevSiblings()) {
if (!sibling.isFunctionDeclaration()) {
const node = path.node;
const parentPath = path.parentPath;
path.remove();
const paths = sibling.insertBefore(node as any);
parentPath.scope.registerDeclaration(paths[0]);
reregisterDeclarations(sibling.insertBefore(node as any));
return;
}
}
Expand Down Expand Up @@ -4366,12 +4364,23 @@ export default function({
if (!sibling.isFunctionDeclaration() && !sibling.isImportDeclaration()) {
const newNode = targetPath.node;
targetPath.remove();
sibling.insertBefore(newNode);
reregisterDeclarations(sibling.insertBefore(newNode));
return;
}
}
}

// Register all declarations
function reregisterDeclarations(pathOrPaths: any) {
if (Array.isArray(pathOrPaths)) {
for (const path of pathOrPaths) {
reregisterDeclarations(path);
}
} else if (pathOrPaths && pathOrPaths.isLabeledStatement) {
pathOrPaths.scope.registerDeclaration(pathOrPaths);
}
}

// Get previous sibling
function getPreviousSibling(targetPath: NodePath): NodePath | undefined {
const siblings = targetPath.getAllPrevSiblings();
Expand Down Expand Up @@ -4472,14 +4481,17 @@ export default function({
// export default function... is a function declaration in babel 7
const targetPath = path.parentPath;
targetPath.replaceWith(types.variableDeclaration("const", declarators));
targetPath.insertAfter(types.exportDefaultDeclaration(node.id));
reregisterDeclarations(targetPath);
reregisterDeclarations(targetPath.insertAfter(types.exportDefaultDeclaration(node.id)));
reorderPathBeforeSiblingStatements(targetPath);
} else {
path.replaceWith(types.variableDeclaration("const", declarators));
reregisterDeclarations(path);
reorderPathBeforeSiblingStatements(path.parentPath);
}
} else {
path.replaceWith(types.variableDeclaration("const", declarators));
reregisterDeclarations(path);
reorderPathBeforeSiblingStatements(path);
}
}
Expand Down Expand Up @@ -4514,6 +4526,7 @@ export default function({
),
])
);
reregisterDeclarations(targetPath);
targetPath.insertAfter(types.exportDefaultDeclaration(id));
reorderPathBeforeSiblingStatements(targetPath);
return;
Expand Down

0 comments on commit 3628002

Please sign in to comment.