Merge pull request #54 from gabelevi/arrow

[Flow] Arrow function type parameter declarations
This commit is contained in:
Sebastian McKenzie 2016-06-24 18:36:07 +01:00 committed by GitHub
commit d976b56bbe
12 changed files with 1192 additions and 1 deletions

View File

@ -2,6 +2,7 @@
/* eslint max-len: 0 */
import { types as tt } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import Parser from "../parser";
let pp = Parser.prototype;
@ -220,7 +221,12 @@ pp.flowParseTypeParameterDeclaration = function () {
let node = this.startNode();
node.params = [];
this.expectRelational("<");
if (this.isRelational("<") || this.match(tt.jsxTagStart)) {
this.next();
} else {
this.unexpected();
}
do {
node.params.push(this.flowParseTypeParameter());
if (!this.isRelational(">")) {
@ -1017,9 +1023,12 @@ export default function (instance) {
// parse function type parameters - function foo<T>() {}
instance.extend("parseFunctionParams", function (inner) {
return function (node) {
const oldInType = this.state.inType;
this.state.inType = true;
if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
}
this.state.inType = oldInType;
inner.call(this, node);
};
});
@ -1053,6 +1062,68 @@ export default function (instance) {
};
});
// We need to support type parameter declarations for arrow functions. This
// is tricky. There are three situations we need to handle
//
// 1. This is either JSX or an arrow function. We'll try JSX first. If that
// fails, we'll try an arrow function. If that fails, we'll throw the JSX
// error.
// 2. This is an arrow function. We'll parse the type parameter declaration,
// parse the rest, make sure the rest is an arrow function, and go from
// there
// 3. This is neither. Just call the inner function
instance.extend("parseMaybeAssign", function (inner) {
return function (...args) {
let jsxError = null;
if (tt.jsxTagStart && this.match(tt.jsxTagStart)) {
const state = this.state.clone();
try {
return inner.apply(this, args);
} catch (err) {
if (err instanceof SyntaxError) {
this.state = state;
jsxError = err;
} else {
throw err;
}
}
}
// Need to push something onto the context to stop
// the JSX plugin from messing with the tokens
this.state.context.push(ct.parenExpression);
if (jsxError != null || this.isRelational("<")) {
let arrowExpression;
let typeParameters;
try {
const oldInType = this.state.inType;
this.state.inType = true;
typeParameters = this.flowParseTypeParameterDeclaration();
this.state.inType = oldInType;
arrowExpression = inner.apply(this, args);
arrowExpression.typeParameters = typeParameters;
} catch (err) {
throw jsxError || err;
}
if (arrowExpression.type === "ArrowFunctionExpression") {
return arrowExpression;
} else if (jsxError != null) {
throw jsxError;
} else {
this.raise(
typeParameters.start,
"Expected an arrow function after this type parameter declaration",
);
}
}
this.state.context.pop();
return inner.apply(this, args);
};
});
// handle return types for arrow functions
instance.extend("parseArrow", function (inner) {
return function (node) {

View File

@ -0,0 +1,33 @@
{
"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

@ -0,0 +1 @@
<T>"I'm not an arrow function"

View File

@ -0,0 +1,4 @@
{
"plugins": ["flow", "jsx"],
"throws": "Unterminated JSX contents (1:3)"
}

View File

@ -0,0 +1 @@
<T>"I'm not an arrow function"

View File

@ -0,0 +1,4 @@
{
"plugins": ["flow"],
"throws": "Expected an arrow function after this type parameter declaration (1:0)"
}

View File

@ -0,0 +1,5 @@
<T>() => 123;
<T>(x) => 123;
<T>(x: number) => 123;
<T>(x: number) => { 123 };

View File

@ -0,0 +1,530 @@
{
"type": "File",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"program": {
"type": "Program",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 3,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 12
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [],
"body": {
"type": "NumericLiteral",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 1,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 14,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 14
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 17,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 13
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 5
}
},
"name": "x"
}
],
"body": {
"type": "NumericLiteral",
"start": 24,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 29,
"end": 51,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 22
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 32,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 21
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 5
}
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 34,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 13
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 36,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 13
}
}
}
}
}
],
"body": {
"type": "NumericLiteral",
"start": 47,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 18
},
"end": {
"line": 3,
"column": 21
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 30,
"end": 31,
"loc": {
"start": {
"line": 3,
"column": 1
},
"end": {
"line": 3,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 52,
"end": 78,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 55,
"end": 77,
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 25
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
}
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 57,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 5
},
"end": {
"line": 4,
"column": 13
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 59,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 13
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 70,
"end": 77,
"loc": {
"start": {
"line": 4,
"column": 18
},
"end": {
"line": 4,
"column": 25
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 72,
"end": 75,
"loc": {
"start": {
"line": 4,
"column": 20
},
"end": {
"line": 4,
"column": 23
}
},
"expression": {
"type": "NumericLiteral",
"start": 72,
"end": 75,
"loc": {
"start": {
"line": 4,
"column": 20
},
"end": {
"line": 4,
"column": 23
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
}
}
],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 52,
"end": 55,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 53,
"end": 54,
"loc": {
"start": {
"line": 4,
"column": 1
},
"end": {
"line": 4,
"column": 2
}
},
"name": "T"
}
]
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["flow", "jsx"]
}

View File

@ -0,0 +1,5 @@
<T>() => 123;
<T>(x) => 123;
<T>(x: number) => 123;
<T>(x: number) => { 123 };

View File

@ -0,0 +1,530 @@
{
"type": "File",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"program": {
"type": "Program",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 3,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 12
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [],
"body": {
"type": "NumericLiteral",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 1,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 14,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 14
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 17,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 13
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 5
}
},
"name": "x"
}
],
"body": {
"type": "NumericLiteral",
"start": 24,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 29,
"end": 51,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 22
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 32,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 21
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 5
}
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 34,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 13
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 36,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 13
}
}
}
}
}
],
"body": {
"type": "NumericLiteral",
"start": 47,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 18
},
"end": {
"line": 3,
"column": 21
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 30,
"end": 31,
"loc": {
"start": {
"line": 3,
"column": 1
},
"end": {
"line": 3,
"column": 2
}
},
"name": "T"
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 52,
"end": 78,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 26
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 55,
"end": 77,
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 25
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
}
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 57,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 5
},
"end": {
"line": 4,
"column": 13
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 59,
"end": 65,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 13
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 70,
"end": 77,
"loc": {
"start": {
"line": 4,
"column": 18
},
"end": {
"line": 4,
"column": 25
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 72,
"end": 75,
"loc": {
"start": {
"line": 4,
"column": 20
},
"end": {
"line": 4,
"column": 23
}
},
"expression": {
"type": "NumericLiteral",
"start": 72,
"end": 75,
"loc": {
"start": {
"line": 4,
"column": 20
},
"end": {
"line": 4,
"column": 23
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
}
}
],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 52,
"end": 55,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 3
}
},
"params": [
{
"type": "TypeParameter",
"start": 53,
"end": 54,
"loc": {
"start": {
"line": 4,
"column": 1
},
"end": {
"line": 4,
"column": 2
}
},
"name": "T"
}
]
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["flow"]
}