From 7a8a0a3c500ca726d2ab3bee0ae9f3fb9d8d39b8 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 5 Oct 2022 20:37:49 +0900 Subject: [PATCH] fix(eslint-plugin): [consistent-indexed-object-style] handle interface generic (#5746) * fix(eslint-plugin): [consistent-indexed-object-style] handle interface generic * fix lint error --- .../rules/consistent-indexed-object-style.ts | 4 ++-- .../consistent-indexed-object-style.test.ts | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts index 2b260865a4c4..535692f3fa86 100644 --- a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts +++ b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts @@ -1,5 +1,5 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; -import { AST_NODE_TYPES } from '@typescript-eslint/utils'; +import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils'; import { createRule } from '../util'; @@ -67,7 +67,7 @@ export default createRule({ if (parentId) { const scope = context.getScope(); - const superVar = scope.set.get(parentId.name); + const superVar = ASTUtils.findVariable(scope, parentId.name); if (superVar) { const isCircular = superVar.references.some( item => diff --git a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts index 5c59f92b92de..6bdc76362e0a 100644 --- a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts @@ -39,7 +39,16 @@ interface Foo { [key: string]: Foo; } `, - + ` +interface Foo { + [key: string]: Foo; +} + `, + ` +interface Foo { + [key: string]: Foo | string; +} + `, // Type literal 'type Foo = {};', ` @@ -328,6 +337,17 @@ type Foo = Readonly>; }, { code: ` +interface Foo { + [k: string]: T; +} + `, + output: ` +type Foo = Record; + `, + errors: [{ messageId: 'preferRecord', line: 2, column: 1 }], + }, + { + code: ` interface Foo { [k: string]: A.Foo; }