Skip to content

Commit

Permalink
add --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
薛定谔的猫 committed Aug 27, 2017
1 parent 39c2c30 commit c0ec4bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 12 additions & 7 deletions lib/rules/lines-between-class-methods.js
Expand Up @@ -16,7 +16,7 @@ module.exports = {
recommended: false
},

fixable: null,
fixable: "whitespace",

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

if (typeof config === "object") {
if (typeof config === "string") {
options.multiLine = options.singleLine = config === "always";
} else {
options.multiLine = config.multiLine === "always";
options.singleLine = config.singleLine === "always";
} else {
options.multiLine = options.singleLine = config === "always";
}

const ALWAYS_MESSAGE = "Methods must be padded by blank lines.";
Expand All @@ -70,8 +70,8 @@ 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) {
Expand All @@ -93,7 +93,12 @@ module.exports = {
if (shouldPadded !== isPadded) {
context.report({
node,
message: shouldPadded ? ALWAYS_MESSAGE : NEVER_MESSAGE
message: shouldPadded ? ALWAYS_MESSAGE : NEVER_MESSAGE,
fix(fixer) {
return shouldPadded
? fixer.insertTextBefore(next, "\n")
: fixer.replaceTextRange([cur.range[1], next.range[0]], "\n");
}
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/lib/rules/lines-between-class-methods.js
Expand Up @@ -31,11 +31,10 @@ ruleTester.run("lines-between-class-methods", rule, {
"class foo{\nconstructor(){}\n\n}",
"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 c0ec4bf

Please sign in to comment.