Skip to content

Commit

Permalink
New: Rule - 'lines-between-class-methods' (fixes eslint#5949)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Apr 24, 2016
1 parent fc78e78 commit df5711c
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Expand Up @@ -161,6 +161,7 @@
"key-spacing": "off",
"keyword-spacing": "off",
"lines-around-comment": "off",
"lines-between-class-methods": "off",
"max-depth": "off",
"max-len": "off",
"max-nested-callbacks": "off",
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -170,6 +170,7 @@ These rules relate to style guidelines, and are therefore quite subjective:
* [keyword-spacing](keyword-spacing.md): enforce consistent spacing before and after keywords (fixable)
* [linebreak-style](linebreak-style.md): enforce consistent linebreak style (fixable)
* [lines-around-comment](lines-around-comment.md): require empty lines around comments
* [lines-between-class-methods](lines-between-class-methods.md): enforce consistent padding between class methods
* [max-depth](max-depth.md): enforce a maximum depth that blocks can be nested
* [max-len](max-len.md): enforce a maximum line length
* [max-nested-callbacks](max-nested-callbacks.md): enforce a maximum depth that callbacks can be nested
Expand Down
206 changes: 206 additions & 0 deletions docs/rules/lines-between-class-methods.md
@@ -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.
68 changes: 68 additions & 0 deletions lib/rules/lines-between-class-methods.js
@@ -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"]
}
];

0 comments on commit df5711c

Please sign in to comment.