Skip to content

Commit beea88c

Browse files
[babel 8] Rename TSImportType.argument to .source (#17610)
1 parent d7c397e commit beea88c

File tree

18 files changed

+282
-318
lines changed

18 files changed

+282
-318
lines changed

eslint/babel-eslint-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"@typescript-eslint/scope-manager": "^6.19.0",
5252
"dedent": "^1.5.3",
5353
"eslint": "^9.21.0",
54-
"typescript-eslint": "8.39.1"
54+
"typescript-eslint": "^8.48.0"
5555
},
5656
"conditions": {
5757
"BABEL_8_BREAKING": [

eslint/babel-eslint-parser/test/typescript-estree.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const PROPS_TO_REMOVE = [
1919
{ key: "identifierName", type: null },
2020
// For legacy estree AST
2121
{ key: "attributes", type: "ImportExpression" },
22+
23+
// Babel sets `computed: false`, while typescript-estree omits it
24+
{ key: "computed", value: false, type: "TSEnumMember" },
2225
];
2326

2427
// TODO: remove the ESLint token fixes after they are fixed in upstream
@@ -53,8 +56,9 @@ function deeplyRemoveProperties(obj, props) {
5356
for (const [k, v] of Object.entries(obj)) {
5457
if (
5558
props.some(
56-
({ key, type }) =>
59+
({ key, value, type }) =>
5760
key === k &&
61+
(value === undefined || value === v) &&
5862
((type === "Node" && obj.type) || type === obj.type || type == null),
5963
)
6064
) {
@@ -78,6 +82,22 @@ function deeplyRemoveProperties(obj, props) {
7882
}
7983
}
8084

85+
function deeplyMakePlainObject(obj) {
86+
if (!obj || typeof obj !== "object") return;
87+
88+
if (Array.isArray(obj)) {
89+
for (const el of obj) {
90+
deeplyMakePlainObject(el);
91+
}
92+
return;
93+
}
94+
95+
Object.setPrototypeOf(obj, Object.prototype);
96+
for (const v of Object.values(obj)) {
97+
deeplyMakePlainObject(v);
98+
}
99+
}
100+
81101
// Only ESLint 9 or above will be tested
82102
(isESLint9 ? describe : describe.skip)(
83103
"Babel should output the same AST as TypeScript-Estree",
@@ -139,6 +159,10 @@ function deeplyRemoveProperties(obj, props) {
139159
} else {
140160
fixTSESLintTokens(tsEstreeAST);
141161
}
162+
163+
deeplyMakePlainObject(babelAST);
164+
deeplyMakePlainObject(tsEstreeAST);
165+
142166
expect(babelAST).toEqual(tsEstreeAST);
143167
}
144168

@@ -304,7 +328,7 @@ function deeplyRemoveProperties(obj, props) {
304328
["function type", "var v: (x: string) => string"],
305329

306330
["conditional type", "type M = T extends Q ? string : number"],
307-
["import type", "var v: import('foo')"],
331+
...(IS_BABEL_8 ? [["import type", "var v: import('foo')"]] : []),
308332
["instantiation expression optional chain", "a?.b<c>"],
309333
["type parameter", "type Id<T> = T"],
310334

@@ -341,9 +365,6 @@ function deeplyRemoveProperties(obj, props) {
341365
// Pending release https://github.com/microsoft/TypeScript/pull/61764
342366
"typescript/explicit-resource-management/valid-for-using-declaration-binding-of/input.js",
343367

344-
// Pending https://github.com/typescript-eslint/typescript-eslint/issues/11474
345-
"typescript/types/import-type-options-with-trailing-comma/input.ts",
346-
347368
// ts-eslint/tsc does not support arrow generic in tsx mode
348369
"typescript/arrow-function/async-await-null/input.ts",
349370
"typescript/arrow-function/async-generic-after-await/input.ts",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,13 @@ export function TSModuleBlock(this: Printer, node: t.TSModuleBlock) {
720720
}
721721

722722
export function TSImportType(this: Printer, node: t.TSImportType) {
723-
const { argument, qualifier, options } = node;
723+
const { qualifier, options } = node;
724724
this.word("import");
725725
this.token("(");
726-
this.print(argument);
726+
this.print(
727+
//@ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
728+
process.env.BABEL_8_BREAKING ? node.source : node.argument,
729+
);
727730
if (options) {
728731
this.token(",");
729732
this.print(options);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,13 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
595595
this.raise(TSErrors.UnsupportedImportTypeArgument, this.state.startLoc);
596596
if (process.env.BABEL_8_BREAKING) {
597597
// Consume as an non-conditional type so that we can recover from this error
598-
node.argument = this.tsParseNonConditionalType() as any;
598+
node.source = this.tsParseNonConditionalType() as any;
599599
} else {
600600
node.argument = super.parseExprAtom() as any;
601601
}
602602
} else {
603603
if (process.env.BABEL_8_BREAKING) {
604-
node.argument = this.tsParseLiteralTypeNode();
604+
node.source = this.parseStringLiteral(this.state.value);
605605
} else {
606606
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
607607
node.argument = this.parseStringLiteral(this.state.value);

packages/babel-parser/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,10 @@ export interface TsLiteralType extends TsTypeBase {
18481848

18491849
export interface TsImportType extends TsTypeBase {
18501850
type: "TSImportType";
1851+
/** Only in Babel 7 */
18511852
argument: TsLiteralType;
1853+
/** Only in Babel 8 */
1854+
source: StringLiteral;
18521855
qualifier?: TsEntityName | null;
18531856
typeArguments?: TsTypeParameterInstantiation | null;
18541857
/**

packages/babel-parser/test/fixtures/estree/typescript/import-dot-this/output.json

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
"exprName": {
3030
"type": "TSImportType",
3131
"start":14,"end":32,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":32}},
32-
"argument": {
33-
"type": "TSLiteralType",
32+
"source": {
33+
"type": "Literal",
3434
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
35-
"literal": {
36-
"type": "Literal",
37-
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
38-
"value": "./x",
39-
"raw": "'./x'"
40-
}
35+
"value": "./x",
36+
"raw": "'./x'"
4137
},
4238
"options": null,
4339
"qualifier": {
@@ -78,15 +74,11 @@
7874
"typeAnnotation": {
7975
"type": "TSImportType",
8076
"start":41,"end":59,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":25}},
81-
"argument": {
82-
"type": "TSLiteralType",
77+
"source": {
78+
"type": "Literal",
8379
"start":48,"end":53,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
84-
"literal": {
85-
"type": "Literal",
86-
"start":48,"end":53,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
87-
"value": "./y",
88-
"raw": "'./y'"
89-
}
80+
"value": "./y",
81+
"raw": "'./y'"
9082
},
9183
"options": null,
9284
"qualifier": {
@@ -126,15 +118,11 @@
126118
"typeAnnotation": {
127119
"type": "TSImportType",
128120
"start":68,"end":91,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":30}},
129-
"argument": {
130-
"type": "TSLiteralType",
121+
"source": {
122+
"type": "Literal",
131123
"start":75,"end":79,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
132-
"literal": {
133-
"type": "Literal",
134-
"start":75,"end":79,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
135-
"value": "/z",
136-
"raw": "\"/z\""
137-
}
124+
"value": "/z",
125+
"raw": "\"/z\""
138126
},
139127
"options": null,
140128
"qualifier": {

packages/babel-parser/test/fixtures/estree/typescript/import-with-options/output.json

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
"exprName": {
3030
"type": "TSImportType",
3131
"start":14,"end":55,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":55}},
32-
"argument": {
33-
"type": "TSLiteralType",
32+
"source": {
33+
"type": "Literal",
3434
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
35-
"literal": {
36-
"type": "Literal",
37-
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
38-
"value": "./x",
39-
"raw": "'./x'"
40-
}
35+
"value": "./x",
36+
"raw": "'./x'"
4137
},
4238
"options": {
4339
"type": "ObjectExpression",
@@ -121,15 +117,11 @@
121117
"typeAnnotation": {
122118
"type": "TSImportType",
123119
"start":64,"end":107,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":50}},
124-
"argument": {
125-
"type": "TSLiteralType",
120+
"source": {
121+
"type": "Literal",
126122
"start":71,"end":76,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
127-
"literal": {
128-
"type": "Literal",
129-
"start":71,"end":76,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
130-
"value": "./y",
131-
"raw": "'./y'"
132-
}
123+
"value": "./y",
124+
"raw": "'./y'"
133125
},
134126
"options": {
135127
"type": "ObjectExpression",
@@ -218,15 +210,11 @@
218210
"typeAnnotation": {
219211
"type": "TSImportType",
220212
"start":116,"end":172,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":63}},
221-
"argument": {
222-
"type": "TSLiteralType",
213+
"source": {
214+
"type": "Literal",
223215
"start":123,"end":127,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
224-
"literal": {
225-
"type": "Literal",
226-
"start":123,"end":127,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
227-
"value": "/z",
228-
"raw": "\"/z\""
229-
}
216+
"value": "/z",
217+
"raw": "\"/z\""
230218
},
231219
"options": {
232220
"type": "ObjectExpression",

packages/babel-parser/test/fixtures/estree/typescript/import/output.json

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
"exprName": {
3030
"type": "TSImportType",
3131
"start":14,"end":27,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":27}},
32-
"argument": {
33-
"type": "TSLiteralType",
32+
"source": {
33+
"type": "Literal",
3434
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
35-
"literal": {
36-
"type": "Literal",
37-
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
38-
"value": "./x",
39-
"raw": "'./x'"
40-
}
35+
"value": "./x",
36+
"raw": "'./x'"
4137
},
4238
"options": null,
4339
"qualifier": null,
@@ -72,15 +68,11 @@
7268
"typeAnnotation": {
7369
"type": "TSImportType",
7470
"start":36,"end":51,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":22}},
75-
"argument": {
76-
"type": "TSLiteralType",
71+
"source": {
72+
"type": "Literal",
7773
"start":43,"end":48,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
78-
"literal": {
79-
"type": "Literal",
80-
"start":43,"end":48,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
81-
"value": "./y",
82-
"raw": "'./y'"
83-
}
74+
"value": "./y",
75+
"raw": "'./y'"
8476
},
8577
"options": null,
8678
"qualifier": {
@@ -120,15 +112,11 @@
120112
"typeAnnotation": {
121113
"type": "TSImportType",
122114
"start":60,"end":88,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":35}},
123-
"argument": {
124-
"type": "TSLiteralType",
115+
"source": {
116+
"type": "Literal",
125117
"start":67,"end":71,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
126-
"literal": {
127-
"type": "Literal",
128-
"start":67,"end":71,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":18}},
129-
"value": "/z",
130-
"raw": "\"/z\""
131-
}
118+
"value": "/z",
119+
"raw": "\"/z\""
132120
},
133121
"options": null,
134122
"qualifier": {

packages/babel-parser/test/fixtures/typescript/type-arguments/empty-type-import/output.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@
2727
"typeAnnotation": {
2828
"type": "TSImportType",
2929
"start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}},
30-
"argument": {
31-
"type": "TSLiteralType",
30+
"source": {
31+
"type": "StringLiteral",
3232
"start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16}},
33-
"literal": {
34-
"type": "StringLiteral",
35-
"start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16}},
36-
"extra": {
37-
"rawValue": "",
38-
"raw": "\"\""
39-
},
40-
"value": ""
41-
}
33+
"extra": {
34+
"rawValue": "",
35+
"raw": "\"\""
36+
},
37+
"value": ""
4238
},
4339
"options": null,
4440
"typeArguments": {

packages/babel-parser/test/fixtures/typescript/types/import-type-dynamic-errors/output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"typeAnnotation": {
2424
"type": "TSImportType",
2525
"start":9,"end":18,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":18,"index":18}},
26-
"argument": {
26+
"source": {
2727
"type": "TSLiteralType",
2828
"start":16,"end":17,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":17,"index":17}},
2929
"literal": {
@@ -50,7 +50,7 @@
5050
"typeAnnotation": {
5151
"type": "TSImportType",
5252
"start":29,"end":42,"loc":{"start":{"line":2,"column":9,"index":29},"end":{"line":2,"column":22,"index":42}},
53-
"argument": {
53+
"source": {
5454
"type": "TSLiteralType",
5555
"start":36,"end":41,"loc":{"start":{"line":2,"column":16,"index":36},"end":{"line":2,"column":21,"index":41}},
5656
"literal": {
@@ -84,7 +84,7 @@
8484
"typeAnnotation": {
8585
"type": "TSImportType",
8686
"start":53,"end":62,"loc":{"start":{"line":3,"column":9,"index":53},"end":{"line":3,"column":18,"index":62}},
87-
"argument": {
87+
"source": {
8888
"type": "TSTypeReference",
8989
"start":60,"end":61,"loc":{"start":{"line":3,"column":16,"index":60},"end":{"line":3,"column":17,"index":61}},
9090
"typeName": {

0 commit comments

Comments
 (0)