Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [no-empty-interface] noEmptyWithSuper fixer #1247

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
],
},
],
});