Fix arrow param locations with flow types

This patch corrects the end locations for params in arrow functions
which use type params.
This commit is contained in:
Daniel Tschinder 2016-06-26 12:59:19 +02:00 committed by Daniel Tschinder
parent f576865ce9
commit cd987cd8e4
12 changed files with 47 additions and 74 deletions

View File

@ -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);

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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": {

View File

@ -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": {

View File

@ -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",

View File

@ -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": {

View File

@ -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",

View File

@ -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": []
}
}

View File

@ -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",

View File

@ -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",