Skip to content

Commit

Permalink
Fix spread after optional case; add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Retsam committed Oct 9, 2018
1 parent 9dc2aee commit 6146a3a
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -513,14 +513,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// If there's a rest element, it must be at the end of the tuple
let seenOptionalElement = false;
node.elementTypes.forEach((elementNode, i) => {
if (
elementNode.type === "TSRestType" &&
i !== node.elementTypes.length - 1
) {
this.raise(
elementNode.start,
"A rest element must be last in a tuple type.",
);
if (elementNode.type === "TSRestType") {
if (i !== node.elementTypes.length - 1) {
this.raise(
elementNode.start,
"A rest element must be last in a tuple type.",
);
}
} else if (elementNode.type === "TSOptionalType") {
seenOptionalElement = true;
} else if (seenOptionalElement) {
Expand Down
@@ -0,0 +1 @@
function foo(...args: [number, string?, ...number[]]) {}
@@ -0,0 +1,242 @@
{
"type": "File",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 56
}
},
"program": {
"type": "Program",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 56
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 56
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"async": false,
"params": [
{
"type": "RestElement",
"start": 13,
"end": 52,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 52
}
},
"argument": {
"type": "Identifier",
"start": 16,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "args"
},
"name": "args"
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 20,
"end": 52,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 52
}
},
"typeAnnotation": {
"type": "TSTupleType",
"start": 22,
"end": 52,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 52
}
},
"elementTypes": [
{
"type": "TSNumberKeyword",
"start": 23,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 29
}
}
},
{
"type": "TSOptionalType",
"start": 31,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 38
}
},
"typeAnnotation": {
"type": "TSStringKeyword",
"start": 31,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 37
}
}
}
},
{
"type": "TSRestType",
"start": 40,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 51
}
},
"typeAnnotation": {
"type": "TSArrayType",
"start": 43,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 43
},
"end": {
"line": 1,
"column": 51
}
},
"elementType": {
"type": "TSNumberKeyword",
"start": 43,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 43
},
"end": {
"line": 1,
"column": 49
}
}
}
}
}
]
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 54,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 54
},
"end": {
"line": 1,
"column": 56
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

0 comments on commit 6146a3a

Please sign in to comment.