Skip to content

Commit 9e7abf9

Browse files
committed
feat: update TSImportType node
1 parent 5e2a993 commit 9e7abf9

File tree

9 files changed

+420
-67
lines changed

9 files changed

+420
-67
lines changed

packages/scope-manager/src/referencer/TypeVisitor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ class TypeVisitor extends Visitor {
258258
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
259259
if (node.exprName.type === AST_NODE_TYPES.Identifier) {
260260
this.#referencer.currentScope().referenceValue(node.exprName);
261+
} else if (node.exprName.type === AST_NODE_TYPES.TSImportType) {
262+
this.visit(node.exprName);
261263
} else {
262264
let expr = node.exprName.left;
263265
while (expr.type !== AST_NODE_TYPES.Identifier) {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
type A = typeof import('A');
2-
type B = import("B").X<Y>;
2+
type B = import('B').X<Y>;
3+
// prettier-ignore
4+
type C = typeof /* test */ import('A');

packages/types/src/ts-estree.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,8 +1416,7 @@ export interface TSImportEqualsDeclaration extends BaseNode {
14161416

14171417
export interface TSImportType extends BaseNode {
14181418
type: AST_NODE_TYPES.TSImportType;
1419-
isTypeOf: boolean;
1420-
parameter: TypeNode;
1419+
argument: TypeNode;
14211420
qualifier: EntityName | null;
14221421
typeParameters: TSTypeParameterInstantiation | null;
14231422
}
@@ -1686,7 +1685,7 @@ export interface TSTypePredicate extends BaseNode {
16861685

16871686
export interface TSTypeQuery extends BaseNode {
16881687
type: AST_NODE_TYPES.TSTypeQuery;
1689-
exprName: EntityName;
1688+
exprName: EntityName | TSImportType;
16901689
}
16911690

16921691
export interface TSTypeReference extends BaseNode {

packages/typescript-estree/src/convert.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,19 +2542,32 @@ export class Converter {
25422542
return result;
25432543
}
25442544

2545-
case SyntaxKind.ImportType:
2546-
return this.createNode<TSESTree.TSImportType>(node, {
2545+
case SyntaxKind.ImportType: {
2546+
const range = getRange(node, this.ast);
2547+
if (node.isTypeOf) {
2548+
const token = findNextToken(node.getFirstToken()!, node, this.ast)!;
2549+
range[0] = token.getStart(this.ast);
2550+
}
2551+
const result = this.createNode<TSESTree.TSImportType>(node, {
25472552
type: AST_NODE_TYPES.TSImportType,
2548-
isTypeOf: !!node.isTypeOf,
2549-
parameter: this.convertChild(node.argument),
2553+
argument: this.convertChild(node.argument),
25502554
qualifier: this.convertChild(node.qualifier),
25512555
typeParameters: node.typeArguments
25522556
? this.convertTypeArgumentsToTypeParameters(
25532557
node.typeArguments,
25542558
node,
25552559
)
25562560
: null,
2561+
range: range,
25572562
});
2563+
if (node.isTypeOf) {
2564+
return this.createNode<TSESTree.TSTypeQuery>(node, {
2565+
type: AST_NODE_TYPES.TSTypeQuery,
2566+
exprName: result,
2567+
});
2568+
}
2569+
return result;
2570+
}
25582571

25592572
case SyntaxKind.EnumDeclaration: {
25602573
const result = this.createNode<TSESTree.TSEnumDeclaration>(node, {

packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export interface EstreeToTsNodeTypes {
218218
| ts.NewExpression
219219
| ts.CallExpression;
220220
[AST_NODE_TYPES.TSTypePredicate]: ts.TypePredicateNode;
221-
[AST_NODE_TYPES.TSTypeQuery]: ts.TypeQueryNode;
221+
[AST_NODE_TYPES.TSTypeQuery]: ts.TypeQueryNode | ts.ImportTypeNode;
222222
[AST_NODE_TYPES.TSTypeReference]: ts.TypeReferenceNode;
223223
[AST_NODE_TYPES.TSUnionType]: ts.UnionTypeNode;
224224
[AST_NODE_TYPES.UpdateExpression]:

packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,6 @@ tester.addFixturePatternConfig('typescript/basics', {
349349
* TODO: report it to babel
350350
*/
351351
'interface-with-extends-member-expression',
352-
/**
353-
* @see https://github.com/typescript-eslint/typescript-eslint/issues/2998
354-
*/
355-
'type-import-type',
356-
'type-import-type-with-type-parameters-in-type-reference',
357352
/**
358353
* Not yet supported in Babel
359354
* Directive field is not added to module and namespace

packages/typescript-estree/tests/ast-alignment/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
151151
};
152152
}
153153
},
154+
TSImportType(node: any) {
155+
if (!node.typeParameters) {
156+
node.typeParameters = null;
157+
}
158+
if (!node.qualifier) {
159+
node.qualifier = null;
160+
}
161+
/**
162+
* https://github.com/babel/babel/issues/12833
163+
*/
164+
if (node.argument) {
165+
node.argument = {
166+
type: AST_NODE_TYPES.TSLiteralType,
167+
literal: node.argument,
168+
loc: {
169+
start: { ...node.argument.loc.start },
170+
end: { ...node.argument.loc.end },
171+
},
172+
range: [...node.argument.range],
173+
};
174+
}
175+
},
154176
TSTypePredicate(node) {
155177
if (!node.typeAnnotation) {
156178
node.typeAnnotation = null;

packages/typescript-estree/tests/snapshots/typescript/basics/type-import-type-with-type-parameters-in-type-reference.src.ts.shot

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,7 @@ Object {
8484
},
8585
"params": Array [
8686
Object {
87-
"isTypeOf": false,
88-
"loc": Object {
89-
"end": Object {
90-
"column": 28,
91-
"line": 1,
92-
},
93-
"start": Object {
94-
"column": 11,
95-
"line": 1,
96-
},
97-
},
98-
"parameter": Object {
87+
"argument": Object {
9988
"literal": Object {
10089
"loc": Object {
10190
"end": Object {
@@ -131,6 +120,16 @@ Object {
131120
],
132121
"type": "TSLiteralType",
133122
},
123+
"loc": Object {
124+
"end": Object {
125+
"column": 28,
126+
"line": 1,
127+
},
128+
"start": Object {
129+
"column": 11,
130+
"line": 1,
131+
},
132+
},
134133
"qualifier": Object {
135134
"loc": Object {
136135
"end": Object {

0 commit comments

Comments
 (0)