Skip to content

Commit 2bfaa26

Browse files
committed
[[FIX]] Update parsing of object "rest" property
The latest version of the proposal disallows BindingPattern as the right-hand side of a "rest" property. tc39/proposal-object-rest-spread#50 http://tc39.github.io/tc39-notes/2017-05_may-23.html#16ih-why-allow-bindingpattern-for-bindingrestparameter-for-object-rest-maybe-we-should-just-allow-identifiers
1 parent 58bb28e commit 2bfaa26

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/jshint.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,7 +3312,7 @@ var JSHINT = (function() {
33123312

33133313
(function(x) {
33143314
x.nud = function() {
3315-
var b, f, i, p, t, isGeneratorMethod = false, nextVal;
3315+
var b, f, i, p, t, isGeneratorMethod = false, nextVal, expr;
33163316
var props = Object.create(null); // All properties, including accessors
33173317

33183318
b = state.tokens.curr.line !== startLine(state.tokens.next);
@@ -3502,7 +3502,8 @@ var JSHINT = (function() {
35023502
}
35033503
};
35043504
var assignmentProperty = function() {
3505-
var id;
3505+
var id, expr;
3506+
35063507
if (checkPunctuator(state.tokens.next, "[")) {
35073508
advance("[");
35083509
expression(10);
@@ -3523,7 +3524,16 @@ var JSHINT = (function() {
35233524
warning("W141", state.tokens.next, "object rest property", "objspreadrest");
35243525
}
35253526

3526-
nextInnerDE();
3527+
// Due to visual symmetry with the array rest property (and the early
3528+
// design of the language feature), developers may mistakenly assume
3529+
// any expression is valid in this position. Parse an expression and
3530+
// issue an error in order to recover more gracefully from this
3531+
// condition.
3532+
expr = expression(10);
3533+
3534+
if (expr.type !== '(identifier)') {
3535+
error("E030", expr, expr.value);
3536+
}
35273537
} else {
35283538
id = identifier();
35293539
}

tests/unit/unstable/objrestspread.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ exports.rest = function (test) {
7979
];
8080

8181
TestRun(test, "nested array pattern, final")
82+
.addError(1, "Expected an identifier and instead saw '['.")
83+
.addError(2, "Expected an identifier and instead saw '['.")
84+
.addError(3, "Expected an identifier and instead saw '['.")
85+
.addError(4, "Expected an identifier and instead saw '['.")
86+
.addError(5, "Expected an identifier and instead saw '['.")
8287
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
8388

8489
code = [
@@ -90,19 +95,24 @@ exports.rest = function (test) {
9095
];
9196

9297
TestRun(test, "nested array pattern, not final")
98+
.addError(1, "Expected an identifier and instead saw '['.")
9399
.addError(1, "Invalid element after rest element.")
100+
.addError(2, "Expected an identifier and instead saw '['.")
94101
.addError(2, "Invalid element after rest element.")
102+
.addError(3, "Expected an identifier and instead saw '['.")
95103
.addError(3, "Invalid element after rest element.")
104+
.addError(4, "Expected an identifier and instead saw '['.")
96105
.addError(4, "Invalid element after rest element.")
106+
.addError(5, "Expected an identifier and instead saw '['.")
97107
.addError(5, "Invalid element after rest element.")
98108
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
99109

100110
TestRun(test, "nested array pattern, empty")
101-
.addError(1, "Empty destructuring.")
111+
.addError(1, "Expected an identifier and instead saw '['.")
102112
.test("({ ...[] } = {});", { esversion: 6, unstable: { objspreadrest: true } });
103113

104114
TestRun(test, "nested object pattern, empty")
105-
.addError(1, "Empty destructuring.")
115+
.addError(1, "Expected an identifier and instead saw '{'.")
106116
.test("({ ...{} } = {});", { esversion: 6, unstable: { objspreadrest: true } });
107117

108118
test.done();

0 commit comments

Comments
 (0)