Skip to content

Commit 048fc14

Browse files
committed
TSMappedType
1 parent 3df23a5 commit 048fc14

File tree

7 files changed

+121
-127
lines changed

7 files changed

+121
-127
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,22 @@ export function TSMappedType(this: Printer, node: t.TSMappedType) {
423423
}
424424

425425
this.token("[");
426-
this.word(
427-
!process.env.BABEL_8_BREAKING
428-
? (typeParameter.name as unknown as string)
429-
: (typeParameter.name as unknown as t.Identifier).name,
430-
);
426+
if (process.env.BABEL_8_BREAKING) {
427+
// @ts-expect-error Babel 8 AST shape
428+
this.word(node.key.name);
429+
} else {
430+
this.word(typeParameter.name);
431+
}
432+
431433
this.space();
432434
this.word("in");
433435
this.space();
434-
this.print(typeParameter.constraint, typeParameter);
436+
if (process.env.BABEL_8_BREAKING) {
437+
// @ts-expect-error Babel 8 AST shape
438+
this.print(node.constraint, node);
439+
} else {
440+
this.print(typeParameter.constraint, typeParameter);
441+
}
435442

436443
if (nameType) {
437444
this.space();

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -995,13 +995,6 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
995995
return this.match(tt._in);
996996
}
997997

998-
tsParseMappedTypeParameter(): N.TsTypeParameter {
999-
const node = this.startNode<N.TsTypeParameter>();
1000-
node.name = this.tsParseTypeParameterName();
1001-
node.constraint = this.tsExpectThenParseType(tt._in);
1002-
return this.finishNode(node, "TSTypeParameter");
1003-
}
1004-
1005998
tsParseMappedType(): N.TsMappedType {
1006999
const node = this.startNode<N.TsMappedType>();
10071000

@@ -1016,7 +1009,16 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
10161009
}
10171010

10181011
this.expect(tt.bracketL);
1019-
node.typeParameter = this.tsParseMappedTypeParameter();
1012+
if (process.env.BABEL_8_BREAKING) {
1013+
node.key = this.tsParseTypeParameterName() as N.Identifier;
1014+
node.constraint = this.tsExpectThenParseType(tt._in);
1015+
} else {
1016+
const typeParameter = this.startNode<N.TsTypeParameter>();
1017+
typeParameter.name = this.tsParseTypeParameterName();
1018+
typeParameter.constraint = this.tsExpectThenParseType(tt._in);
1019+
// @ts-expect-error for Babel 7
1020+
node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
1021+
}
10201022
node.nameType = this.eatContextual(tt._as) ? this.tsParseType() : null;
10211023

10221024
this.expect(tt.bracketR);

packages/babel-parser/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,8 +1705,9 @@ export interface TsIndexedAccessType extends TsTypeBase {
17051705

17061706
export interface TsMappedType extends TsTypeBase {
17071707
type: "TSMappedType";
1708+
key: Identifier;
1709+
constraint: TsType;
17081710
readonly?: true | "+" | "-";
1709-
typeParameter: TsTypeParameter;
17101711
optional?: true | "+" | "-";
17111712
typeAnnotation: TsType | undefined | null;
17121713
nameType: TsType | undefined | null;

packages/babel-parser/test/fixtures/typescript/types/mapped-as/output.json

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,22 @@
3333
"typeAnnotation": {
3434
"type": "TSMappedType",
3535
"start":32,"end":72,"loc":{"start":{"line":1,"column":32,"index":32},"end":{"line":3,"column":1,"index":72}},
36-
"typeParameter": {
37-
"type": "TSTypeParameter",
38-
"start":37,"end":49,"loc":{"start":{"line":2,"column":3,"index":37},"end":{"line":2,"column":15,"index":49}},
39-
"name": {
40-
"type": "Identifier",
41-
"start":37,"end":38,"loc":{"start":{"line":2,"column":3,"index":37},"end":{"line":2,"column":4,"index":38},"identifierName":"K"},
42-
"name": "K"
43-
},
44-
"constraint": {
45-
"type": "TSTypeOperator",
46-
"start":42,"end":49,"loc":{"start":{"line":2,"column":8,"index":42},"end":{"line":2,"column":15,"index":49}},
47-
"operator": "keyof",
48-
"typeAnnotation": {
49-
"type": "TSTypeReference",
50-
"start":48,"end":49,"loc":{"start":{"line":2,"column":14,"index":48},"end":{"line":2,"column":15,"index":49}},
51-
"typeName": {
52-
"type": "Identifier",
53-
"start":48,"end":49,"loc":{"start":{"line":2,"column":14,"index":48},"end":{"line":2,"column":15,"index":49},"identifierName":"T"},
54-
"name": "T"
55-
}
36+
"key": {
37+
"type": "Identifier",
38+
"start":37,"end":38,"loc":{"start":{"line":2,"column":3,"index":37},"end":{"line":2,"column":4,"index":38},"identifierName":"K"},
39+
"name": "K"
40+
},
41+
"constraint": {
42+
"type": "TSTypeOperator",
43+
"start":42,"end":49,"loc":{"start":{"line":2,"column":8,"index":42},"end":{"line":2,"column":15,"index":49}},
44+
"operator": "keyof",
45+
"typeAnnotation": {
46+
"type": "TSTypeReference",
47+
"start":48,"end":49,"loc":{"start":{"line":2,"column":14,"index":48},"end":{"line":2,"column":15,"index":49}},
48+
"typeName": {
49+
"type": "Identifier",
50+
"start":48,"end":49,"loc":{"start":{"line":2,"column":14,"index":48},"end":{"line":2,"column":15,"index":49},"identifierName":"T"},
51+
"name": "T"
5652
}
5753
}
5854
},
@@ -115,26 +111,22 @@
115111
"typeAnnotation": {
116112
"type": "TSMappedType",
117113
"start":101,"end":149,"loc":{"start":{"line":5,"column":26,"index":101},"end":{"line":7,"column":1,"index":149}},
118-
"typeParameter": {
119-
"type": "TSTypeParameter",
120-
"start":106,"end":118,"loc":{"start":{"line":6,"column":3,"index":106},"end":{"line":6,"column":15,"index":118}},
121-
"name": {
122-
"type": "Identifier",
123-
"start":106,"end":107,"loc":{"start":{"line":6,"column":3,"index":106},"end":{"line":6,"column":4,"index":107},"identifierName":"K"},
124-
"name": "K"
125-
},
126-
"constraint": {
127-
"type": "TSTypeOperator",
128-
"start":111,"end":118,"loc":{"start":{"line":6,"column":8,"index":111},"end":{"line":6,"column":15,"index":118}},
129-
"operator": "keyof",
130-
"typeAnnotation": {
131-
"type": "TSTypeReference",
132-
"start":117,"end":118,"loc":{"start":{"line":6,"column":14,"index":117},"end":{"line":6,"column":15,"index":118}},
133-
"typeName": {
134-
"type": "Identifier",
135-
"start":117,"end":118,"loc":{"start":{"line":6,"column":14,"index":117},"end":{"line":6,"column":15,"index":118},"identifierName":"T"},
136-
"name": "T"
137-
}
114+
"key": {
115+
"type": "Identifier",
116+
"start":106,"end":107,"loc":{"start":{"line":6,"column":3,"index":106},"end":{"line":6,"column":4,"index":107},"identifierName":"K"},
117+
"name": "K"
118+
},
119+
"constraint": {
120+
"type": "TSTypeOperator",
121+
"start":111,"end":118,"loc":{"start":{"line":6,"column":8,"index":111},"end":{"line":6,"column":15,"index":118}},
122+
"operator": "keyof",
123+
"typeAnnotation": {
124+
"type": "TSTypeReference",
125+
"start":117,"end":118,"loc":{"start":{"line":6,"column":14,"index":117},"end":{"line":6,"column":15,"index":118}},
126+
"typeName": {
127+
"type": "Identifier",
128+
"start":117,"end":118,"loc":{"start":{"line":6,"column":14,"index":117},"end":{"line":6,"column":15,"index":118},"identifierName":"T"},
129+
"name": "T"
138130
}
139131
}
140132
},
@@ -234,26 +226,22 @@
234226
"typeAnnotation": {
235227
"type": "TSMappedType",
236228
"start":181,"end":237,"loc":{"start":{"line":9,"column":29,"index":181},"end":{"line":11,"column":1,"index":237}},
237-
"typeParameter": {
238-
"type": "TSTypeParameter",
239-
"start":186,"end":198,"loc":{"start":{"line":10,"column":3,"index":186},"end":{"line":10,"column":15,"index":198}},
240-
"name": {
241-
"type": "Identifier",
242-
"start":186,"end":187,"loc":{"start":{"line":10,"column":3,"index":186},"end":{"line":10,"column":4,"index":187},"identifierName":"K"},
243-
"name": "K"
244-
},
245-
"constraint": {
246-
"type": "TSTypeOperator",
247-
"start":191,"end":198,"loc":{"start":{"line":10,"column":8,"index":191},"end":{"line":10,"column":15,"index":198}},
248-
"operator": "keyof",
249-
"typeAnnotation": {
250-
"type": "TSTypeReference",
251-
"start":197,"end":198,"loc":{"start":{"line":10,"column":14,"index":197},"end":{"line":10,"column":15,"index":198}},
252-
"typeName": {
253-
"type": "Identifier",
254-
"start":197,"end":198,"loc":{"start":{"line":10,"column":14,"index":197},"end":{"line":10,"column":15,"index":198},"identifierName":"T"},
255-
"name": "T"
256-
}
229+
"key": {
230+
"type": "Identifier",
231+
"start":186,"end":187,"loc":{"start":{"line":10,"column":3,"index":186},"end":{"line":10,"column":4,"index":187},"identifierName":"K"},
232+
"name": "K"
233+
},
234+
"constraint": {
235+
"type": "TSTypeOperator",
236+
"start":191,"end":198,"loc":{"start":{"line":10,"column":8,"index":191},"end":{"line":10,"column":15,"index":198}},
237+
"operator": "keyof",
238+
"typeAnnotation": {
239+
"type": "TSTypeReference",
240+
"start":197,"end":198,"loc":{"start":{"line":10,"column":14,"index":197},"end":{"line":10,"column":15,"index":198}},
241+
"typeName": {
242+
"type": "Identifier",
243+
"start":197,"end":198,"loc":{"start":{"line":10,"column":14,"index":197},"end":{"line":10,"column":15,"index":198},"identifierName":"T"},
244+
"name": "T"
257245
}
258246
}
259247
},

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

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@
2424
"typeAnnotation": {
2525
"type": "TSMappedType",
2626
"start":10,"end":36,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":36,"index":36}},
27-
"typeParameter": {
28-
"type": "TSTypeParameter",
29-
"start":13,"end":24,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":24,"index":24}},
30-
"name": {
31-
"type": "Identifier",
32-
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"P"},
33-
"name": "P"
34-
},
35-
"constraint": {
36-
"type": "TSStringKeyword",
37-
"start":18,"end":24,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":24,"index":24}}
38-
}
27+
"key": {
28+
"type": "Identifier",
29+
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"P"},
30+
"name": "P"
31+
},
32+
"constraint": {
33+
"type": "TSStringKeyword",
34+
"start":18,"end":24,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":24,"index":24}}
3935
},
4036
"nameType": null,
4137
"typeAnnotation": {
@@ -68,18 +64,14 @@
6864
"type": "TSMappedType",
6965
"start":48,"end":84,"loc":{"start":{"line":2,"column":10,"index":48},"end":{"line":2,"column":46,"index":84}},
7066
"readonly": true,
71-
"typeParameter": {
72-
"type": "TSTypeParameter",
73-
"start":60,"end":71,"loc":{"start":{"line":2,"column":22,"index":60},"end":{"line":2,"column":33,"index":71}},
74-
"name": {
75-
"type": "Identifier",
76-
"start":60,"end":61,"loc":{"start":{"line":2,"column":22,"index":60},"end":{"line":2,"column":23,"index":61},"identifierName":"P"},
77-
"name": "P"
78-
},
79-
"constraint": {
80-
"type": "TSStringKeyword",
81-
"start":65,"end":71,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":2,"column":33,"index":71}}
82-
}
67+
"key": {
68+
"type": "Identifier",
69+
"start":60,"end":61,"loc":{"start":{"line":2,"column":22,"index":60},"end":{"line":2,"column":23,"index":61},"identifierName":"P"},
70+
"name": "P"
71+
},
72+
"constraint": {
73+
"type": "TSStringKeyword",
74+
"start":65,"end":71,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":2,"column":33,"index":71}}
8375
},
8476
"nameType": null,
8577
"optional": true,
@@ -113,18 +105,14 @@
113105
"type": "TSMappedType",
114106
"start":96,"end":134,"loc":{"start":{"line":3,"column":10,"index":96},"end":{"line":3,"column":48,"index":134}},
115107
"readonly": "+",
116-
"typeParameter": {
117-
"type": "TSTypeParameter",
118-
"start":109,"end":120,"loc":{"start":{"line":3,"column":23,"index":109},"end":{"line":3,"column":34,"index":120}},
119-
"name": {
120-
"type": "Identifier",
121-
"start":109,"end":110,"loc":{"start":{"line":3,"column":23,"index":109},"end":{"line":3,"column":24,"index":110},"identifierName":"P"},
122-
"name": "P"
123-
},
124-
"constraint": {
125-
"type": "TSStringKeyword",
126-
"start":114,"end":120,"loc":{"start":{"line":3,"column":28,"index":114},"end":{"line":3,"column":34,"index":120}}
127-
}
108+
"key": {
109+
"type": "Identifier",
110+
"start":109,"end":110,"loc":{"start":{"line":3,"column":23,"index":109},"end":{"line":3,"column":24,"index":110},"identifierName":"P"},
111+
"name": "P"
112+
},
113+
"constraint": {
114+
"type": "TSStringKeyword",
115+
"start":114,"end":120,"loc":{"start":{"line":3,"column":28,"index":114},"end":{"line":3,"column":34,"index":120}}
128116
},
129117
"nameType": null,
130118
"optional": "+",
@@ -158,18 +146,14 @@
158146
"type": "TSMappedType",
159147
"start":146,"end":183,"loc":{"start":{"line":4,"column":10,"index":146},"end":{"line":4,"column":47,"index":183}},
160148
"readonly": "-",
161-
"typeParameter": {
162-
"type": "TSTypeParameter",
163-
"start":159,"end":170,"loc":{"start":{"line":4,"column":23,"index":159},"end":{"line":4,"column":34,"index":170}},
164-
"name": {
165-
"type": "Identifier",
166-
"start":159,"end":160,"loc":{"start":{"line":4,"column":23,"index":159},"end":{"line":4,"column":24,"index":160},"identifierName":"P"},
167-
"name": "P"
168-
},
169-
"constraint": {
170-
"type": "TSStringKeyword",
171-
"start":164,"end":170,"loc":{"start":{"line":4,"column":28,"index":164},"end":{"line":4,"column":34,"index":170}}
172-
}
149+
"key": {
150+
"type": "Identifier",
151+
"start":159,"end":160,"loc":{"start":{"line":4,"column":23,"index":159},"end":{"line":4,"column":24,"index":160},"identifierName":"P"},
152+
"name": "P"
153+
},
154+
"constraint": {
155+
"type": "TSStringKeyword",
156+
"start":164,"end":170,"loc":{"start":{"line":4,"column":28,"index":164},"end":{"line":4,"column":34,"index":170}}
173157
},
174158
"nameType": null,
175159
"optional": "-",

packages/babel-types/src/definitions/typescript.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,22 @@ defineType("TSIndexedAccessType", {
357357

358358
defineType("TSMappedType", {
359359
aliases: ["TSType"],
360-
visitor: ["typeParameter", "nameType", "typeAnnotation"],
361-
builder: ["typeParameter", "typeAnnotation", "nameType"],
362-
fields: {
360+
visitor: process.env.BABEL_8_BREAKING
361+
? ["key", "constraint", "nameType", "typeAnnotation"]
362+
: ["typeParameter", "nameType", "typeAnnotation"],
363+
builder: process.env.BABEL_8_BREAKING
364+
? ["key", "constraint", "nameType", "typeAnnotation"]
365+
: ["typeParameter", "typeAnnotation", "nameType"],
366+
fields: {
367+
...(process.env.BABEL_8_BREAKING
368+
? {
369+
key: validateType("Identifier"),
370+
constraint: validateType("TSType"),
371+
}
372+
: {
373+
typeParameter: validateType("TSTypeParameter"),
374+
}),
363375
readonly: validateOptional(assertOneOf(true, false, "+", "-")),
364-
typeParameter: validateType("TSTypeParameter"),
365376
optional: validateOptional(assertOneOf(true, false, "+", "-")),
366377
typeAnnotation: validateOptionalType("TSType"),
367378
nameType: validateOptionalType("TSType"),

packages/babel-types/test/fields.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const ignoredFields = {
6262
parameters: true,
6363
typeAnnotation: true,
6464
},
65+
TSMappedType: { typeParameter: true },
6566
}
6667
: {
6768
TSFunctionType: { params: true, returnType: true },

0 commit comments

Comments
 (0)