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

fix: babel-traverse constant flag in bindings #13777

Closed
wants to merge 1 commit into from
Closed

Conversation

rufman
Copy link

@rufman rufman commented Sep 17, 2021

flag bindings in loops as not constant.

Fixes: #13760

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

flag bindings in loops as not constant.

Fixes: babel#13760
@rufman rufman marked this pull request as draft September 17, 2021 17:47
@babel-bot
Copy link
Collaborator

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

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 82b2d96:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@JLHwung
Copy link
Contributor

JLHwung commented Sep 21, 2021

I don't think we should fix the issue on @babel/traverse, see #13760 (comment).

Comment on lines +47 to +52
for (let node of path.getAncestry()) {
if (node.isLoop()) {
debugger;
this.constant = false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When node.isFunction() we can break the loop, since variables don't cross function boundaries:
    for (;;) () => { var x };
    is constant.
  2. We only need to do this if we have a var, not for const or let
  3. We don't need to collect the whole ancestry, since we'll only use part of it (up to a function). We can do something like
    let { parentPath } = path;
    while (parentPath && !parentPath.isFunction()) {
      // ...
      ({ parentPath } = parentPath);
    }

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit dangerous to mark all bindings within for loop as constant violations. The const within loop is constant anyway.

We can handle it in

if (local) {
this.registerConstantViolation(bindingPath);
} else {
this.bindings[name] = new Binding({
identifier: id,
scope: this,
path: bindingPath,
kind: kind,
});

After we initialize the binding for var n within the for loop, we should also register the constant violation if we see a Loop before a Function when we walk up the ancestry.

In this way the constantViolations are also correct: The binding declaration itself violates the constant because the declaration is effectively hoisted to a function parent and the initializer assignment are executed for more than one time.

@liuxingbaoyu liuxingbaoyu added this to the Babel 8.0 milestone Sep 26, 2022
@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 Jan 8, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 8, 2023
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: @babel/plugin-transform-react-constant-elements transforms non constant element in for loop
6 participants