Skip to content

Commit

Permalink
Add autofix to declaration-bang-space-after (#3598)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored and jeddy3 committed Aug 29, 2018
1 parent 1ff68f9 commit 6b4ed62
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO

#### Declaration

- [`declaration-bang-space-after`](../../lib/rules/declaration-bang-space-after/README.md): Require a single space or disallow whitespace after the bang of declarations.
- [`declaration-bang-space-after`](../../lib/rules/declaration-bang-space-after/README.md): Require a single space or disallow whitespace after the bang of declarations (Autofixable).
- [`declaration-bang-space-before`](../../lib/rules/declaration-bang-space-before/README.md): Require a single space or disallow whitespace before the bang of declarations (Autofixable).
- [`declaration-colon-newline-after`](../../lib/rules/declaration-colon-newline-after/README.md): Require a newline or disallow whitespace after the colon of declarations (Autofixable).
- [`declaration-colon-space-after`](../../lib/rules/declaration-colon-space-after/README.md): Require a single space or disallow whitespace after the colon of declarations (Autofixable).
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/declaration-bang-space-after/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ a { color: pink !important; }
* The space after this exclamation mark */
```

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
41 changes: 41 additions & 0 deletions lib/rules/declaration-bang-space-after/__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 @@ -41,38 +42,59 @@ testRule(rule, {
reject: [
{
code: "a { color: pink!important; }",
fixed: "a { color: pink! important; }",
description: "no space after",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink! global; }",
fixed: "a { color: pink! global; }",
description: "two spaces after",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink!\nimportant; }",
fixed: "a { color: pink! important; }",
description: "newline after",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink!\r\nexciting; }",
fixed: "a { color: pink! exciting; }",
description: "CRLF after",
message: messages.expectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink !/*comment*/important; }",
fixed: "a { color: pink ! /*comment*/important; }",
description: "comment after",
message: messages.expectedAfter(),
line: 1,
column: 17
},
{
code: "a { color: pink !/*comment*/global; }",
fixed: "a { color: pink ! /*comment*/global; }",
description: "comment after",
message: messages.expectedAfter(),
line: 1,
column: 17
}
]
});

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

accept: [
{
Expand Down Expand Up @@ -100,24 +122,43 @@ testRule(rule, {
reject: [
{
code: "a { color: pink! important; }",
fixed: "a { color: pink!important; }",
description: "space after",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink!\nimportant; }",
fixed: "a { color: pink!important; }",
description: "newline after",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink!\r\nimportant; }",
fixed: "a { color: pink!important; }",
description: "CRLF after",
message: messages.rejectedAfter(),
line: 1,
column: 16
},
{
code: "a { color: pink ! /*comment*/important; }",
fixed: "a { color: pink !/*comment*/important; }",
description: "comment after",
message: messages.rejectedAfter(),
line: 1,
column: 17
},
{
code: "a { color: pink ! /*comment*/global; }",
fixed: "a { color: pink !/*comment*/global; }",
description: "comment after",
message: messages.rejectedAfter(),
line: 1,
column: 17
}
]
});
42 changes: 40 additions & 2 deletions lib/rules/declaration-bang-space-after/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const declarationBangSpaceChecker = require("../declarationBangSpaceChecker");
const declarationValueIndex = require("../../utils/declarationValueIndex");
const ruleMessages = require("../../utils/ruleMessages");
const validateOptions = require("../../utils/validateOptions");
const whitespaceChecker = require("../../utils/whitespaceChecker");
Expand All @@ -12,7 +13,7 @@ const messages = ruleMessages(ruleName, {
rejectedAfter: () => 'Unexpected whitespace after "!"'
});

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,7 +28,44 @@ const rule = function(expectation) {
root,
result,
locationChecker: checker.after,
checkedRuleName: ruleName
checkedRuleName: ruleName,
fix: context.fix
? (decl, index) => {
let bangIndex = index - declarationValueIndex(decl);
const value = decl.raws.value ? decl.raws.value.raw : decl.value;
let target;
let setFixed;
if (bangIndex < value.length) {
target = value;
setFixed = value => {
if (decl.raws.value) {
decl.raws.value.raw = value;
} else {
decl.value = value;
}
};
} else if (decl.important) {
target = decl.raws.important || " !important";
bangIndex -= value.length;
setFixed = value => {
decl.raws.important = value;
};
} else {
return false; // not standard
}

const targetBefore = target.slice(0, bangIndex + 1);
const targetAfter = target.slice(bangIndex + 1);

if (expectation === "always") {
setFixed(targetBefore + targetAfter.replace(/^\s*/, " "));
return true;
} else if (expectation === "never") {
setFixed(targetBefore + targetAfter.replace(/^\s*/, ""));
return true;
}
}
: null
});
};
};
Expand Down

0 comments on commit 6b4ed62

Please sign in to comment.