Skip to content

Commit

Permalink
fix: add changes requested in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 27, 2021
1 parent 8706fc5 commit 200b97d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 13 additions & 11 deletions packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts
Expand Up @@ -2,6 +2,7 @@ import { createRule } from '../util';
import {
AST_NODE_TYPES,
TSESTree,
TSESLint,
} from '@typescript-eslint/experimental-utils';

type MessageIds = 'preferRecord' | 'preferIndexSignature';
Expand Down Expand Up @@ -66,6 +67,7 @@ export default createRule<Options, MessageIds>({
node: TSESTree.Node,
prefix: string,
postfix: string,
safeFix = true,
): void {
if (members.length !== 1) {
return;
Expand Down Expand Up @@ -98,14 +100,16 @@ export default createRule<Options, MessageIds>({
context.report({
node,
messageId: 'preferRecord',
fix(fixer) {
const key = sourceCode.getText(keyType.typeAnnotation);
const value = sourceCode.getText(valueType.typeAnnotation);
const record = member.readonly
? `Readonly<Record<${key}, ${value}>>`
: `Record<${key}, ${value}>`;
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
},
fix: safeFix
? (fixer): TSESLint.RuleFix => {
const key = sourceCode.getText(keyType.typeAnnotation);
const value = sourceCode.getText(valueType.typeAnnotation);
const record = member.readonly
? `Readonly<Record<${key}, ${value}>>`
: `Record<${key}, ${value}>`;
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
}
: null,
});
}

Expand All @@ -115,9 +119,6 @@ export default createRule<Options, MessageIds>({
},

TSInterfaceDeclaration(node): void {
if (node.extends?.length) {
return;
}
let genericTypes = '';

if ((node.typeParameters?.params ?? []).length > 0) {
Expand All @@ -131,6 +132,7 @@ export default createRule<Options, MessageIds>({
node,
`type ${node.id.name}${genericTypes} = `,
';',
!node.extends?.length,
);
},
};
Expand Down
Expand Up @@ -74,11 +74,6 @@ interface Foo {
`
interface Foo {
[];
}
`,
`
interface B extends A {
[index: number]: unknown;
}
`,
// 'index-signature'
Expand Down Expand Up @@ -170,6 +165,20 @@ type Foo<A> = Record<string, A>;
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Interface with extends
{
code: `
interface B extends A {
[index: number]: unknown;
}
`,
output: `
interface B extends A {
[index: number]: unknown;
}
`,
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},
// Readonly interface with generic parameter
{
code: `
Expand Down

0 comments on commit 200b97d

Please sign in to comment.