-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add
consistent-type-definitions
rule (#463)
Deprecates `prefer-interface`
- Loading branch information
1 parent
747bfcb
commit ec87d06
Showing
12 changed files
with
407 additions
and
11 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
74 changes: 74 additions & 0 deletions
74
packages/eslint-plugin/docs/rules/consistent-type-definitions.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,74 @@ | ||
# Consistent with type definition either `interface` or `type` (consistent-type-definitions) | ||
|
||
There are two ways to define a type. | ||
|
||
```ts | ||
// type alias | ||
type T1 = { | ||
a: string; | ||
b: number; | ||
}; | ||
|
||
// interface keyword | ||
interface T2 { | ||
a: string; | ||
b: number; | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
This rule accepts one string option: | ||
|
||
- `"interface"`: enforce using `interface`s for object type definitions. | ||
- `"type"`: enforce using `type`s for object type definitions. | ||
|
||
For example: | ||
|
||
```CJSON | ||
{ | ||
// Use type for object definitions | ||
"@typescript-eslint/consistent-type-definitions": ["error", "type"] | ||
} | ||
``` | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code with `interface` option. | ||
|
||
```ts | ||
type T = { x: number }; | ||
``` | ||
|
||
Examples of **correct** code with `interface` option. | ||
|
||
```ts | ||
type T = string; | ||
type Foo = string | {}; | ||
|
||
interface T { | ||
x: number; | ||
} | ||
``` | ||
|
||
Examples of **incorrect** code with `type` option. | ||
|
||
```ts | ||
interface T { | ||
x: number; | ||
} | ||
``` | ||
|
||
Examples of **correct** code with `type` option. | ||
|
||
```ts | ||
type T = { x: number }; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you specifically want to use an interface or type literal for stylistic reasons, you can disable this rule. | ||
|
||
## Compatibility | ||
|
||
- TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) |
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
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
103 changes: 103 additions & 0 deletions
103
packages/eslint-plugin/src/rules/consistent-type-definitions.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,103 @@ | ||
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'consistent-type-definitions', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Consistent with type definition either `interface` or `type`', | ||
category: 'Stylistic Issues', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
interfaceOverType: 'Use an `interface` instead of a `type`', | ||
typeOverInterface: 'Use a `type` instead of an `interface`', | ||
}, | ||
schema: [ | ||
{ | ||
enum: ['interface', 'type'], | ||
}, | ||
], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: ['interface'], | ||
create(context, [option]) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
// VariableDeclaration with kind type has only one VariableDeclarator | ||
"TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"( | ||
node: TSESTree.TSTypeAliasDeclaration, | ||
) { | ||
if (option === 'interface') { | ||
context.report({ | ||
node: node.id, | ||
messageId: 'interfaceOverType', | ||
fix(fixer) { | ||
const typeNode = node.typeParameters || node.id; | ||
const fixes: TSESLint.RuleFix[] = []; | ||
|
||
const firstToken = sourceCode.getFirstToken(node); | ||
if (firstToken) { | ||
fixes.push(fixer.replaceText(firstToken, 'interface')); | ||
fixes.push( | ||
fixer.replaceTextRange( | ||
[typeNode.range[1], node.typeAnnotation.range[0]], | ||
' ', | ||
), | ||
); | ||
} | ||
|
||
const afterToken = sourceCode.getTokenAfter(node.typeAnnotation); | ||
if ( | ||
afterToken && | ||
afterToken.type === 'Punctuator' && | ||
afterToken.value === ';' | ||
) { | ||
fixes.push(fixer.remove(afterToken)); | ||
} | ||
|
||
return fixes; | ||
}, | ||
}); | ||
} | ||
}, | ||
TSInterfaceDeclaration(node) { | ||
if (option === 'type') { | ||
context.report({ | ||
node: node.id, | ||
messageId: 'typeOverInterface', | ||
fix(fixer) { | ||
const typeNode = node.typeParameters || node.id; | ||
const fixes: TSESLint.RuleFix[] = []; | ||
|
||
const firstToken = sourceCode.getFirstToken(node); | ||
if (firstToken) { | ||
fixes.push(fixer.replaceText(firstToken, 'type')); | ||
fixes.push( | ||
fixer.replaceTextRange( | ||
[typeNode.range[1], node.body.range[0]], | ||
' = ', | ||
), | ||
); | ||
} | ||
|
||
if (node.extends) { | ||
node.extends.forEach(heritage => { | ||
const typeIdentifier = sourceCode.getText(heritage); | ||
fixes.push( | ||
fixer.insertTextAfter(node.body, ` & ${typeIdentifier}`), | ||
); | ||
}); | ||
} | ||
|
||
return fixes; | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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
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.