[ts] Allow optional binding pattern parameters within types/interfaces (#12227)
This commit is contained in:
parent
ddb7e05f07
commit
d51aa6d761
@ -2573,7 +2573,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
// Allow type annotations inside of a parameter list.
|
||||
parseAssignableListItemTypes(param: N.Pattern) {
|
||||
if (this.eat(tt.question)) {
|
||||
if (param.type !== "Identifier" && !this.state.isDeclareContext) {
|
||||
if (
|
||||
param.type !== "Identifier" &&
|
||||
!this.state.isDeclareContext &&
|
||||
!this.state.inType
|
||||
) {
|
||||
this.raise(param.start, TSErrors.PatternIsOptional);
|
||||
}
|
||||
|
||||
|
||||
5
packages/babel-parser/test/fixtures/typescript/declare/pattern-parameters/input.ts
vendored
Normal file
5
packages/babel-parser/test/fixtures/typescript/declare/pattern-parameters/input.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
declare interface B {
|
||||
foo([]?): void;
|
||||
bar({}, []?): any;
|
||||
baz(a: string, b: number, []?): void;
|
||||
}
|
||||
138
packages/babel-parser/test/fixtures/typescript/declare/pattern-parameters/output.json
vendored
Normal file
138
packages/babel-parser/test/fixtures/typescript/declare/pattern-parameters/output.json
vendored
Normal file
@ -0,0 +1,138 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TSInterfaceDeclaration",
|
||||
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19},"identifierName":"B"},
|
||||
"name": "B"
|
||||
},
|
||||
"body": {
|
||||
"type": "TSInterfaceBody",
|
||||
"start":20,"end":102,"loc":{"start":{"line":1,"column":20},"end":{"line":5,"column":1}},
|
||||
"body": [
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":24,"end":39,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":17}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":24,"end":27,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"foo"},
|
||||
"name": "foo"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":28,"end":31,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":32,"end":38,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":34,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":42,"end":60,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":42,"end":45,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"bar"},
|
||||
"name": "bar"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ObjectPattern",
|
||||
"start":46,"end":48,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}},
|
||||
"properties": []
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":50,"end":53,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":13}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":54,"end":59,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSAnyKeyword",
|
||||
"start":56,"end":59,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":63,"end":100,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":39}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":63,"end":66,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"baz"},
|
||||
"name": "baz"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":67,"end":76,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":15},"identifierName":"a"},
|
||||
"name": "a",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":68,"end":76,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":15}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSStringKeyword",
|
||||
"start":70,"end":76,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":15}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":78,"end":87,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":26},"identifierName":"b"},
|
||||
"name": "b",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":79,"end":87,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":26}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start":81,"end":87,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":26}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":89,"end":92,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":31}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":93,"end":99,"loc":{"start":{"line":4,"column":32},"end":{"line":4,"column":38}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":95,"end":99,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"declare": true
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
5
packages/babel-parser/test/fixtures/typescript/interface/pattern-parameters/input.ts
vendored
Normal file
5
packages/babel-parser/test/fixtures/typescript/interface/pattern-parameters/input.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
interface A {
|
||||
foo([]?): void;
|
||||
bar({}, []?): any;
|
||||
baz(a: string, b: number, []?): void;
|
||||
}
|
||||
137
packages/babel-parser/test/fixtures/typescript/interface/pattern-parameters/output.json
vendored
Normal file
137
packages/babel-parser/test/fixtures/typescript/interface/pattern-parameters/output.json
vendored
Normal file
@ -0,0 +1,137 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":94,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":94,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TSInterfaceDeclaration",
|
||||
"start":0,"end":94,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"A"},
|
||||
"name": "A"
|
||||
},
|
||||
"body": {
|
||||
"type": "TSInterfaceBody",
|
||||
"start":12,"end":94,"loc":{"start":{"line":1,"column":12},"end":{"line":5,"column":1}},
|
||||
"body": [
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":16,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":17}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":16,"end":19,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"foo"},
|
||||
"name": "foo"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":20,"end":23,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":24,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":26,"end":30,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":34,"end":52,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":34,"end":37,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"bar"},
|
||||
"name": "bar"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ObjectPattern",
|
||||
"start":38,"end":40,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}},
|
||||
"properties": []
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":42,"end":45,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":13}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":46,"end":51,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSAnyKeyword",
|
||||
"start":48,"end":51,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":55,"end":92,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":39}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":55,"end":58,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"baz"},
|
||||
"name": "baz"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":59,"end":68,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":15},"identifierName":"a"},
|
||||
"name": "a",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":60,"end":68,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":15}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSStringKeyword",
|
||||
"start":62,"end":68,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":15}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":70,"end":79,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":26},"identifierName":"b"},
|
||||
"name": "b",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":71,"end":79,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":26}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start":73,"end":79,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":26}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":81,"end":84,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":31}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":85,"end":91,"loc":{"start":{"line":4,"column":32},"end":{"line":4,"column":38}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":87,"end":91,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
5
packages/babel-parser/test/fixtures/typescript/types/pattern-parameters/input.ts
vendored
Normal file
5
packages/babel-parser/test/fixtures/typescript/types/pattern-parameters/input.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
type C = {
|
||||
foo([]?): void;
|
||||
bar({}, []?): any;
|
||||
baz(a: string, b: number, []?): void;
|
||||
};
|
||||
137
packages/babel-parser/test/fixtures/typescript/types/pattern-parameters/output.json
vendored
Normal file
137
packages/babel-parser/test/fixtures/typescript/types/pattern-parameters/output.json
vendored
Normal file
@ -0,0 +1,137 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":92,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":92,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TSTypeAliasDeclaration",
|
||||
"start":0,"end":92,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"C"},
|
||||
"name": "C"
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeLiteral",
|
||||
"start":9,"end":91,"loc":{"start":{"line":1,"column":9},"end":{"line":5,"column":1}},
|
||||
"members": [
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":13,"end":28,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":17}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":13,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"foo"},
|
||||
"name": "foo"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":17,"end":20,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":21,"end":27,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":23,"end":27,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":31,"end":49,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":31,"end":34,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"bar"},
|
||||
"name": "bar"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "ObjectPattern",
|
||||
"start":35,"end":37,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}},
|
||||
"properties": []
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":39,"end":42,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":13}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":43,"end":48,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSAnyKeyword",
|
||||
"start":45,"end":48,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSMethodSignature",
|
||||
"start":52,"end":89,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":39}},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start":52,"end":55,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"baz"},
|
||||
"name": "baz"
|
||||
},
|
||||
"computed": false,
|
||||
"parameters": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":56,"end":65,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":15},"identifierName":"a"},
|
||||
"name": "a",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":57,"end":65,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":15}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSStringKeyword",
|
||||
"start":59,"end":65,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":15}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start":67,"end":76,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":26},"identifierName":"b"},
|
||||
"name": "b",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":68,"end":76,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":26}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start":70,"end":76,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":26}}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ArrayPattern",
|
||||
"start":78,"end":81,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":31}},
|
||||
"elements": [],
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":82,"end":88,"loc":{"start":{"line":4,"column":32},"end":{"line":4,"column":38}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSVoidKeyword",
|
||||
"start":84,"end":88,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
@ -126,7 +126,6 @@ defaultValueInFunctionTypes.ts
|
||||
deleteOperator1.ts
|
||||
deleteOperatorInStrictMode.ts
|
||||
dependencyViaImportAlias.ts
|
||||
destructureOptionalParameter.ts
|
||||
destructuredDeclarationEmit.ts
|
||||
doubleUnderscoreExportStarConflict.ts
|
||||
duplicateIdentifierBindingElementInParameterDeclaration1.ts
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user