Skip to content

Commit 7a8a0a3

Browse files
authoredOct 5, 2022
fix(eslint-plugin): [consistent-indexed-object-style] handle interface generic (#5746)
* fix(eslint-plugin): [consistent-indexed-object-style] handle interface generic * fix lint error
1 parent 93bf147 commit 7a8a0a3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed
 

‎packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
2-
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
2+
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
33

44
import { createRule } from '../util';
55

@@ -67,7 +67,7 @@ export default createRule<Options, MessageIds>({
6767

6868
if (parentId) {
6969
const scope = context.getScope();
70-
const superVar = scope.set.get(parentId.name);
70+
const superVar = ASTUtils.findVariable(scope, parentId.name);
7171
if (superVar) {
7272
const isCircular = superVar.references.some(
7373
item =>

‎packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ interface Foo {
3939
[key: string]: Foo;
4040
}
4141
`,
42-
42+
`
43+
interface Foo<T> {
44+
[key: string]: Foo<T>;
45+
}
46+
`,
47+
`
48+
interface Foo<T> {
49+
[key: string]: Foo<T> | string;
50+
}
51+
`,
4352
// Type literal
4453
'type Foo = {};',
4554
`
@@ -328,6 +337,17 @@ type Foo<A, B> = Readonly<Record<A, B>>;
328337
},
329338
{
330339
code: `
340+
interface Foo<T> {
341+
[k: string]: T;
342+
}
343+
`,
344+
output: `
345+
type Foo<T> = Record<string, T>;
346+
`,
347+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
348+
},
349+
{
350+
code: `
331351
interface Foo {
332352
[k: string]: A.Foo;
333353
}

0 commit comments

Comments
 (0)
Please sign in to comment.