Skip to content

Commit a53c2fa

Browse files
authored
fix(ts): skip func-type param start on parsing (#14293)
* fix(ts): skip func-type param start on parsing Currently we skip the param start at tokenizing level, assuming `}` only matches `{` and `]` only matches `[`. However, as ES evolves such assumptions are no longer valid. Furthermore, we reinterpret `}` as template continuation in parseTemplateSubstitution, therefore, the skip param routine should be moved to parsing level instead of tokenizing. * fix: tt.braceL is consumed in parseObjectLike
1 parent aa5ff36 commit a53c2fa

30 files changed

Lines changed: 659 additions & 21 deletions

File tree

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,33 +1233,32 @@ export default (superClass: Class<Parser>): Class<Parser> =>
12331233
}
12341234

12351235
if (this.match(tt.braceL)) {
1236-
let braceStackCounter = 1;
1237-
this.next();
1238-
1239-
while (braceStackCounter > 0) {
1240-
if (this.match(tt.braceL)) {
1241-
++braceStackCounter;
1242-
} else if (this.match(tt.braceR)) {
1243-
--braceStackCounter;
1244-
}
1245-
this.next();
1236+
// Return true if we can parse an object pattern without errors
1237+
const { errors } = this.state;
1238+
const previousErrorCount = errors.length;
1239+
try {
1240+
this.parseObjectLike(tt.braceR, true);
1241+
return errors.length === previousErrorCount;
1242+
} catch {
1243+
return false;
12461244
}
1247-
return true;
12481245
}
12491246

12501247
if (this.match(tt.bracketL)) {
1251-
let braceStackCounter = 1;
12521248
this.next();
1253-
1254-
while (braceStackCounter > 0) {
1255-
if (this.match(tt.bracketL)) {
1256-
++braceStackCounter;
1257-
} else if (this.match(tt.bracketR)) {
1258-
--braceStackCounter;
1259-
}
1260-
this.next();
1249+
// Return true if we can parse an array pattern without errors
1250+
const { errors } = this.state;
1251+
const previousErrorCount = errors.length;
1252+
try {
1253+
this.parseBindingList(
1254+
tt.bracketR,
1255+
charCodes.rightSquareBracket,
1256+
true,
1257+
);
1258+
return errors.length === previousErrorCount;
1259+
} catch {
1260+
return false;
12611261
}
1262-
return true;
12631262
}
12641263

12651264
return false;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type F = ([ x = #[0] ]) => {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sourceType": "module",
3+
"plugins": ["typescript", ["recordAndTuple", { "syntaxType": "hash" }]]
4+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "TSTypeAliasDeclaration",
12+
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"F"},
16+
"name": "F"
17+
},
18+
"typeAnnotation": {
19+
"type": "TSFunctionType",
20+
"start":9,"end":29,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":29,"index":29}},
21+
"parameters": [
22+
{
23+
"type": "ArrayPattern",
24+
"start":10,"end":22,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":22,"index":22}},
25+
"elements": [
26+
{
27+
"type": "AssignmentPattern",
28+
"start":12,"end":20,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":20,"index":20}},
29+
"left": {
30+
"type": "Identifier",
31+
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
32+
"name": "x"
33+
},
34+
"right": {
35+
"type": "TupleExpression",
36+
"start":16,"end":20,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":20,"index":20}},
37+
"elements": [
38+
{
39+
"type": "NumericLiteral",
40+
"start":18,"end":19,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":19,"index":19}},
41+
"extra": {
42+
"rawValue": 0,
43+
"raw": "0"
44+
},
45+
"value": 0
46+
}
47+
]
48+
}
49+
}
50+
]
51+
}
52+
],
53+
"typeAnnotation": {
54+
"type": "TSTypeAnnotation",
55+
"start":24,"end":29,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":29,"index":29}},
56+
"typeAnnotation": {
57+
"type": "TSTypeLiteral",
58+
"start":27,"end":29,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":29,"index":29}},
59+
"members": []
60+
}
61+
}
62+
}
63+
}
64+
],
65+
"directives": []
66+
}
67+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type F = ([
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unexpected token (1:11)"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type F = ({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unexpected token (1:11)"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type F = ({ x = #{} }) => {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"sourceType": "module",
3+
"plugins": [
4+
"typescript",
5+
[
6+
"recordAndTuple",
7+
{
8+
"syntaxType": "hash"
9+
}
10+
]
11+
]
12+
}

0 commit comments

Comments
 (0)