Skip to content

feat(eslint-plugin): [no-empty-function] new allow option overrideMethods #4923

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

Merged
merged 3 commits into from
May 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/eslint-plugin/docs/rules/no-empty-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ This rule adds the following options:
type AdditionalAllowOptionEntries =
| 'private-constructors'
| 'protected-constructors'
| 'decoratedFunctions';
| 'decoratedFunctions'
| 'overrideMethods';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question before merge: should we go with camel case or kebab case? ESLint always uses kebab case, and so are the two other options here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think it hugely matters - I think we use a mix of everything in this codebase.
we can always write a lint rule and standardise later!


type AllowOptionEntries =
| BaseNoEmptyFunctionAllowOptionEntries
Expand Down Expand Up @@ -77,6 +78,22 @@ class Foo {
}
```

### allow: `overrideMethods`

Examples of correct code for the `{ "allow": ["overrideMethods"] }` option:

```ts
abstract class Base {
protected greet(): void {
console.log('Hello!');
}
}

class Foo extends Base {
protected override greet(): void {}
}
```

## How to Use

```jsonc
Expand Down
16 changes: 15 additions & 1 deletion packages/eslint-plugin/src/rules/no-empty-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const schema = util.deepMerge(
'asyncFunctions',
'asyncMethods',
'decoratedFunctions',
'overrideMethods',
],
},
},
Expand Down Expand Up @@ -63,6 +64,7 @@ export default util.createRule<Options, MessageIds>({
);
const isAllowedPrivateConstructors = allow.includes('private-constructors');
const isAllowedDecoratedFunctions = allow.includes('decoratedFunctions');
const isAllowedOverrideMethods = allow.includes('overrideMethods');

/**
* Check if the method body is empty
Expand Down Expand Up @@ -138,12 +140,24 @@ export default util.createRule<Options, MessageIds>({
return false;
}

function isAllowedEmptyOverrideMethod(
node: TSESTree.FunctionExpression,
): boolean {
return (
isAllowedOverrideMethods &&
isBodyEmpty(node) &&
node.parent?.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.override === true
);
}

return {
...rules,
FunctionExpression(node): void {
if (
isAllowedEmptyConstructor(node) ||
isAllowedEmptyDecoratedFunctions(node)
isAllowedEmptyDecoratedFunctions(node) ||
isAllowedEmptyOverrideMethod(node)
) {
return;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class Foo {
`,
options: [{ allow: ['decoratedFunctions'] }],
},
{
code: `
class Foo extends Base {
override foo() {}
}
`,
options: [{ allow: ['overrideMethods'] }],
},
],

invalid: [
Expand Down Expand Up @@ -192,5 +200,22 @@ class Foo {
},
],
},
{
code: `
class Foo extends Base {
override foo() {}
}
`,
errors: [
{
messageId: 'unexpected',
data: {
name: "method 'foo'",
},
line: 3,
column: 18,
},
],
},
],
});