Use first binding for multiple var declarations (#5745)
* Use first binding for multiple var declarations Since var declarations after initial binding have no effect, use the first declaration. Fixes #2378 * Include hoisted function bindings * Missing newline in expected.js * Simplify constantViolations in new Binding on existing * clarify comment language
This commit is contained in:
committed by
Henry Zhu
parent
677160385c
commit
2225892348
@@ -13,6 +13,14 @@ import type NodePath from "../path";
|
||||
|
||||
export default class Binding {
|
||||
constructor({ existing, identifier, scope, path, kind }) {
|
||||
// this if condition is true only if the re-binding does not result in an error
|
||||
// e.g. if rebinding kind is 'var' or 'hoisted', and previous was 'param'
|
||||
if (existing) {
|
||||
// we maintain the original binding but update constantViolations
|
||||
existing.constantViolations = existing.constantViolations.concat(path);
|
||||
return existing;
|
||||
}
|
||||
|
||||
this.identifier = identifier;
|
||||
this.scope = scope;
|
||||
this.path = path;
|
||||
@@ -26,14 +34,6 @@ export default class Binding {
|
||||
this.references = 0;
|
||||
|
||||
this.clearValue();
|
||||
|
||||
if (existing) {
|
||||
this.constantViolations = [].concat(
|
||||
existing.path,
|
||||
existing.constantViolations,
|
||||
this.constantViolations,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
constantViolations: Array<NodePath>;
|
||||
|
||||
11
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/actual.js
vendored
Normal file
11
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/actual.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
function f(a, b) {
|
||||
var a = "redeclared";
|
||||
return b;
|
||||
}
|
||||
|
||||
function g(a) {
|
||||
function a() {
|
||||
return "function, redeclared";
|
||||
}
|
||||
return a();
|
||||
}
|
||||
12
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/expected.js
vendored
Normal file
12
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
function f(z, b) {
|
||||
var z = "redeclared";
|
||||
return b;
|
||||
}
|
||||
|
||||
function g(z) {
|
||||
function z() {
|
||||
return "function, redeclared";
|
||||
}
|
||||
|
||||
return z();
|
||||
}
|
||||
3
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/options.json
vendored
Normal file
3
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["./plugin"]
|
||||
}
|
||||
9
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/plugin.js
vendored
Normal file
9
packages/babel-traverse/test/fixtures/rename/parameter-redeclaration/plugin.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
module.exports = function() {
|
||||
return {
|
||||
visitor: {
|
||||
Function(path) {
|
||||
path.scope.rename("a", "z");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user