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

Commit

Permalink
Merge pull request #57 from danez/fix-arrow-location
Browse files Browse the repository at this point in the history
Fix arrow param locations with flow types
  • Loading branch information
Sebastian McKenzie committed Aug 16, 2016
2 parents 0b87d4c + cd987cd commit 43cdd2f
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 74 deletions.
22 changes: 14 additions & 8 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,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) {
Expand Down Expand Up @@ -866,15 +877,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);
}
Expand All @@ -887,7 +893,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);
Expand Down Expand Up @@ -986,7 +992,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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": []
}
}
}
Original file line number Diff line number Diff line change
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.

Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 43cdd2f

Please sign in to comment.