From 64ca55cfe0c187ec026e0ec5230d8b8ac54cdf8a Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 3 Jul 2016 12:13:36 +0200 Subject: [PATCH] Fixes Syntax error position with flow optional type --- src/parser/expression.js | 2 +- src/parser/util.js | 6 +- test/fixtures/flow/optional-type/4/actual.js | 1 + .../flow/optional-type/4/expected.json | 153 ++++++++++++++++++ test/fixtures/flow/optional-type/5/actual.js | 1 + .../flow/optional-type/5/options.json | 3 + 6 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/flow/optional-type/4/actual.js create mode 100644 test/fixtures/flow/optional-type/4/expected.json create mode 100644 test/fixtures/flow/optional-type/5/actual.js create mode 100644 test/fixtures/flow/optional-type/5/options.json diff --git a/src/parser/expression.js b/src/parser/expression.js index 16e297147b..f45f0550cb 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -558,7 +558,7 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow if (first) { first = false; } else { - this.expect(tt.comma); + this.expect(tt.comma, refNeedsArrowPos.start || null); if (this.match(tt.parenR) && this.hasPlugin("trailingFunctionCommas")) { optionalCommaStart = this.state.start; break; diff --git a/src/parser/util.js b/src/parser/util.js index c8cdeda9f1..7ab9c7a766 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -71,10 +71,10 @@ pp.semicolon = function () { }; // Expect a token of a given type. If found, consume it, otherwise, -// raise an unexpected token error. +// raise an unexpected token error at given pos. -pp.expect = function (type) { - return this.eat(type) || this.unexpected(); +pp.expect = function (type, pos) { + return this.eat(type) || this.unexpected(pos); }; // Raise an unexpected token error. diff --git a/test/fixtures/flow/optional-type/4/actual.js b/test/fixtures/flow/optional-type/4/actual.js new file mode 100644 index 0000000000..32edff9f50 --- /dev/null +++ b/test/fixtures/flow/optional-type/4/actual.js @@ -0,0 +1 @@ +const f = (...x?) => {} diff --git a/test/fixtures/flow/optional-type/4/expected.json b/test/fixtures/flow/optional-type/4/expected.json new file mode 100644 index 0000000000..a570eafd13 --- /dev/null +++ b/test/fixtures/flow/optional-type/4/expected.json @@ -0,0 +1,153 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "f" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 10, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "RestElement", + "start": 11, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "argument": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "name": "x" + }, + "optional": true + } + ], + "body": { + "type": "BlockStatement", + "start": 21, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/optional-type/5/actual.js b/test/fixtures/flow/optional-type/5/actual.js new file mode 100644 index 0000000000..7ea08edfc0 --- /dev/null +++ b/test/fixtures/flow/optional-type/5/actual.js @@ -0,0 +1 @@ +a ? b : c ? d : e ? f : g ? h : i ? j : k ? l : m ? n : (o ? p : =); diff --git a/test/fixtures/flow/optional-type/5/options.json b/test/fixtures/flow/optional-type/5/options.json new file mode 100644 index 0000000000..157ebff900 --- /dev/null +++ b/test/fixtures/flow/optional-type/5/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:65)" +}