Skip to content

Commit

Permalink
add --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
薛定谔的猫 committed Sep 4, 2017
1 parent 39c2c30 commit 9a5c9cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
27 changes: 17 additions & 10 deletions lib/rules/lines-between-class-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
recommended: false
},

fixable: null,
fixable: "whitespace",

schema: [
{
Expand Down Expand Up @@ -47,11 +47,13 @@ module.exports = {
const options = {};
const config = context.options[0] || "always";

if (typeof config === "object") {
options.multiLine = config.multiLine === "always";
options.singleLine = config.singleLine === "always";
// "always" => {multiLine: "always", singleLine: "always"}
// "never" => {multiLine: "never", singleLine: "never"}
if (typeof config === "string") {
options.multiLine = options.singleLine = config;
} else {
options.multiLine = options.singleLine = config === "always";
options.multiLine = config.multiLine;
options.singleLine = config.singleLine;
}

const ALWAYS_MESSAGE = "Methods must be padded by blank lines.";
Expand All @@ -70,11 +72,12 @@ module.exports = {
}

/**
* Checks the given BlockStatement node to be padded if the block is not empty.
* @param {ASTNode} node The AST node of a BlockStatement.
* Checks the given MethodDefinition node to be padded.
* @param {ASTNode} node The AST node of a MethodDefinition.
* @returns {void} undefined.
*/
function checkPadding(node) {

const body = node.body;

for (let i = 0; i < body.length - 1; i++) {
Expand All @@ -88,12 +91,16 @@ module.exports = {
const next = sourceCode.getFirstToken(body[i + 1]);
const isPadded = isPaddingBetweenTokens(cur, next);
const isMulti = body[i].range[1].line - body[i].range[0].line > 1;
const shouldPadded = isMulti ? options.multiLine : options.singleLine;

if (shouldPadded !== isPadded) {
if (isMulti && (options.multiLine === "always") !== isPadded || !isMulti && (options.singleLine === "always") !== isPadded) {
context.report({
node,
message: shouldPadded ? ALWAYS_MESSAGE : NEVER_MESSAGE
message: isPadded ? NEVER_MESSAGE : ALWAYS_MESSAGE,
fix(fixer) {
return isPadded
? fixer.replaceTextRange([cur.range[1], next.range[0]], "\n")
: fixer.insertTextAfter(cur, "\n");
}
});
}
}
Expand Down
20 changes: 14 additions & 6 deletions tests/lib/rules/lines-between-class-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.";

Expand All @@ -28,14 +29,21 @@ ruleTester.run("lines-between-class-methods", rule, {
valid: [
"class foo{}",
"class foo{\n\n}",
"class foo{\nconstructor(){}\n\n}",
"class foo{constructor(){}\n}",
"class foo{\nconstructor(){}}",
"class foo{ bar(){}\n\nbaz(){}}",
{ code: "class foo{}", options: ["never"] },
{ code: "class foo{ bar(){}baz(){}}", options: ["never"] }
{ code: "class foo{ bar(){}\nbaz(){}}", options: ["never"] }
],
invalid: [
{ code: "class foo{ bar(){}baz(){}}", output: null, errors: [{ message: ALWAYS_MESSAGE }] },
{ code: "class foo{ bar(){}\nbaz(){}}", output: null, options: ["always"], errors: [{ message: ALWAYS_MESSAGE }] },
{ code: "class foo{ bar(){}\n\nbaz(){}}", output: null, options: ["never"], errors: [{ message: NEVER_MESSAGE }] }
{
code: "class foo{ bar(){}\nbaz(){}}",
output: "class foo{ bar(){}\n\nbaz(){}}",
errors: [{ message: ALWAYS_MESSAGE }]
}, {
code: "class foo{ bar(){}\n\nbaz(){}}",
output: "class foo{ bar(){}\nbaz(){}}",
options: ["never"],
errors: [{ message: NEVER_MESSAGE }]
}
]
});

0 comments on commit 9a5c9cf

Please sign in to comment.