forked from eslint/eslint
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Rule - 'lines-between-class-methods' (fixes eslint#5949)
- Loading branch information
Showing
5 changed files
with
519 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
# Enforce lines between class methods (lines-between-class-methods) | ||
|
||
Some style guides require class methods to have a empty line between them. The | ||
goal is to improve readability by visually separating the methods from each | ||
other. | ||
|
||
```js | ||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
b () { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
Since it's good to have a consistent code style, you should either always add a | ||
empty line between methods or never do it. | ||
|
||
## Rule Details | ||
|
||
This rule enforces empty lines between class methods. | ||
|
||
## Options | ||
|
||
This rule has a string option: | ||
|
||
* `"always"` (default) requires one or more empty line between class methods | ||
* `"never"` disallows empty lines between class methods | ||
|
||
### always | ||
|
||
Examples of **incorrect** code for this rule with the default `"always"` option: | ||
|
||
```js | ||
/*eslint lines-between-class-methods: ["error", "always"]*/ | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
b () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
b () { | ||
// ... | ||
} | ||
c () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
// comment | ||
b () { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule with the default `"always"` option: | ||
|
||
```js | ||
/*eslint lines-between-class-methods: ["error", "always"]*/ | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
b () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
b () { | ||
// ... | ||
} | ||
|
||
c () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
|
||
b () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
// comment | ||
b () { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
### never | ||
|
||
Examples of **incorrect** code for this rule with the `"never"` option: | ||
|
||
```js | ||
/*eslint lines-between-class-methods: ["error", "never"]*/ | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
b () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
b () { | ||
// ... | ||
} | ||
|
||
c () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
|
||
|
||
b () { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule with the `"never"` option: | ||
|
||
```js | ||
/*eslint lines-between-class-methods: ["error", "never"]*/ | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
b () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
b () { | ||
// ... | ||
} | ||
c () { | ||
// ... | ||
} | ||
} | ||
|
||
class T { | ||
a () { | ||
// ... | ||
} | ||
// comment | ||
b () { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
You can turn this rule off if you are not concerned with the consistency of spacing between class methods. |
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,68 @@ | ||
/** | ||
* @fileoverview A rule to ensure empty lines between class functions. | ||
* @author Linus Unnebäck <https://github.com/LinusU> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
var config = context.options[0] || "always"; | ||
|
||
var ALWAYS_MESSAGE = "Class methods must be separated by at least one blank line.", | ||
NEVER_MESSAGE = "Class methods must not be separated by blank lines."; | ||
|
||
var sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
"ClassBody:exit": function(node) { | ||
node.body.reduce(function(prev, next) { | ||
var firstEmptyLine = null; | ||
var nextLine = prev.loc.end.line + 1; | ||
var comments = sourceCode.getComments(prev).trailing; | ||
|
||
for (var i = 0; i < comments.length; i++) { | ||
var comment = comments[i]; | ||
|
||
if (comment.loc.start.line > nextLine) { | ||
firstEmptyLine = nextLine; | ||
break; | ||
} | ||
|
||
nextLine = comment.loc.end.line + 1; | ||
} | ||
|
||
if (firstEmptyLine === null && next.loc.start.line > nextLine) { | ||
firstEmptyLine = nextLine; | ||
} | ||
|
||
if (config === "always" && firstEmptyLine === null) { | ||
context.report({ | ||
node: node, | ||
loc: { line: next.loc.start.line, column: next.loc.start.column }, | ||
message: ALWAYS_MESSAGE | ||
}); | ||
} | ||
|
||
if (config === "never" && firstEmptyLine !== null) { | ||
context.report({ | ||
node: node, | ||
loc: { line: firstEmptyLine, column: 0 }, | ||
message: NEVER_MESSAGE | ||
}); | ||
} | ||
|
||
return next; | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports.schema = [ | ||
{ | ||
enum: ["always", "never"] | ||
} | ||
]; |
Oops, something went wrong.