Skip to content

Commit

Permalink
Add autofix to declaration-colon-newline-after
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 20, 2018
1 parent 3bb26b2 commit 75bc43f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Expand Up @@ -266,7 +266,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO

- [`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-before`](../../lib/rules/declaration-bang-space-before/README.md): Require a single space or disallow whitespace before the bang of declarations.
- [`declaration-colon-newline-after`](../../lib/rules/declaration-colon-newline-after/README.md): Require a newline or disallow whitespace after the colon of declarations.
- [`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).
- [`declaration-colon-space-before`](../../lib/rules/declaration-colon-space-before/README.md): Require a single space or disallow whitespace before the colon of declarations (Autofixable).
- [`declaration-empty-line-before`](../../lib/rules/declaration-empty-line-before/README.md): Require or disallow an empty line before declarations (Autofixable).
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/declaration-colon-newline-after/README.md
Expand Up @@ -12,6 +12,8 @@ a {
* The newline after this colon */
```

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"|"always-multi-line"`
Expand Down
76 changes: 74 additions & 2 deletions lib/rules/declaration-colon-newline-after/__tests__/index.js
Expand Up @@ -6,6 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: ["always"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -46,38 +47,99 @@ testRule(rule, {
reject: [
{
code: "a { color :pink; }",
fixed: "a { color :\npink; }",
description: "no newline after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : pink; }",
fixed: "a { color :\n pink; }",
description: "two spaces after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color :\tpink; }",
fixed: "a { color :\n\tpink; }",
description: "tab after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : pink; }",
fixed: "a { color :\n pink; }",
description: "space after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : \npink; }",
fixed: "a { color :\npink; }",
description: "space and newline after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : \r\npink; }",
fixed: "a { color :\r\npink; }",
description: "space and CRLF after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : \t\n\t\npink; }",
fixed: "a { color :\n\t\npink; }",
description: "space and newline tab after",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color :/*comment*/pink; }",
fixed: "a { color :/*comment*/\npink; }",
description: "comment",
message: messages.expectedAfter(),
line: 1,
column: 22
},
{
code: "a { color : /*comment*/ pink; }",
fixed: "a { color : /*comment*/\n pink; }",
description: "comment",
message: messages.expectedAfter(),
line: 1,
column: 23
},
{
code: "a { color : \n/*comment*/ pink; }",
fixed: "a { color :\n/*comment*/ pink; }",
description: "comment before newline",
message: messages.expectedAfter(),
line: 1,
column: 11
},
{
code: "a { color : pink; }",
fixed: "a { color :\n pink; }",
description: "multi space",
message: messages.expectedAfter(),
line: 1,
column: 18
}
]
});

testRule(rule, {
ruleName,
config: ["always-multi-line"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -120,7 +182,12 @@ testRule(rule, {
" box-shadow: 0 0 0 1px #5b9dd9\n" +
" 0 0 2px 1px rgba(30, 140, 190, 0.8);\n" +
"}",

fixed:
"a {\n" +
" box-shadow:\n" +
" 0 0 0 1px #5b9dd9\n" +
" 0 0 2px 1px rgba(30, 140, 190, 0.8);\n" +
"}",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 13
Expand All @@ -131,7 +198,12 @@ testRule(rule, {
" box-shadow:0 0 0 1px #5b9dd9\n" +
" 0 0 2px 1px rgba(30, 140, 190, 0.8);\n" +
"}",

fixed:
"a {\n" +
" box-shadow:\n" +
"0 0 0 1px #5b9dd9\n" +
" 0 0 2px 1px rgba(30, 140, 190, 0.8);\n" +
"}",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 13
Expand Down
24 changes: 19 additions & 5 deletions lib/rules/declaration-colon-newline-after/index.js
Expand Up @@ -15,7 +15,7 @@ const messages = ruleMessages(ruleName, {
'Expected newline after ":" with a multi-line declaration'
});

const rule = function(expectation) {
const rule = function(expectation, options, context) {
const checker = whitespaceChecker("newline", expectation, messages);
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down Expand Up @@ -43,16 +43,30 @@ const rule = function(expectation) {
if (propPlusColon[i] !== ":") {
continue;
}
const indexToCheck =
propPlusColon.substr(propPlusColon[i], 3) === "/*"
? propPlusColon.indexOf("*/", i) + 1
: i;
const indexToCheck = /^[^\S\r\n]*\/\*/.test(propPlusColon.slice(i + 1))
? propPlusColon.indexOf("*/", i) + 1
: i;

checker.afterOneOnly({
source: propPlusColon,
index: indexToCheck,
lineCheckStr: decl.value,
err: m => {
if (context.fix) {
const between = decl.raws.between;
const betweenStart = declarationValueIndex(decl) - between.length;
const sliceIndex = indexToCheck - betweenStart + 1;
const betweenBefore = between.slice(0, sliceIndex);
const betweenAfter = between.slice(sliceIndex);
if (/^\s*\r?\n/.test(betweenAfter)) {
decl.raws.between =
betweenBefore + betweenAfter.replace(/^[^\S\r\n]*/, "");
} else {
decl.raws.between =
betweenBefore + context.newline + betweenAfter;
}
return;
}
report({
message: m,
node: decl,
Expand Down

0 comments on commit 75bc43f

Please sign in to comment.