Skip to content

Commit

Permalink
Add autofix to value-list-max-empty-lines (#3814)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored and ntwb committed Nov 27, 2018
1 parent 703bd3b commit f7aba89
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Expand Up @@ -252,7 +252,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
- [`value-list-comma-newline-before`](../../lib/rules/value-list-comma-newline-before/README.md): Require a newline or disallow whitespace before the commas of value lists.
- [`value-list-comma-space-after`](../../lib/rules/value-list-comma-space-after/README.md): Require a single space or disallow whitespace after the commas of value lists (Autofixable).
- [`value-list-comma-space-before`](../../lib/rules/value-list-comma-space-before/README.md): Require a single space or disallow whitespace before the commas of value lists (Autofixable).
- [`value-list-max-empty-lines`](../../lib/rules/value-list-max-empty-lines/README.md): Limit the number of adjacent empty lines within value lists.
- [`value-list-max-empty-lines`](../../lib/rules/value-list-max-empty-lines/README.md): Limit the number of adjacent empty lines within value lists (Autofixable).

#### Custom property

Expand Down
4 changes: 3 additions & 1 deletion lib/rules/value-list-max-empty-lines/README.md
Expand Up @@ -13,6 +13,8 @@ a {
* This empty line */
```

The `--fix` option on the [command line](../../../docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.

## Options

`int`: Maximum number of empty lines.
Expand All @@ -33,7 +35,7 @@ a {

```css
a {
padding:
padding:
10px
10px
10px
Expand Down
60 changes: 60 additions & 0 deletions lib/rules/value-list-max-empty-lines/__tests__/index.js
Expand Up @@ -6,6 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: [0],
fix: true,

accept: [
{
Expand Down Expand Up @@ -49,52 +50,105 @@ testRule(rule, {
},
{
code: "a { box-shadow: inset 0 2px 0 #dcffa6,\r\n0 2px 5px #000; }"
},
{
code: "a { padding: 10px /*\n\n\n\n\n\n*/ 10px 10px 10px }"
}
],

reject: [
{
code: "a { padding: 10px\n\n10px 10px 10px }",
fixed: "a { padding: 10px\n10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px\r\n\r\n10px 10px 10px }",
fixed: "a { padding: 10px\r\n10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px 10px 10px\n\n10px }",
fixed: "a { padding: 10px 10px 10px\n10px }",
message: messages.expected(0),
line: 1,
column: 19
},
{
code: "a { padding: 10px 10px 10px\r\n\r\n10px }",
fixed: "a { padding: 10px 10px 10px\r\n10px }",
message: messages.expected(0),
line: 1,
column: 19
},
{
code: "a { box-shadow: inset 0 2px 0 #dcffa6,\n\n0 2px 5px #000; }",
fixed: "a { box-shadow: inset 0 2px 0 #dcffa6,\n0 2px 5px #000; }",
message: messages.expected(0),
line: 1,
column: 27
},
{
code: "a { box-shadow: inset 0 2px 0 #dcffa6,\r\n\r\n0 2px 5px #000; }",
fixed: "a { box-shadow: inset 0 2px 0 #dcffa6,\r\n0 2px 5px #000; }",
message: messages.expected(0),
line: 1,
column: 27
},
{
code: "a { padding: 10px\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n10px 10px 10px }",
fixed: "a { padding: 10px\n10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px\n\n\n\n10px\n\n\n10px\n\n10px }",
fixed: "a { padding: 10px\n10px\n10px\n10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code:
"a { padding: 10px\r\n\r\n\r\n\r\n10px\r\n\r\n\r\n10px\r\n\r\n10px }",
fixed: "a { padding: 10px\r\n10px\r\n10px\r\n10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px\n\n /*comment*/ 10px 10px 10px }",
fixed: "a { padding: 10px\n /*comment*/ 10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px\r\n\r\n /*comment*/ 10px 10px 10px }",
fixed: "a { padding: 10px\r\n /*comment*/ 10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
},
{
code: "a { padding: 10px\n\n \n\n\t\n\n10px 10px 10px }",
fixed: "a { padding: 10px\n \n\t\n10px 10px 10px }",
message: messages.expected(0),
line: 1,
column: 9
}
]
});

testRule(rule, {
ruleName,
config: [1],
fix: true,

accept: [
{
Expand Down Expand Up @@ -123,37 +177,43 @@ testRule(rule, {
reject: [
{
code: "a { padding: 10px\n\n\n10px 10px 10px }",
fixed: "a { padding: 10px\n\n10px 10px 10px }",
message: messages.expected(1),
line: 1,
column: 9
},
{
code: "a { padding: 10px\r\n\r\n\r\n10px 10px 10px }",
fixed: "a { padding: 10px\r\n\r\n10px 10px 10px }",
message: messages.expected(1),
line: 1,
column: 9
},
{
code: "a { padding: 10px 10px 10px\n\n\n10px }",
fixed: "a { padding: 10px 10px 10px\n\n10px }",
message: messages.expected(1),
line: 1,
column: 19
},
{
code: "a { padding: 10px 10px 10px\r\n\r\n\r\n10px }",
fixed: "a { padding: 10px 10px 10px\r\n\r\n10px }",
message: messages.expected(1),
line: 1,
column: 19
},
{
code: "a { box-shadow: inset 0 2px 0 #dcffa6,\n\n\n0 2px 5px #000; }",
fixed: "a { box-shadow: inset 0 2px 0 #dcffa6,\n\n0 2px 5px #000; }",
message: messages.expected(1),
line: 1,
column: 27
},
{
code:
"a { box-shadow: inset 0 2px 0 #dcffa6,\r\n\r\n\r\n0 2px 5px #000; }",
fixed: "a { box-shadow: inset 0 2px 0 #dcffa6,\r\n\r\n0 2px 5px #000; }",
message: messages.expected(1),
line: 1,
column: 27
Expand Down
35 changes: 33 additions & 2 deletions lib/rules/value-list-max-empty-lines/index.js
Expand Up @@ -13,7 +13,7 @@ const messages = ruleMessages(ruleName, {
`Expected no more than ${max} empty ${max === 1 ? "line" : "lines"}`
});

const rule = function(max) {
const rule = function(max, options, context) {
const maxAdjacentNewlines = max + 1;

return (root, result) => {
Expand All @@ -27,10 +27,12 @@ const rule = function(max) {
}

root.walkDecls(decl => {
const value = decl.value;
const value = decl.raws.value ? decl.raws.value.raw : decl.value;
const repeatLFNewLines = _.repeat("\n", maxAdjacentNewlines);
const repeatCRLFNewLines = _.repeat("\r\n", maxAdjacentNewlines);

const fixIndices = [];

styleSearch({ source: value, target: "\n" }, match => {
if (
value.substr(match.startIndex + 1, maxAdjacentNewlines) ===
Expand All @@ -45,6 +47,12 @@ const rule = function(max) {
index -= 1;
}

if (context.fix) {
fixIndices.push(index);

return;
}

report({
message: messages.expected(max),
node: decl,
Expand All @@ -54,6 +62,29 @@ const rule = function(max) {
});
}
});

if (fixIndices.length) {
const maxAdjacentNewLinesStr = _.repeat(
context.newline,
maxAdjacentNewlines
);
let fixedValue = value;

fixIndices
.sort((a, b) => b - a)
.forEach(index => {
fixedValue =
fixedValue.slice(0, index) +
maxAdjacentNewLinesStr +
fixedValue.slice(index).replace(/^[\r\n]*/, "");
});

if (decl.raws.value) {
decl.raws.value.raw = fixedValue;
} else {
decl.value = fixedValue;
}
}
});
};
};
Expand Down

0 comments on commit f7aba89

Please sign in to comment.