Skip to content

Commit 69246b6

Browse files
authored
[babel 8] fix properties name for function-like TS nodes (#13709)
* Align AST * Add tests for parser * Generator * Add babel-types support * Address review * Address review
1 parent 335622f commit 69246b6

File tree

150 files changed

+2757
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+2757
-105
lines changed

packages/babel-generator/src/generators/typescript.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,21 @@ export function tsPrintFunctionOrConstructorType(
225225
// todo: missing type FunctionOrConstructorType
226226
node: any,
227227
) {
228-
const { typeParameters, parameters } = node;
228+
const { typeParameters } = node;
229+
const parameters = process.env.BABEL_8_BREAKING
230+
? node.params
231+
: node.parameters;
229232
this.print(typeParameters, node);
230233
this.token("(");
231234
this._parameters(parameters, node);
232235
this.token(")");
233236
this.space();
234237
this.token("=>");
235238
this.space();
236-
this.print(node.typeAnnotation.typeAnnotation, node);
239+
const returnType = process.env.BABEL_8_BREAKING
240+
? node.returnType
241+
: node.typeAnnotation;
242+
this.print(returnType.typeAnnotation, node);
237243
}
238244

239245
export function TSTypeReference(this: Printer, node: t.TSTypeReference) {
@@ -648,12 +654,18 @@ export function TSNamespaceExportDeclaration(
648654
}
649655

650656
export function tsPrintSignatureDeclarationBase(this: Printer, node: any) {
651-
const { typeParameters, parameters } = node;
657+
const { typeParameters } = node;
658+
const parameters = process.env.BABEL_8_BREAKING
659+
? node.params
660+
: node.parameters;
652661
this.print(typeParameters, node);
653662
this.token("(");
654663
this._parameters(parameters, node);
655664
this.token(")");
656-
this.print(node.typeAnnotation, node);
665+
const returnType = process.env.BABEL_8_BREAKING
666+
? node.returnType
667+
: node.typeAnnotation;
668+
this.print(returnType, node);
657669
}
658670

659671
export function tsPrintClassMemberModifiers(this: Printer, node: any, isField) {

packages/babel-parser/src/plugins/typescript/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
584584
): void {
585585
// Arrow fns *must* have return token (`=>`). Normal functions can omit it.
586586
const returnTokenRequired = returnToken === tt.arrow;
587+
588+
// https://github.com/babel/babel/issues/9231
589+
const paramsKey = process.env.BABEL_8_BREAKING ? "params" : "parameters";
590+
const returnTypeKey = process.env.BABEL_8_BREAKING
591+
? "returnType"
592+
: "typeAnnotation";
593+
587594
signature.typeParameters = this.tsTryParseTypeParameters();
588595
this.expect(tt.parenL);
589-
signature.parameters = this.tsParseBindingListForSignature();
596+
signature[paramsKey] = this.tsParseBindingListForSignature();
590597
if (returnTokenRequired) {
591-
signature.typeAnnotation =
598+
signature[returnTypeKey] =
592599
this.tsParseTypeOrTypePredicateAnnotation(returnToken);
593600
} else if (this.match(returnToken)) {
594-
signature.typeAnnotation =
601+
signature[returnTypeKey] =
595602
this.tsParseTypeOrTypePredicateAnnotation(returnToken);
596603
}
597604
}
@@ -683,21 +690,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
683690
}
684691
this.tsFillSignature(tt.colon, method);
685692
this.tsParseTypeMemberSemicolon();
693+
const paramsKey = process.env.BABEL_8_BREAKING
694+
? "params"
695+
: "parameters";
696+
const returnTypeKey = process.env.BABEL_8_BREAKING
697+
? "returnType"
698+
: "typeAnnotation";
686699
if (method.kind === "get") {
687-
if (method.parameters.length > 0) {
700+
if (method[paramsKey].length > 0) {
688701
this.raise(this.state.pos, Errors.BadGetterArity);
689-
if (this.isThisParam(method.parameters[0])) {
702+
if (this.isThisParam(method[paramsKey][0])) {
690703
this.raise(
691704
this.state.pos,
692705
TSErrors.AccesorCannotDeclareThisParameter,
693706
);
694707
}
695708
}
696709
} else if (method.kind === "set") {
697-
if (method.parameters.length !== 1) {
710+
if (method[paramsKey].length !== 1) {
698711
this.raise(this.state.pos, Errors.BadSetterArity);
699712
} else {
700-
const firstParameter = method.parameters[0];
713+
const firstParameter = method[paramsKey][0];
701714
if (this.isThisParam(firstParameter)) {
702715
this.raise(
703716
this.state.pos,
@@ -720,9 +733,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
720733
);
721734
}
722735
}
723-
if (method.typeAnnotation) {
736+
if (method[returnTypeKey]) {
724737
this.raise(
725-
method.typeAnnotation.start,
738+
method[returnTypeKey].start,
726739
TSErrors.SetAccesorCannotHaveReturnType,
727740
);
728741
}

packages/babel-parser/src/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,11 @@ export type TsSignatureDeclaration =
12551255

12561256
export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & {
12571257
// Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions.
1258+
params: $ReadOnlyArray<
1259+
Identifier | RestElement | ObjectPattern | ArrayPattern,
1260+
>,
1261+
returnType: ?TsTypeAnnotation,
1262+
// TODO(Babel-8): Remove
12581263
parameters: $ReadOnlyArray<
12591264
Identifier | RestElement | ObjectPattern | ArrayPattern,
12601265
>,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare interface B {
2+
foo([]?): void;
3+
bar({}, []?): any;
4+
baz(a: string, b: number, []?): void;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "TSInterfaceDeclaration",
12+
"start":0,"end":102,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19},"identifierName":"B"},
16+
"name": "B"
17+
},
18+
"body": {
19+
"type": "TSInterfaceBody",
20+
"start":20,"end":102,"loc":{"start":{"line":1,"column":20},"end":{"line":5,"column":1}},
21+
"body": [
22+
{
23+
"type": "TSMethodSignature",
24+
"start":24,"end":39,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":17}},
25+
"key": {
26+
"type": "Identifier",
27+
"start":24,"end":27,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"foo"},
28+
"name": "foo"
29+
},
30+
"computed": false,
31+
"parameters": [
32+
{
33+
"type": "ArrayPattern",
34+
"start":28,"end":31,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9}},
35+
"elements": [],
36+
"optional": true
37+
}
38+
],
39+
"typeAnnotation": {
40+
"type": "TSTypeAnnotation",
41+
"start":32,"end":38,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}},
42+
"typeAnnotation": {
43+
"type": "TSVoidKeyword",
44+
"start":34,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
45+
}
46+
},
47+
"kind": "method"
48+
},
49+
{
50+
"type": "TSMethodSignature",
51+
"start":42,"end":60,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
52+
"key": {
53+
"type": "Identifier",
54+
"start":42,"end":45,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"bar"},
55+
"name": "bar"
56+
},
57+
"computed": false,
58+
"parameters": [
59+
{
60+
"type": "ObjectPattern",
61+
"start":46,"end":48,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}},
62+
"properties": []
63+
},
64+
{
65+
"type": "ArrayPattern",
66+
"start":50,"end":53,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":13}},
67+
"elements": [],
68+
"optional": true
69+
}
70+
],
71+
"typeAnnotation": {
72+
"type": "TSTypeAnnotation",
73+
"start":54,"end":59,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
74+
"typeAnnotation": {
75+
"type": "TSAnyKeyword",
76+
"start":56,"end":59,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
77+
}
78+
},
79+
"kind": "method"
80+
},
81+
{
82+
"type": "TSMethodSignature",
83+
"start":63,"end":100,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":39}},
84+
"key": {
85+
"type": "Identifier",
86+
"start":63,"end":66,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"baz"},
87+
"name": "baz"
88+
},
89+
"computed": false,
90+
"parameters": [
91+
{
92+
"type": "Identifier",
93+
"start":67,"end":76,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":15},"identifierName":"a"},
94+
"name": "a",
95+
"typeAnnotation": {
96+
"type": "TSTypeAnnotation",
97+
"start":68,"end":76,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":15}},
98+
"typeAnnotation": {
99+
"type": "TSStringKeyword",
100+
"start":70,"end":76,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":15}}
101+
}
102+
}
103+
},
104+
{
105+
"type": "Identifier",
106+
"start":78,"end":87,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":26},"identifierName":"b"},
107+
"name": "b",
108+
"typeAnnotation": {
109+
"type": "TSTypeAnnotation",
110+
"start":79,"end":87,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":26}},
111+
"typeAnnotation": {
112+
"type": "TSNumberKeyword",
113+
"start":81,"end":87,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":26}}
114+
}
115+
}
116+
},
117+
{
118+
"type": "ArrayPattern",
119+
"start":89,"end":92,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":31}},
120+
"elements": [],
121+
"optional": true
122+
}
123+
],
124+
"typeAnnotation": {
125+
"type": "TSTypeAnnotation",
126+
"start":93,"end":99,"loc":{"start":{"line":4,"column":32},"end":{"line":4,"column":38}},
127+
"typeAnnotation": {
128+
"type": "TSVoidKeyword",
129+
"start":95,"end":99,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
130+
}
131+
},
132+
"kind": "method"
133+
}
134+
]
135+
},
136+
"declare": true
137+
}
138+
],
139+
"directives": []
140+
}
141+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": true
3+
}

packages/babel-parser/test/fixtures/typescript/declare/pattern-parameters/output.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
"name": "foo"
2929
},
3030
"computed": false,
31-
"parameters": [
31+
"params": [
3232
{
3333
"type": "ArrayPattern",
3434
"start":28,"end":31,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9}},
3535
"elements": [],
3636
"optional": true
3737
}
3838
],
39-
"typeAnnotation": {
39+
"returnType": {
4040
"type": "TSTypeAnnotation",
4141
"start":32,"end":38,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}},
4242
"typeAnnotation": {
@@ -55,7 +55,7 @@
5555
"name": "bar"
5656
},
5757
"computed": false,
58-
"parameters": [
58+
"params": [
5959
{
6060
"type": "ObjectPattern",
6161
"start":46,"end":48,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}},
@@ -68,7 +68,7 @@
6868
"optional": true
6969
}
7070
],
71-
"typeAnnotation": {
71+
"returnType": {
7272
"type": "TSTypeAnnotation",
7373
"start":54,"end":59,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
7474
"typeAnnotation": {
@@ -87,7 +87,7 @@
8787
"name": "baz"
8888
},
8989
"computed": false,
90-
"parameters": [
90+
"params": [
9191
{
9292
"type": "Identifier",
9393
"start":67,"end":76,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":15},"identifierName":"a"},
@@ -121,7 +121,7 @@
121121
"optional": true
122122
}
123123
],
124-
"typeAnnotation": {
124+
"returnType": {
125125
"type": "TSTypeAnnotation",
126126
"start":93,"end":99,"loc":{"start":{"line":4,"column":32},"end":{"line":4,"column":38}},
127127
"typeAnnotation": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface I {
2+
(x: number): void;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy