Skip to content

Commit

Permalink
fix: incorrect constantViolations with destructuring (#16522)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu谩ng J霉nli脿ng <jlhwung@gmail.com>
  • Loading branch information
liuxingbaoyu and JLHwung committed Jun 4, 2024
1 parent c2acb5d commit 4463aa5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/scope/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ class Scope {

// A redeclaration of an existing variable is a modification
if (local) {
this.registerConstantViolation(bindingPath);
local.reassign(bindingPath);
} else {
this.bindings[name] = new Binding({
identifier: id,
Expand Down
21 changes: 21 additions & 0 deletions packages/babel-traverse/test/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,25 @@ describe("scope", () => {
`);
});
});

describe("constantViolations", () => {
it("var redeclarations should not be treated as constantViolations", () => {
const program = getPath(`
function v() { }
function f() {
var a = 1;
var {
currentPoint: a,
centp: v,
} = {};
}
`);

const bindingV = program.scope.getBinding("v");
expect(bindingV.constantViolations).toHaveLength(0);

const bindingA = program.get("body.1.body").scope.getBinding("a");
expect(bindingA.constantViolations).toHaveLength(1);
});
});
});

0 comments on commit 4463aa5

Please sign in to comment.