Skip to content

Commit

Permalink
fix parsing issue with patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 21, 2017
1 parent 272c105 commit 231a638
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
47 changes: 29 additions & 18 deletions lib/Parser.js
Expand Up @@ -636,6 +636,9 @@ class Parser extends Tapable {
}

walkFunctionDeclaration(statement) {
statement.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(statement.params, function() {
if(statement.body.type === "BlockStatement") {
this.prewalkStatement(statement.body);
Expand Down Expand Up @@ -797,24 +800,15 @@ class Parser extends Tapable {
switch(declarator.type) {
case "VariableDeclarator":
{
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
if(renameIdentifier && declarator.id.type === "Identifier" && this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
// renaming with "var a = b;"
if(!this.applyPluginsBailResult1("rename " + renameIdentifier, declarator.init)) {
this.scope.renames["$" + declarator.id.name] = this.scope.renames["$" + renameIdentifier] || renameIdentifier;
const idx = this.scope.definitions.indexOf(declarator.id.name);
if(idx >= 0) this.scope.definitions.splice(idx, 1);
}
} else {
this.enterPattern(declarator.id, (name, decl) => {
if(!this.applyPluginsBailResult1("var-" + declarator.kind + " " + name, decl)) {
if(!this.applyPluginsBailResult1("var " + name, decl)) {
this.scope.renames["$" + name] = undefined;
this.enterPattern(declarator.id, (name, decl) => {
if(!this.applyPluginsBailResult1("var-" + declarator.kind + " " + name, decl)) {
if(!this.applyPluginsBailResult1("var " + name, decl)) {
this.scope.renames["$" + name] = undefined;
if(this.scope.definitions.indexOf(name) < 0)
this.scope.definitions.push(name);
}
}
});
}
}
});
break;
}
}
Expand All @@ -827,7 +821,14 @@ class Parser extends Tapable {
case "VariableDeclarator":
{
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
if(!renameIdentifier || declarator.id.type !== "Identifier" || !this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
if(renameIdentifier && declarator.id.type === "Identifier" && this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
// renaming with "var a = b;"
if(!this.applyPluginsBailResult1("rename " + renameIdentifier, declarator.init)) {
this.scope.renames["$" + declarator.id.name] = this.scope.renames["$" + renameIdentifier] || renameIdentifier;
const idx = this.scope.definitions.indexOf(declarator.id.name);
if(idx >= 0) this.scope.definitions.splice(idx, 1);
}
} else {
this.walkPattern(declarator.id);
if(declarator.init)
this.walkExpression(declarator.init);
Expand All @@ -845,6 +846,11 @@ class Parser extends Tapable {
this["walk" + pattern.type](pattern);
}

walkAssignmentPattern(pattern) {
this.walkExpression(pattern.right);
this.walkPattern(pattern.left);
}

walkObjectPattern(pattern) {
for(let i = 0, len = pattern.properties.length; i < len; i++) {
const prop = pattern.properties[i];
Expand Down Expand Up @@ -912,6 +918,9 @@ class Parser extends Tapable {
}

walkFunctionExpression(expression) {
expression.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(expression.params, function() {
if(expression.body.type === "BlockStatement") {
this.prewalkStatement(expression.body);
Expand All @@ -923,6 +932,9 @@ class Parser extends Tapable {
}

walkArrowFunctionExpression(expression) {
expression.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(expression.params, function() {
if(expression.body.type === "BlockStatement") {
this.prewalkStatement(expression.body);
Expand Down Expand Up @@ -1192,7 +1204,6 @@ class Parser extends Tapable {

enterAssignmentPattern(pattern, onIdent) {
this.enterPattern(pattern.left, onIdent);
this.walkExpression(pattern.right);
}

evaluateExpression(expression) {
Expand Down
7 changes: 6 additions & 1 deletion test/cases/parsing/issue-3273/index.js
Expand Up @@ -26,8 +26,13 @@ it("should hide import by pattern in function", function() {
}({ test: "ok" }));
});

it("should allow import in default", function() {
it("should allow import in default (incorrect)", function() {
var { other = test, test } = { test: "ok" };
test.should.be.eql("ok");
(typeof other).should.be.eql("undefined");
});

it("should allow import in default", function() {
var { other = test } = { test: "ok" };
other.should.be.eql("test");
});

0 comments on commit 231a638

Please sign in to comment.