Skip to content

Commit 51d50f9

Browse files
authored
Fix TS non-array type start (babel#17106)
1 parent 48d915c commit 51d50f9

File tree

18 files changed

+409
-71
lines changed

18 files changed

+409
-71
lines changed

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,10 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
11271127
tsParseTupleElementType(): N.TsNamedTupleMember | N.TsType {
11281128
// parses `...TsType[]`
11291129

1130-
const { startLoc } = this.state;
1130+
const restStartLoc = this.state.startLoc;
11311131

11321132
const rest = this.eat(tt.ellipsis);
1133+
const { startLoc } = this.state;
11331134

11341135
let labeled: boolean;
11351136
let label: N.Identifier;
@@ -1146,7 +1147,6 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
11461147
type = this.tsParseType();
11471148
} else if (chAfterWord === charCodes.questionMark) {
11481149
optional = true;
1149-
const startLoc = this.state.startLoc;
11501150
const wordName = this.state.value;
11511151
const typeOrLabel = this.tsParseNonArrayType();
11521152

@@ -1176,7 +1176,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
11761176
if (labeled) {
11771177
let labeledNode: Undone<N.TsNamedTupleMember>;
11781178
if (label) {
1179-
labeledNode = this.startNodeAtNode<N.TsNamedTupleMember>(label);
1179+
labeledNode = this.startNodeAt<N.TsNamedTupleMember>(startLoc);
11801180
labeledNode.optional = optional;
11811181
labeledNode.label = label;
11821182
labeledNode.elementType = type;
@@ -1189,7 +1189,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
11891189
);
11901190
}
11911191
} else {
1192-
labeledNode = this.startNodeAtNode<N.TsNamedTupleMember>(type);
1192+
labeledNode = this.startNodeAt<N.TsNamedTupleMember>(startLoc);
11931193
labeledNode.optional = optional;
11941194
this.raise(TSErrors.InvalidTupleMemberLabel, type);
11951195
// @ts-expect-error This produces an invalid AST, but at least we don't drop
@@ -1199,13 +1199,13 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
11991199
}
12001200
type = this.finishNode(labeledNode, "TSNamedTupleMember");
12011201
} else if (optional) {
1202-
const optionalTypeNode = this.startNodeAtNode<N.TsOptionalType>(type);
1202+
const optionalTypeNode = this.startNodeAt<N.TsOptionalType>(startLoc);
12031203
optionalTypeNode.typeAnnotation = type;
12041204
type = this.finishNode(optionalTypeNode, "TSOptionalType");
12051205
}
12061206

12071207
if (rest) {
1208-
const restNode = this.startNodeAt<N.TsRestType>(startLoc);
1208+
const restNode = this.startNodeAt<N.TsRestType>(restStartLoc);
12091209
restNode.typeAnnotation = type;
12101210
type = this.finishNode(restNode, "TSRestType");
12111211
}
@@ -1383,15 +1383,16 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
13831383
}
13841384

13851385
tsParseArrayTypeOrHigher(): N.TsType {
1386+
const { startLoc } = this.state;
13861387
let type = this.tsParseNonArrayType();
13871388
while (!this.hasPrecedingLineBreak() && this.eat(tt.bracketL)) {
13881389
if (this.match(tt.bracketR)) {
1389-
const node = this.startNodeAtNode<N.TsArrayType>(type);
1390+
const node = this.startNodeAt<N.TsArrayType>(startLoc);
13901391
node.elementType = type;
13911392
this.expect(tt.bracketR);
13921393
type = this.finishNode(node, "TSArrayType");
13931394
} else {
1394-
const node = this.startNodeAtNode<N.TsIndexedAccessType>(type);
1395+
const node = this.startNodeAt<N.TsIndexedAccessType>(startLoc);
13951396
node.objectType = type;
13961397
node.indexType = this.tsParseType();
13971398
this.expect(tt.bracketR);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Element<T> = T extends (infer U)[] ? U : T;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": ["typescript"],
3+
"createParenthesizedExpressions": true,
4+
"BABEL_8_BREAKING": true
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "TSTypeAliasDeclaration",
12+
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":5,"end":12,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":12,"index":12},"identifierName":"Element"},
16+
"name": "Element"
17+
},
18+
"typeParameters": {
19+
"type": "TSTypeParameterDeclaration",
20+
"start":12,"end":15,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":15,"index":15}},
21+
"params": [
22+
{
23+
"type": "TSTypeParameter",
24+
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14}},
25+
"name": {
26+
"type": "Identifier",
27+
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"T"},
28+
"name": "T"
29+
}
30+
}
31+
]
32+
},
33+
"typeAnnotation": {
34+
"type": "TSConditionalType",
35+
"start":18,"end":47,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":47,"index":47}},
36+
"checkType": {
37+
"type": "TSTypeReference",
38+
"start":18,"end":19,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":19,"index":19}},
39+
"typeName": {
40+
"type": "Identifier",
41+
"start":18,"end":19,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":19,"index":19},"identifierName":"T"},
42+
"name": "T"
43+
}
44+
},
45+
"extendsType": {
46+
"type": "TSArrayType",
47+
"start":28,"end":39,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":39,"index":39}},
48+
"elementType": {
49+
"type": "TSParenthesizedType",
50+
"start":28,"end":37,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":37,"index":37}},
51+
"typeAnnotation": {
52+
"type": "TSInferType",
53+
"start":29,"end":36,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":36,"index":36}},
54+
"typeParameter": {
55+
"type": "TSTypeParameter",
56+
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36}},
57+
"name": {
58+
"type": "Identifier",
59+
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"U"},
60+
"name": "U"
61+
}
62+
}
63+
}
64+
}
65+
},
66+
"trueType": {
67+
"type": "TSTypeReference",
68+
"start":42,"end":43,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":43,"index":43}},
69+
"typeName": {
70+
"type": "Identifier",
71+
"start":42,"end":43,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":43,"index":43},"identifierName":"U"},
72+
"name": "U"
73+
}
74+
},
75+
"falseType": {
76+
"type": "TSTypeReference",
77+
"start":46,"end":47,"loc":{"start":{"line":1,"column":46,"index":46},"end":{"line":1,"column":47,"index":47}},
78+
"typeName": {
79+
"type": "Identifier",
80+
"start":46,"end":47,"loc":{"start":{"line":1,"column":46,"index":46},"end":{"line":1,"column":47,"index":47},"identifierName":"T"},
81+
"name": "T"
82+
}
83+
}
84+
}
85+
}
86+
],
87+
"directives": [],
88+
"extra": {
89+
"topLevelAwait": false
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"plugins": ["typescript"],
3-
"createParenthesizedExpressions": true,
43
"BABEL_8_BREAKING": true
54
}

packages/babel-parser/test/fixtures/typescript/types/conditional-infer/output.json

+13-13
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@
4646
"type": "TSArrayType",
4747
"start":28,"end":39,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":39,"index":39}},
4848
"elementType": {
49-
"type": "TSParenthesizedType",
50-
"start":28,"end":37,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":37,"index":37}},
51-
"typeAnnotation": {
52-
"type": "TSInferType",
53-
"start":29,"end":36,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":36,"index":36}},
54-
"typeParameter": {
55-
"type": "TSTypeParameter",
56-
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36}},
57-
"name": {
58-
"type": "Identifier",
59-
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"U"},
60-
"name": "U"
61-
}
49+
"type": "TSInferType",
50+
"start":29,"end":36,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":36,"index":36}},
51+
"typeParameter": {
52+
"type": "TSTypeParameter",
53+
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36}},
54+
"name": {
55+
"type": "Identifier",
56+
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"U"},
57+
"name": "U"
6258
}
59+
},
60+
"extra": {
61+
"parenthesized": true,
62+
"parenStart": 28
6363
}
6464
}
6565
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = ({ foo: string })["foo"];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"typescript"
4+
],
5+
"BABEL_8_BREAKING": false
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "TSTypeAliasDeclaration",
12+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
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":"T"},
16+
"name": "T"
17+
},
18+
"typeAnnotation": {
19+
"type": "TSIndexedAccessType",
20+
"start":9,"end":33,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":33,"index":33}},
21+
"objectType": {
22+
"type": "TSParenthesizedType",
23+
"start":9,"end":26,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":26,"index":26}},
24+
"typeAnnotation": {
25+
"type": "TSTypeLiteral",
26+
"start":10,"end":25,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":25,"index":25}},
27+
"members": [
28+
{
29+
"type": "TSPropertySignature",
30+
"start":12,"end":23,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":23,"index":23}},
31+
"key": {
32+
"type": "Identifier",
33+
"start":12,"end":15,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":15,"index":15},"identifierName":"foo"},
34+
"name": "foo"
35+
},
36+
"computed": false,
37+
"typeAnnotation": {
38+
"type": "TSTypeAnnotation",
39+
"start":15,"end":23,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":23,"index":23}},
40+
"typeAnnotation": {
41+
"type": "TSStringKeyword",
42+
"start":17,"end":23,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":23,"index":23}}
43+
}
44+
}
45+
}
46+
]
47+
}
48+
},
49+
"indexType": {
50+
"type": "TSLiteralType",
51+
"start":27,"end":32,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":32,"index":32}},
52+
"literal": {
53+
"type": "StringLiteral",
54+
"start":27,"end":32,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":32,"index":32}},
55+
"extra": {
56+
"rawValue": "foo",
57+
"raw": "\"foo\""
58+
},
59+
"value": "foo"
60+
}
61+
}
62+
}
63+
}
64+
],
65+
"directives": [],
66+
"extra": {
67+
"topLevelAwait": false
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = ({ foo: string })["foo"];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"typescript"
4+
],
5+
"BABEL_8_BREAKING": true
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "TSTypeAliasDeclaration",
12+
"start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":34,"index":34}},
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":"T"},
16+
"name": "T"
17+
},
18+
"typeAnnotation": {
19+
"type": "TSIndexedAccessType",
20+
"start":9,"end":33,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":33,"index":33}},
21+
"objectType": {
22+
"type": "TSTypeLiteral",
23+
"start":10,"end":25,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":25,"index":25}},
24+
"members": [
25+
{
26+
"type": "TSPropertySignature",
27+
"start":12,"end":23,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":23,"index":23}},
28+
"key": {
29+
"type": "Identifier",
30+
"start":12,"end":15,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":15,"index":15},"identifierName":"foo"},
31+
"name": "foo"
32+
},
33+
"computed": false,
34+
"typeAnnotation": {
35+
"type": "TSTypeAnnotation",
36+
"start":15,"end":23,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":23,"index":23}},
37+
"typeAnnotation": {
38+
"type": "TSStringKeyword",
39+
"start":17,"end":23,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":23,"index":23}}
40+
}
41+
}
42+
}
43+
],
44+
"extra": {
45+
"parenthesized": true,
46+
"parenStart": 9
47+
}
48+
},
49+
"indexType": {
50+
"type": "TSLiteralType",
51+
"start":27,"end":32,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":32,"index":32}},
52+
"literal": {
53+
"type": "StringLiteral",
54+
"start":27,"end":32,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":32,"index":32}},
55+
"extra": {
56+
"rawValue": "foo",
57+
"raw": "\"foo\""
58+
},
59+
"value": "foo"
60+
}
61+
}
62+
}
63+
}
64+
],
65+
"directives": [],
66+
"extra": {
67+
"topLevelAwait": false
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = ({ field: `${string}` });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
"typescript"
4+
],
5+
"createParenthesizedExpressions": true,
6+
"BABEL_8_BREAKING": true
7+
}

0 commit comments

Comments
 (0)