Skip to content

Commit

Permalink
Update scope info after destructuring transform (#14494)
Browse files Browse the repository at this point in the history
  • Loading branch information
peey committed May 4, 2022
1 parent 60151e2 commit 97d1967
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
@@ -0,0 +1,23 @@
// 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 names = Object.keys(path.getBindingIdentifiers());
for (const name of names) {
const b = path.scope.getBinding(name);
if (!b) {
throw new Error(`No binding for ${name}`);
}
}
},
});
},
},
},
};
};
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-destructuring/src/index.ts
Expand Up @@ -62,6 +62,7 @@ export default declare((api, options: Options) => {
// top-level statement.
path.replaceWith(declaration.node);
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
path.scope.crawl();
},

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

scope.crawl();
return;
}

Expand Down Expand Up @@ -123,6 +125,7 @@ export default declare((api, options: 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 @@ -147,6 +150,7 @@ export default declare((api, options: 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 @@ -614,24 +614,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 @@ -682,5 +670,5 @@ export function convertAssignmentExpression(
}

path.replaceWithMultiple(nodes);
path.scope.crawl();
scope.crawl();
}
@@ -0,0 +1,3 @@
{
"plugins": ["../../../res/checkScopeInfo.js"]
}
@@ -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"
]
}

0 comments on commit 97d1967

Please sign in to comment.