Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Fix arrow param locations with flow types
Browse files Browse the repository at this point in the history
This patch corrects the end locations for params in arrow functions
which use type params.
  • Loading branch information
danez committed Jun 29, 2016
1 parent 84b1bc5 commit 4d7fcca
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 71 deletions.
16 changes: 11 additions & 5 deletions src/plugins/flow.js
Expand Up @@ -843,15 +843,21 @@ export default function (instance) {
};
});

function typeCastToParameter(node) {
function typeCastToParameter(node, parser) {
node.expression.typeAnnotation = node.typeAnnotation;
return node.expression;

return parser.finishNodeAt(
node.expression,
node.expression.type,
node.typeAnnotation.end,
node.typeAnnotation.loc.end
);
}

instance.extend("toAssignable", function (inner) {
return function (node) {
if (node.type === "TypeCastExpression") {
return typeCastToParameter(node);
return typeCastToParameter(node, this);
} else {
return inner.apply(this, arguments);
}
Expand All @@ -864,7 +870,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] = typeCastToParameter(expr, this);
}
}
return inner.call(this, exprList, isBinding);
Expand Down Expand Up @@ -963,7 +969,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;
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/flow/optional-type/3/expected.json
Expand Up @@ -126,15 +126,15 @@
"left": {
"type": "Identifier",
"start": 15,
"end": 16,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 16
"column": 24
}
},
"name": "y",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/flow/regression/issue-2493/expected.json
Expand Up @@ -109,15 +109,15 @@
"left": {
"type": "Identifier",
"start": 13,
"end": 21,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 21
"column": 28
}
},
"name": "greeting",
Expand Down
Expand Up @@ -64,15 +64,15 @@
{
"type": "Identifier",
"start": 2,
"end": 7,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 7
"column": 17
}
},
"name": "props",
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/flow/type-annotations/101/expected.json
Expand Up @@ -126,15 +126,15 @@
{
"type": "RestElement",
"start": 2,
"end": 10,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 10
"column": 20
}
},
"argument": {
Expand Down Expand Up @@ -229,4 +229,4 @@
],
"directives": []
}
}
}
8 changes: 4 additions & 4 deletions test/fixtures/flow/type-annotations/103/expected.json
Expand Up @@ -142,15 +142,15 @@
{
"type": "Identifier",
"start": 16,
"end": 24,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 24
"column": 32
}
},
"name": "rootPath",
Expand Down Expand Up @@ -188,15 +188,15 @@
{
"type": "RestElement",
"start": 34,
"end": 49,
"end": 64,
"loc": {
"start": {
"line": 1,
"column": 34
},
"end": {
"line": 1,
"column": 49
"column": 64
}
},
"argument": {
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/flow/type-annotations/69/expected.json
Expand Up @@ -95,15 +95,15 @@
{
"type": "Identifier",
"start": 17,
"end": 20,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
"column": 25
}
},
"name": "foo",
Expand Down Expand Up @@ -158,15 +158,15 @@
{
"type": "Identifier",
"start": 27,
"end": 30,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 30
"column": 35
}
},
"name": "bar",
Expand Down Expand Up @@ -244,4 +244,4 @@
],
"directives": []
}
}
}
6 changes: 3 additions & 3 deletions test/fixtures/flow/type-annotations/80/expected.json
Expand Up @@ -63,15 +63,15 @@
{
"type": "RestElement",
"start": 2,
"end": 9,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 9
"column": 24
}
},
"argument": {
Expand Down Expand Up @@ -194,4 +194,4 @@
],
"directives": []
}
}
}
Expand Up @@ -155,15 +155,15 @@
{
"type": "Identifier",
"start": 13,
"end": 14,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
"column": 25
}
},
"name": "x",
Expand Down Expand Up @@ -257,4 +257,4 @@
],
"directives": []
}
}
}
33 changes: 0 additions & 33 deletions test/fixtures/flow/type-parameter-declaration/arrow/expected.json

This file was deleted.

Expand Up @@ -257,15 +257,15 @@
{
"type": "Identifier",
"start": 33,
"end": 34,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 5
"column": 13
}
},
"name": "x",
Expand Down Expand Up @@ -392,15 +392,15 @@
{
"type": "Identifier",
"start": 56,
"end": 57,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
"column": 13
}
},
"name": "x",
Expand Down
Expand Up @@ -257,15 +257,15 @@
{
"type": "Identifier",
"start": 33,
"end": 34,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 5
"column": 13
}
},
"name": "x",
Expand Down Expand Up @@ -392,15 +392,15 @@
{
"type": "Identifier",
"start": 56,
"end": 57,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
"column": 13
}
},
"name": "x",
Expand Down Expand Up @@ -527,4 +527,4 @@
],
"directives": []
}
}
}

0 comments on commit 4d7fcca

Please sign in to comment.