Skip to content

Commit

Permalink
Chore: rm unused check & add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
薛定谔的猫 committed Sep 30, 2017
1 parent 971e15d commit 870e2e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
45 changes: 20 additions & 25 deletions lib/rules/lines-between-class-members.js
Expand Up @@ -63,31 +63,26 @@ module.exports = {
const body = node.body;

for (let i = 0; i < body.length - 1; i++) {

// only check padding lines after class members(skip empty).
if (body[i]) {

const curFirst = sourceCode.getFirstToken(body[i]);
const curLast = sourceCode.getLastToken(body[i]);
const comments = sourceCode.getCommentsBefore(body[i + 1]);
const nextFirst = comments.length ? comments[0] : sourceCode.getFirstToken(body[i + 1]);
const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
const skip = !isMulti && options[1].exceptAfterSingleLine;


if ((options[0] === "always" && !skip && !isPadded) ||
(options[0] === "never" && isPadded)) {
context.report({
node: body[i + 1],
message: isPadded ? NEVER_MESSAGE : ALWAYS_MESSAGE,
fix(fixer) {
return isPadded
? fixer.replaceTextRange([curLast.range[1], nextFirst.range[0]], "\n")
: fixer.insertTextAfter(curLast, "\n");
}
});
}
const curFirst = sourceCode.getFirstToken(body[i]);
const curLast = sourceCode.getLastToken(body[i]);
const comments = sourceCode.getCommentsBefore(body[i + 1]);
const nextFirst = comments.length ? comments[0] : sourceCode.getFirstToken(body[i + 1]);
const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
const skip = !isMulti && options[1].exceptAfterSingleLine;


if ((options[0] === "always" && !skip && !isPadded) ||
(options[0] === "never" && isPadded)) {
context.report({
node: body[i + 1],
message: isPadded ? NEVER_MESSAGE : ALWAYS_MESSAGE,
fix(fixer) {
return isPadded
? fixer.replaceTextRange([curLast.range[1], nextFirst.range[0]], "\n")
: fixer.insertTextAfter(curLast, "\n");
}
});
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/lines-between-class-members.js
Expand Up @@ -28,6 +28,7 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("lines-between-class-members", rule, {
valid: [
"class foo{}",
"class foo{;;}",
"class foo{\n\n}",
"class foo{constructor(){}\n}",
"class foo{\nconstructor(){}}",
Expand All @@ -36,6 +37,9 @@ ruleTester.run("lines-between-class-members", rule, {
"class foo{ bar(){}\n\n/*comments*/baz(){}}",
"class foo{ bar(){}\n\n//comments\nbaz(){}}",

"class foo{ bar(){}\n\n;;baz(){}}",
"class foo{ bar(){};\n\nbaz(){}}",

{ code: "class foo{ bar(){}\nbaz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}\n/*comments*/baz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}\n//comments\nbaz(){}}", options: ["never"] },
Expand Down

0 comments on commit 870e2e5

Please sign in to comment.