Skip to content

feat: stricter parent types for the AST #9560

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

Merged
merged 3 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/ast-spec/src/element/TSEnumMember/spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,7 +13,6 @@ interface TSEnumMemberBase extends BaseNode {
| PropertyNameNonComputed;
initializer: Expression | undefined;
computed: boolean;
parent: TSEnumBody;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/ast-spec/src/special/TSEnumBody/spec.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 9 additions & 11 deletions packages/eslint-plugin/src/rules/class-literal-property-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,15 @@ export default createRule<Options, MessageIds>({

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({
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/class-methods-use-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ export default createRule<Options, MessageIds>({
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,
};
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/method-signature-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ export default createRule<Options, MessageIds>({
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(
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-extraneous-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export default createRule<Options, MessageIds>({

return {
ClassBody(node): void {
const parent = node.parent as
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression;
const parent = node.parent;

if (parent.superClass || isAllowWithDecorator(parent)) {
return;
Expand Down
9 changes: 1 addition & 8 deletions packages/eslint-plugin/src/rules/no-misused-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export default createRule<Options, MessageIds>({
if (
node.moduleReference.type === AST_NODE_TYPES.TSExternalModuleReference
) {
const synthesizedImport = {
const synthesizedImport: TSESTree.ImportDeclaration = {
...node,
type: AST_NODE_TYPES.ImportDeclaration,
source: node.moduleReference.expression,
Expand All @@ -325,9 +325,11 @@ export default createRule<Options, MessageIds>({
...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);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
9 changes: 1 addition & 8 deletions packages/eslint-plugin/src/rules/no-useless-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
181 changes: 180 additions & 1 deletion packages/types/src/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Loading