From cd987cd8e48c913d2bb85e130f3dcae1ed6431a9 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 26 Jun 2016 12:59:19 +0200 Subject: [PATCH] Fix arrow param locations with flow types This patch corrects the end locations for params in arrow functions which use type params. --- src/plugins/flow.js | 22 ++++++++----- .../flow/optional-type/3/expected.json | 4 +-- .../flow/regression/issue-2493/expected.json | 4 +-- .../1/expected.json | 4 +-- .../flow/type-annotations/101/expected.json | 6 ++-- .../flow/type-annotations/103/expected.json | 8 ++--- .../flow/type-annotations/69/expected.json | 10 +++--- .../flow/type-annotations/80/expected.json | 6 ++-- .../existential-type-param-2/expected.json | 6 ++-- .../arrow/expected.json | 33 ------------------- .../arrow_with_jsx/expected.json | 8 ++--- .../arrow_without_jsx/expected.json | 10 +++--- 12 files changed, 47 insertions(+), 74 deletions(-) delete mode 100644 test/fixtures/flow/type-parameter-declaration/arrow/expected.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index cbad58e5ea..3107e2dedf 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -685,6 +685,17 @@ pp.flowParseTypeAnnotatableIdentifier = function (requireTypeAnnotation, canBeOp return ident; }; +pp.typeCastToParameter = function (node) { + node.expression.typeAnnotation = node.typeAnnotation; + + return this.finishNodeAt( + node.expression, + node.expression.type, + node.typeAnnotation.end, + node.typeAnnotation.loc.end + ); +}; + export default function (instance) { // plain function return types: function name(): string {} instance.extend("parseFunctionBody", function (inner) { @@ -862,15 +873,10 @@ export default function (instance) { }; }); - function typeCastToParameter(node) { - node.expression.typeAnnotation = node.typeAnnotation; - return node.expression; - } - instance.extend("toAssignable", function (inner) { return function (node) { if (node.type === "TypeCastExpression") { - return typeCastToParameter(node); + return this.typeCastToParameter(node); } else { return inner.apply(this, arguments); } @@ -883,7 +889,7 @@ export default function (instance) { for (let i = 0; i < exprList.length; i++) { let expr = exprList[i]; if (expr && expr.type === "TypeCastExpression") { - exprList[i] = typeCastToParameter(expr); + exprList[i] = this.typeCastToParameter(expr); } } return inner.call(this, exprList, isBinding); @@ -982,7 +988,7 @@ export default function (instance) { }; }); - // parse type parameters for object method shorthand + // parse type parameters for object method shorthand instance.extend("parseObjPropValue", function (inner) { return function (prop) { let typeParameters; diff --git a/test/fixtures/flow/optional-type/3/expected.json b/test/fixtures/flow/optional-type/3/expected.json index 1105c60134..4958cf281a 100644 --- a/test/fixtures/flow/optional-type/3/expected.json +++ b/test/fixtures/flow/optional-type/3/expected.json @@ -126,7 +126,7 @@ "left": { "type": "Identifier", "start": 15, - "end": 16, + "end": 24, "loc": { "start": { "line": 1, @@ -134,7 +134,7 @@ }, "end": { "line": 1, - "column": 16 + "column": 24 } }, "name": "y", diff --git a/test/fixtures/flow/regression/issue-2493/expected.json b/test/fixtures/flow/regression/issue-2493/expected.json index 3cd5ae4637..7442f6e26f 100644 --- a/test/fixtures/flow/regression/issue-2493/expected.json +++ b/test/fixtures/flow/regression/issue-2493/expected.json @@ -109,7 +109,7 @@ "left": { "type": "Identifier", "start": 13, - "end": 21, + "end": 28, "loc": { "start": { "line": 1, @@ -117,7 +117,7 @@ }, "end": { "line": 1, - "column": 21 + "column": 28 } }, "name": "greeting", diff --git a/test/fixtures/flow/trailing-function-commas-type/1/expected.json b/test/fixtures/flow/trailing-function-commas-type/1/expected.json index ef16f0c04d..114d371dff 100644 --- a/test/fixtures/flow/trailing-function-commas-type/1/expected.json +++ b/test/fixtures/flow/trailing-function-commas-type/1/expected.json @@ -64,7 +64,7 @@ { "type": "Identifier", "start": 2, - "end": 7, + "end": 17, "loc": { "start": { "line": 1, @@ -72,7 +72,7 @@ }, "end": { "line": 1, - "column": 7 + "column": 17 } }, "name": "props", diff --git a/test/fixtures/flow/type-annotations/101/expected.json b/test/fixtures/flow/type-annotations/101/expected.json index 7cc317bf8f..ae370710de 100644 --- a/test/fixtures/flow/type-annotations/101/expected.json +++ b/test/fixtures/flow/type-annotations/101/expected.json @@ -126,7 +126,7 @@ { "type": "RestElement", "start": 2, - "end": 10, + "end": 20, "loc": { "start": { "line": 1, @@ -134,7 +134,7 @@ }, "end": { "line": 1, - "column": 10 + "column": 20 } }, "argument": { @@ -229,4 +229,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/type-annotations/103/expected.json b/test/fixtures/flow/type-annotations/103/expected.json index 5eda80448f..0ef3fc5e61 100644 --- a/test/fixtures/flow/type-annotations/103/expected.json +++ b/test/fixtures/flow/type-annotations/103/expected.json @@ -142,7 +142,7 @@ { "type": "Identifier", "start": 16, - "end": 24, + "end": 32, "loc": { "start": { "line": 1, @@ -150,7 +150,7 @@ }, "end": { "line": 1, - "column": 24 + "column": 32 } }, "name": "rootPath", @@ -188,7 +188,7 @@ { "type": "RestElement", "start": 34, - "end": 49, + "end": 64, "loc": { "start": { "line": 1, @@ -196,7 +196,7 @@ }, "end": { "line": 1, - "column": 49 + "column": 64 } }, "argument": { diff --git a/test/fixtures/flow/type-annotations/69/expected.json b/test/fixtures/flow/type-annotations/69/expected.json index 299efd3fe5..81386821a4 100644 --- a/test/fixtures/flow/type-annotations/69/expected.json +++ b/test/fixtures/flow/type-annotations/69/expected.json @@ -95,7 +95,7 @@ { "type": "Identifier", "start": 17, - "end": 20, + "end": 25, "loc": { "start": { "line": 1, @@ -103,7 +103,7 @@ }, "end": { "line": 1, - "column": 20 + "column": 25 } }, "name": "foo", @@ -158,7 +158,7 @@ { "type": "Identifier", "start": 27, - "end": 30, + "end": 35, "loc": { "start": { "line": 1, @@ -166,7 +166,7 @@ }, "end": { "line": 1, - "column": 30 + "column": 35 } }, "name": "bar", @@ -244,4 +244,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/type-annotations/80/expected.json b/test/fixtures/flow/type-annotations/80/expected.json index 35d21db92f..5df2f5ec56 100644 --- a/test/fixtures/flow/type-annotations/80/expected.json +++ b/test/fixtures/flow/type-annotations/80/expected.json @@ -63,7 +63,7 @@ { "type": "RestElement", "start": 2, - "end": 9, + "end": 24, "loc": { "start": { "line": 1, @@ -71,7 +71,7 @@ }, "end": { "line": 1, - "column": 9 + "column": 24 } }, "argument": { @@ -194,4 +194,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/type-annotations/existential-type-param-2/expected.json b/test/fixtures/flow/type-annotations/existential-type-param-2/expected.json index c9047516f4..12bf9c1462 100644 --- a/test/fixtures/flow/type-annotations/existential-type-param-2/expected.json +++ b/test/fixtures/flow/type-annotations/existential-type-param-2/expected.json @@ -155,7 +155,7 @@ { "type": "Identifier", "start": 13, - "end": 14, + "end": 25, "loc": { "start": { "line": 1, @@ -163,7 +163,7 @@ }, "end": { "line": 1, - "column": 14 + "column": 25 } }, "name": "x", @@ -257,4 +257,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/type-parameter-declaration/arrow/expected.json b/test/fixtures/flow/type-parameter-declaration/arrow/expected.json deleted file mode 100644 index 016fc254c0..0000000000 --- a/test/fixtures/flow/type-parameter-declaration/arrow/expected.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 0, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 0 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 0, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 0 - } - }, - "sourceType": "module", - "body": [], - "directives": [] - } -} \ No newline at end of file diff --git a/test/fixtures/flow/type-parameter-declaration/arrow_with_jsx/expected.json b/test/fixtures/flow/type-parameter-declaration/arrow_with_jsx/expected.json index 6316556c6e..c1220867e5 100644 --- a/test/fixtures/flow/type-parameter-declaration/arrow_with_jsx/expected.json +++ b/test/fixtures/flow/type-parameter-declaration/arrow_with_jsx/expected.json @@ -257,7 +257,7 @@ { "type": "Identifier", "start": 33, - "end": 34, + "end": 42, "loc": { "start": { "line": 3, @@ -265,7 +265,7 @@ }, "end": { "line": 3, - "column": 5 + "column": 13 } }, "name": "x", @@ -392,7 +392,7 @@ { "type": "Identifier", "start": 56, - "end": 57, + "end": 65, "loc": { "start": { "line": 4, @@ -400,7 +400,7 @@ }, "end": { "line": 4, - "column": 5 + "column": 13 } }, "name": "x", diff --git a/test/fixtures/flow/type-parameter-declaration/arrow_without_jsx/expected.json b/test/fixtures/flow/type-parameter-declaration/arrow_without_jsx/expected.json index 6316556c6e..712058fe08 100644 --- a/test/fixtures/flow/type-parameter-declaration/arrow_without_jsx/expected.json +++ b/test/fixtures/flow/type-parameter-declaration/arrow_without_jsx/expected.json @@ -257,7 +257,7 @@ { "type": "Identifier", "start": 33, - "end": 34, + "end": 42, "loc": { "start": { "line": 3, @@ -265,7 +265,7 @@ }, "end": { "line": 3, - "column": 5 + "column": 13 } }, "name": "x", @@ -392,7 +392,7 @@ { "type": "Identifier", "start": 56, - "end": 57, + "end": 65, "loc": { "start": { "line": 4, @@ -400,7 +400,7 @@ }, "end": { "line": 4, - "column": 5 + "column": 13 } }, "name": "x", @@ -527,4 +527,4 @@ ], "directives": [] } -} \ No newline at end of file +}