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

Commit

Permalink
Optional names for function types and object type indexers (#197)
Browse files Browse the repository at this point in the history
* Use .gitattributes to ignore files with weird newlines

* [Flow] Make parameter names in function types optional

* [Flow] Anonymous function types with single params can omit parens

* [Flow] Optional names for object indexer keys

* Add noAnonFunctionType explicitly to state

* Adjust gitattributes as files have been fixed
  • Loading branch information
gabelevi authored and danez committed Nov 9, 2016
1 parent e05bbee commit 6431247
Show file tree
Hide file tree
Showing 66 changed files with 5,017 additions and 14 deletions.
89 changes: 76 additions & 13 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,13 @@ pp.flowParseObjectTypeIndexer = function (node, isStatic, variance) {
node.static = isStatic;

this.expect(tt.bracketL);
node.id = this.flowParseObjectPropertyKey();
node.key = this.flowParseTypeInitialiser();
if (this.lookahead().type === tt.colon) {
node.id = this.flowParseObjectPropertyKey();
node.key = this.flowParseTypeInitialiser();
} else {
node.id = null;
node.key = this.flowParseType();
}
this.expect(tt.bracketR);
node.value = this.flowParseTypeInitialiser();
node.variance = variance;
Expand Down Expand Up @@ -466,19 +471,37 @@ pp.flowParseTupleType = function () {
};

pp.flowParseFunctionTypeParam = function () {
let name = null;
let optional = false;
let typeAnnotation = null;
let node = this.startNode();
node.name = this.parseIdentifier();
if (this.eat(tt.question)) {
optional = true;
const lh = this.lookahead();
if (lh.type === tt.colon ||
lh.type === tt.question) {
name = this.parseIdentifier();
if (this.eat(tt.question)) {
optional = true;
}
typeAnnotation = this.flowParseTypeInitialiser();
} else {
typeAnnotation = this.flowParseType();
}
node.name = name;
node.optional = optional;
node.typeAnnotation = this.flowParseTypeInitialiser();
node.typeAnnotation = typeAnnotation;
return this.finishNode(node, "FunctionTypeParam");
};

pp.flowParseFunctionTypeParams = function () {
let ret = { params: [], rest: null };
pp.reinterpretTypeAsFunctionTypeParam = function (type) {
let node = this.startNodeAt(type.start, type.loc);
node.name = null;
node.optional = false;
node.typeAnnotation = type;
return this.finishNode(node, "FunctionTypeParam");
};

pp.flowParseFunctionTypeParams = function (params = []) {
let ret = { params, rest: null };
while (this.match(tt.name)) {
ret.params.push(this.flowParseFunctionTypeParam());
if (!this.match(tt.parenR)) {
Expand Down Expand Up @@ -529,6 +552,7 @@ pp.flowParsePrimaryType = function () {
let tmp;
let type;
let isGroupedType = false;
let oldNoAnonFunctionType = this.state.noAnonFunctionType;

switch (this.state.type) {
case tt.name:
Expand Down Expand Up @@ -574,12 +598,30 @@ pp.flowParsePrimaryType = function () {
}

if (isGroupedType) {
this.state.noAnonFunctionType = false;
type = this.flowParseType();
this.expect(tt.parenR);
return type;
this.state.noAnonFunctionType = oldNoAnonFunctionType;

// A `,` or a `) =>` means this is an anonymous function type
if (this.state.noAnonFunctionType ||
!(this.match(tt.comma) ||
(this.match(tt.parenR) && this.lookahead().type === tt.arrow))) {
this.expect(tt.parenR);
return type;
} else {
// Eat a comma if there is one
this.eat(tt.comma);
}
}

if (type) {
tmp = this.flowParseFunctionTypeParams(
[this.reinterpretTypeAsFunctionTypeParam(type)],
);
} else {
tmp = this.flowParseFunctionTypeParams();
}

tmp = this.flowParseFunctionTypeParams();
node.params = tmp.params;
node.rest = tmp.rest;

Expand All @@ -588,6 +630,7 @@ pp.flowParsePrimaryType = function () {
this.expect(tt.arrow);

node.returnType = this.flowParseType();

node.typeParameters = null;

return this.finishNode(node, "FunctionTypeAnnotation");
Expand Down Expand Up @@ -668,12 +711,25 @@ pp.flowParsePrefixType = function () {
}
};

pp.flowParseAnonFunctionWithoutParens = function () {
const param = this.flowParsePrefixType();
if (!this.state.noAnonFunctionType && this.eat(tt.arrow)) {
const node = this.startNodeAt(param.start, param.loc);
node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
node.rest = null;
node.returnType = this.flowParseType();
node.typeParameters = null;
return this.finishNode(node, "FunctionTypeAnnotation");
}
return param;
};

pp.flowParseIntersectionType = function () {
let node = this.startNode();
let type = this.flowParsePrefixType();
let type = this.flowParseAnonFunctionWithoutParens();
node.types = [type];
while (this.eat(tt.bitwiseAND)) {
node.types.push(this.flowParsePrefixType());
node.types.push(this.flowParseAnonFunctionWithoutParens());
}
return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
};
Expand Down Expand Up @@ -1156,7 +1212,10 @@ export default function (instance) {
instance.extend("parseAsyncArrowFromCallExpression", function (inner) {
return function (node, call) {
if (this.match(tt.colon)) {
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
this.state.noAnonFunctionType = true;
node.returnType = this.flowParseTypeAnnotation();
this.state.noAnonFunctionType = oldNoAnonFunctionType;
}

return inner.call(this, node, call);
Expand Down Expand Up @@ -1238,7 +1297,11 @@ export default function (instance) {
if (this.match(tt.colon)) {
let state = this.state.clone();
try {
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
this.state.noAnonFunctionType = true;
let returnType = this.flowParseTypeAnnotation();
this.state.noAnonFunctionType = oldNoAnonFunctionType;

if (this.canInsertSemicolon()) this.unexpected();
if (!this.match(tt.arrow)) this.unexpected();
// assign after it is clear it is an arrow
Expand Down
8 changes: 7 additions & 1 deletion src/tokenizer/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export default class State {

this.potentialArrowAt = -1;

this.inMethod = this.inFunction = this.inGenerator = this.inAsync = this.inType = false;
this.inMethod =
this.inFunction =
this.inGenerator =
this.inAsync =
this.inType =
this.noAnonFunctionType =
false;

this.labels = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var f = (x): number => 123 => 123;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ; (1:27)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var f = (x): string | number => 123 => 123;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ; (1:36)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type A = string => void
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"sourceType": "module",
"body": [
{
"type": "TypeAlias",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "A"
},
"name": "A"
},
"typeParameters": null,
"right": {
"type": "FunctionTypeAnnotation",
"start": 9,
"end": 23,
"loc": {
"start": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 15
}
},
"end": {
"line": 1,
"column": 23
}
},
"params": [
{
"type": "FunctionTypeParam",
"start": 9,
"end": 18,
"loc": {
"start": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 15
}
},
"end": {
"line": 1,
"column": 18
}
},
"name": null,
"optional": false,
"typeAnnotation": {
"type": "StringTypeAnnotation",
"start": 9,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 15
}
}
}
}
],
"rest": null,
"returnType": {
"type": "VoidTypeAnnotation",
"start": 19,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 23
}
}
},
"typeParameters": null
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type A = Array<string> => void
Loading

0 comments on commit 6431247

Please sign in to comment.