Skip to content

Commit

Permalink
feat: create TSTypeQuery node when TSImportType has isTypeOf (#3076)
Browse files Browse the repository at this point in the history
* feat: update TSImportType node

* fix: update visitor keys

* chore: document and refactor 'extra' to 'parserSettings' (#5834)

* chore(website): fix renamed Sponsorship docs link (#5882)

* docs: Mention wide globs performance implications in monorepos docs and parser README (#5864)

* docs: Mention wide globs performance implications in monorepos docs and parser readme

* Update docs/linting/typed-linting/MONOREPOS.md

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
Co-authored-by: Adnan Hashmi <56730784+adnanhashmi09@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 26, 2022
1 parent 0414e4d commit 04488c2
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 66 deletions.
3 changes: 1 addition & 2 deletions packages/ast-spec/src/type/TSImportType/spec.ts
Expand Up @@ -6,8 +6,7 @@ import type { TypeNode } from '../../unions/TypeNode';

export interface TSImportType extends BaseNode {
type: AST_NODE_TYPES.TSImportType;
isTypeOf: boolean;
parameter: TypeNode;
argument: TypeNode;
qualifier: EntityName | null;
typeParameters: TSTypeParameterInstantiation | null;
}
3 changes: 2 additions & 1 deletion packages/ast-spec/src/type/TSTypeQuery/spec.ts
Expand Up @@ -2,9 +2,10 @@ import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { TSTypeParameterInstantiation } from '../../special/spec';
import type { EntityName } from '../../unions/EntityName';
import type { TSImportType } from '../TSImportType/spec';

export interface TSTypeQuery extends BaseNode {
type: AST_NODE_TYPES.TSTypeQuery;
exprName: EntityName;
exprName: EntityName | TSImportType;
typeParameters?: TSTypeParameterInstantiation;
}
9 changes: 8 additions & 1 deletion packages/scope-manager/src/referencer/TypeVisitor.ts
Expand Up @@ -260,7 +260,10 @@ class TypeVisitor extends Visitor {

// a type query `typeof foo` is a special case that references a _non-type_ variable,
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
let entityName: TSESTree.Identifier | TSESTree.ThisExpression;
let entityName:
| TSESTree.Identifier
| TSESTree.ThisExpression
| TSESTree.TSImportType;
if (node.exprName.type === AST_NODE_TYPES.TSQualifiedName) {
let iter = node.exprName;
while (iter.left.type === AST_NODE_TYPES.TSQualifiedName) {
Expand All @@ -269,6 +272,10 @@ class TypeVisitor extends Visitor {
entityName = iter.left;
} else {
entityName = node.exprName;

if (node.exprName.type === AST_NODE_TYPES.TSImportType) {
this.visit(node.exprName);
}
}
if (entityName.type === AST_NODE_TYPES.Identifier) {
this.#referencer.currentScope().referenceValue(entityName);
Expand Down
@@ -1,2 +1,2 @@
type A = typeof import('A');
type B = import("B").X<Y>;
type B = import('B').X<Y>;
21 changes: 17 additions & 4 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2730,19 +2730,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),
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
Expand Up @@ -225,7 +225,7 @@ export interface EstreeToTsNodeTypes {
| ts.CallExpression
| ts.TypeQueryNode;
[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
Expand Up @@ -341,11 +341,6 @@ tester.addFixturePatternConfig('typescript/basics', {
* @see https://github.com/babel/babel/issues/12884
*/
'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
Expand Up @@ -292,6 +292,28 @@ export function preprocessBabylonAST(ast: File): any {
delete node.loc.start.index;
}
},
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],
};
}
},
TSTypePredicate(node: any) {
if (node.loc?.start?.index) {
delete node.loc.start.index;
Expand Down
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
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
Expand Up @@ -96,7 +96,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

0 comments on commit 04488c2

Please sign in to comment.