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
Register binding for newly created vars for commonjs transforms #14097
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/50508/ |
Nice one! Thanks loads for doing this. I guess, strictly speaking, should also make sure that old bindings are also removed. e.g.: // Input
import React from "react";
// Output
var _react = require("react"); So, as well as the new Also, vars which are used in CommonJS are renamed: // Input
let module, exports, require, __dirname, __filename;
// Output
let _module, _exports, _require, _dirname, _filename; Do bindings for these var name changes get dealt with too? (maybe they do automatically as they're just variable renaming not creation of new vars, I'm not sure) I don't know how difficult it would be to handle the above. Certainly your PR as it is already is a big improvement on before. |
@overlookmotel Earlier there were no bindings registered for the newly created vars as of this, I think that there is no way we can remove the old bindings which weren't present actually. |
Ah sorry I wasn't clear. The Your PR creates a new binding for the new variable But I think the old binding for the old variable Does that make more sense? |
@overlookmotel Now I think I understand, the old bindings should be deleted which were present before the plugin ran, like for React variable, Right? |
@overlookmotel Can you suggest a way with help of which oldBindings can be deleted? |
Yes, that's exactly what I meant. I'm afraid no, I can't offer any suggestions. I'm not actually very familiar with the code in Babel which deals with scopes/bindings. Maybe one of the maintainers can offer some guidance. Or it may be that it's fine to leave it as it is. The biggest fault was that the new bindings weren't registered (which you've solved). It's of lesser importance that the old bindings are removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely remove the old bindings, but it can be done in a separate PR!
path.get("body").forEach(path => { | ||
if (headers.indexOf(path.node) === -1) return; | ||
if (path.isVariableDeclaration()) { | ||
path.scope.registerDeclaration(path); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: this code is the same as
babel/packages/babel-core/src/transformation/file/file.ts
Lines 217 to 223 in bee3e35
this.path.unshiftContainer("body", nodes); | |
// TODO: NodePath#unshiftContainer should automatically register new | |
// bindings. | |
this.path.get("body").forEach(path => { | |
if (nodes.indexOf(path.node) === -1) return; | |
if (path.isVariableDeclaration()) this.scope.registerDeclaration(path); | |
}); |
Oh, it looks like I already have a PR for the "remove old bindings" part: #10640 |
Newly created vars will get registered as soon as
path.unshiftContainer()
happens.