Skip to content

Commit

Permalink
Add autofix to media-feature-range-operator-space-after (#3639)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored and jeddy3 committed Sep 9, 2018
1 parent bec670c commit 1545572
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
- [`media-feature-colon-space-before`](../../lib/rules/media-feature-colon-space-before/README.md): Require a single space or disallow whitespace before the colon in media features (Autofixable).
- [`media-feature-name-case`](../../lib/rules/media-feature-name-case/README.md): Specify lowercase or uppercase for media feature names.
- [`media-feature-parentheses-space-inside`](../../lib/rules/media-feature-parentheses-space-inside/README.md): Require a single space or disallow whitespace on the inside of the parentheses within media features.
- [`media-feature-range-operator-space-after`](../../lib/rules/media-feature-range-operator-space-after/README.md): Require a single space or disallow whitespace after the range operator in media features.
- [`media-feature-range-operator-space-after`](../../lib/rules/media-feature-range-operator-space-after/README.md): Require a single space or disallow whitespace after the range operator in media features (Autofixable).
- [`media-feature-range-operator-space-before`](../../lib/rules/media-feature-range-operator-space-before/README.md): Require a single space or disallow whitespace before the range operator in media features (Autofixable).

#### Media query list
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/media-feature-range-operator-space-after/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 after the range operator in media
* The space after this */
```

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"`
Expand Down
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 @@ -37,65 +38,89 @@ testRule(rule, {
reject: [
{
code: "@media (width<600px) {}",
fixed: "@media (width< 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@mEdIa (width<600px) {}",
fixed: "@mEdIa (width< 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@MEDIA (width<600px) {}",
fixed: "@MEDIA (width< 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@media (width<= 600px) {}",
fixed: "@media (width<= 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width=\t600px) {}",
fixed: "@media (width= 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@media (width>\n600px) {}",
fixed: "@media (width> 600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@media (width>\r\n600px) {}",
fixed: "@media (width> 600px) {}",
description: "CRLF",
message: messages.expectedAfter(),
line: 1,
column: 15
},
{
code: "@media (width>=600px) and (width< 3em) {}",
fixed: "@media (width>= 600px) and (width< 3em) {}",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width> 600px) and (width=3em) {}",
fixed: "@media (width> 600px) and (width= 3em) {}",
message: messages.expectedAfter(),
line: 1,
column: 34
},
{
code: "@media (width>=600px) and (width<3em) {}",
fixed: "@media (width>= 600px) and (width< 3em) {}",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width</**/600px) {}",
fixed: "@media (width< /**/600px) {}",
message: messages.expectedAfter(),
line: 1,
column: 15
}
]
});

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

accept: [
{
Expand Down Expand Up @@ -127,58 +152,81 @@ testRule(rule, {
reject: [
{
code: "@media (width < 600px) {}",
fixed: "@media (width <600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@mEdIa (width < 600px) {}",
fixed: "@mEdIa (width <600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@MEDIA (width < 600px) {}",
fixed: "@MEDIA (width <600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width <= 600px) {}",
fixed: "@media (width <=600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 17
},
{
code: "@media (width =\t600px) {}",
fixed: "@media (width =600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width >\n600px) {}",
fixed: "@media (width >600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width >\r\n600px) {}",
fixed: "@media (width >600px) {}",
description: "CRLF",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "@media (width >= 600px) and (width <3em) {}",
fixed: "@media (width >=600px) and (width <3em) {}",
message: messages.rejectedAfter(),
line: 1,
column: 17
},
{
code: "@media (width >600px) and (width = 3em) {}",
fixed: "@media (width >600px) and (width =3em) {}",
message: messages.rejectedAfter(),
line: 1,
column: 35
},
{
code: "@media (width >= 600px) and (width < 3em) {}",
fixed: "@media (width >=600px) and (width <3em) {}",
message: messages.rejectedAfter(),
line: 1,
column: 17
},
{
code: "@media (width = /**/ 600px) {}",
fixed: "@media (width =/**/ 600px) {}",
message: messages.rejectedAfter(),
line: 1,
column: 16
}
]
});
34 changes: 31 additions & 3 deletions lib/rules/media-feature-range-operator-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const messages = ruleMessages(ruleName, {
rejectedAfter: () => "Unexpected whitespace after range operator"
});

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 @@ -26,16 +26,44 @@ const rule = function(expectation) {
}

root.walkAtRules(/^media$/i, atRule => {
findMediaOperator(atRule, checkAfterOperator);
const fixOperatorIndices = [];
const fix = context.fix ? index => fixOperatorIndices.push(index) : null;
findMediaOperator(atRule, (match, params, node) => {
checkAfterOperator(match, params, node, fix);
});

if (fixOperatorIndices.length) {
let params = atRule.raws.params
? atRule.raws.params.raw
: atRule.params;
fixOperatorIndices.sort((a, b) => b - a).forEach(index => {
const beforeOperator = params.slice(0, index + 1);
const afterOperator = params.slice(index + 1);
if (expectation === "always") {
params = beforeOperator + afterOperator.replace(/^\s*/, " ");
} else if (expectation === "never") {
params = beforeOperator + afterOperator.replace(/^\s*/, "");
}
});
if (atRule.raws.params) {
atRule.raws.params.raw = params;
} else {
atRule.params = params;
}
}
});

function checkAfterOperator(match, params, node) {
function checkAfterOperator(match, params, node, fix) {
const endIndex = match.startIndex + match.target.length - 1;

checker.after({
source: params,
index: endIndex,
err: m => {
if (fix) {
fix(endIndex);
return;
}
report({
message: m,
node,
Expand Down

0 comments on commit 1545572

Please sign in to comment.