Skip to content

Commit

Permalink
Update: Rename 'class-padding' to 'lines-between-class-methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Apr 24, 2016
1 parent e0b315b commit 2835960
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 109 deletions.
2 changes: 1 addition & 1 deletion conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
"brace-style": "off",
"callback-return": "off",
"camelcase": "off",
"class-padding": "off",
"comma-dangle": "error",
"comma-spacing": "off",
"comma-style": "off",
Expand Down Expand Up @@ -162,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
2 changes: 1 addition & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ These rules relate to style guidelines, and are therefore quite subjective:
* [block-spacing](block-spacing.md): enforce consistent spacing inside single-line blocks (fixable)
* [brace-style](brace-style.md): enforce consistent brace style for blocks
* [camelcase](camelcase.md): enforce camelcase naming convention
* [class-padding](class-padding.md): enforce consistent padding between class methods
* [comma-spacing](comma-spacing.md): enforce consistent spacing before and after commas (fixable)
* [comma-style](comma-style.md): enforce consistent comma style
* [computed-property-spacing](computed-property-spacing.md): enforce consistent spacing inside computed property brackets (fixable)
Expand All @@ -171,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enforce padding between class methods (class-padding)
# Enforce lines between class methods (lines-between-class-methods)

Some style guides require class methods to have a blank line between them. The
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.

Expand All @@ -16,26 +16,26 @@ class T {
}
```

Since it's good to have a consistent code style, you should either always write
padding between methods or never do it.
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 consistent padding between class methods.
This rule enforces empty lines between class methods.

## Options

This rule has a string option:

* `"always"` (default) requires padding between class methods
* `"never"` disallows padding between class methods
* `"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 class-padding: ["error", "always"]*/
/*eslint lines-between-class-methods: ["error", "always"]*/

class T {
a () {
Expand All @@ -59,17 +59,6 @@ class T {
}
}

class T {
a () {
// ...
}


b () {
// ...
}
}

class T {
a () {
// ...
Expand All @@ -84,7 +73,7 @@ class T {
Examples of **correct** code for this rule with the default `"always"` option:

```js
/*eslint class-padding: ["error", "always"]*/
/*eslint lines-between-class-methods: ["error", "always"]*/

class T {
a () {
Expand All @@ -110,6 +99,17 @@ class T {
}
}

class T {
a () {
// ...
}


b () {
// ...
}
}

class T {
a () {
// ...
Expand All @@ -127,7 +127,7 @@ class T {
Examples of **incorrect** code for this rule with the `"never"` option:

```js
/*eslint class-padding: ["error", "never"]*/
/*eslint lines-between-class-methods: ["error", "never"]*/

class T {
a () {
Expand Down Expand Up @@ -167,7 +167,7 @@ class T {
Examples of **correct** code for this rule with the `"never"` option:

```js
/*eslint class-padding: ["error", "never"]*/
/*eslint lines-between-class-methods: ["error", "never"]*/

class T {
a () {
Expand Down Expand Up @@ -203,4 +203,4 @@ class T {

## When Not To Use It

You can turn this rule off if you are not concerned with the consistency of padding between class methods.
You can turn this rule off if you are not concerned with the consistency of spacing between class methods.
60 changes: 0 additions & 60 deletions lib/rules/class-padding.js

This file was deleted.

68 changes: 68 additions & 0 deletions lib/rules/lines-between-class-methods.js
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"]
}
];
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @fileoverview Tests for class-padding rule.
* @fileoverview Tests for lines-between-class-methods rule.
* @author Linus Unnebäck <https://github.com/LinusU>
*/

Expand All @@ -9,18 +9,18 @@
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/class-padding"),
var rule = require("../../../lib/rules/lines-between-class-methods"),
RuleTester = require("../../../lib/testers/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester(),
ALWAYS_MESSAGE = "Class functions must be separated by blank line.",
NEVER_MESSAGE = "Class functions must not be separated by blank lines.";
ALWAYS_MESSAGE = "Class methods must be separated by at least one blank line.",
NEVER_MESSAGE = "Class methods must not be separated by blank lines.";

ruleTester.run("class-padding", rule, {
ruleTester.run("lines-between-class-methods", rule, {
valid: [
{
code: "class T {\na() {}\nb() {}\n}",
Expand Down Expand Up @@ -101,6 +101,11 @@ ruleTester.run("class-padding", rule, {
code: "class T {\na() {}\n/**/ b() {}\n}",
options: ["never"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "class T {\na() {}\n\n\nb() {}\n}",
options: ["always"],
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand All @@ -123,7 +128,7 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 4,
line: 3,
column: 1
}
]
Expand All @@ -147,7 +152,7 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 4,
line: 3,
column: 1
}
]
Expand Down Expand Up @@ -176,12 +181,12 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 4,
line: 3,
column: 1
},
{
message: NEVER_MESSAGE,
line: 6,
line: 5,
column: 1
}
]
Expand All @@ -193,19 +198,7 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 5,
column: 1
}
]
},
{
code: "class T {\na() {}\n\n\nb() {}\n}",
options: ["always"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: ALWAYS_MESSAGE,
line: 5,
line: 3,
column: 1
}
]
Expand All @@ -229,7 +222,7 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 6,
line: 4,
column: 1
}
]
Expand All @@ -241,7 +234,7 @@ ruleTester.run("class-padding", rule, {
errors: [
{
message: NEVER_MESSAGE,
line: 6,
line: 3,
column: 1
}
]
Expand Down

0 comments on commit 2835960

Please sign in to comment.