Disallow trailing comma after rest (#9311)
* Add new tests * Use state instead of param and disallow comma in [...a,]=[] * Unify error messages * Object destructuring * Update whitelist
This commit is contained in:
@@ -546,17 +546,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
// No mandatory elements may follow optional elements
|
||||
// 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") {
|
||||
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") {
|
||||
node.elementTypes.forEach(elementNode => {
|
||||
if (elementNode.type === "TSOptionalType") {
|
||||
seenOptionalElement = true;
|
||||
} else if (seenOptionalElement) {
|
||||
} else if (seenOptionalElement && elementNode.type !== "TSRestType") {
|
||||
this.raise(
|
||||
elementNode.start,
|
||||
"A required element cannot follow an optional element.",
|
||||
@@ -573,6 +566,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
const restNode: N.TsRestType = this.startNode();
|
||||
this.next(); // skips ellipsis
|
||||
restNode.typeAnnotation = this.tsParseType();
|
||||
this.checkCommaAfterRest(tt.bracketR, "type");
|
||||
return this.finishNode(restNode, "TSRestType");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user