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
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function render() {
const nodes = [];

for (let i = 0; i < 5; i++) {
const o = "foo";
const n = i;
nodes.push(<div>{n}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function render() {
const nodes = [];

for (let i = 0; i < 5; i++) {
const o = "foo";
const n = i;
nodes.push(<div>{n}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function render() {
const nodes = [];

for (const node of nodes) {
nodes.push(<div>{node}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function render() {
const nodes = [];

for (const node of nodes) {
nodes.push(<div>{node}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function render() {
const nodes = [];

for (const node of nodes) {
const n = node;
nodes.push(<div>{n}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function render() {
const nodes = [];

for (const node of nodes) {
const n = node;
nodes.push(<div>{n}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function render() {
const nodes = [];

for (let i = 0; i < 5; i++) {
const o = "foo";
const n = i;
nodes.push(<div>{o}</div>);
}

return nodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function render() {
const nodes = [];

for (let i = 0; i < 5; i++) {
const o = "foo";
const n = i;
nodes.push(<div>{o}</div>);
}

return nodes;
}
7 changes: 7 additions & 0 deletions packages/babel-traverse/src/scope/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export default class Binding {
this.path = path;
this.kind = kind;

for (let node of path.getAncestry()) {
if (node.isLoop()) {
debugger;
this.constant = false;
}
}
Comment on lines +47 to +52
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);
    }


this.clearValue();
}

Expand Down
23 changes: 23 additions & 0 deletions packages/babel-traverse/test/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,29 @@ describe("scope", () => {
expect(
getPath("var a = 1; var a = 2;").scope.getBinding("a").constant,
).toBe(false);
expect(
getPath("for (var n of ns) { var a = 1; }").scope.getBinding("a")
.constant,
).toBe(false);
expect(
getPath("for (var n in ns) { var a = 1; }").scope.getBinding("a")
.constant,
).toBe(false);
expect(
getPath("for (var i = 0; i < n; i++) { var a = 1; }").scope.getBinding(
"a",
).constant,
).toBe(false);
expect(
getPath("var i = 0; while (i != 1) { var a = 1; }").scope.getBinding(
"a",
).constant,
).toBe(false);
expect(
getPath("var i = 0; do { var a = 1; } while (i != 1)").scope.getBinding(
"a",
).constant,
).toBe(false);
});

it("purity", function () {
Expand Down