Skip to content

Commit fb4afde

Browse files
成仕伟marijnh
成仕伟
authored andcommitted
Destructuring assignment rest element could include simple parenthesized target
FIX acornjs#872
1 parent 6230c89 commit fb4afde

File tree

5 files changed

+120
-17
lines changed

5 files changed

+120
-17
lines changed

acorn/src/expression.js

-9
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,6 @@ pp.parseProperty = function(isPattern, refDestructuringErrors) {
750750
}
751751
return this.finishNode(prop, "RestElement")
752752
}
753-
// To disallow parenthesized identifier via `this.toAssignable()`.
754-
if (this.type === tt.parenL && refDestructuringErrors) {
755-
if (refDestructuringErrors.parenthesizedAssign < 0) {
756-
refDestructuringErrors.parenthesizedAssign = this.start
757-
}
758-
if (refDestructuringErrors.parenthesizedBind < 0) {
759-
refDestructuringErrors.parenthesizedBind = this.start
760-
}
761-
}
762753
// Parse argument.
763754
prop.argument = this.parseMaybeAssign(false, refDestructuringErrors)
764755
// To disallow trailing comma via `this.toAssignable()`.

acorn/src/parseutil.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pp.checkPatternErrors = function(refDestructuringErrors, isAssign) {
127127
if (refDestructuringErrors.trailingComma > -1)
128128
this.raiseRecoverable(refDestructuringErrors.trailingComma, "Comma is not permitted after the rest element")
129129
let parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind
130-
if (parens > -1) this.raiseRecoverable(parens, "Parenthesized pattern")
130+
if (parens > -1) this.raiseRecoverable(parens, isAssign ? "Assigning to rvalue" : "Parenthesized pattern")
131131
}
132132

133133
pp.checkExpressionErrors = function(refDestructuringErrors, andThrow) {

test/tests-harmony.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13089,7 +13089,7 @@ testFail("[v] += ary", "Assigning to rvalue (1:0)", {ecmaVersion: 6});
1308913089

1309013090
testFail("[2] = 42", "Assigning to rvalue (1:1)", {ecmaVersion: 6});
1309113091

13092-
testFail("({ obj:20 }) = 42", "Parenthesized pattern (1:0)", {ecmaVersion: 6});
13092+
testFail("({ obj:20 }) = 42", "Assigning to rvalue (1:0)", {ecmaVersion: 6});
1309313093

1309413094
testFail("( { get x() {} } = 0)", "Object pattern can't contain getter or setter (1:8)", {ecmaVersion: 6});
1309513095

@@ -16159,7 +16159,7 @@ testFail("'use strict'; bar: function x() {}", "Unexpected token (1:19)", {ecmaV
1615916159
testFail("'use strict'; bar: function* x() {}", "Unexpected token (1:19)", {ecmaVersion: 6})
1616016160
testFail("bar: function* x() {}", "Unexpected token (1:13)", {ecmaVersion: 6})
1616116161

16162-
testFail("({x, y}) = {}", "Parenthesized pattern (1:0)", {ecmaVersion: 6})
16162+
testFail("({x, y}) = {}", "Assigning to rvalue (1:0)", {ecmaVersion: 6})
1616316163

1616416164
test("[x, (y), {z, u: (v)}] = foo", {}, {ecmaVersion: 6})
1616516165

test/tests-rest-spread-properties.js

+114-2
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,119 @@ test("({a:b,...obj}) => {}", {
813813
"sourceType": "script"
814814
}, { ecmaVersion: 9 })
815815

816+
test("({...(obj)} = foo)", {
817+
type: "Program",
818+
start: 0,
819+
end: 18,
820+
loc: {
821+
start: {
822+
line: 1,
823+
column: 0
824+
},
825+
end: {
826+
line: 1,
827+
column: 18
828+
}
829+
},
830+
body: [
831+
{
832+
type: "ExpressionStatement",
833+
start: 0,
834+
end: 18,
835+
loc: {
836+
start: {
837+
line: 1,
838+
column: 0
839+
},
840+
end: {
841+
line: 1,
842+
column: 18
843+
}
844+
},
845+
expression: {
846+
type: "AssignmentExpression",
847+
start: 1,
848+
end: 17,
849+
loc: {
850+
start: {
851+
line: 1,
852+
column: 1
853+
},
854+
end: {
855+
line: 1,
856+
column: 17
857+
}
858+
},
859+
operator: "=",
860+
left: {
861+
type: "ObjectPattern",
862+
start: 1,
863+
end: 11,
864+
loc: {
865+
start: {
866+
line: 1,
867+
column: 1
868+
},
869+
end: {
870+
line: 1,
871+
column: 11
872+
}
873+
},
874+
properties: [
875+
{
876+
type: "RestElement",
877+
start: 2,
878+
end: 10,
879+
loc: {
880+
start: {
881+
line: 1,
882+
column: 2
883+
},
884+
end: {
885+
line: 1,
886+
column: 10
887+
}
888+
},
889+
argument: {
890+
type: "Identifier",
891+
start: 6,
892+
end: 9,
893+
loc: {
894+
start: {
895+
line: 1,
896+
column: 6
897+
},
898+
end: {
899+
line: 1,
900+
column: 9
901+
}
902+
},
903+
name: "obj"
904+
}
905+
}
906+
]
907+
},
908+
right: {
909+
type: "Identifier",
910+
start: 14,
911+
end: 17,
912+
loc: {
913+
start: {
914+
line: 1,
915+
column: 14
916+
},
917+
end: {
918+
line: 1,
919+
column: 17
920+
}
921+
},
922+
name: "foo"
923+
}
924+
}
925+
}
926+
],
927+
}, { ecmaVersion: 9, locations: true })
928+
816929
testFail("let {...obj1,} = foo", "Comma is not permitted after the rest element (1:12)", { ecmaVersion: 9 })
817930
testFail("let {...obj1,a} = foo", "Comma is not permitted after the rest element (1:12)", { ecmaVersion: 9 })
818931
testFail("let {...obj1,...obj2} = foo", "Comma is not permitted after the rest element (1:12)", { ecmaVersion: 9 })
@@ -823,8 +936,7 @@ testFail("let {...[a,b]} = foo", "Unexpected token (1:8)", { ecmaVersion: 9 })
823936
testFail("({...obj1,} = foo)", "Comma is not permitted after the rest element (1:9)", { ecmaVersion: 9 })
824937
testFail("({...obj1,a} = foo)", "Comma is not permitted after the rest element (1:9)", { ecmaVersion: 9 })
825938
testFail("({...obj1,...obj2} = foo)", "Comma is not permitted after the rest element (1:9)", { ecmaVersion: 9 })
826-
testFail("({...(obj)} = foo)", "Parenthesized pattern (1:5)", { ecmaVersion: 9 })
827-
testFail("({...(a,b)} = foo)", "Parenthesized pattern (1:5)", { ecmaVersion: 9 })
939+
testFail("({...(a,b)} = foo)", "Assigning to rvalue (1:5)", { ecmaVersion: 9 })
828940
testFail("({...{a,b}} = foo)", "Unexpected token (1:5)", { ecmaVersion: 9 })
829941
testFail("({...[a,b]} = foo)", "Unexpected token (1:5)", { ecmaVersion: 9 })
830942
testFail("({...obj} = foo)", "Unexpected token (1:2)", { ecmaVersion: 8 })

test/tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27174,7 +27174,7 @@ testFail("func() = 4",
2717427174
"Assigning to rvalue (1:0)");
2717527175

2717627176
testFail("(1 + 1) = 10",
27177-
"Parenthesized pattern (1:0)");
27177+
"Assigning to rvalue (1:0)");
2717827178

2717927179
testFail("1++",
2718027180
"Assigning to rvalue (1:0)");
@@ -27189,7 +27189,7 @@ testFail("--1",
2718927189
"Assigning to rvalue (1:2)");
2719027190

2719127191
testFail("for((1 + 1) in list) process(x);",
27192-
"Parenthesized pattern (1:4)");
27192+
"Assigning to rvalue (1:4)");
2719327193

2719427194
testFail("[",
2719527195
"Unexpected token (1:1)");
@@ -29437,7 +29437,7 @@ testFail("for (x \\u006ff y) {}", "Unexpected token (1:7)", {ecmaVersion: 6})
2943729437
testFail("function x () { new.ta\\u0072get }", "'new.target' must not contain escaped characters (1:16)", {ecmaVersion: 6})
2943829438
testFail("class X { st\\u0061tic y() {} }", "Unexpected token (1:22)", {ecmaVersion: 6})
2943929439

29440-
testFail("(x=1)=2", "Parenthesized pattern (1:0)")
29440+
testFail("(x=1)=2", "Assigning to rvalue (1:0)")
2944129441
testFail("(x=1)=2", "Assigning to rvalue (1:1)", {ecmaVersion: 6})
2944229442

2944329443
test("(foo = [])[0] = 4;", {})

0 commit comments

Comments
 (0)