Skip to content

Commit 4463aa5

Browse files
fix: incorrect constantViolations with destructuring (#16522)
Co-authored-by: Huáng Jùnliàng <[email protected]>
1 parent c2acb5d commit 4463aa5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/babel-traverse/src/scope/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ class Scope {
833833

834834
// A redeclaration of an existing variable is a modification
835835
if (local) {
836-
this.registerConstantViolation(bindingPath);
836+
local.reassign(bindingPath);
837837
} else {
838838
this.bindings[name] = new Binding({
839839
identifier: id,

packages/babel-traverse/test/scope.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,4 +1138,25 @@ describe("scope", () => {
11381138
`);
11391139
});
11401140
});
1141+
1142+
describe("constantViolations", () => {
1143+
it("var redeclarations should not be treated as constantViolations", () => {
1144+
const program = getPath(`
1145+
function v() { }
1146+
function f() {
1147+
var a = 1;
1148+
var {
1149+
currentPoint: a,
1150+
centp: v,
1151+
} = {};
1152+
}
1153+
`);
1154+
1155+
const bindingV = program.scope.getBinding("v");
1156+
expect(bindingV.constantViolations).toHaveLength(0);
1157+
1158+
const bindingA = program.get("body.1.body").scope.getBinding("a");
1159+
expect(bindingA.constantViolations).toHaveLength(1);
1160+
});
1161+
});
11411162
});

0 commit comments

Comments
 (0)