Skip to content

Commit

Permalink
feat(eslint-plugin): [no-empty-interface] noEmptyWithSuper fixer (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimabory authored and bradzacher committed Nov 25, 2019
1 parent f5c0e02 commit b91b0ba
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-dynamic-delete`](./docs/rules/no-dynamic-delete.md) | Bans usage of the delete operator with computed key expressions | | :wrench: | |
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | | |
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-extra-non-null-assertion`](./docs/rules/no-extra-non-null-assertion.md) | Disallow extra non-null assertion | | | |
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |
Expand Down
17 changes: 17 additions & 0 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default util.createRule<Options, MessageIds>({
category: 'Best Practices',
recommended: 'error',
},
fixable: 'code',
messages: {
noEmpty: 'An empty interface is equivalent to `{}`.',
noEmptyWithSuper:
Expand All @@ -41,6 +42,8 @@ export default util.createRule<Options, MessageIds>({
create(context, [{ allowSingleExtends }]) {
return {
TSInterfaceDeclaration(node): void {
const sourceCode = context.getSourceCode();

if (node.body.body.length !== 0) {
// interface contains members --> Nothing to report
return;
Expand All @@ -59,6 +62,20 @@ export default util.createRule<Options, MessageIds>({
context.report({
node: node.id,
messageId: 'noEmptyWithSuper',
fix(fixer) {
if (node.extends && node.extends.length) {
return [
fixer.replaceText(
node,
`type ${sourceCode.getText(
node.id,
)} = ${sourceCode.getText(node.extends[0])}`,
),
];
}

return null;
},
});
}
}
Expand Down
58 changes: 58 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,63 @@ interface Bar extends Foo {}
},
],
},
{
code: 'interface Foo extends Array<number> {}',
output: 'type Foo = Array<number>',
errors: [
{
messageId: 'noEmptyWithSuper',
line: 1,
column: 11,
},
],
},
{
code: 'interface Foo extends Array<number | {}> { }',
output: 'type Foo = Array<number | {}>',
errors: [
{
messageId: 'noEmptyWithSuper',
line: 1,
column: 11,
},
],
},
{
code: `
interface Bar {
bar: string;
}
interface Foo extends Array<Bar> {}
`,
output: `
interface Bar {
bar: string;
}
type Foo = Array<Bar>
`,
errors: [
{
messageId: 'noEmptyWithSuper',
line: 5,
column: 11,
},
],
},
{
code: `
type R = Record<string, unknown>;
interface Foo extends R { };`,
output: `
type R = Record<string, unknown>;
type Foo = R;`,
errors: [
{
messageId: 'noEmptyWithSuper',
line: 3,
column: 11,
},
],
},
],
});

0 comments on commit b91b0ba

Please sign in to comment.