Skip to content

Commit 9be2c7f

Browse files
authored
[ts] treat single type param with a constraint as unambiguous (#15436)
1 parent 43b2fc8 commit 9be2c7f

9 files changed

Lines changed: 289 additions & 10 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,6 +3479,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
34793479
reportReservedArrowTypeParam(node: any) {
34803480
if (
34813481
node.params.length === 1 &&
3482+
!node.params[0].constraint &&
34823483
!node.extra?.trailingComma &&
34833484
this.getPluginOption("typescript", "disallowAmbiguousJSXLike")
34843485
) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<T>() => 1;
22
<T>(x) => 1;
3+
<T = unknown>(x) => 1;

packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/output.json

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"type": "File",
3-
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":12,"index":24}},
3+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":47}},
44
"errors": [
55
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (1:0)",
6-
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (2:0)"
6+
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (2:0)",
7+
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (3:0)"
78
],
89
"program": {
910
"type": "Program",
10-
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":12,"index":24}},
11+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":47}},
1112
"sourceType": "script",
1213
"interpreter": null,
1314
"body": [
@@ -80,6 +81,48 @@
8081
]
8182
}
8283
}
84+
},
85+
{
86+
"type": "ExpressionStatement",
87+
"start":25,"end":47,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":22,"index":47}},
88+
"expression": {
89+
"type": "ArrowFunctionExpression",
90+
"start":25,"end":46,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":21,"index":46}},
91+
"id": null,
92+
"generator": false,
93+
"async": false,
94+
"params": [
95+
{
96+
"type": "Identifier",
97+
"start":39,"end":40,"loc":{"start":{"line":3,"column":14,"index":39},"end":{"line":3,"column":15,"index":40},"identifierName":"x"},
98+
"name": "x"
99+
}
100+
],
101+
"body": {
102+
"type": "NumericLiteral",
103+
"start":45,"end":46,"loc":{"start":{"line":3,"column":20,"index":45},"end":{"line":3,"column":21,"index":46}},
104+
"extra": {
105+
"rawValue": 1,
106+
"raw": "1"
107+
},
108+
"value": 1
109+
},
110+
"typeParameters": {
111+
"type": "TSTypeParameterDeclaration",
112+
"start":25,"end":38,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":13,"index":38}},
113+
"params": [
114+
{
115+
"type": "TSTypeParameter",
116+
"start":26,"end":37,"loc":{"start":{"line":3,"column":1,"index":26},"end":{"line":3,"column":12,"index":37}},
117+
"name": "T",
118+
"default": {
119+
"type": "TSUnknownKeyword",
120+
"start":30,"end":37,"loc":{"start":{"line":3,"column":5,"index":30},"end":{"line":3,"column":12,"index":37}}
121+
}
122+
}
123+
]
124+
}
125+
}
83126
}
84127
],
85128
"directives": []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
<T,>() => 1;
22
<T,>(x) => 1;
3+
<T extends unknown>(x) => 1;
4+
<T = unknown,>(x) => 1;

packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/output.json

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"type": "File",
3-
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":13,"index":26}},
3+
"start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":23,"index":79}},
44
"program": {
55
"type": "Program",
6-
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":13,"index":26}},
6+
"start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":23,"index":79}},
77
"sourceType": "script",
88
"interpreter": null,
99
"body": [
@@ -82,6 +82,93 @@
8282
}
8383
}
8484
}
85+
},
86+
{
87+
"type": "ExpressionStatement",
88+
"start":27,"end":55,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":28,"index":55}},
89+
"expression": {
90+
"type": "ArrowFunctionExpression",
91+
"start":27,"end":54,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":27,"index":54}},
92+
"id": null,
93+
"generator": false,
94+
"async": false,
95+
"params": [
96+
{
97+
"type": "Identifier",
98+
"start":47,"end":48,"loc":{"start":{"line":3,"column":20,"index":47},"end":{"line":3,"column":21,"index":48},"identifierName":"x"},
99+
"name": "x"
100+
}
101+
],
102+
"body": {
103+
"type": "NumericLiteral",
104+
"start":53,"end":54,"loc":{"start":{"line":3,"column":26,"index":53},"end":{"line":3,"column":27,"index":54}},
105+
"extra": {
106+
"rawValue": 1,
107+
"raw": "1"
108+
},
109+
"value": 1
110+
},
111+
"typeParameters": {
112+
"type": "TSTypeParameterDeclaration",
113+
"start":27,"end":46,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":19,"index":46}},
114+
"params": [
115+
{
116+
"type": "TSTypeParameter",
117+
"start":28,"end":45,"loc":{"start":{"line":3,"column":1,"index":28},"end":{"line":3,"column":18,"index":45}},
118+
"name": "T",
119+
"constraint": {
120+
"type": "TSUnknownKeyword",
121+
"start":38,"end":45,"loc":{"start":{"line":3,"column":11,"index":38},"end":{"line":3,"column":18,"index":45}}
122+
}
123+
}
124+
]
125+
}
126+
}
127+
},
128+
{
129+
"type": "ExpressionStatement",
130+
"start":56,"end":79,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":23,"index":79}},
131+
"expression": {
132+
"type": "ArrowFunctionExpression",
133+
"start":56,"end":78,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":22,"index":78}},
134+
"id": null,
135+
"generator": false,
136+
"async": false,
137+
"params": [
138+
{
139+
"type": "Identifier",
140+
"start":71,"end":72,"loc":{"start":{"line":4,"column":15,"index":71},"end":{"line":4,"column":16,"index":72},"identifierName":"x"},
141+
"name": "x"
142+
}
143+
],
144+
"body": {
145+
"type": "NumericLiteral",
146+
"start":77,"end":78,"loc":{"start":{"line":4,"column":21,"index":77},"end":{"line":4,"column":22,"index":78}},
147+
"extra": {
148+
"rawValue": 1,
149+
"raw": "1"
150+
},
151+
"value": 1
152+
},
153+
"typeParameters": {
154+
"type": "TSTypeParameterDeclaration",
155+
"start":56,"end":70,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":14,"index":70}},
156+
"params": [
157+
{
158+
"type": "TSTypeParameter",
159+
"start":57,"end":68,"loc":{"start":{"line":4,"column":1,"index":57},"end":{"line":4,"column":12,"index":68}},
160+
"name": "T",
161+
"default": {
162+
"type": "TSUnknownKeyword",
163+
"start":61,"end":68,"loc":{"start":{"line":4,"column":5,"index":61},"end":{"line":4,"column":12,"index":68}}
164+
}
165+
}
166+
],
167+
"extra": {
168+
"trailingComma": 68
169+
}
170+
}
171+
}
85172
}
86173
],
87174
"directives": []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
<T,>() => 1;
22
<T,>(x) => 1;
3+
<T extends unknown>(x) => 1;
4+
<T = unknown,>(x) => 1;

packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/output.json

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"type": "File",
3-
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":13,"index":26}},
3+
"start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":23,"index":79}},
44
"program": {
55
"type": "Program",
6-
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":13,"index":26}},
6+
"start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":23,"index":79}},
77
"sourceType": "script",
88
"interpreter": null,
99
"body": [
@@ -90,6 +90,101 @@
9090
}
9191
}
9292
}
93+
},
94+
{
95+
"type": "ExpressionStatement",
96+
"start":27,"end":55,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":28,"index":55}},
97+
"expression": {
98+
"type": "ArrowFunctionExpression",
99+
"start":27,"end":54,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":27,"index":54}},
100+
"id": null,
101+
"generator": false,
102+
"async": false,
103+
"params": [
104+
{
105+
"type": "Identifier",
106+
"start":47,"end":48,"loc":{"start":{"line":3,"column":20,"index":47},"end":{"line":3,"column":21,"index":48},"identifierName":"x"},
107+
"name": "x"
108+
}
109+
],
110+
"body": {
111+
"type": "NumericLiteral",
112+
"start":53,"end":54,"loc":{"start":{"line":3,"column":26,"index":53},"end":{"line":3,"column":27,"index":54}},
113+
"extra": {
114+
"rawValue": 1,
115+
"raw": "1"
116+
},
117+
"value": 1
118+
},
119+
"typeParameters": {
120+
"type": "TSTypeParameterDeclaration",
121+
"start":27,"end":46,"loc":{"start":{"line":3,"column":0,"index":27},"end":{"line":3,"column":19,"index":46}},
122+
"params": [
123+
{
124+
"type": "TSTypeParameter",
125+
"start":28,"end":45,"loc":{"start":{"line":3,"column":1,"index":28},"end":{"line":3,"column":18,"index":45}},
126+
"name": {
127+
"type": "Identifier",
128+
"start":28,"end":29,"loc":{"start":{"line":3,"column":1,"index":28},"end":{"line":3,"column":2,"index":29},"identifierName":"T"},
129+
"name": "T"
130+
},
131+
"constraint": {
132+
"type": "TSUnknownKeyword",
133+
"start":38,"end":45,"loc":{"start":{"line":3,"column":11,"index":38},"end":{"line":3,"column":18,"index":45}}
134+
}
135+
}
136+
]
137+
}
138+
}
139+
},
140+
{
141+
"type": "ExpressionStatement",
142+
"start":56,"end":79,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":23,"index":79}},
143+
"expression": {
144+
"type": "ArrowFunctionExpression",
145+
"start":56,"end":78,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":22,"index":78}},
146+
"id": null,
147+
"generator": false,
148+
"async": false,
149+
"params": [
150+
{
151+
"type": "Identifier",
152+
"start":71,"end":72,"loc":{"start":{"line":4,"column":15,"index":71},"end":{"line":4,"column":16,"index":72},"identifierName":"x"},
153+
"name": "x"
154+
}
155+
],
156+
"body": {
157+
"type": "NumericLiteral",
158+
"start":77,"end":78,"loc":{"start":{"line":4,"column":21,"index":77},"end":{"line":4,"column":22,"index":78}},
159+
"extra": {
160+
"rawValue": 1,
161+
"raw": "1"
162+
},
163+
"value": 1
164+
},
165+
"typeParameters": {
166+
"type": "TSTypeParameterDeclaration",
167+
"start":56,"end":70,"loc":{"start":{"line":4,"column":0,"index":56},"end":{"line":4,"column":14,"index":70}},
168+
"params": [
169+
{
170+
"type": "TSTypeParameter",
171+
"start":57,"end":68,"loc":{"start":{"line":4,"column":1,"index":57},"end":{"line":4,"column":12,"index":68}},
172+
"name": {
173+
"type": "Identifier",
174+
"start":57,"end":58,"loc":{"start":{"line":4,"column":1,"index":57},"end":{"line":4,"column":2,"index":58},"identifierName":"T"},
175+
"name": "T"
176+
},
177+
"default": {
178+
"type": "TSUnknownKeyword",
179+
"start":61,"end":68,"loc":{"start":{"line":4,"column":5,"index":61},"end":{"line":4,"column":12,"index":68}}
180+
}
181+
}
182+
],
183+
"extra": {
184+
"trailingComma": 68
185+
}
186+
}
187+
}
93188
}
94189
],
95190
"directives": []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<T>() => 1;
22
<T>(x) => 1;
3+
<T = unknown>(x) => 1;

packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"type": "File",
3-
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":12,"index":24}},
3+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":47}},
44
"errors": [
55
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (1:0)",
6-
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (2:0)"
6+
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (2:0)",
7+
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (3:0)"
78
],
89
"program": {
910
"type": "Program",
10-
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":12,"index":24}},
11+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":22,"index":47}},
1112
"sourceType": "script",
1213
"interpreter": null,
1314
"body": [
@@ -88,6 +89,52 @@
8889
]
8990
}
9091
}
92+
},
93+
{
94+
"type": "ExpressionStatement",
95+
"start":25,"end":47,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":22,"index":47}},
96+
"expression": {
97+
"type": "ArrowFunctionExpression",
98+
"start":25,"end":46,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":21,"index":46}},
99+
"id": null,
100+
"generator": false,
101+
"async": false,
102+
"params": [
103+
{
104+
"type": "Identifier",
105+
"start":39,"end":40,"loc":{"start":{"line":3,"column":14,"index":39},"end":{"line":3,"column":15,"index":40},"identifierName":"x"},
106+
"name": "x"
107+
}
108+
],
109+
"body": {
110+
"type": "NumericLiteral",
111+
"start":45,"end":46,"loc":{"start":{"line":3,"column":20,"index":45},"end":{"line":3,"column":21,"index":46}},
112+
"extra": {
113+
"rawValue": 1,
114+
"raw": "1"
115+
},
116+
"value": 1
117+
},
118+
"typeParameters": {
119+
"type": "TSTypeParameterDeclaration",
120+
"start":25,"end":38,"loc":{"start":{"line":3,"column":0,"index":25},"end":{"line":3,"column":13,"index":38}},
121+
"params": [
122+
{
123+
"type": "TSTypeParameter",
124+
"start":26,"end":37,"loc":{"start":{"line":3,"column":1,"index":26},"end":{"line":3,"column":12,"index":37}},
125+
"name": {
126+
"type": "Identifier",
127+
"start":26,"end":27,"loc":{"start":{"line":3,"column":1,"index":26},"end":{"line":3,"column":2,"index":27},"identifierName":"T"},
128+
"name": "T"
129+
},
130+
"default": {
131+
"type": "TSUnknownKeyword",
132+
"start":30,"end":37,"loc":{"start":{"line":3,"column":5,"index":30},"end":{"line":3,"column":12,"index":37}}
133+
}
134+
}
135+
]
136+
}
137+
}
91138
}
92139
],
93140
"directives": []

0 commit comments

Comments
 (0)