Fix scope of function body when var redeclares param (#11158)

* Fix scope of function body when var redeclares param

* Fix empty var declarations

* Apply suggestions
This commit is contained in:
Daryl Tan
2020-03-01 23:26:22 +08:00
committed by GitHub
parent a39beda58b
commit 9015fda3c1
17 changed files with 140 additions and 4 deletions

View File

@@ -64,6 +64,31 @@ export default function convertFunctionParams(path, loose) {
for (let i = 0; i < params.length; i++) {
const param = params[i];
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();
if (paramIsAssignmentPattern && (loose || node.kind === "set")) {
const left = param.get("left");
@@ -146,7 +171,8 @@ export default function convertFunctionParams(path, loose) {
path.ensureBlock();
if (state.iife) {
body.push(callDelegate(path, scope));
// we don't want to hoist the inner declarations up
body.push(callDelegate(path, scope, false));
path.set("body", t.blockStatement(body));
} else {
path.get("body").unshiftContainer("body", body);