From 407c97c9c2fe453fc2ae940c3d3b11c90d9dbc8e Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Fri, 10 Feb 2017 15:03:15 +0100 Subject: [PATCH] Fix #321 by allowing question marks in type params (#338) * Fix #321 by allowing question marks in type params * Require commas between params --- src/plugins/flow.js | 2 +- .../regression/issue-321-failing/actual.js | 1 + .../regression/issue-321-failing/options.json | 3 + .../flow/regression/issue-321/actual.js | 2 + .../flow/regression/issue-321/expected.json | 618 ++++++++++++++++++ 5 files changed, 625 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/flow/regression/issue-321-failing/actual.js create mode 100644 test/fixtures/flow/regression/issue-321-failing/options.json create mode 100644 test/fixtures/flow/regression/issue-321/actual.js create mode 100644 test/fixtures/flow/regression/issue-321/expected.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index f4876a3f2d..14f5b48562 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -569,7 +569,7 @@ pp.reinterpretTypeAsFunctionTypeParam = function (type) { pp.flowParseFunctionTypeParams = function (params = []) { const ret = { params, rest: null }; - while (this.match(tt.name)) { + while (!this.match(tt.parenR) && !this.match(tt.ellipsis)) { ret.params.push(this.flowParseFunctionTypeParam()); if (!this.match(tt.parenR)) { this.expect(tt.comma); diff --git a/test/fixtures/flow/regression/issue-321-failing/actual.js b/test/fixtures/flow/regression/issue-321-failing/actual.js new file mode 100644 index 0000000000..7955a29f4b --- /dev/null +++ b/test/fixtures/flow/regression/issue-321-failing/actual.js @@ -0,0 +1 @@ +const fn: ( Object, Object Object ) => void = ( o1, o2, ) => o1; diff --git a/test/fixtures/flow/regression/issue-321-failing/options.json b/test/fixtures/flow/regression/issue-321-failing/options.json new file mode 100644 index 0000000000..78ffeb9294 --- /dev/null +++ b/test/fixtures/flow/regression/issue-321-failing/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected , (1:27)" +} diff --git a/test/fixtures/flow/regression/issue-321/actual.js b/test/fixtures/flow/regression/issue-321/actual.js new file mode 100644 index 0000000000..c4d6e74d84 --- /dev/null +++ b/test/fixtures/flow/regression/issue-321/actual.js @@ -0,0 +1,2 @@ +const fn: ( Object, ?Object ) => void = ( o1, o2 ) => o1; +const fn: ( Object, ?Object, ) => void = ( o1, o2, ) => o1; diff --git a/test/fixtures/flow/regression/issue-321/expected.json b/test/fixtures/flow/regression/issue-321/expected.json new file mode 100644 index 0000000000..b301432004 --- /dev/null +++ b/test/fixtures/flow/regression/issue-321/expected.json @@ -0,0 +1,618 @@ +{ + "type": "File", + "start": 0, + "end": 117, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 59 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 117, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 59 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 56 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 37 + }, + "identifierName": "fn" + }, + "name": "fn", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 8, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start": 10, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "params": [ + { + "type": "FunctionTypeParam", + "start": 12, + "end": 19, + "loc": { + "start": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "Object" + }, + "name": "Object" + } + } + }, + { + "type": "FunctionTypeParam", + "start": 20, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NullableTypeAnnotation", + "start": 20, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "Object" + }, + "name": "Object" + } + } + } + } + ], + "rest": null, + "returnType": { + "type": "VoidTypeAnnotation", + "start": 33, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 37 + } + } + }, + "typeParameters": null + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 40, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 56 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 44 + }, + "identifierName": "o1" + }, + "name": "o1" + }, + { + "type": "Identifier", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 48 + }, + "identifierName": "o2" + }, + "name": "o2" + } + ], + "body": { + "type": "Identifier", + "start": 54, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 54 + }, + "end": { + "line": 1, + "column": 56 + }, + "identifierName": "o1" + }, + "name": "o1" + } + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 58, + "end": 117, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 59 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 64, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 58 + } + }, + "id": { + "type": "Identifier", + "start": 64, + "end": 96, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 38 + }, + "identifierName": "fn" + }, + "name": "fn", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 66, + "end": 96, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start": 68, + "end": 96, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "params": [ + { + "type": "FunctionTypeParam", + "start": 70, + "end": 77, + "loc": { + "start": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 70, + "end": 76, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 70, + "end": 76, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "Object" + }, + "name": "Object" + } + } + }, + { + "type": "FunctionTypeParam", + "start": 78, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NullableTypeAnnotation", + "start": 78, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 27 + }, + "identifierName": "Object" + }, + "name": "Object" + } + } + } + } + ], + "rest": null, + "returnType": { + "type": "VoidTypeAnnotation", + "start": 92, + "end": 96, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 38 + } + } + }, + "typeParameters": null + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 99, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 58 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 101, + "end": 103, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 45 + }, + "identifierName": "o1" + }, + "name": "o1" + }, + { + "type": "Identifier", + "start": 105, + "end": 107, + "loc": { + "start": { + "line": 2, + "column": 47 + }, + "end": { + "line": 2, + "column": 49 + }, + "identifierName": "o2" + }, + "name": "o2" + } + ], + "body": { + "type": "Identifier", + "start": 114, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 56 + }, + "end": { + "line": 2, + "column": 58 + }, + "identifierName": "o1" + }, + "name": "o1" + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file