Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create TSTypeQuery node when TSImportType has isTypeOf #3076

Merged
merged 6 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/scope-manager/src/referencer/TypeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ class TypeVisitor extends Visitor {
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
if (node.exprName.type === AST_NODE_TYPES.Identifier) {
this.#referencer.currentScope().referenceValue(node.exprName);
} else if (node.exprName.type === AST_NODE_TYPES.TSImportType) {
this.visit(node.exprName);
} else {
let expr = node.exprName.left;
while (expr.type !== AST_NODE_TYPES.Identifier) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
type A = typeof import('A');
type B = import("B").X<Y>;
type B = import('B').X<Y>;
5 changes: 2 additions & 3 deletions packages/types/src/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,8 +1416,7 @@ export interface TSImportEqualsDeclaration extends BaseNode {

export interface TSImportType extends BaseNode {
type: AST_NODE_TYPES.TSImportType;
isTypeOf: boolean;
parameter: TypeNode;
argument: TypeNode;
qualifier: EntityName | null;
typeParameters: TSTypeParameterInstantiation | null;
}
Expand Down Expand Up @@ -1686,7 +1685,7 @@ export interface TSTypePredicate extends BaseNode {

export interface TSTypeQuery extends BaseNode {
type: AST_NODE_TYPES.TSTypeQuery;
exprName: EntityName;
exprName: EntityName | TSImportType;
}

export interface TSTypeReference extends BaseNode {
Expand Down
21 changes: 17 additions & 4 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2542,19 +2542,32 @@ export class Converter {
return result;
}

case SyntaxKind.ImportType:
return this.createNode<TSESTree.TSImportType>(node, {
case SyntaxKind.ImportType: {
const range = getRange(node, this.ast);
if (node.isTypeOf) {
const token = findNextToken(node.getFirstToken()!, node, this.ast)!;
range[0] = token.getStart(this.ast);
}
const result = this.createNode<TSESTree.TSImportType>(node, {
type: AST_NODE_TYPES.TSImportType,
isTypeOf: !!node.isTypeOf,
parameter: this.convertChild(node.argument),
argument: this.convertChild(node.argument),
Comment on lines -2737 to +2741
Copy link
Member Author

@armano2 armano2 Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i renamed this property from parameter to argument as is more aligned with typescript/babel and rest of types

eg, call expression has arguments

qualifier: this.convertChild(node.qualifier),
typeParameters: node.typeArguments
? this.convertTypeArgumentsToTypeParameters(
node.typeArguments,
node,
)
: null,
range: range,
});
if (node.isTypeOf) {
return this.createNode<TSESTree.TSTypeQuery>(node, {
type: AST_NODE_TYPES.TSTypeQuery,
exprName: result,
});
}
return result;
}

case SyntaxKind.EnumDeclaration: {
const result = this.createNode<TSESTree.TSEnumDeclaration>(node, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export interface EstreeToTsNodeTypes {
| ts.NewExpression
| ts.CallExpression;
[AST_NODE_TYPES.TSTypePredicate]: ts.TypePredicateNode;
[AST_NODE_TYPES.TSTypeQuery]: ts.TypeQueryNode;
[AST_NODE_TYPES.TSTypeQuery]: ts.TypeQueryNode | ts.ImportTypeNode;
[AST_NODE_TYPES.TSTypeReference]: ts.TypeReferenceNode;
[AST_NODE_TYPES.TSUnionType]: ts.UnionTypeNode;
[AST_NODE_TYPES.UpdateExpression]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,6 @@ tester.addFixturePatternConfig('typescript/basics', {
* TODO: report it to babel
*/
'interface-with-extends-member-expression',
/**
* @see https://github.com/typescript-eslint/typescript-eslint/issues/2998
*/
'type-import-type',
'type-import-type-with-type-parameters-in-type-reference',
/**
* Not yet supported in Babel
* Directive field is not added to module and namespace
Expand Down
22 changes: 22 additions & 0 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
};
}
},
TSImportType(node: any) {
if (!node.typeParameters) {
node.typeParameters = null;
}
if (!node.qualifier) {
node.qualifier = null;
}
/**
* https://github.com/babel/babel/issues/12833
*/
if (node.argument) {
node.argument = {
type: AST_NODE_TYPES.TSLiteralType,
literal: node.argument,
loc: {
start: { ...node.argument.loc.start },
end: { ...node.argument.loc.end },
},
range: [...node.argument.range],
};
}
armano2 marked this conversation as resolved.
Show resolved Hide resolved
},
TSTypePredicate(node) {
if (!node.typeAnnotation) {
node.typeAnnotation = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,7 @@ Object {
},
"params": Array [
Object {
"isTypeOf": false,
"loc": Object {
"end": Object {
"column": 28,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"parameter": Object {
"argument": Object {
"literal": Object {
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -131,6 +120,16 @@ Object {
],
"type": "TSLiteralType",
},
"loc": Object {
"end": Object {
"column": 28,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"qualifier": Object {
"loc": Object {
"end": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,27 @@ Object {
],
"type": "TSTypeAliasDeclaration",
"typeAnnotation": Object {
"isTypeOf": true,
"loc": Object {
"end": Object {
"column": 27,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"parameter": Object {
"literal": Object {
"exprName": Object {
"argument": Object {
"literal": Object {
"loc": Object {
"end": Object {
"column": 26,
"line": 1,
},
"start": Object {
"column": 23,
"line": 1,
},
},
"range": Array [
23,
26,
],
"raw": "'A'",
"type": "Literal",
"value": "A",
},
"loc": Object {
"end": Object {
"column": 26,
Expand All @@ -65,33 +73,41 @@ Object {
23,
26,
],
"raw": "'A'",
"type": "Literal",
"value": "A",
"type": "TSLiteralType",
},
"loc": Object {
"end": Object {
"column": 26,
"column": 27,
"line": 1,
},
"start": Object {
"column": 23,
"column": 16,
"line": 1,
},
},
"qualifier": null,
"range": Array [
23,
26,
16,
27,
],
"type": "TSLiteralType",
"type": "TSImportType",
"typeParameters": null,
},
"loc": Object {
"end": Object {
"column": 27,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"qualifier": null,
"range": Array [
9,
27,
],
"type": "TSImportType",
"typeParameters": null,
"type": "TSTypeQuery",
},
},
Object {
Expand Down Expand Up @@ -129,18 +145,7 @@ Object {
],
"type": "TSTypeAliasDeclaration",
"typeAnnotation": Object {
"isTypeOf": false,
"loc": Object {
"end": Object {
"column": 25,
"line": 2,
},
"start": Object {
"column": 9,
"line": 2,
},
},
"parameter": Object {
"argument": Object {
"literal": Object {
"loc": Object {
"end": Object {
Expand All @@ -156,7 +161,7 @@ Object {
45,
48,
],
"raw": "\\"B\\"",
"raw": "'B'",
"type": "Literal",
"value": "B",
},
Expand All @@ -176,6 +181,16 @@ Object {
],
"type": "TSLiteralType",
},
"loc": Object {
"end": Object {
"column": 25,
"line": 2,
},
"start": Object {
"column": 9,
"line": 2,
},
},
"qualifier": Object {
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -542,7 +557,7 @@ Object {
48,
],
"type": "String",
"value": "\\"B\\"",
"value": "'B'",
},
Object {
"loc": Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/visitor-keys/src/visitor-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const additionalKeys: AdditionalKeys = {
TSExternalModuleReference: ['expression'],
TSFunctionType: ['typeParameters', 'params', 'returnType'],
TSImportEqualsDeclaration: ['id', 'moduleReference'],
TSImportType: ['parameter', 'qualifier', 'typeParameters'],
TSImportType: ['argument', 'qualifier', 'typeParameters'],
TSIndexedAccessType: ['indexType', 'objectType'],
TSIndexSignature: ['parameters', 'typeAnnotation'],
TSInferType: ['typeParameter'],
Expand Down