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 declaration-colon-newline-after #3588

Merged
merged 2 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -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 (Autofixable).
- [`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
Original file line number Diff line number Diff line change
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
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 @@ -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
Original file line number Diff line number Diff line change
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