[Typescript] - Fix SyntaxError in async arrow functions with rest params (#7297)

* Fix: [Typescript] - SyntaxError in async arrow functions with rest params

* Fix optional params and add test code in flow
This commit is contained in:
K Sashi Kumar 2018-02-01 01:35:24 +05:30 committed by Nicolò Ribaudo
parent eb38ea2b10
commit ed98d2491e
10 changed files with 705 additions and 3 deletions

View File

@ -1683,7 +1683,13 @@ export default class ExpressionParser extends LValParser {
if (allowEmpty && this.match(tt.comma)) {
elt = null;
} else if (this.match(tt.ellipsis)) {
elt = this.parseSpread(refShorthandDefaultPos);
const spreadNodeStartPos = this.state.start;
const spreadNodeStartLoc = this.state.startLoc;
elt = this.parseParenItem(
this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos),
spreadNodeStartPos,
spreadNodeStartLoc,
);
if (refTrailingCommaPos && this.match(tt.comma)) {
refTrailingCommaPos.start = this.state.start;

View File

@ -183,10 +183,18 @@ export default class LValParser extends NodeUtils {
// Parses spread element.
parseSpread<T: RestElement | SpreadElement>(refShorthandDefaultPos: ?Pos): T {
parseSpread<T: RestElement | SpreadElement>(
refShorthandDefaultPos: ?Pos,
refNeedsArrowPos?: ?Pos,
): T {
const node = this.startNode();
this.next();
node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos);
node.argument = this.parseMaybeAssign(
false,
refShorthandDefaultPos,
undefined,
refNeedsArrowPos,
);
return this.finishNode(node, "SpreadElement");
}

View File

@ -0,0 +1 @@
async (...args?: any) => {};

View File

@ -0,0 +1,149 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "RestElement",
"start": 7,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 20
}
},
"argument": {
"type": "Identifier",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "args"
},
"name": "args"
},
"optional": true,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 15,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 20
}
},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 25,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 27
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
async (...args: any) => {};

View File

@ -0,0 +1,148 @@
{
"type": "File",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"program": {
"type": "Program",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "RestElement",
"start": 7,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 19
}
},
"argument": {
"type": "Identifier",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "args"
},
"name": "args"
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 14,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 19
}
},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 24,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 26
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
async(...args?: any[]) : any => {}

View File

@ -0,0 +1,194 @@
{
"type": "File",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"returnType": {
"type": "TSTypeAnnotation",
"start": 23,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 28
}
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start": 25,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 28
}
}
}
},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "RestElement",
"start": 6,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 21
}
},
"argument": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "args"
},
"name": "args"
},
"optional": true,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 14,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 21
}
},
"typeAnnotation": {
"type": "TSArrayType",
"start": 16,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 21
}
},
"elementType": {
"type": "TSAnyKeyword",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 32,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 34
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
async (...args: any[]) : any => {}

View File

@ -0,0 +1,193 @@
{
"type": "File",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 34
}
},
"returnType": {
"type": "TSTypeAnnotation",
"start": 23,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 28
}
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start": 25,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 28
}
}
}
},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "RestElement",
"start": 7,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 21
}
},
"argument": {
"type": "Identifier",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "args"
},
"name": "args"
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 14,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 21
}
},
"typeAnnotation": {
"type": "TSArrayType",
"start": 16,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 21
}
},
"elementType": {
"type": "TSAnyKeyword",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
}
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 32,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 34
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}