Skip to content

Commit

Permalink
Add autofix for double-slash-comment-empty-line-before (#230)
Browse files Browse the repository at this point in the history
* Add autofix

* Address PR comments
  • Loading branch information
OriR authored and kristerkari committed Mar 31, 2018
1 parent d1476d6 commit adbc14e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,30 @@ const alwaysGeneralTests = {
{
code: `
// comment 1
// comment 2
`,
fixed: `
// comment 1
// comment 2
`,
message: messages.expected
},
{
code: "// comment\r\n// comment",
fixed: "// comment\r\n\r\n// comment",
description: "One windows newline between comments.",
message: messages.expected
},
{
code: `
a { color: pink;
// comment w/o empty line
top: 0; }
`,
fixed: `
a { color: pink;
// comment w/o empty line
top: 0; }
`,
Expand All @@ -73,6 +85,8 @@ const alwaysGeneralTests = {
{
code:
"a { color: pink;\r\n// comment w/o empty lines, Win style\r\ntop: 0; }",
fixed:
"a { color: pink;\r\n\r\n// comment w/o empty lines, Win style\r\ntop: 0; }",
description: "One Windows newline before comment.",
message: messages.expected
}
Expand All @@ -88,6 +102,7 @@ testRule(rule, {
config: ["always"],
syntax: "scss",
skipBasicChecks: true,
fix: true,

accept: alwaysGeneralTests.accept.concat([
{
Expand All @@ -103,6 +118,11 @@ testRule(rule, {
reject: alwaysGeneralTests.reject.concat([
{
code: `a {
// First-nested, no empty line before.
color: pink;
}`,
fixed: `a {
// First-nested, no empty line before.
color: pink;
}`,
Expand All @@ -119,16 +139,17 @@ testRule(rule, {
config: ["always", { except: ["first-nested"] }],
syntax: "scss",
skipBasicChecks: true,
fix: true,

accept: alwaysGeneralTests.accept.concat([
{
code: `
a {
// First nested, now empty line.
// First nested, no empty line.
color: pink;
}
`,
description: "First nested, now empty line."
description: "First nested, no empty line."
}
]),

Expand All @@ -137,6 +158,11 @@ testRule(rule, {
code: `
a {
// First-nested, with empty line (rejected).
color: pink;
}`,
fixed: `
a {
// First-nested, with empty line (rejected).
color: pink;
}`,
Expand Down Expand Up @@ -174,6 +200,7 @@ testRule(rule, {
config: ["always", { ignore: ["between-comments"] }],
syntax: "scss",
skipBasicChecks: true,
fix: true,

accept: [
{
Expand Down Expand Up @@ -230,6 +257,15 @@ testRule(rule, {
top: 0;
}
`,
fixed: `
a {
color: pink;
/// comment
/// comment
top: 0;
}
`,
description: "Multiple comments, inside ruleset, no empty lines.",
message: messages.expected
}
Expand Down
24 changes: 23 additions & 1 deletion src/rules/double-slash-comment-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const messages = utils.ruleMessages(ruleName, {

const stylelintCommandPrefix = "stylelint-";

export default function(expectation, options) {
export default function(expectation, options, context) {
return (root, result) => {
const validOptions = utils.validateOptions(
result,
Expand All @@ -37,6 +37,17 @@ export default function(expectation, options) {
return;
}

const fix = (comment, match, replace) => {
const escapedMatch = match.replace(
/(\r)?\n/g,
(_, r) => (r ? "\\r\\n" : "\\n")
);
comment.raws.before = comment.raws.before.replace(
new RegExp(`^${escapedMatch}`),
replace
);
};

root.walkComments(comment => {
// Only process // comments
if (!comment.raws.inline && !comment.inline) {
Expand Down Expand Up @@ -90,6 +101,17 @@ export default function(expectation, options) {
return;
}

if (context.fix) {
if (expectEmptyLineBefore && !hasEmptyLineBefore) {
fix(comment, context.newline, context.newline + context.newline);
return;
}
if (!expectEmptyLineBefore && hasEmptyLineBefore) {
fix(comment, context.newline + context.newline, context.newline);
return;
}
}

const message = expectEmptyLineBefore
? messages.expected
: messages.rejected;
Expand Down

0 comments on commit adbc14e

Please sign in to comment.