Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(eslint-plugin): add
consistent-indexed-object-style
rule (#2401)
- Loading branch information
1 parent
229631e
commit d7dc108
Showing
6 changed files
with
393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/eslint-plugin/docs/rules/consistent-indexed-object-style.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Enforce or disallow the use of the record type (`consistent-indexed-object-style`) | ||
|
||
TypeScript supports defining object show keys can be flexible using an index signature. TypeScript also has a builtin type named `Record` to create an empty object defining only an index signature. For example, the following types are equal: | ||
|
||
```ts | ||
interface Foo { | ||
[key: string]: unknown; | ||
} | ||
|
||
type Foo = { | ||
[key: string]: unknown; | ||
}; | ||
|
||
type Foo = Record<string, unknown>; | ||
``` | ||
|
||
## Options | ||
|
||
- `"record"`: Set to `"record"` to only allow the `Record` type. Set to `"index-signature"` to only allow index signatures. (Defaults to `"record"`) | ||
|
||
For example: | ||
|
||
```CJSON | ||
{ | ||
"@typescript-eslint/consistent-type-definitions": ["error", "index-signature"] | ||
} | ||
``` | ||
|
||
## Rule details | ||
|
||
This rule enforces a consistent way to define records. | ||
|
||
Examples of **incorrect** code with `record` option. | ||
|
||
```ts | ||
interface Foo { | ||
[key: string]: unknown; | ||
} | ||
|
||
type Foo = { | ||
[key: string]: unknown; | ||
}; | ||
``` | ||
|
||
Examples of **correct** code with `record` option. | ||
|
||
```ts | ||
type Foo = Record<string, unknown>; | ||
``` | ||
|
||
Examples of **incorrect** code with `index-signature` option. | ||
|
||
```ts | ||
type Foo = Record<string, unknown>; | ||
``` | ||
|
||
Examples of **correct** code with `index-signature` option. | ||
|
||
```ts | ||
interface Foo { | ||
[key: string]: unknown; | ||
} | ||
|
||
type Foo = { | ||
[key: string]: unknown; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { createRule } from '../util'; | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
|
||
type MessageIds = 'preferRecord' | 'preferIndexSignature'; | ||
type Options = ['record' | 'index-signature']; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'consistent-indexed-object-style', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce or disallow the use of the record type', | ||
category: 'Stylistic Issues', | ||
// too opinionated to be recommended | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferRecord: 'A record is preferred over an index signature', | ||
preferIndexSignature: 'An index signature is preferred over a record.', | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: ['record', 'index-signature'], | ||
}, | ||
], | ||
}, | ||
defaultOptions: ['record'], | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
if (context.options[0] === 'index-signature') { | ||
return { | ||
TSTypeReference(node): void { | ||
const typeName = node.typeName; | ||
if (typeName.type !== AST_NODE_TYPES.Identifier) { | ||
return; | ||
} | ||
if (typeName.name !== 'Record') { | ||
return; | ||
} | ||
|
||
const params = node.typeParameters?.params; | ||
if (params?.length !== 2) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'preferIndexSignature', | ||
fix(fixer) { | ||
const key = sourceCode.getText(params[0]); | ||
const type = sourceCode.getText(params[1]); | ||
return fixer.replaceText(node, `{ [key: ${key}]: ${type} }`); | ||
}, | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
function checkMembers( | ||
members: TSESTree.TypeElement[], | ||
node: TSESTree.Node, | ||
prefix: string, | ||
postfix: string, | ||
): void { | ||
if (members.length !== 1) { | ||
return; | ||
} | ||
const [member] = members; | ||
|
||
if (member.type !== AST_NODE_TYPES.TSIndexSignature) { | ||
return; | ||
} | ||
|
||
const [parameter] = member.parameters; | ||
|
||
if (!parameter) { | ||
return; | ||
} | ||
|
||
if (parameter.type !== AST_NODE_TYPES.Identifier) { | ||
return; | ||
} | ||
const keyType = parameter.typeAnnotation; | ||
if (!keyType) { | ||
return; | ||
} | ||
|
||
const valueType = member.typeAnnotation; | ||
if (!valueType) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'preferRecord', | ||
fix(fixer) { | ||
const key = sourceCode.getText(keyType.typeAnnotation); | ||
const value = sourceCode.getText(valueType.typeAnnotation); | ||
return fixer.replaceText( | ||
node, | ||
`${prefix}Record<${key}, ${value}>${postfix}`, | ||
); | ||
}, | ||
}); | ||
} | ||
|
||
return { | ||
TSTypeLiteral(node): void { | ||
checkMembers(node.members, node, '', ''); | ||
}, | ||
|
||
TSInterfaceDeclaration(node): void { | ||
checkMembers(node.body.body, node, `type ${node.id.name} = `, ';'); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.