diff --git a/acorn.js b/acorn.js index 3156eaf4c6..65905a3703 100644 --- a/acorn.js +++ b/acorn.js @@ -1605,7 +1605,7 @@ case tt.bracketL: var node = this.startNode(); this.next(); - node.elements = this.parseBindingList(tt.bracketR, true); + node.elements = this.parseBindingList(tt.bracketR, true, true); return this.finishNode(node, "ArrayPattern"); case tt.braceL: @@ -1616,16 +1616,22 @@ } }; - pp.parseBindingList = function(close, allowEmpty) { + pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) { var elts = [], first = true; while (!this.eat(close)) { - first ? first = false : this.expect(tt.comma); - if (this.type === tt.ellipsis) { + if (first) first = false; + else this.expect(tt.comma); + if (allowEmpty && this.type === tt.comma) { + elts.push(null); + } else if (allowTrailingComma && this.afterTrailingComma(close)) { + break; + } else if (this.type === tt.ellipsis) { elts.push(this.parseRest()); this.expect(close); break; + } else { + elts.push(this.parseMaybeDefault()); } - elts.push(allowEmpty && this.type === tt.comma ? null : this.parseMaybeDefault()); } return elts; }; @@ -2567,7 +2573,7 @@ node.id = this.parseIdent(); } this.expect(tt.parenL); - node.params = this.parseBindingList(tt.parenR, false); + node.params = this.parseBindingList(tt.parenR, false, false); this.parseFunctionBody(node, allowExpressionBody); return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); }; @@ -2578,7 +2584,7 @@ var node = this.startNode(); this.initFunction(node); this.expect(tt.parenL); - node.params = this.parseBindingList(tt.parenR, false); + node.params = this.parseBindingList(tt.parenR, false, false); var allowExpressionBody; if (this.options.ecmaVersion >= 6) { node.generator = isGenerator; diff --git a/test/tests-harmony.js b/test/tests-harmony.js index 288b74a8ef..3fde855cfb 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -15263,6 +15263,109 @@ test("var _\\u{104A6} = 10;", { ] }, {ecmaVersion: 6}); +test("let [x,] = [1]", { + "start": 0, + "body": [ + { + "start": 0, + "declarations": [ + { + "start": 4, + "id": { + "start": 4, + "elements": [ + { + "start": 5, + "name": "x", + "type": "Identifier", + "end": 6 + } + ], + "type": "ArrayPattern", + "end": 8 + }, + "init": { + "start": 11, + "elements": [ + { + "start": 12, + "value": 1, + "raw": "1", + "type": "Literal", + "end": 13 + } + ], + "type": "ArrayExpression", + "end": 14 + }, + "type": "VariableDeclarator", + "end": 14 + } + ], + "kind": "let", + "type": "VariableDeclaration", + "end": 14 + } + ], + "type": "Program", + "end": 14 +}, {ecmaVersion: 6}); + +test("let {x} = y", { + "start": 0, + "body": [ + { + "start": 0, + "declarations": [ + { + "start": 4, + "id": { + "start": 4, + "properties": [ + { + "start": 5, + "method": false, + "shorthand": true, + "computed": false, + "key": { + "start": 5, + "name": "x", + "type": "Identifier", + "end": 6 + }, + "kind": "init", + "value": { + "start": 5, + "name": "x", + "type": "Identifier", + "end": 6 + }, + "type": "Property", + "end": 6 + } + ], + "type": "ObjectPattern", + "end": 7 + }, + "init": { + "start": 10, + "name": "y", + "type": "Identifier", + "end": 11 + }, + "type": "VariableDeclarator", + "end": 11 + } + ], + "kind": "let", + "type": "VariableDeclaration", + "end": 11 + } + ], + "type": "Program", + "end": 11 +}, {ecmaVersion: 6}) + testFail("var _𖫵 = 11;", "Unexpected character '𖫵' (1:5)", {ecmaVersion: 6}); testFail("var 𫠞_ = 12;", "Unexpected character '𫠞' (1:4)", {ecmaVersion: 6}); testFail("var 𫠝_ = 10;", "Unexpected character '𫠝' (1:4)", {ecmaVersion: 5});