8000 feat(eslint-plugin): add extension rule `lines-between-class-members`… · dandv/typescript-eslint@08f93e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08f93e6

Browse files
authored
feat(eslint-plugin): add extension rule lines-between-class-members (typescript-eslint#1684)
1 parent f3f3bf8 commit 08f93e6

File tree

7 files changed

+491
-0
lines changed

7 files changed

+491
-0
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ In these cases, we create what we call an extension rule; a rule within our plug
187187
| [`@typescript-eslint/indent`](./docs/rules/indent.md) | Enforce consistent indentation | | :wrench: | |
188188
| [`@typescript-eslint/init-declarations`](./docs/rules/init-declarations.md) | require or disallow initialization in variable declarations | | | |
189189
| [`@typescript-eslint/keyword-spacing`](./docs/rules/keyword-spacing.md) | Enforce consistent spacing before and after keywords | | :wrench: | |
190+
| [`@typescript-eslint/lines-between-class-members`](./docs/rules/lines-between-class-members.md) | Require or disallow an empty line between class members | | :wrench: | |
190191
| [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
191192
| [`@typescript-eslint/no-dupe-class-members`](./docs/rules/no-dupe-class-members.md) | Disallow duplicate class members | | | |
192193
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Require or disallow an empty line between class members (`lines-between-class-members`)
2+
3+
This rule improves readability by enforcing lines between class members. It will not check empty lines before the first member and after the last member. This rule require or disallow an empty line between class members.
4+
5+
## Rule Details
6+
7+
This rule extends the base [`eslint/lines-between-class-members`](https://eslint.org/docs/rules/lines-between-class-members) rule.
8+
It adds support for ignoring overload methods in a class.
9+
10+
See the [ESLint documentation](https://eslint.org/docs/rules/lines-between-class-members) for more details on the `lines-between-class-members` rule.
11+
12+
## Rule Changes
13+
14+
```cjson
15+
{
16+
// note you must disable the base rule as it can report incorrect errors
17+
"lines-between-class-members": "off",
18+
"@typescript-eslint/lines-between-class-members": ["error"]
19+
}
20+
```
21+
22+
In addition to the options supported by the `lines-between-class-members` rule in ESLint core, the rule adds the following options:
23+
24+
## Options
25+
26+
This rule has a string option and an object option.
27+
28+
- Object option:
29+
30+
- `"exceptAfterOverload": true` (default) - Skip checking empty lines after overload class members
31+
- `"exceptAfterOverload": false` - **do not** skip checking empty lines after overload class members
32+
33+
- [See the other options allowed](https://github.com/eslint/eslint/blob/master/docs/rules/lines-between-class-members.md#options)
34+
35+
### `exceptAfterOverload: true`
36+
37+
Examples of **correct** code for the `{ "exceptAfterOverload": true }` option:
38+
39+
```ts
40+
/*eslint @typescript-eslint/lines-between-class-members: ["error", "always", { "exceptAfterOverload": true }]*/
41+
42+
class foo {
43+
bar(a: string): void;
44+
bar(a: string, b: string): void;
45+
bar(a: string, b: string) {}
46+
47+
baz() {}
48+
49+
qux() {}
50+
}
51+
```
52+
53+
### `exceptAfterOverload: false`
54+
55+
Examples of **correct** code for the `{ "exceptAfterOverload": false }` option:
56+
57+
```ts
58+
/*eslint @typescript-eslint/lines-between-class-members: ["error", "always", { "exceptAfterOverload": false }]*/
59+
60+
class foo {
61+
bar(a: string): void;
62+
63+
bar(a: string, b: string): void;
64+
65+
bar(a: string, b: string) {}
66+
67+
baz() {}
68+
69+
qux() {}
70+
}
71+
```
72+
73+
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/lines-between-class-members.md)</sup>

packages/eslint-plugin/src/configs/all.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"@typescript-eslint/func-call-spacing": "error",
2525
"indent": "off",
2626
"@typescript-eslint/indent": "error",
27+
"lines-between-class-members": "off",
28+
"@typescript-eslint/lines-between-class-members": "error",
2729
"init-declarations": "off",
2830
"@typescript-eslint/init-declarations": "error",
2931
"keyword-spacing": "off",

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import typeAnnotationSpacing from './type-annotation-spacing';
100100
import typedef from './typedef';
101101
import unboundMethod from './unbound-method';
102102
import unifiedSignatures from './unified-signatures';
103+
import linesBetweenClassMembers from './lines-between-class-members';
103104

104105
export default {
105106
'adjacent-overload-signatures': adjacentOverloadSignatures,
@@ -204,4 +205,5 @@ export default {
204205
typedef: typedef,
205206
'unbound-method': unboundMethod,
206207
'unified-signatures': unifiedSignatures,
208+
'lines-between-class-members': linesBetweenClassMembers,
207209
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
5+
import baseRule from 'eslint/lib/rules/lines-between-class-members';
6+
import * as util from '../util';
7+
8+
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
9+
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
10+
11+
const schema = util.deepMerge(
12+
{ ...baseRule.meta.schema },
13+
{
14+
1: {
15+
exceptAfterOverload: {
16+
type: 'booleean',
17+
default: true,
18+
},
19+
},
20+
},
21+
);
22+
23+
export default util.createRule<Options, MessageIds>({
24+
name: 'lines-between-class-members',
25+
meta: {
26+
type: 'layout',
27+
docs: {
28+
description: 'Require or disallow an empty line between class members',
29+
category: 'Stylistic Issues',
30+
recommended: false,
31+
extendsBaseRule: true,
32+
},
33+
fixable: 'whitespace',
34+
schema,
35+
messages: baseRule.meta.messages,
36+
},
37+
defaultOptions: [
38+
'always',
39+
{
40+
exceptAfterOverload: true,
41+
exceptAfterSingleLine: false,
42+
},
43+
],
44+
create(context, options) {
45+
const rules = baseRule.create(context);
46+
const exceptAfterOverload =
47+
options[1]?.exceptAfterOverload && options[0] === 'always';
48+
49+
function isOverload(node: TSESTree.Node): boolean {
50+
return (
51+
node.type === AST_NODE_TYPES.MethodDefinition &&
52+
node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression
53+
);
54+
}
55+
56+
return {
57+
ClassBody(node): void {
58+
const body = exceptAfterOverload
59+
? node.body.filter(node => !isOverload(node))
60+
: node.body;
61+
62+
rules.ClassBody({ ...node, body });
63+
},
64+
};
65+
},
66+
});

0 commit comments

Comments
 (0)
0