diff --git a/packages/ast-spec/src/element/TSEnumMember/spec.ts b/packages/ast-spec/src/element/TSEnumMember/spec.ts index 3e506809dab9..9dd1ddeee696 100644 --- a/packages/ast-spec/src/element/TSEnumMember/spec.ts +++ b/packages/ast-spec/src/element/TSEnumMember/spec.ts @@ -1,6 +1,5 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; -import type { TSEnumBody } from '../../special/spec'; import type { Expression } from '../../unions/Expression'; import type { PropertyNameComputed, @@ -14,7 +13,6 @@ interface TSEnumMemberBase extends BaseNode { | PropertyNameNonComputed; initializer: Expression | undefined; computed: boolean; - parent: TSEnumBody; } /** diff --git a/packages/ast-spec/src/special/TSEnumBody/spec.ts b/packages/ast-spec/src/special/TSEnumBody/spec.ts index f6eef26a1afe..3cdb9bbccdf0 100644 --- a/packages/ast-spec/src/special/TSEnumBody/spec.ts +++ b/packages/ast-spec/src/special/TSEnumBody/spec.ts @@ -1,10 +1,8 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; -import type { TSEnumDeclaration } from '../../declaration/TSEnumDeclaration/spec'; import type { TSEnumMember } from '../../element/TSEnumMember/spec'; export interface TSEnumBody extends BaseNode { type: AST_NODE_TYPES.TSEnumBody; members: TSEnumMember[]; - parent: TSEnumDeclaration; } diff --git a/packages/eslint-plugin-internal/src/rules/no-typescript-default-import.ts b/packages/eslint-plugin-internal/src/rules/no-typescript-default-import.ts index 594ac9f51c2f..dd252175f9a3 100644 --- a/packages/eslint-plugin-internal/src/rules/no-typescript-default-import.ts +++ b/packages/eslint-plugin-internal/src/rules/no-typescript-default-import.ts @@ -41,7 +41,7 @@ export default createRule({ 'ImportDeclaration > ImportDefaultSpecifier'( node: TSESTree.ImportDefaultSpecifier, ): void { - const importStatement = node.parent as TSESTree.ImportDeclaration; + const importStatement = node.parent; if (importStatement.source.value === 'typescript') { context.report({ node, diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index 6813e80f60d8..816c7803604a 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -169,17 +169,15 @@ export default createRule({ const name = getStringValue(node.key); - if (node.parent.type === AST_NODE_TYPES.ClassBody) { - const hasDuplicateKeySetter = node.parent.body.some(element => { - return ( - element.type === AST_NODE_TYPES.MethodDefinition && - element.kind === 'set' && - getStringValue(element.key) === name - ); - }); - if (hasDuplicateKeySetter) { - return; - } + const hasDuplicateKeySetter = node.parent.body.some(element => { + return ( + element.type === AST_NODE_TYPES.MethodDefinition && + element.kind === 'set' && + getStringValue(element.key) === name + ); + }); + if (hasDuplicateKeySetter) { + return; } context.report({ diff --git a/packages/eslint-plugin/src/rules/class-methods-use-this.ts b/packages/eslint-plugin/src/rules/class-methods-use-this.ts index 6236a46ddb7b..4f574471e03b 100644 --- a/packages/eslint-plugin/src/rules/class-methods-use-this.ts +++ b/packages/eslint-plugin/src/rules/class-methods-use-this.ts @@ -114,9 +114,7 @@ export default createRule({ if (member?.parent.type === AST_NODE_TYPES.ClassBody) { stack = { member, - class: member.parent.parent as - | TSESTree.ClassDeclaration - | TSESTree.ClassExpression, + class: member.parent.parent, usesThis: false, parent: stack, }; diff --git a/packages/eslint-plugin/src/rules/method-signature-style.ts b/packages/eslint-plugin/src/rules/method-signature-style.ts index 0a17da9c0913..48207e5cc91a 100644 --- a/packages/eslint-plugin/src/rules/method-signature-style.ts +++ b/packages/eslint-plugin/src/rules/method-signature-style.ts @@ -133,9 +133,7 @@ export default createRule({ const members = parent.type === AST_NODE_TYPES.TSInterfaceBody ? parent.body - : parent.type === AST_NODE_TYPES.TSTypeLiteral - ? parent.members - : []; + : parent.members; const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = members.filter( diff --git a/packages/eslint-plugin/src/rules/no-extraneous-class.ts b/packages/eslint-plugin/src/rules/no-extraneous-class.ts index a7c05f8cc5a8..854cec6b40eb 100644 --- a/packages/eslint-plugin/src/rules/no-extraneous-class.ts +++ b/packages/eslint-plugin/src/rules/no-extraneous-class.ts @@ -79,9 +79,7 @@ export default createRule({ return { ClassBody(node): void { - const parent = node.parent as - | TSESTree.ClassDeclaration - | TSESTree.ClassExpression; + const parent = node.parent; if (parent.superClass || isAllowWithDecorator(parent)) { return; diff --git a/packages/eslint-plugin/src/rules/no-misused-new.ts b/packages/eslint-plugin/src/rules/no-misused-new.ts index 932a85cc6080..f877f9d7d9cb 100644 --- a/packages/eslint-plugin/src/rules/no-misused-new.ts +++ b/packages/eslint-plugin/src/rules/no-misused-new.ts @@ -99,14 +99,7 @@ export default createRule({ node: TSESTree.MethodDefinition, ): void { if (node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression) { - if ( - isMatchingParentType( - node.parent.parent as - | TSESTree.ClassDeclaration - | TSESTree.ClassExpression, - node.value.returnType, - ) - ) { + if (isMatchingParentType(node.parent.parent, node.value.returnType)) { context.report({ node, messageId: 'errorMessageClass', diff --git a/packages/eslint-plugin/src/rules/no-restricted-imports.ts b/packages/eslint-plugin/src/rules/no-restricted-imports.ts index abd0a891b05e..f61baa7ed396 100644 --- a/packages/eslint-plugin/src/rules/no-restricted-imports.ts +++ b/packages/eslint-plugin/src/rules/no-restricted-imports.ts @@ -314,7 +314,7 @@ export default createRule({ if ( node.moduleReference.type === AST_NODE_TYPES.TSExternalModuleReference ) { - const synthesizedImport = { + const synthesizedImport: TSESTree.ImportDeclaration = { ...node, type: AST_NODE_TYPES.ImportDeclaration, source: node.moduleReference.expression, @@ -325,9 +325,11 @@ export default createRule({ ...node.id, type: AST_NODE_TYPES.ImportDefaultSpecifier, local: node.id, + // @ts-expect-error -- parent types are incompatible but it's fine for the purposes of this extension + parent: node.id.parent, }, ], - } satisfies TSESTree.ImportDeclaration; + }; return checkImportNode(synthesizedImport); } }, diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-qualifier.ts b/packages/eslint-plugin/src/rules/no-unnecessary-qualifier.ts index 329b26d87aee..5b90c5674367 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-qualifier.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-qualifier.ts @@ -160,7 +160,7 @@ export default createRule({ 'TSModuleDeclaration > TSModuleBlock'( node: TSESTree.TSModuleBlock, ): void { - enterDeclaration(node.parent as TSESTree.TSModuleDeclaration); + enterDeclaration(node.parent); }, TSEnumDeclaration: enterDeclaration, 'ExportNamedDeclaration[declaration.type="TSModuleDeclaration"]': diff --git a/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts b/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts index 0d165ae7eb5c..c9625dd5f62a 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts @@ -171,14 +171,7 @@ export default createRule({ const { parent } = node; - /** - * @see https://github.com/typescript-eslint/typescript-eslint/issues/6225 - */ - const switchStatement = parent as TSESTree.SwitchStatement; - - const leftType = parserServices.getTypeAtLocation( - switchStatement.discriminant, - ); + const leftType = parserServices.getTypeAtLocation(parent.discriminant); const rightType = parserServices.getTypeAtLocation(node.test); if (isMismatchedComparison(leftType, rightType)) { diff --git a/packages/eslint-plugin/src/rules/no-useless-constructor.ts b/packages/eslint-plugin/src/rules/no-useless-constructor.ts index dcfc7dd976de..cd91a71c2002 100644 --- a/packages/eslint-plugin/src/rules/no-useless-constructor.ts +++ b/packages/eslint-plugin/src/rules/no-useless-constructor.ts @@ -22,14 +22,7 @@ function checkAccessibility(node: TSESTree.MethodDefinition): boolean { case 'private': return false; case 'public': - if ( - node.parent.type === AST_NODE_TYPES.ClassBody && - ( - node.parent.parent as - | TSESTree.ClassDeclaration - | TSESTree.ClassExpression - ).superClass - ) { + if (node.parent.parent.superClass) { return false; } break; diff --git a/packages/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index 464ff4dc620c..0ac4dbbbf6d9 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -16,10 +16,189 @@ declare module './generated/ast-spec' { interface AccessorPropertyComputedName { parent: TSESTree.ClassBody; } - interface AccessorPropertyNonComputedName { parent: TSESTree.ClassBody; } + interface TSAbstractAccessorPropertyComputedName { + parent: TSESTree.ClassBody; + } + interface TSAbstractAccessorPropertyNonComputedName { + parent: TSESTree.ClassBody; + } + + interface CatchClause { + parent: TSESTree.TryStatement; + } + + interface ClassBody { + parent: TSESTree.ClassExpression | TSESTree.ClassDeclaration; + } + + interface ExportSpecifier { + parent: TSESTree.ExportNamedDeclaration; + } + + interface ImportAttribute { + parent: TSESTree.ImportDeclaration | TSESTree.ImportExpression; + } + + interface ImportDefaultSpecifier { + parent: TSESTree.ImportDeclaration; + } + + interface ImportNamespaceSpecifier { + parent: TSESTree.ImportDeclaration; + } + + interface ImportSpecifier { + parent: + | TSESTree.ExportAllDeclaration + | TSESTree.ExportNamedDeclaration + | TSESTree.ImportDeclaration; + } + + interface JSXAttribute { + parent: TSESTree.JSXOpeningElement; + } + + interface JSXClosingElement { + parent: TSESTree.JSXElement; + } + + interface JSXClosingFragment { + parent: TSESTree.JSXFragment; + } + + interface JSXOpeningElement { + parent: TSESTree.JSXElement; + } + + interface JSXOpeningFragment { + parent: TSESTree.JSXFragment; + } + + interface JSXSpreadAttribute { + parent: TSESTree.JSXOpeningElement; + } + + interface MethodDefinitionComputedName { + parent: TSESTree.ClassBody; + } + interface MethodDefinitionNonComputedName { + parent: TSESTree.ClassBody; + } + interface TSAbstractMethodDefinitionComputedName { + parent: TSESTree.ClassBody; + } + interface TSAbstractMethodDefinitionNonComputedName { + parent: TSESTree.ClassBody; + } + + interface PropertyComputedName { + parent: TSESTree.ObjectExpression | TSESTree.ObjectPattern; + } + interface PropertyNonComputedName { + parent: TSESTree.ObjectExpression | TSESTree.ObjectPattern; + } + + interface PropertyDefinitionComputedName { + parent: TSESTree.ClassBody; + } + interface PropertyDefinitionNonComputedName { + parent: TSESTree.ClassBody; + } + interface TSAbstractPropertyDefinitionComputedName { + parent: TSESTree.ClassBody; + } + interface TSAbstractPropertyDefinitionNonComputedName { + parent: TSESTree.ClassBody; + } + + interface SpreadElement { + parent: + | TSESTree.ArrayExpression + | TSESTree.CallExpression + | TSESTree.ObjectExpression; + } + + interface StaticBlock { + parent: TSESTree.ClassBody; + } + + interface SwitchCase { + parent: TSESTree.SwitchStatement; + } + + interface TemplateElement { + parent: TSESTree.TemplateLiteral | TSESTree.TSTemplateLiteralType; + } + + interface TSCallSignatureDeclaration { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + + interface TSConstructSignatureDeclaration { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + + interface TSClassImplements { + parent: TSESTree.ClassDeclaration | TSESTree.ClassExpression; + } + + interface TSEnumBody { + parent: TSESTree.TSEnumDeclaration; + } + + interface TSEnumMemberComputedName { + parent: TSESTree.TSEnumBody; + } + interface TSEnumMemberNonComputedName { + parent: TSESTree.TSEnumBody; + } + + interface TSIndexSignature { + parent: + | TSESTree.ClassBody + | TSESTree.TSInterfaceBody + | TSESTree.TSTypeLiteral; + } + + interface TSInterfaceBody { + parent: TSESTree.TSInterfaceDeclaration; + } + + interface TSInterfaceHeritage { + parent: TSESTree.TSInterfaceBody; + } + + interface TSMethodSignatureComputedName { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + interface TSMethodSignatureNonComputedName { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + + interface TSModuleBlock { + parent: TSESTree.TSModuleDeclaration; + } + + interface TSParameterProperty { + parent: TSESTree.FunctionLike; + } + + interface TSPropertySignatureComputedName { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + interface TSPropertySignatureNonComputedName { + parent: TSESTree.TSInterfaceBody | TSESTree.TSTypeLiteral; + } + + interface TSTypeParameter { + parent: + | TSESTree.TSInferType + | TSESTree.TSTypeParameterDeclaration + | TSESTree.TSMappedType; + } } export * as TSESTree from './generated/ast-spec';