Skip to content

Commit 231a638

Browse files
committed
fix parsing issue with patterns
1 parent 272c105 commit 231a638

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

lib/Parser.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ class Parser extends Tapable {
636636
}
637637

638638
walkFunctionDeclaration(statement) {
639+
statement.params.forEach(param => {
640+
this.walkPattern(param);
641+
});
639642
this.inScope(statement.params, function() {
640643
if(statement.body.type === "BlockStatement") {
641644
this.prewalkStatement(statement.body);
@@ -797,24 +800,15 @@ class Parser extends Tapable {
797800
switch(declarator.type) {
798801
case "VariableDeclarator":
799802
{
800-
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
801-
if(renameIdentifier && declarator.id.type === "Identifier" && this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
802-
// renaming with "var a = b;"
803-
if(!this.applyPluginsBailResult1("rename " + renameIdentifier, declarator.init)) {
804-
this.scope.renames["$" + declarator.id.name] = this.scope.renames["$" + renameIdentifier] || renameIdentifier;
805-
const idx = this.scope.definitions.indexOf(declarator.id.name);
806-
if(idx >= 0) this.scope.definitions.splice(idx, 1);
807-
}
808-
} else {
809-
this.enterPattern(declarator.id, (name, decl) => {
810-
if(!this.applyPluginsBailResult1("var-" + declarator.kind + " " + name, decl)) {
811-
if(!this.applyPluginsBailResult1("var " + name, decl)) {
812-
this.scope.renames["$" + name] = undefined;
803+
this.enterPattern(declarator.id, (name, decl) => {
804+
if(!this.applyPluginsBailResult1("var-" + declarator.kind + " " + name, decl)) {
805+
if(!this.applyPluginsBailResult1("var " + name, decl)) {
806+
this.scope.renames["$" + name] = undefined;
807+
if(this.scope.definitions.indexOf(name) < 0)
813808
this.scope.definitions.push(name);
814-
}
815809
}
816-
});
817-
}
810+
}
811+
});
818812
break;
819813
}
820814
}
@@ -827,7 +821,14 @@ class Parser extends Tapable {
827821
case "VariableDeclarator":
828822
{
829823
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
830-
if(!renameIdentifier || declarator.id.type !== "Identifier" || !this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
824+
if(renameIdentifier && declarator.id.type === "Identifier" && this.applyPluginsBailResult1("can-rename " + renameIdentifier, declarator.init)) {
825+
// renaming with "var a = b;"
826+
if(!this.applyPluginsBailResult1("rename " + renameIdentifier, declarator.init)) {
827+
this.scope.renames["$" + declarator.id.name] = this.scope.renames["$" + renameIdentifier] || renameIdentifier;
828+
const idx = this.scope.definitions.indexOf(declarator.id.name);
829+
if(idx >= 0) this.scope.definitions.splice(idx, 1);
830+
}
831+
} else {
831832
this.walkPattern(declarator.id);
832833
if(declarator.init)
833834
this.walkExpression(declarator.init);
@@ -845,6 +846,11 @@ class Parser extends Tapable {
845846
this["walk" + pattern.type](pattern);
846847
}
847848

849+
walkAssignmentPattern(pattern) {
850+
this.walkExpression(pattern.right);
851+
this.walkPattern(pattern.left);
852+
}
853+
848854
walkObjectPattern(pattern) {
849855
for(let i = 0, len = pattern.properties.length; i < len; i++) {
850856
const prop = pattern.properties[i];
@@ -912,6 +918,9 @@ class Parser extends Tapable {
912918
}
913919

914920
walkFunctionExpression(expression) {
921+
expression.params.forEach(param => {
922+
this.walkPattern(param);
923+
});
915924
this.inScope(expression.params, function() {
916925
if(expression.body.type === "BlockStatement") {
917926
this.prewalkStatement(expression.body);
@@ -923,6 +932,9 @@ class Parser extends Tapable {
923932
}
924933

925934
walkArrowFunctionExpression(expression) {
935+
expression.params.forEach(param => {
936+
this.walkPattern(param);
937+
});
926938
this.inScope(expression.params, function() {
927939
if(expression.body.type === "BlockStatement") {
928940
this.prewalkStatement(expression.body);
@@ -1192,7 +1204,6 @@ class Parser extends Tapable {
11921204

11931205
enterAssignmentPattern(pattern, onIdent) {
11941206
this.enterPattern(pattern.left, onIdent);
1195-
this.walkExpression(pattern.right);
11961207
}
11971208

11981209
evaluateExpression(expression) {

test/cases/parsing/issue-3273/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ it("should hide import by pattern in function", function() {
2626
}({ test: "ok" }));
2727
});
2828

29-
it("should allow import in default", function() {
29+
it("should allow import in default (incorrect)", function() {
3030
var { other = test, test } = { test: "ok" };
3131
test.should.be.eql("ok");
32+
(typeof other).should.be.eql("undefined");
33+
});
34+
35+
it("should allow import in default", function() {
36+
var { other = test } = { test: "ok" };
3237
other.should.be.eql("test");
3338
});

0 commit comments

Comments
 (0)