-
-
Notifications
You must be signed in to change notification settings - Fork 257
Optional names for function types and object type indexers #197
Changes from 5 commits
a7dbb42
d898dd0
60171ac
168f83b
88e9335
da279e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
* text eol=lf | ||
test/fixtures/es2015/uncategorised/22/actual.js binary | ||
test/fixtures/es2015/uncategorised/23/actual.js binary | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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)) { | ||
|
@@ -529,6 +552,7 @@ pp.flowParsePrimaryType = function () { | |
let tmp; | ||
let type; | ||
let isGroupedType = false; | ||
let oldNoAnonFunctionType = this.state.noAnonFunctionType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will do! |
||
|
||
switch (this.state.type) { | ||
case tt.name: | ||
|
@@ -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; | ||
|
||
|
@@ -588,6 +630,7 @@ pp.flowParsePrimaryType = function () { | |
this.expect(tt.arrow); | ||
|
||
node.returnType = this.flowParseType(); | ||
|
||
node.typeParameters = null; | ||
|
||
return this.finishNode(node, "FunctionTypeAnnotation"); | ||
|
@@ -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"); | ||
}; | ||
|
@@ -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); | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var f = (x): number => 123 => 123; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have a better error message that says something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's not easy to do. The problem is that the first |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the issue you had with these two files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they have weird characters that the
.gitattribute
doesn't like if it treats them as text