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
aladdin-add authored and 薛定谔的猫 committed Aug 25, 2017
1 parent ff8c4bb commit 78ce6d0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint-recommended.js
Expand Up @@ -62,6 +62,7 @@ module.exports = {
"linebreak-style": "off",
"lines-around-comment": "off",
"lines-around-directive": "off",
"lines-between-class-methods": "off",
"max-depth": "off",
"max-len": "off",
"max-lines": "off",
Expand Down
5 changes: 5 additions & 0 deletions docs/rules/lines-between-class-methods.md
@@ -0,0 +1,5 @@
# enforce lines between class methods (lines-between-class-methods)

## Rule Details

## Options
106 changes: 106 additions & 0 deletions lib/rules/lines-between-class-methods.js
@@ -0,0 +1,106 @@
/**
* @fileoverview Rule to enforce lines between class methods
* @author 薛定谔的猫<hh_2013@foxmail.com>
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

// const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------


module.exports = {
meta: {
docs: {
description: "enforce lines between class methods",
category: "Stylistic Issues",
recommended: false
},

fixable: null,

schema: [
{
oneOf: [
{
enum: ["always", "never"]
},
{
type: "object",
properties: {
multiLine: {
enum: ["always", "never"]
},
singleLine: {
enum: ["always", "never"]
}
},
additionalProperties: false,
minProperties: 1
}
]
}
]
},

create(context) {

// const options = {};
const config = context.options[0] || "always";

const ALWAYS_MESSAGE = "Methods must be padded by blank lines.";
const NEVER_MESSAGE = "Methods must not be padded by blank lines.";

const sourceCode = context.getSourceCode();

/**
* Checks if there is padding between two tokens
* @param {Token} first The first token
* @param {Token} second The second token
* @returns {boolean} True if there is at least a line between the tokens
*/
function isPaddingBetweenTokens(first, second) {
return second.loc.start.line - first.loc.end.line >= 2;
}

/**
* Checks the given BlockStatement node to be padded if the block is not empty.
* @param {ASTNode} node The AST node of a BlockStatement.
* @returns {void} undefined.
*/
function checkPadding(node) {
for (let i = 0; i < node.body.length - 1; i++) {
const prev = sourceCode.getLastToken(node.body[i]);
const next = sourceCode.getFirstToken(node.body[i + 1]);
const padded = isPaddingBetweenTokens(prev, next);

if (config === "always" && !padded) {
context.report({
node,
message: ALWAYS_MESSAGE
});
} else if (config === "never" && padded) {
context.report({
node,
message: NEVER_MESSAGE
});
}
}
}

return {
ClassBody: checkPadding
};

}
};
35 changes: 35 additions & 0 deletions tests/lib/rules/lines-between-class-methods.js
@@ -0,0 +1,35 @@
/**
* @fileoverview Tests for lines-between-class-methods rule.
* @author 薛定谔的猫<hh_2013@foxmail.com>
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const ALWAYS_MESSAGE = "Methods must be padded by blank lines.";
const NEVER_MESSAGE = "Methods must not be padded by blank lines.";

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

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });

ruleTester.run("lines-between-class-methods", rule, {
valid: [
"class foo{ bar(){}\n\nbaz(){}}"
],
invalid: [
{ code: "class foo{ bar(){}baz(){}}", output: null, errors: [{ message: ALWAYS_MESSAGE }] },
{ code: "class foo{ bar(){}\n\nbaz(){}}", output: null, options: ["never"], errors: [{ message: NEVER_MESSAGE }] }
]
});

0 comments on commit 78ce6d0

Please sign in to comment.