Skip to content

Commit

Permalink
Merge pull request #7403 from webpack/fix/7335
Browse files Browse the repository at this point in the history
fix argument TDZ and shadowing
  • Loading branch information
sokra committed May 25, 2018
2 parents bb0731d + f22fffd commit c513cac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
50 changes: 27 additions & 23 deletions lib/Parser.js
Expand Up @@ -1030,23 +1030,28 @@ class Parser extends Tapable {
}

walkForInStatement(statement) {
if (statement.left.type === "VariableDeclaration")
if (statement.left.type === "VariableDeclaration") {
this.walkVariableDeclaration(statement.left);
else this.walkPattern(statement.left);
} else {
this.walkPattern(statement.left);
}
this.walkExpression(statement.right);
this.walkStatement(statement.body);
}

prewalkForOfStatement(statement) {
if (statement.left.type === "VariableDeclaration")
if (statement.left.type === "VariableDeclaration") {
this.prewalkVariableDeclaration(statement.left);
}
this.prewalkStatement(statement.body);
}

walkForOfStatement(statement) {
if (statement.left.type === "VariableDeclaration")
if (statement.left.type === "VariableDeclaration") {
this.walkVariableDeclaration(statement.left);
else this.walkPattern(statement.left);
} else {
this.walkPattern(statement.left);
}
this.walkExpression(statement.right);
this.walkStatement(statement.body);
}
Expand All @@ -1062,8 +1067,10 @@ class Parser extends Tapable {
walkFunctionDeclaration(statement) {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
for (const param of statement.params) this.walkPattern(param);
this.inScope(statement.params, () => {
for (const param of statement.params) {
this.walkPattern(param);
}
if (statement.body.type === "BlockStatement") {
this.detectStrictMode(statement.body.body);
this.prewalkStatement(statement.body);
Expand Down Expand Up @@ -1355,13 +1362,10 @@ class Parser extends Tapable {
}

walkExpressions(expressions) {
for (
let expressionsIndex = 0, len = expressions.length;
expressionsIndex < len;
expressionsIndex++
) {
const expression = expressions[expressionsIndex];
if (expression) this.walkExpression(expression);
for (const expression of expressions) {
if (expression) {
this.walkExpression(expression);
}
}
}

Expand Down Expand Up @@ -1469,8 +1473,10 @@ class Parser extends Tapable {
walkFunctionExpression(expression) {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
for (const param of expression.params) this.walkPattern(param);
this.inScope(expression.params, () => {
for (const param of expression.params) {
this.walkPattern(param);
}
if (expression.body.type === "BlockStatement") {
this.detectStrictMode(expression.body.body);
this.prewalkStatement(expression.body);
Expand All @@ -1483,8 +1489,10 @@ class Parser extends Tapable {
}

walkArrowFunctionExpression(expression) {
for (const param of expression.params) this.walkPattern(param);
this.inScope(expression.params, () => {
for (const param of expression.params) {
this.walkPattern(param);
}
if (expression.body.type === "BlockStatement") {
this.detectStrictMode(expression.body.body);
this.prewalkStatement(expression.body);
Expand Down Expand Up @@ -1640,7 +1648,9 @@ class Parser extends Tapable {
if (functionExpression.body.type === "BlockStatement") {
this.prewalkStatement(functionExpression.body);
this.walkStatement(functionExpression.body);
} else this.walkExpression(functionExpression.body);
} else {
this.walkExpression(functionExpression.body);
}
});
this.scope.topLevelScope = wasTopLevel;
}
Expand Down Expand Up @@ -1743,13 +1753,7 @@ class Parser extends Tapable {

this.scope.renames.set("this", null);

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

for (const param of params) {
if (typeof param !== "string") {
this.enterPattern(param, param => {
this.scope.renames.set(param, null);
Expand Down
1 change: 1 addition & 0 deletions test/cases/parsing/issue-7335/a.js
@@ -0,0 +1 @@
export default 9;
27 changes: 27 additions & 0 deletions test/cases/parsing/issue-7335/index.js
@@ -0,0 +1,27 @@
import x from "./a";

const sum1 = (x, y, total = x + y) => total;
const id1 = (a = x) => a;

function sum2(x, y, total = x + y) { return total; }
function id2(a = x) { return a; }

const sum3 = function(x, y, total = x + y) { return total; }
const id3 = function(a = x) { return a; }

it("should shadow imported bindings", () => {
// Arrow functions
expect(sum1(2, 3)).toBe(5);
expect(id1(1)).toBe(1);
expect(id1()).toBe(9);

// Function declarations
expect(sum2(2, 3)).toBe(5);
expect(id2(1)).toBe(1);
expect(id2()).toBe(9);

// Function expressions
expect(sum3(2, 3)).toBe(5);
expect(id3(1)).toBe(1);
expect(id3()).toBe(9);
});

0 comments on commit c513cac

Please sign in to comment.