8000 feat(eslint-plugin): [no-empty-interface] noEmptyWithSuper fixer (#1247) · lipis/typescript-eslint@b91b0ba · GitHub
[go: up one dir, main page]

Skip to content

Commit b91b0ba

Browse files
dimaborybradzacher
authored andcommitted
feat(eslint-plugin): [no-empty-interface] noEmptyWithSuper fixer (typescript-eslint#1247)
1 parent f5c0e02 commit b91b0ba

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
162162
| [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
163163
| [`@typescript-eslint/no-dynamic-delete`](./docs/rules/no-dynamic-delete.md) | Bans usage of the delete operator with computed key expressions | | :wrench: | |
164164
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
165-
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | | |
165+
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | :wrench: |
166166
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type | :heavy_check_mark: | :wrench: | |
167167
| [`@typescript-eslint/no-extra-non-null-assertion`](./docs/rules/no-extra-non-null-assertion.md) | Disallow extra non-null assertion | | | |
168168
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |

packages/eslint-plugin/src/rules/no-empty-interface.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default util.createRule<Options, MessageIds>({
1616
category: 'Best Practices',
1717
recommended: 'error',
1818
},
19+
fixable: 'code',
1920
messages: {
2021
noEmpty: 'An empty interface is equivalent to `{}`.',
2122
noEmptyWithSuper:
@@ -41,6 +42,8 @@ export default util.createRule<Options, MessageIds>({
4142
create(context, [{ allowSingleExtends }]) {
4243
return {
4344
TSInterfaceDeclaration(node): void {
45+
const sourceCode = context.getSourceCode();
46+
4447
if (node.body.body.length !== 0) {
4548
// interface contains members --> Nothing to report
4649
return;
@@ -59,6 +62,20 @@ export default util.createRule<Options, MessageIds>({
5962
context.report({
6063
node: node.id,
6164
messageId: 'noEmptyWithSuper',
65+
fix(fixer) {
66+
if (node.extends && node.extends.length) {
67+
return [
68+
fixer.replaceText(
69+
node,
70+
`type ${sourceCode.getText(
71+
node.id,
72+
)} = ${sourceCode.getText(node.extends[0])}`,
73+
),
74+
];
75+
}
76+
77+
return null;
78+
},
6279
});
6380
}
6481
}

packages/eslint-plugin/tests/rules/no-empty-interface.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,63 @@ interface Bar extends Foo {}
7373
},
7474
],
7575
},
76+
{
77+
code: 'interface Foo extends Array<number> {}',
78+
output: 'type Foo = Array<number>',
79+
errors: [
80+
{
81+
messageId: 'noEmptyWithSuper',
82+
line: 1,
83+
column: 11,
84+
},
85+
],
86+
},
87+
{
88+
code: 'interface Foo extends Array<number | {}> { }',
89+
output: 'type Foo = Array<number | {}>',
90+
errors: [
91+
{
92+
messageId: 'noEmptyWithSuper',
93+
line: 1,
94+
column: 11,
95+
},
96+
],
97+
},
98+
{
99+
code: `
100+
interface Bar {
101+
bar: string;
102+
}
103+
interface Foo extends Array<Bar> {}
104+
`,
105+
output: `
106+
interface Bar {
107+
bar: string;
108+
}
109+
type Foo = Array<Bar>
110+
`,
111+
errors: [
112+
{
113+
messageId: 'noEmptyWithSuper',
114+
line: 5,
115+
column: 11,
116+
},
117+
],
118+
},
119+
{
120+
code: `
121+
type R = Record<string, unknown>;
122+
interface Foo extends R { };`,
123+
output: `
124+
type R = Record<string, unknown>;
125+
type Foo = R;`,
126+
errors: [
127+
{
128+
messageId: 'noEmptyWithSuper',
129+
line: 3,
130+
column: 11,
131+
},
132+
],
133+
},
76134
],
77135
});

0 commit comments

Comments
 (0)
0