diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index c23fd5d3c6..72aa5c4b9e 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -342,17 +342,18 @@ export default (superClass: Class): Class => } tsParseBindingListForSignature(): $ReadOnlyArray< - N.Identifier | N.RestElement | N.ObjectPattern, + N.Identifier | N.RestElement | N.ObjectPattern | N.ArrayPattern, > { return this.parseBindingList(tt.parenR).map(pattern => { if ( pattern.type !== "Identifier" && pattern.type !== "RestElement" && - pattern.type !== "ObjectPattern" + pattern.type !== "ObjectPattern" && + pattern.type !== "ArrayPattern" ) { throw this.unexpected( pattern.start, - `Name in a signature must be an Identifier or ObjectPattern, instead got ${ + `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${ pattern.type }`, ); @@ -794,6 +795,21 @@ export default (superClass: Class): Class => return true; } + if (this.match(tt.bracketL)) { + let braceStackCounter = 1; + this.next(); + + while (braceStackCounter > 0) { + if (this.match(tt.bracketL)) { + ++braceStackCounter; + } else if (this.match(tt.bracketR)) { + --braceStackCounter; + } + this.next(); + } + return true; + } + return false; } diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index f00eb1be27..06e5a03a71 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1076,7 +1076,9 @@ export type TsSignatureDeclaration = export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & { // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. - parameters: $ReadOnlyArray, + parameters: $ReadOnlyArray< + Identifier | RestElement | ObjectPattern | ArrayPattern, + >, typeAnnotation: ?TsTypeAnnotation, }; diff --git a/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/input.js b/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/input.js index 8b22756932..56f3b25d96 100644 --- a/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/input.js +++ b/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/input.js @@ -1 +1,3 @@ type MyType = ({ theme }: any) => any + +type AnotherType = ([a]: any) => any diff --git a/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/output.json b/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/output.json index c93fa7945f..38916a8426 100644 --- a/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/output.json +++ b/packages/babel-parser/test/fixtures/typescript/regression/destructuring-in-function-type/output.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 37, + "end": 75, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 37 + "line": 3, + "column": 36 } }, "program": { "type": "Program", "start": 0, - "end": 37, + "end": 75, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 37 + "line": 3, + "column": 36 } }, "sourceType": "module", @@ -209,6 +209,149 @@ } } } + }, + { + "type": "TSTypeAliasDeclaration", + "start": 39, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "start": 44, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 16 + }, + "identifierName": "AnotherType" + }, + "name": "AnotherType" + }, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 58, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "parameters": [ + { + "type": "ArrayPattern", + "start": 59, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 20 + }, + "end": { + "line": 3, + "column": 28 + } + }, + "elements": [ + { + "type": "Identifier", + "start": 60, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "a" + }, + "name": "a" + } + ], + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 62, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 23 + }, + "end": { + "line": 3, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 64, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 25 + }, + "end": { + "line": 3, + "column": 28 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 69, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 72, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 36 + } + } + } + } + } } ], "directives": []