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:
Peeyush Kushwaha
2017-07-25 00:13:17 +05:30
committed by Henry Zhu
parent 677160385c
commit 2225892348
5 changed files with 43 additions and 8 deletions

View File

@@ -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>;

View File

@@ -0,0 +1,11 @@
function f(a, b) {
var a = "redeclared";
return b;
}
function g(a) {
function a() {
return "function, redeclared";
}
return a();
}

View File

@@ -0,0 +1,12 @@
function f(z, b) {
var z = "redeclared";
return b;
}
function g(z) {
function z() {
return "function, redeclared";
}
return z();
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}

View File

@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Function(path) {
path.scope.rename("a", "z");
}
}
};
}