Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autofix to selector-list-comma-space-before #3447

Merged
merged 3 commits into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
- [`selector-list-comma-newline-after`](../../lib/rules/selector-list-comma-newline-after/README.md): Require a newline or disallow whitespace after the commas of selector lists.
- [`selector-list-comma-newline-before`](../../lib/rules/selector-list-comma-newline-before/README.md): Require a newline or disallow whitespace before the commas of selector lists.
- [`selector-list-comma-space-after`](../../lib/rules/selector-list-comma-space-after/README.md): Require a single space or disallow whitespace after the commas of selector lists.
- [`selector-list-comma-space-before`](../../lib/rules/selector-list-comma-space-before/README.md): Require a single space or disallow whitespace before the commas of selector lists.
- [`selector-list-comma-space-before`](../../lib/rules/selector-list-comma-space-before/README.md): Require a single space or disallow whitespace before the commas of selector lists (Autofixable).

#### Rule

Expand Down
2 changes: 2 additions & 0 deletions lib/rules/selector-list-comma-space-before/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Require a single space or disallow whitespace before the commas of selector list
* The space before this comma */
```

The `--fix` option on the command line can automatically fix all of the problems reported by this rule.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor update to make it like the other READMEs:

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

`string`: `"always"|"never"|"always-single-line"|"never-single-line"`
Expand Down
31 changes: 31 additions & 0 deletions lib/rules/selector-list-comma-space-before/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: ["always"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -57,53 +58,68 @@ testRule(rule, {
reject: [
{
code: "a,b {}",
fixed: "a ,b {}",
message: messages.expectedBefore(),
line: 1,
column: 2
},
{
code: "a ,b {}",
fixed: "a ,b {}",
message: messages.expectedBefore(),
line: 1,
column: 4
},
{
code: "a\n,b {}",
fixed: "a ,b {}",
message: messages.expectedBefore(),
line: 2,
column: 1
},
{
code: "a\r\n,b {}",
fixed: "a ,b {}",
description: "CRLF",
message: messages.expectedBefore(),
line: 2,
column: 1
},
{
code: "a\t,b {}",
fixed: "a ,b {}",
message: messages.expectedBefore(),
line: 1,
column: 3
},
{
code: "a ,b,c {}",
fixed: "a ,b ,c {}",
message: messages.expectedBefore(),
line: 1,
column: 5
},
{
code: "a ,b ,c {}",
fixed: "a ,b ,c {}",
message: messages.expectedBefore(),
line: 1,
column: 7
},
{
code: "a,b,c {}",
fixed: "a ,b ,c {}",
message: messages.expectedBefore(),
line: 1,
column: 2
}
]
});

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

accept: [
{
Expand Down Expand Up @@ -139,43 +155,50 @@ testRule(rule, {
reject: [
{
code: "a ,b {}",
fixed: "a,b {}",
message: messages.rejectedBefore(),
line: 1,
column: 3
},
{
code: "a ,b {}",
fixed: "a,b {}",
message: messages.rejectedBefore(),
line: 1,
column: 4
},
{
code: "a\n,b {}",
fixed: "a,b {}",
message: messages.rejectedBefore(),
line: 2,
column: 1
},
{
code: "a\r\n,b {}",
fixed: "a,b {}",
description: "CRLF",
message: messages.rejectedBefore(),
line: 2,
column: 1
},
{
code: "a\t,b {}",
fixed: "a,b {}",
message: messages.rejectedBefore(),
line: 1,
column: 3
},
{
code: "a,b ,c {}",
fixed: "a,b,c {}",
message: messages.rejectedBefore(),
line: 1,
column: 5
},
{
code: "a,b ,c {}",
fixed: "a,b,c {}",
message: messages.rejectedBefore(),
line: 1,
column: 6
Expand All @@ -186,6 +209,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["always-single-line"],
fix: true,

accept: [
{
Expand All @@ -204,18 +228,21 @@ testRule(rule, {
reject: [
{
code: "a,b {}",
fixed: "a ,b {}",
message: messages.expectedBeforeSingleLine(),
line: 1,
column: 2
},
{
code: "a,b {\n}",
fixed: "a ,b {\n}",
message: messages.expectedBeforeSingleLine(),
line: 1,
column: 2
},
{
code: "a,b {\r\n}",
fixed: "a ,b {\r\n}",
description: "CRLF",
message: messages.expectedBeforeSingleLine(),
line: 1,
Expand All @@ -227,6 +254,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["never-single-line"],
fix: true,

accept: [
{
Expand All @@ -245,18 +273,21 @@ testRule(rule, {
reject: [
{
code: "a ,b {}",
fixed: "a,b {}",
message: messages.rejectedBeforeSingleLine(),
line: 1,
column: 3
},
{
code: "a ,b {\n}",
fixed: "a,b {\n}",
message: messages.rejectedBeforeSingleLine(),
line: 1,
column: 3
},
{
code: "a ,b {\r\n}",
fixed: "a,b {\r\n}",
description: "CRLF",
message: messages.rejectedBeforeSingleLine(),
line: 1,
Expand Down
34 changes: 32 additions & 2 deletions lib/rules/selector-list-comma-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const messages = ruleMessages(ruleName, {
'Unexpected whitespace before "," in a single-line list'
});

const rule = function(expectation) {
const rule = function(expectation, options, context) {
const checker = whitespaceChecker("space", expectation, messages);
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand All @@ -27,12 +27,42 @@ const rule = function(expectation) {
return;
}

let fixData;

selectorListCommaWhitespaceChecker({
root,
result,
locationChecker: checker.before,
checkedRuleName: ruleName
checkedRuleName: ruleName,
fix: context.fix
? (ruleNode, index) => {
fixData = fixData || new Map();
const commaIndices = fixData.get(ruleNode) || [];
commaIndices.push(index);
fixData.set(ruleNode, commaIndices);
return true;
}
: null
});
if (fixData) {
fixData.forEach((commaIndices, ruleNode) => {
let selector = ruleNode.selector;
commaIndices
.sort()
.reverse()
.forEach(index => {
let beforeSelector = selector.slice(0, index);
const afterSelector = selector.slice(index);
if (expectation.indexOf("always") >= 0) {
beforeSelector = beforeSelector.replace(/\s*$/, " ");
} else if (expectation.indexOf("never") >= 0) {
beforeSelector = beforeSelector.replace(/\s*$/, "");
}
selector = beforeSelector + afterSelector;
});
ruleNode.selector = selector;
});
}
};
};

Expand Down
8 changes: 6 additions & 2 deletions lib/rules/selectorListCommaWhitespaceChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ module.exports = function(opts) {
opts.locationChecker({
source,
index,
err: m =>
err: m => {
if (opts.fix && opts.fix(node, index)) {
return;
}
report({
message: m,
node,
index,
result: opts.result,
ruleName: opts.checkedRuleName
})
});
}
});
}
};