From cd133ff8e14beba681cdf4d2a0337aad3d983931 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Tue, 28 Feb 2017 05:56:56 -0600 Subject: [PATCH] Ensure consistent start args for parseParenItem (#386) --- src/parser/expression.js | 2 +- src/plugins/flow.js | 6 +- test/fixtures/flow/comment/spread/actual.js | 4 + .../flow/comment/spread/expected.json | 275 ++++++++++++++++++ 4 files changed, 283 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/flow/comment/spread/actual.js create mode 100644 test/fixtures/flow/comment/spread/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index 54d48e01ab..bf5dda237d 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -595,7 +595,7 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow const spreadNodeStartPos = this.state.start; const spreadNodeStartLoc = this.state.startLoc; spreadStart = this.state.start; - exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartLoc, spreadNodeStartPos)); + exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartPos, spreadNodeStartLoc)); break; } else { exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos)); diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 04579ae047..330a5f3591 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -940,14 +940,14 @@ export default function (instance) { }); instance.extend("parseParenItem", function (inner) { - return function (node, startLoc, startPos) { - node = inner.call(this, node, startLoc, startPos); + return function (node, startPos, startLoc) { + node = inner.call(this, node, startPos, startLoc); if (this.eat(tt.question)) { node.optional = true; } if (this.match(tt.colon)) { - const typeCastNode = this.startNodeAt(startLoc, startPos); + const typeCastNode = this.startNodeAt(startPos, startLoc); typeCastNode.expression = node; typeCastNode.typeAnnotation = this.flowParseTypeAnnotation(); diff --git a/test/fixtures/flow/comment/spread/actual.js b/test/fixtures/flow/comment/spread/actual.js new file mode 100644 index 0000000000..4c4266c5ca --- /dev/null +++ b/test/fixtures/flow/comment/spread/actual.js @@ -0,0 +1,4 @@ +/* hi */ +function commentsAttachedToIdentifier() { + var x = (...args: any) => {}; +} diff --git a/test/fixtures/flow/comment/spread/expected.json b/test/fixtures/flow/comment/spread/expected.json new file mode 100644 index 0000000000..313e33c4ac --- /dev/null +++ b/test/fixtures/flow/comment/spread/expected.json @@ -0,0 +1,275 @@ +{ + "type": "File", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 9, + "end": 84, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 18, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 37 + }, + "identifierName": "commentsAttachedToIdentifier" + }, + "name": "commentsAttachedToIdentifier", + "leadingComments": null + }, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 49, + "end": 84, + "loc": { + "start": { + "line": 2, + "column": 40 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 53, + "end": 82, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 57, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "id": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 61, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "RestElement", + "start": 62, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "argument": { + "type": "Identifier", + "start": 65, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "args" + }, + "name": "args" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 69, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start": 71, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 20 + }, + "end": { + "line": 3, + "column": 23 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 79, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "var" + } + ], + "directives": [] + }, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " hi ", + "start": 0, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " hi ", + "start": 0, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + } + ] +} \ No newline at end of file