Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 3 commits intov8fromv8-6225-stricter-parents
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/ast-spec/src/element/TSEnumMember/spec.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/ast-spec/src/special/TSEnumBody/spec.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/no-extraneous-class.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/no-misused-new.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/no-restricted-imports.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionpackages/types/src/ts-estree.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp