Skip to content

Commit

Permalink
Fix empty var declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
openorclose committed Feb 21, 2020
1 parent 394eb68 commit 6decfca
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 25 deletions.
38 changes: 23 additions & 15 deletions packages/babel-plugin-transform-parameters/src/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,34 @@ export default function convertFunctionParams(path, loose) {
const body = [];
const params = path.get("params");

const bindings = scope.bindings;

function isRedeclaredVarOrFunction(name) {
const kind = bindings[name]?.kind;
return kind === "var" || kind === "hoisted";
}

let firstOptionalIndex = null;

for (let i = 0; i < params.length; i++) {
const param = params[i];

// If a parameter's binding shows that its kind is var or hoisted,
// this name was legally redeclared in the function body,
// and we need to wrap the function body in an IIFE.
if (
!state.iife &&
Object.keys(param.getBindingIdentifiers()).some(isRedeclaredVarOrFunction)
) {
state.iife = true;
for (const name of Object.keys(param.getBindingIdentifiers())) {
const constantViolations = scope.bindings[name]?.constantViolations;
if (constantViolations) {
for (const redeclarator of constantViolations) {
const node = redeclarator.node;
// If a constant violation is a var or a function declaration,
// we first check to see if it's a var without an init.
// If so, we remove that declarator.
// Otherwise, we have to wrap it in an IIFE.
switch (node.type) {
case "VariableDeclarator":
if (node.init === null) {
redeclarator.remove();
} else {
state.iife = true;
}
break;
case "FunctionDeclaration":
state.iife = true;
break;
}
}
}
}

const paramIsAssignmentPattern = param.isAssignmentPattern();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function f(a = 2, b = 3) {
var a, b = 4;
var a;
var b;
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function f() {
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
return function () {
var b = 4;
return a + b;
}();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function f(a = 2, b = 3) {
var a;
var { a } = { a: 4 };
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function f() {
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
return function () {
var _a = {
a: 4
},
a = _a.a;
return a + b;
}();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function f(a = 2, b = 3) {
var { a } = { a: 4 };
var a;
var b;
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function f() {
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
return function () {
var _a = {
a: 4
},
a = _a.a;
return a + b;
}();
}
10 changes: 2 additions & 8 deletions packages/babel-traverse/src/scope/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,8 @@ export default class Scope {

parent.references[name] = true;

// A redeclaration of an existing variable is a modification.
// However, a var or function declaration
// with the same name as a function parameter
// is valid, and should take precedence.
if (
local &&
!(local.kind === "param" && (kind === "var" || kind === "hoisted"))
) {
// A redeclaration of an existing variable is a modification
if (local) {
this.registerConstantViolation(bindingPath);
} else {
this.bindings[name] = new Binding({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function f(a, b) {
function f(z, b) {
var z = "redeclared";
return b;
}

function g(a) {
function g(z) {
function z() {
return "function, redeclared";
}
Expand Down

0 comments on commit 6decfca

Please sign in to comment.