This repository has been archived by the owner on Jan 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add rule `interface-over-type-literal` * rename rule as `prefer-interface` * update roadmap
- Loading branch information
Showing
5 changed files
with
195 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Prefer an interface declaration over a type literal (type T = { ... }) (prefer-interface) | ||
|
||
Interfaces are generally preferred over type literals because interfaces can be implemented, extended and merged. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule. | ||
|
||
```ts | ||
type T = { x: number; } | ||
``` | ||
Examples of **correct** code for this rule. | ||
```ts | ||
type T = string; | ||
type Foo = string | { } | ||
|
||
interface T { | ||
x: number; | ||
} | ||
``` | ||
|
||
## Options | ||
```CJSON | ||
{ | ||
"interface-over-type-literal": "error" | ||
} | ||
``` | ||
|
||
## 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @fileoverview Prefer an interface declaration over a type literal (type T = { ... }) | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
const util = require("../util"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
"Prefer an interface declaration over a type literal (type T = { ... })", | ||
extraDescription: [util.tslintRule("interface-over-type-literal")], | ||
category: "TypeScript", | ||
url: util.metaDocsUrl("prefer-interface"), | ||
}, | ||
fixable: "code", | ||
messages: { | ||
interfaceOverType: "Use an interface instead of a type literal.", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
// VariableDeclaration with kind type has only one VariableDeclarator | ||
"VariableDeclaration[kind='type'] > VariableDeclarator[init.type='TSTypeLiteral']"( | ||
node | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "interfaceOverType", | ||
fix(fixer) { | ||
const typeNode = node.typeParameters || node.id; | ||
|
||
const fixes = [ | ||
fixer.replaceText( | ||
sourceCode.getFirstToken(node.parent), | ||
"interface" | ||
), | ||
fixer.replaceTextRange( | ||
[typeNode.range[1], node.init.range[0]], | ||
" " | ||
), | ||
]; | ||
|
||
const afterToken = sourceCode.getTokenAfter(node.init); | ||
|
||
if ( | ||
afterToken && | ||
afterToken.type === "Punctuator" && | ||
afterToken.value === ";" | ||
) { | ||
fixes.push(fixer.remove(afterToken)); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* @fileoverview Prefer an interface declaration over a type literal (type T = { ... }) | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/prefer-interface"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "typescript-eslint-parser", | ||
}); | ||
|
||
ruleTester.run("interface-over-type-literal", rule, { | ||
valid: [ | ||
`var foo = { };`, | ||
`type U = string;`, | ||
`type V = { x: number; } | { y: string; };`, | ||
` | ||
type Record<T, U> = { | ||
[K in T]: U; | ||
} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `type T = { x: number; }`, | ||
output: `interface T { x: number; }`, | ||
errors: [ | ||
{ | ||
messageId: "interfaceOverType", | ||
line: 1, | ||
column: 6, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `type T={ x: number; }`, | ||
output: `interface T { x: number; }`, | ||
errors: [ | ||
{ | ||
messageId: "interfaceOverType", | ||
line: 1, | ||
column: 6, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `type T= { x: number; }`, | ||
output: `interface T { x: number; }`, | ||
errors: [ | ||
{ | ||
messageId: "interfaceOverType", | ||
line: 1, | ||
column: 6, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
export type W<T> = { | ||
x: T, | ||
}; | ||
`, | ||
output: ` | ||
export interface W<T> { | ||
x: T, | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "interfaceOverType", | ||
line: 2, | ||
column: 13, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |