Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import type { ReportFixFunction } from '@typescript-eslint/utils/ts-eslint';

import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';

import { createRule, isParenthesized, nullThrows } from '../util';
import {
createRule,
getFixOrSuggest,
isParenthesized,
nullThrows,
} from '../util';

type MessageIds = 'preferIndexSignature' | 'preferRecord';
type MessageIds =
| 'preferIndexSignature'
| 'preferIndexSignatureSuggestion'
| 'preferRecord';
type Options = ['index-signature' | 'record'];

export default createRule<Options, MessageIds>({
Expand All @@ -17,8 +25,12 @@ export default createRule<Options, MessageIds>({
recommended: 'stylistic',
},
fixable: 'code',
// eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- suggestions are exposed through a helper.
hasSuggestions: true,
messages: {
preferIndexSignature: 'An index signature is preferred over a record.',
preferIndexSignatureSuggestion:
'Change into an index signature instead of a record.',
preferRecord: 'A record is preferred over an index signature.',
},
schema: [
Expand Down Expand Up @@ -113,14 +125,27 @@ export default createRule<Options, MessageIds>({
return;
}

const indexParam = params[0];

const shouldFix =
indexParam.type === AST_NODE_TYPES.TSStringKeyword ||
indexParam.type === AST_NODE_TYPES.TSNumberKeyword ||
indexParam.type === AST_NODE_TYPES.TSSymbolKeyword;

context.report({
node,
messageId: 'preferIndexSignature',
fix(fixer) {
const key = context.sourceCode.getText(params[0]);
const type = context.sourceCode.getText(params[1]);
return fixer.replaceText(node, `{ [key: ${key}]: ${type} }`);
},
...getFixOrSuggest({
fixOrSuggest: shouldFix ? 'fix' : 'suggest',
suggestion: {
messageId: 'preferIndexSignatureSuggestion',
fix: fixer => {
const key = context.sourceCode.getText(params[0]);
const type = context.sourceCode.getText(params[1]);
return fixer.replaceText(node, `{ [key: ${key}]: ${type} }`);
},
},
}),
});
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,57 @@ interface Foo {
output: 'type Foo = Generic<{ [key: string]: any }>;',
},

// Record with an index node that may potentially break index-signature style
{
code: 'type Foo = Record<string | number, any>;',
errors: [
{
column: 12,
line: 1,
messageId: 'preferIndexSignature',
suggestions: [
{
messageId: 'preferIndexSignatureSuggestion',
output: 'type Foo = { [key: string | number]: any };',
},
],
},
],
options: ['index-signature'],
},
{
code: "type Foo = Record<Exclude<'a' | 'b' | 'c', 'a'>, any>;",
errors: [
{
column: 12,
line: 1,
messageId: 'preferIndexSignature',
suggestions: [
{
messageId: 'preferIndexSignatureSuggestion',
output:
"type Foo = { [key: Exclude<'a' | 'b' | 'c', 'a'>]: any };",
},
],
},
],
options: ['index-signature'],
},

// Record with valid index node should use an auto-fix
{
code: 'type Foo = Record<number, any>;',
errors: [{ column: 12, line: 1, messageId: 'preferIndexSignature' }],
options: ['index-signature'],
output: 'type Foo = { [key: number]: any };',
},
{
code: 'type Foo = Record<symbol, any>;',
errors: [{ column: 12, line: 1, messageId: 'preferIndexSignature' }],
options: ['index-signature'],
output: 'type Foo = { [key: symbol]: any };',
},

// Function types
{
code: 'function foo(arg: Record<string, any>) {}',
Expand Down
Loading