Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scope info after destructuring transform #14494

Merged
merged 8 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,22 @@
// checkScopeInfo.js
module.exports = () => {
return {
visitor: {
Program: {
exit(programPath) {
// so that this plugin is always called after the transform-destructuring plugin
programPath.traverse({
VariableDeclarator(path) {
const b = path.scope.getBinding(path.get("id").node.name);
if (!b) {
throw new Error(
`No original binding for ${path.get("id").node.name}`
);
}
},
});
},
},
},
};
};
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-destructuring/src/index.ts
Expand Up @@ -54,6 +54,7 @@ export default declare((api, options) => {
// top-level statement.
path.replaceWith(declaration.node);
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
path.scope.crawl();
},

ForXStatement(path) {
Expand Down Expand Up @@ -82,6 +83,7 @@ export default declare((api, options) => {
t.expressionStatement(t.assignmentExpression("=", left, temp)),
);

scope.crawl();
return;
}

Expand Down Expand Up @@ -115,6 +117,7 @@ export default declare((api, options) => {
const block = node.body;
// @ts-expect-error: ensureBlock ensures that node.body is a BlockStatement
block.body = nodes.concat(block.body);
scope.crawl();
},

CatchClause({ node, scope }) {
Expand All @@ -139,6 +142,7 @@ export default declare((api, options) => {
destructuring.init(pattern, ref);

node.body.body = nodes.concat(node.body.body);
scope.crawl();
},

AssignmentExpression(path, state) {
Expand Down
16 changes: 2 additions & 14 deletions packages/babel-plugin-transform-destructuring/src/util.ts
Expand Up @@ -619,24 +619,12 @@ export function convertVariableDeclaration(
}
}

// Need to unmark the current binding to this var as a param, or other hoists
// could be placed above this ref.
// https://github.com/babel/babel/issues/4516
for (const nodeOut of nodesOut) {
if (!nodeOut.declarations) continue;
for (const declaration of nodeOut.declarations) {
const { name } = declaration.id;
if (scope.bindings[name]) {
scope.bindings[name].kind = nodeOut.kind;
}
}
}

if (nodesOut.length === 1) {
path.replaceWith(nodesOut[0]);
} else {
path.replaceWithMultiple(nodesOut);
}
scope.crawl();
}

export function convertAssignmentExpression(
Expand Down Expand Up @@ -687,5 +675,5 @@ export function convertAssignmentExpression(
}

path.replaceWithMultiple(nodes);
path.scope.crawl();
scope.crawl();
}
@@ -0,0 +1,3 @@
{
"plugins": ["relative/path/to/checkScopeInfo.js"]
peey marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring"],
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"assumptions": {
"arrayLikeIsIterable": true
}
Expand Down
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring"],
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"assumptions": {
"iterableIsArray": true
}
Expand Down
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring"],
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"assumptions": {
"objectRestNoSymbols": true
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
"transform-parameters",
"transform-block-scoping",
"proposal-object-rest-spread",
"transform-regenerator"
"transform-regenerator",
"../../../res/checkScopeInfo.js"
]
}