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

Register binding for newly created vars for commonjs transforms #14097

Merged
merged 1 commit into from Jan 6, 2022

Conversation

The-x-Theorist
Copy link
Contributor

@The-x-Theorist The-x-Theorist commented Jan 4, 2022

Q                       A
Fixed Issues? Fixes #13665 !-- remove the (`) quotes and write "Fixes" before the number to link the issues -->
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Newly created vars will get registered as soon as path.unshiftContainer() happens.

@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/50508/

@overlookmotel
Copy link
Contributor

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 _react binding being created, the old React binding should be destroyed.

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.

@The-x-Theorist
Copy link
Contributor Author

@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.

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jan 5, 2022
@overlookmotel
Copy link
Contributor

overlookmotel commented Jan 5, 2022

Ah sorry I wasn't clear. The React variable exists in the original source code, and so would have a binding (that's what I mean by the "old" binding). After the plugin has run, that var no longer exists, so should not have a binding any more.

Your PR creates a new binding for the new variable _react.

But I think the old binding for the old variable React will still exist, unless it's manually deleted.

Does that make more sense?

@The-x-Theorist
Copy link
Contributor Author

@overlookmotel Now I think I understand, the old bindings should be deleted which were present before the plugin ran, like for React variable, Right?

@The-x-Theorist
Copy link
Contributor Author

@overlookmotel Can you suggest a way with help of which oldBindings can be deleted?

@overlookmotel
Copy link
Contributor

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.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a 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!

Comment on lines +238 to +242
path.get("body").forEach(path => {
if (headers.indexOf(path.node) === -1) return;
if (path.isVariableDeclaration()) {
path.scope.registerDeclaration(path);
}
Copy link
Member

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

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);
});

@nicolo-ribaudo nicolo-ribaudo merged commit ed3036d into babel:main Jan 6, 2022
@nicolo-ribaudo
Copy link
Member

Oh, it looks like I already have a PR for the "remove old bindings" part: #10640

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: scope.hasBinding and scope.getBinding inconsistent for newly created vars
6 participants