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

Commit

Permalink
[Flow] Arrow function type parameter declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
gabelevi committed Jun 24, 2016
1 parent cde17b3 commit 7247bbf
Show file tree
Hide file tree
Showing 13 changed files with 1,192 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(">")) {
Expand Down Expand Up @@ -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);
};
});
Expand Down Expand Up @@ -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;
let state = this.state.clone();
if (tt.jsxTagStart && this.match(tt.jsxTagStart)) {
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) {
Expand Down
33 changes: 33 additions & 0 deletions test/fixtures/flow/type-parameter-declaration/arrow/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<T>"I'm not an arrow function"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["flow", "jsx"],
"throws": "Unterminated JSX contents (1:3)"
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<T>"I'm not an arrow function"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["flow"],
"throws": "Expected an arrow function after this type parameter declaration (1:0)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<T>() => 123;
<T>(x) => 123;
<T>(x: number) => 123;
<T>(x: number) => { 123 };

Loading

0 comments on commit 7247bbf

Please sign in to comment.