Skip to content

Commit

Permalink
Update to @typescript-eslint/typescript-estree 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Feb 8, 2022
1 parent 67b4972 commit 55170b1
Show file tree
Hide file tree
Showing 13 changed files with 2,042 additions and 2,339 deletions.
23 changes: 8 additions & 15 deletions lib/parse/ClassIndex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import type { ClassDeclaration,
TSInterfaceDeclaration,
TSTypeAliasDeclaration,
TSEnumDeclaration,
TypeNode,
TSTypeParameterInstantiation } from '@typescript-eslint/types/dist/ts-estree';

import type { AST, TSESTreeOptions } from '@typescript-eslint/typescript-estree';
import type { AST, TSESTreeOptions, TSESTree } from '@typescript-eslint/typescript-estree';

/**
* A collection of classes, with exported name as key.
Expand Down Expand Up @@ -48,7 +41,7 @@ export interface ClassLoaded extends ClassReference {
// The name of the file the class is defined in.
fileName: string;
// The loaded class declaration.
declaration: ClassDeclaration;
declaration: TSESTree.ClassDeclaration;
// The full AST the class was present in.
ast: AST<TSESTreeOptions>;
// A super class reference if the class has one
Expand All @@ -66,7 +59,7 @@ export interface ClassLoaded extends ClassReference {
/**
* A hash of generic type name to its properties.
*/
export type GenericTypes = Record<string, { type?: TypeNode }>;
export type GenericTypes = Record<string, { type?: TSESTree.TypeNode }>;

/**
* Something (like a class or interface) that may have generic types assigned to it as instantiation.
Expand All @@ -75,7 +68,7 @@ export interface GenericallyTyped<T> {
// The typed value
value: T;
// The generic types of this value
genericTypeInstantiations?: TSTypeParameterInstantiation;
genericTypeInstantiations?: TSESTree.TSTypeParameterInstantiation;
}

/**
Expand All @@ -88,7 +81,7 @@ export interface InterfaceLoaded extends ClassReference {
// The name of the file the interface is defined in.
fileName: string;
// The loaded interface declaration.
declaration: TSInterfaceDeclaration;
declaration: TSESTree.TSInterfaceDeclaration;
// The full AST the interface was present in.
ast: AST<TSESTreeOptions>;
// Super interface references if the interface has them
Expand All @@ -104,7 +97,7 @@ export interface InterfaceLoaded extends ClassReference {
*/
export interface MemberField {
name: string;
range: TypeNode | undefined;
range: TSESTree.TypeNode | undefined;
}

/**
Expand All @@ -117,7 +110,7 @@ export interface TypeLoaded extends ClassReference {
// The name of the file the interface is defined in.
fileName: string;
// The loaded type declaration.
declaration: TSTypeAliasDeclaration;
declaration: TSESTree.TSTypeAliasDeclaration;
// The full AST the interface was present in.
ast: AST<TSESTreeOptions>;
// The tsdoc comment of this class
Expand All @@ -136,7 +129,7 @@ export interface EnumLoaded extends ClassReference {
// The name of the file the interface is defined in.
fileName: string;
// The loaded enum declaration.
declaration: TSEnumDeclaration;
declaration: TSESTree.TSEnumDeclaration;
// The full AST the interface was present in.
ast: AST<TSESTreeOptions>;
// The tsdoc comment of this class
Expand Down
81 changes: 41 additions & 40 deletions lib/parse/ClassLoader.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import * as Path from 'path';
import type {
ClassDeclaration,
TSEnumDeclaration,
TSInterfaceDeclaration,
TSModuleBlock,
TSModuleDeclaration,
TSTypeAliasDeclaration,
} from '@typescript-eslint/types/dist/ts-estree';
import type { AST, TSESTreeOptions } from '@typescript-eslint/typescript-estree';
import type { AST, TSESTreeOptions, TSESTree } from '@typescript-eslint/typescript-estree';
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import type { Logger } from 'winston';
import type { ResolutionContext } from '../resolution/ResolutionContext';
Expand Down Expand Up @@ -43,7 +35,10 @@ export class ClassLoader {
* @param declaration A class declaration.
* @param fileName The file name of the current class.
*/
public getSuperClassName(declaration: ClassDeclaration, fileName: string): GenericallyTyped<string> | undefined {
public getSuperClassName(
declaration: TSESTree.ClassDeclaration,
fileName: string,
): GenericallyTyped<string> | undefined {
if (!declaration.superClass) {
return;
}
Expand All @@ -69,7 +64,10 @@ export class ClassLoader {
* @param declaration An interface declaration.
* @param fileName The file name of the current class.
*/
public getSuperInterfaceNames(declaration: TSInterfaceDeclaration, fileName: string): GenericallyTyped<string>[] {
public getSuperInterfaceNames(
declaration: TSESTree.TSInterfaceDeclaration,
fileName: string,
): GenericallyTyped<string>[] {
return <GenericallyTyped<string>[]> (declaration.extends || [])
// eslint-disable-next-line array-callback-return
.map(extendsExpression => {
Expand All @@ -92,7 +90,10 @@ export class ClassLoader {
* @param declaration A class declaration.
* @param fileName The file name of the current class.
*/
public getClassInterfaceNames(declaration: ClassDeclaration, fileName: string): GenericallyTyped<string>[] {
public getClassInterfaceNames(
declaration: TSESTree.ClassDeclaration,
fileName: string,
): GenericallyTyped<string>[] {
const interfaceNames: GenericallyTyped<string>[] = [];
if (declaration.implements) {
for (const implement of declaration.implements) {
Expand Down Expand Up @@ -154,7 +155,7 @@ export class ClassLoader {
* such as a type alias or enum.
*/
public async loadClassDeclarationFromAst<CI extends boolean, CT extends boolean>(
ast: AST<TSESTreeOptions> | TSModuleBlock,
ast: AST<TSESTreeOptions> | TSESTree.TSModuleBlock,
targetString: string,
classReference: ClassReference,
considerInterfaces: CI,
Expand Down Expand Up @@ -367,7 +368,7 @@ export class ClassLoader {
if (enumMember.id.type === AST_NODE_TYPES.Identifier && enumMember.id.name === enumKey &&
enumMember.initializer && enumMember.initializer.type === AST_NODE_TYPES.Literal) {
// Expose the enum entry as type alias
const typeNode: TSTypeAliasDeclaration = {
const typeNode: TSESTree.TSTypeAliasDeclaration = {
type: AST_NODE_TYPES.TSTypeAliasDeclaration,
id: {
type: AST_NODE_TYPES.Identifier,
Expand Down Expand Up @@ -411,9 +412,9 @@ export class ClassLoader {

// Check if the export assignment refers to a namespace
if (exportAssignment && typeof exportAssignment === 'string' && exportAssignment in declaredNamespaces) {
const namespace: TSModuleDeclaration = declaredNamespaces[exportAssignment];
const namespace: TSESTree.TSModuleDeclaration = declaredNamespaces[exportAssignment];
return this.loadClassDeclarationFromAst(
<TSModuleBlock>namespace.body,
<TSESTree.TSModuleBlock>namespace.body,
targetString,
classReference,
considerInterfaces,
Expand All @@ -430,7 +431,7 @@ export class ClassLoader {
* @param classDeclaration A class or interface declaration.
*/
public collectGenericTypes(
classDeclaration: ClassDeclaration | TSInterfaceDeclaration | TSTypeAliasDeclaration,
classDeclaration: TSESTree.ClassDeclaration | TSESTree.TSInterfaceDeclaration | TSESTree.TSTypeAliasDeclaration,
): GenericTypes {
const genericTypes: GenericTypes = {};
if (classDeclaration.typeParameters) {
Expand Down Expand Up @@ -533,27 +534,27 @@ export class ClassLoader {
public getClassElements(
packageName: string,
fileName: string,
ast: AST<TSESTreeOptions> | TSModuleBlock,
ast: AST<TSESTreeOptions> | TSESTree.TSModuleBlock,
): ClassElements {
const exportedClasses: Record<string, ClassDeclaration> = {};
const exportedInterfaces: Record<string, TSInterfaceDeclaration> = {};
const exportedTypes: Record<string, TSTypeAliasDeclaration> = {};
const exportedEnums: Record<string, TSEnumDeclaration> = {};
const exportedNamespaces: Record<string, TSModuleDeclaration> = {};
const exportedClasses: Record<string, TSESTree.ClassDeclaration> = {};
const exportedInterfaces: Record<string, TSESTree.TSInterfaceDeclaration> = {};
const exportedTypes: Record<string, TSESTree.TSTypeAliasDeclaration> = {};
const exportedEnums: Record<string, TSESTree.TSEnumDeclaration> = {};
const exportedNamespaces: Record<string, TSESTree.TSModuleDeclaration> = {};
const exportedImportedElements: Record<string, ClassReference> = {};
const exportedImportedAll: { packageName: string; fileName: string; fileNameReferenced: string }[] = [];
const exportedImportedAllNamed:
Record<string, { packageName: string; fileName: string; fileNameReferenced: string }> = {};
const exportedUnknowns: Record<string, string> = {};
const declaredClasses: Record<string, ClassDeclaration> = {};
const declaredInterfaces: Record<string, TSInterfaceDeclaration> = {};
const declaredTypes: Record<string, TSTypeAliasDeclaration> = {};
const declaredEnums: Record<string, TSEnumDeclaration> = {};
const declaredNamespaces: Record<string, TSModuleDeclaration> = {};
const declaredClasses: Record<string, TSESTree.ClassDeclaration> = {};
const declaredInterfaces: Record<string, TSESTree.TSInterfaceDeclaration> = {};
const declaredTypes: Record<string, TSESTree.TSTypeAliasDeclaration> = {};
const declaredEnums: Record<string, TSESTree.TSEnumDeclaration> = {};
const declaredNamespaces: Record<string, TSESTree.TSModuleDeclaration> = {};
const importedElements: Record<string, ClassReference> = {};
const importedElementsAllNamed:
Record<string, { packageName: string; fileName: string; fileNameReferenced: string }> = {};
let exportAssignment: string | ClassDeclaration | undefined;
let exportAssignment: string | TSESTree.ClassDeclaration | undefined;

for (const statement of ast.body) {
if (statement.type === AST_NODE_TYPES.ExportNamedDeclaration) {
Expand Down Expand Up @@ -685,15 +686,15 @@ export interface ClassLoaderArgs {
*/
export interface ClassElements {
// Classes that have been declared in a file via `export class A`
exportedClasses: Record<string, ClassDeclaration>;
exportedClasses: Record<string, TSESTree.ClassDeclaration>;
// Interfaces that have been declared in a file via `export interface A`
exportedInterfaces: Record<string, TSInterfaceDeclaration>;
exportedInterfaces: Record<string, TSESTree.TSInterfaceDeclaration>;
// Types that have been declared in a file via `export type A = ...`
exportedTypes: Record<string, TSTypeAliasDeclaration>;
exportedTypes: Record<string, TSESTree.TSTypeAliasDeclaration>;
// Enums that have been declared in a file via `export enum A {...}`
exportedEnums: Record<string, TSEnumDeclaration>;
exportedEnums: Record<string, TSESTree.TSEnumDeclaration>;
// Namespaces that have been declared in a file via `export namespace A { ... }`
exportedNamespaces: Record<string, TSModuleDeclaration>;
exportedNamespaces: Record<string, TSESTree.TSModuleDeclaration>;
// Elements that have been exported via `export { A as B } from "b"`
exportedImportedElements: Record<string, ClassReference>;
// Exports via `export * from "b"`
Expand All @@ -703,19 +704,19 @@ export interface ClassElements {
// Things that have been exported via `export {A as B}`, where the target is not known
exportedUnknowns: Record<string, string>;
// Classes that have been declared in a file via `declare class A`
declaredClasses: Record<string, ClassDeclaration>;
declaredClasses: Record<string, TSESTree.ClassDeclaration>;
// Interfaces that have been declared in a file via `declare interface A`
declaredInterfaces: Record<string, TSInterfaceDeclaration>;
declaredInterfaces: Record<string, TSESTree.TSInterfaceDeclaration>;
// Types that have been declared in a file via `declare type A = ...`
declaredTypes: Record<string, TSTypeAliasDeclaration>;
declaredTypes: Record<string, TSESTree.TSTypeAliasDeclaration>;
// Enums that have been declared in a file via `declare enum A {...}`
declaredEnums: Record<string, TSEnumDeclaration>;
declaredEnums: Record<string, TSESTree.TSEnumDeclaration>;
// Namespaces that have been declared in a file via `declare namespace A { ... }`
declaredNamespaces: Record<string, TSModuleDeclaration>;
declaredNamespaces: Record<string, TSESTree.TSModuleDeclaration>;
// Elements that are imported from elsewhere via `import {A} from ''`
importedElements: Record<string, ClassReference>;
// Elements that are imported from elsewhere via `import * as A from ''`
importedElementsAllNamed: Record<string, { packageName: string; fileName: string; fileNameReferenced: string }>;
// Element exported via `export = ...`
exportAssignment: string | ClassDeclaration | undefined;
exportAssignment: string | TSESTree.ClassDeclaration | undefined;
}
10 changes: 4 additions & 6 deletions lib/parse/CommentLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { MethodDefinition, TSPropertySignature,
TSIndexSignature, BaseNode } from '@typescript-eslint/types/dist/ts-estree';
import type { TSESTree } from '@typescript-eslint/typescript-estree';
import * as commentParse from 'comment-parser';
import type { ClassReference, ClassReferenceLoaded } from './ClassIndex';
import type { ConstructorHolder } from './ConstructorLoader';

import type { DefaultNested, DefaultValue, ParameterRangeUnresolved } from './ParameterLoader';

/**
Expand Down Expand Up @@ -48,7 +46,7 @@ export class CommentLoader {
*/
public getCommentDataFromConstructorSingle(
classLoaded: ClassReferenceLoaded,
constructor: MethodDefinition,
constructor: TSESTree.MethodDefinition,
): ConstructorCommentData {
// Get the constructor comment
const comment = this.getCommentRaw(classLoaded, constructor);
Expand Down Expand Up @@ -94,7 +92,7 @@ export class CommentLoader {
*/
public getCommentDataFromField(
classLoaded: ClassReferenceLoaded,
field: TSPropertySignature | TSIndexSignature,
field: TSESTree.TSPropertySignature | TSESTree.TSIndexSignature,
): CommentData {
const comment = this.getCommentRaw(classLoaded, field);
if (comment) {
Expand Down Expand Up @@ -227,7 +225,7 @@ export class CommentLoader {
* @param classLoaded The loaded class in which the field is defined.
* @param node A node, such as a field or constructor.
*/
public getCommentRaw(classLoaded: ClassReferenceLoaded, node: BaseNode): string | undefined {
public getCommentRaw(classLoaded: ClassReferenceLoaded, node: TSESTree.BaseNode): string | undefined {
const line = node.loc.start.line;
for (const comment of classLoaded.ast.comments || []) {
if (comment.loc.end.line === line - 1) {
Expand Down
23 changes: 10 additions & 13 deletions lib/parse/ConstructorLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ClassDeclaration, MethodDefinition } from '@typescript-eslint/types/dist/ts-estree';
import type { AST, TSESTreeOptions } from '@typescript-eslint/typescript-estree';
import type { AST, TSESTreeOptions, TSESTree } from '@typescript-eslint/typescript-estree';
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import type { ClassIndex, ClassLoaded, ClassReferenceLoaded, GenericallyTyped } from './ClassIndex';
import type { ParameterDataField, ParameterRangeUnresolved, ParameterLoader } from './ParameterLoader';
Expand Down Expand Up @@ -63,16 +62,14 @@ export class ConstructorLoader {
*/
public getConstructor(classLoaded: GenericallyTyped<ClassLoaded>): ConstructorHolder | undefined {
// First look for the constructor in this class
let constructor: MethodDefinition | undefined = this.getConstructorInClass(classLoaded.value.declaration);
let constructor: TSESTree.MethodDefinition | undefined = this.getConstructorInClass(classLoaded.value.declaration);

// If no constructor was found, look in the super class
if (!constructor) {
if (classLoaded.value.superClass) {
const constructorDataSuper = this.getConstructor(classLoaded.value.superClass);
if (constructorDataSuper) {
constructor = constructorDataSuper.constructor;
classLoaded = constructorDataSuper.classLoaded;
}
if (!constructor && classLoaded.value.superClass) {
const constructorDataSuper = this.getConstructor(classLoaded.value.superClass);
if (constructorDataSuper) {
constructor = constructorDataSuper.constructor;
classLoaded = constructorDataSuper.classLoaded;
}
}

Expand All @@ -83,7 +80,7 @@ export class ConstructorLoader {
* Retrieve the constructor in the given class, or undefined if it could not be found.
* @param declaration A class declaration
*/
public getConstructorInClass(declaration: ClassDeclaration): MethodDefinition | undefined {
public getConstructorInClass(declaration: TSESTree.ClassDeclaration): TSESTree.MethodDefinition | undefined {
for (const element of declaration.body.body) {
if (element.type === AST_NODE_TYPES.MethodDefinition &&
element.kind === 'constructor') {
Expand All @@ -99,7 +96,7 @@ export class ConstructorLoader {
* @param ast A parsed typescript file
* @param fileName The file name, for error reporting.
*/
public getClass(className: string, ast: AST<TSESTreeOptions>, fileName: string): ClassDeclaration {
public getClass(className: string, ast: AST<TSESTreeOptions>, fileName: string): TSESTree.ClassDeclaration {
for (const statement of ast.body) {
// Classes in the form of `declare class A {}`
if (statement.type === AST_NODE_TYPES.ClassDeclaration &&
Expand Down Expand Up @@ -136,6 +133,6 @@ export interface ConstructorData<R> {
* Datastructure for holding a constructor and the class it is part of.
*/
export interface ConstructorHolder {
constructor: MethodDefinition;
constructor: TSESTree.MethodDefinition;
classLoaded: GenericallyTyped<ClassLoaded>;
}
8 changes: 4 additions & 4 deletions lib/parse/MemberLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export class MemberLoader {
for (const element of classLoaded.declaration.body.body) {
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (element.type) {
case AST_NODE_TYPES.ClassProperty:
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.MethodDefinition:
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSMethodSignature:
if (element.key.type === 'Identifier') {
// TODO: more types may be needed here, such as AST_NODE_TYPES.TSPropertySignature
const typeNode = element.type === AST_NODE_TYPES.ClassProperty ||
element.type === AST_NODE_TYPES.TSAbstractClassProperty ?
const typeNode = element.type === AST_NODE_TYPES.PropertyDefinition ||
element.type === AST_NODE_TYPES.TSAbstractPropertyDefinition ?
element.typeAnnotation?.typeAnnotation :
undefined;
members.push({
Expand Down

0 comments on commit 55170b1

Please sign in to comment.