Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(eslint-plugin): add extension rule
lines-between-class-members
(…
- Loading branch information
Showing
7 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/eslint-plugin/docs/rules/lines-between-class-members.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Require or disallow an empty line between class members (`lines-between-class-members`) | ||
|
||
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. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/lines-between-class-members`](https://eslint.org/docs/rules/lines-between-class-members) rule. | ||
It adds support for ignoring overload methods in a class. | ||
|
||
See the [ESLint documentation](https://eslint.org/docs/rules/lines-between-class-members) for more details on the `lines-between-class-members` rule. | ||
|
||
## Rule Changes | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"lines-between-class-members": "off", | ||
"@typescript-eslint/lines-between-class-members": ["error"] | ||
} | ||
``` | ||
|
||
In addition to the options supported by the `lines-between-class-members` rule in ESLint core, the rule adds the following options: | ||
|
||
## Options | ||
|
||
This rule has a string option and an object option. | ||
|
||
- Object option: | ||
|
||
- `"exceptAfterOverload": true` (default) - Skip checking empty lines after overload class members | ||
- `"exceptAfterOverload": false` - **do not** skip checking empty lines after overload class members | ||
|
||
- [See the other options allowed](https://github.com/eslint/eslint/blob/master/docs/rules/lines-between-class-members.md#options) | ||
|
||
### `exceptAfterOverload: true` | ||
|
||
Examples of **correct** code for the `{ "exceptAfterOverload": true }` option: | ||
|
||
```ts | ||
/*eslint @typescript-eslint/lines-between-class-members: ["error", "always", { "exceptAfterOverload": true }]*/ | ||
class foo { | ||
bar(a: string): void; | ||
bar(a: string, b: string): void; | ||
bar(a: string, b: string) {} | ||
baz() {} | ||
qux() {} | ||
} | ||
``` | ||
|
||
### `exceptAfterOverload: false` | ||
|
||
Examples of **correct** code for the `{ "exceptAfterOverload": false }` option: | ||
|
||
```ts | ||
/*eslint @typescript-eslint/lines-between-class-members: ["error", "always", { "exceptAfterOverload": false }]*/ | ||
class foo { | ||
bar(a: string): void; | ||
bar(a: string, b: string): void; | ||
bar(a: string, b: string) {} | ||
baz() {} | ||
qux() {} | ||
} | ||
``` | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/lines-between-class-members.md)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/eslint-plugin/src/rules/lines-between-class-members.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/lines-between-class-members'; | ||
import * as util from '../util'; | ||
|
||
type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
const schema = util.deepMerge( | ||
{ ...baseRule.meta.schema }, | ||
{ | ||
1: { | ||
exceptAfterOverload: { | ||
type: 'booleean', | ||
default: true, | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'lines-between-class-members', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Require or disallow an empty line between class members', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
fixable: 'whitespace', | ||
schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: [ | ||
'always', | ||
{ | ||
exceptAfterOverload: true, | ||
exceptAfterSingleLine: false, | ||
}, | ||
], | ||
create(context, options) { | ||
const rules = baseRule.create(context); | ||
const exceptAfterOverload = | ||
options[1]?.exceptAfterOverload && options[0] === 'always'; | ||
|
||
function isOverload(node: TSESTree.Node): boolean { | ||
return ( | ||
node.type === AST_NODE_TYPES.MethodDefinition && | ||
node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression | ||
); | ||
} | ||
|
||
return { | ||
ClassBody(node): void { | ||
const body = exceptAfterOverload | ||
? node.body.filter(node => !isOverload(node)) | ||
: node.body; | ||
|
||
rules.ClassBody({ ...node, body }); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.