diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 6309c8ea8fcb..dd74f5c32cec 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -630,9 +630,8 @@ export default (superClass: Class): Class => /* skipFirstToken */ false, ); - // Validate the elementTypes to ensure: - // No mandatory elements may follow optional elements - // If there's a rest element, it must be at the end of the tuple + // Validate the elementTypes to ensure that no mandatory elements + // follow optional elements let seenOptionalElement = false; node.elementTypes.forEach(elementNode => { if (elementNode.type === "TSOptionalType") { @@ -651,12 +650,6 @@ export default (superClass: Class): Class => const restNode: N.TsRestType = this.startNode(); this.next(); // skips ellipsis restNode.typeAnnotation = this.tsParseType(); - if ( - this.match(tt.comma) && - this.lookaheadCharCode() !== charCodes.rightSquareBracket - ) { - this.raiseRestNotLast(this.state.start); - } return this.finishNode(restNode, "TSRestType"); } diff --git a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/input.ts b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/input.ts deleted file mode 100644 index 2bfeae2e87eb..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/input.ts +++ /dev/null @@ -1 +0,0 @@ -let x: [...number[], string] diff --git a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/options.json b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/options.json deleted file mode 100644 index 82b4f09fc940..000000000000 --- a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "sourceType": "module", - "plugins": ["typescript"], - "throws": "Rest element must be last element (1:19)" -} diff --git a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/input.ts b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/input.ts new file mode 100644 index 000000000000..050a60b540ea --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/input.ts @@ -0,0 +1 @@ +let x: [...[number, string], string] diff --git a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/options.json b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/options.json new file mode 100644 index 000000000000..fe9bffaa5e1a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["typescript"] +} diff --git a/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/output.json b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/output.json new file mode 100644 index 000000000000..5d0076a745ba --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/output.json @@ -0,0 +1,62 @@ +{ + "type": "File", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}}, + "program": { + "type": "Program", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36}}, + "id": { + "type": "Identifier", + "start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36},"identifierName":"x"}, + "name": "x", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":5,"end":36,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":36}}, + "typeAnnotation": { + "type": "TSTupleType", + "start":7,"end":36,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":36}}, + "elementTypes": [ + { + "type": "TSRestType", + "start":8,"end":27,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":27}}, + "typeAnnotation": { + "type": "TSTupleType", + "start":11,"end":27,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":27}}, + "elementTypes": [ + { + "type": "TSNumberKeyword", + "start":12,"end":18,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":18}} + }, + { + "type": "TSStringKeyword", + "start":20,"end":26,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":26}} + } + ] + } + }, + { + "type": "TSStringKeyword", + "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} + } + ] + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file