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 2 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
27 changes: 27 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 Down Expand Up @@ -59,6 +60,32 @@ export default util.createRule<Options, MessageIds>({
context.report({
node: node.id,
messageId: 'noEmptyWithSuper',
fix(fixer) {
if (node.extends && node.extends.length) {
const {
id,
extends: [extend],
body,
} = node;

return [
// replace `interface` keyword to `type`
fixer.replaceTextRange(
[id.parent?.range[0] || 0, id.range[0]],
'type ',
),
// replace `extends` keyword to `=` symbol
fixer.replaceTextRange(
[id.range[1], extend.range[0]],
' = ',
),
// remove brackets `{}`
fixer.removeRange([body.range[0] - 1, body.range[1]]),
];
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
},
});
}
}
Expand Down
62 changes: 62 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as parser from '@typescript-eslint/parser';
import rule from '../../src/rules/no-empty-interface';
import { RuleTester } from '../RuleTester';

Expand Down Expand Up @@ -75,3 +77,63 @@ interface Bar extends Foo {}
},
],
});

describe('no-empty-interface | "noEmptyWithSuper" fixer', () => {
const linter = new TSESLint.Linter();
linter.defineRule('no-empty-interface', rule);
linter.defineParser('@typescript-eslint/parser', parser);

function testOutput(
code: string,
output: string,
allowSingleExtends = false,
) {
it(code, () => {
const result = linter.verifyAndFix(
code,
{
rules: { 'no-empty-interface': [2, { allowSingleExtends }] },
parser: '@typescript-eslint/parser',
},
{ fix: true },
);

expect(result.messages).toHaveLength(0);
expect(result.output).toBe(output);
});
}

testOutput(
'interface Foo extends Array<number> {}',
'type Foo = Array<number>',
);

testOutput(
'interface Foo extends Array<number | {}> { }',
'type Foo = Array<number | {}>',
);

testOutput(
`
interface Bar {
bar: string;
}
interface Foo extends Array<Bar> {}
`,
`
interface Bar {
bar: string;
}
type Foo = Array<Bar>
`,
);

testOutput(
`
type R = Record<string, unknown>;
interface Foo extends R { };`,
`
type R = Record<string, unknown>;
type Foo = R;`,
);
});
bradzacher marked this conversation as resolved.
Show resolved Hide resolved