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 block-closing-brace-newline-after #3443

Merged
merged 2 commits into from Jul 21, 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
Expand Up @@ -282,7 +282,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
#### Block

- [`block-closing-brace-empty-line-before`](../../lib/rules/block-closing-brace-empty-line-before/README.md): Require or disallow an empty line before the closing brace of blocks.
- [`block-closing-brace-newline-after`](../../lib/rules/block-closing-brace-newline-after/README.md): Require a newline or disallow whitespace after the closing brace of blocks.
- [`block-closing-brace-newline-after`](../../lib/rules/block-closing-brace-newline-after/README.md): Require a newline or disallow whitespace after the closing brace of blocks (Autofixable).
- [`block-closing-brace-newline-before`](../../lib/rules/block-closing-brace-newline-before/README.md): Require a newline or disallow whitespace before the closing brace of blocks.
- [`block-closing-brace-space-after`](../../lib/rules/block-closing-brace-space-after/README.md): Require a single space or disallow whitespace after the closing brace of blocks.
- [`block-closing-brace-space-before`](../../lib/rules/block-closing-brace-space-before/README.md): Require a single space or disallow whitespace before the closing brace of blocks.
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/block-closing-brace-newline-after/README.md
Expand Up @@ -29,6 +29,8 @@ This rule allows a trailing semicolon after the closing brace of a block. For ex
}
```

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-single-line"|"never-single-line"|"always-multi-line"|"never-multi-line"`
Expand Down
51 changes: 51 additions & 0 deletions lib/rules/block-closing-brace-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 @@ -72,62 +73,74 @@ testRule(rule, {
reject: [
{
code: "a { color: pink; }b { color: red; }",
fixed: "a { color: pink; }\nb { color: red; }",
message: messages.expectedAfter(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }\n b { color: red; }",
message: messages.expectedAfter(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }\n b { color: red; }",
message: messages.expectedAfter(),
line: 1,
column: 19
},
{
code: "a { color: pink; }\tb { color: red; }",
fixed: "a { color: pink; }\n\tb { color: red; }",
message: messages.expectedAfter(),
line: 1,
column: 19
},
{
code: "@media print { a { color: pink; } b { color: red; }}",
fixed: "@media print { a { color: pink; }\n b { color: red; }}",
message: messages.expectedAfter(),
line: 1,
column: 34
},
{
code:
"@media print { a { color: pink; }} @media screen { b { color: red; }}",
fixed:
"@media print { a { color: pink; }}\n @media screen { b { color: red; }}",
message: messages.expectedAfter(),
line: 1,
column: 35
},
{
code: ".a {} /* stylelint-disable-line block-no-empty */ b {}",
fixed: ".a {} /* stylelint-disable-line block-no-empty */\n b {}",
message: messages.expectedAfter(),
line: 1,
column: 6
},
{
code: ":root {\n --x { color: pink; } ; \n --y { color: red; };\n }",
fixed: ":root {\n --x { color: pink; } ;\n --y { color: red; };\n }",
message: messages.expectedAfter(),
line: 2,
column: 24
},
{
code: ":root {\n --x { color: pink; }; \n --y { color: red; };\n }",
fixed: ":root {\n --x { color: pink; };\n --y { color: red; };\n }",
message: messages.expectedAfter(),
line: 2,
column: 23
},
{
code:
".foo {\n --my-theme: { color: red; }; \n --toolbar-theme: { color: green; };\n }",
fixed:
".foo {\n --my-theme: { color: red; };\n --toolbar-theme: { color: green; };\n }",
description:
"Make sure trailing semicolon works well for blocks outside :root",
message: messages.expectedAfter(),
Expand All @@ -140,6 +153,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["always", { ignoreAtRules: ["if", "else"] }],
fix: true,

accept: [
{
Expand All @@ -160,6 +174,7 @@ testRule(rule, {
reject: [
{
code: "a { color: pink; }b{}",
fixed: "a { color: pink; }\nb{}",
message: messages.expectedAfter(),
line: 1,
column: 19
Expand All @@ -170,6 +185,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["always", { ignoreAtRules: "/if/" }],
fix: true,

accept: [
{
Expand All @@ -183,6 +199,7 @@ testRule(rule, {
reject: [
{
code: "a { color: pink; }b{}",
fixed: "a { color: pink; }\nb{}",
message: messages.expectedAfter(),
line: 1,
column: 19
Expand All @@ -193,6 +210,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["always-single-line"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -234,37 +252,44 @@ testRule(rule, {
reject: [
{
code: "a { color: pink; }b { color: red; }",
fixed: "a { color: pink; }\nb { color: red; }",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }\n b { color: red; }",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }\n b { color: red; }",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; }\tb { color: red; }",
fixed: "a { color: pink; }\n\tb { color: red; }",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "@media print { a { color: pink; } b { color: red; }}",
fixed: "@media print { a { color: pink; }\n b { color: red; }}",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 34
},
{
code:
"@media print { a { color: pink; }} @media screen { b { color: red; }}",
fixed:
"@media print { a { color: pink; }}\n @media screen { b { color: red; }}",
message: messages.expectedAfterSingleLine(),
line: 1,
column: 35
Expand All @@ -275,6 +300,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["never-single-line"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -304,37 +330,44 @@ testRule(rule, {
reject: [
{
code: "a { color: pink; }\nb { color: red; }",
fixed: "a { color: pink; }b { color: red; }",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }b { color: red; }",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; } b { color: red; }",
fixed: "a { color: pink; }b { color: red; }",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "a { color: pink; }\tb { color: red; }",
fixed: "a { color: pink; }b { color: red; }",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 19
},
{
code: "@media print { a { color: pink; }\nb { color: red; }}",
fixed: "@media print { a { color: pink; }b { color: red; }}",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 34
},
{
code:
"@media print { a { color: pink; }}\n @media screen { b { color: red; }}",
fixed:
"@media print { a { color: pink; }}@media screen { b { color: red; }}",
message: messages.rejectedAfterSingleLine(),
line: 1,
column: 35
Expand All @@ -345,6 +378,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["always-multi-line"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -378,44 +412,52 @@ testRule(rule, {
reject: [
{
code: "a { color: pink;\ntop: 0; }b { color: red; }",
fixed: "a { color: pink;\ntop: 0; }\nb { color: red; }",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\r\ntop: 0; }b { color: red; }",
fixed: "a { color: pink;\r\ntop: 0; }\r\nb { color: red; }",
description: "CRLF",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; } b { color: red; }",
fixed: "a { color: pink;\ntop: 0; }\n b { color: red; }",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; } b { color: red; }",
fixed: "a { color: pink;\ntop: 0; }\n b { color: red; }",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; }\tb { color: red; }",
fixed: "a { color: pink;\ntop: 0; }\n\tb { color: red; }",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "@media print { a {\ncolor: pink; } b { color: red; }}",
fixed: "@media print { a {\ncolor: pink; }\n b { color: red; }}",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 15
},
{
code:
"@media print { a {\ncolor: pink; }} @media screen { b {\ncolor: red; }}",
fixed:
"@media print { a {\ncolor: pink; }}\n @media screen { b {\ncolor: red; }}",
message: messages.expectedAfterMultiLine(),
line: 2,
column: 16
Expand All @@ -426,6 +468,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["never-multi-line"],
fix: true,

accept: [
{
Expand Down Expand Up @@ -468,44 +511,52 @@ testRule(rule, {
reject: [
{
code: "a { color: pink;\ntop: 0; }\nb { color: red; }",
fixed: "a { color: pink;\ntop: 0; }b { color: red; }",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\r\ntop: 0; }\r\nb { color: red; }",
fixed: "a { color: pink;\r\ntop: 0; }b { color: red; }",
description: "CRLF",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; } b { color: red; }",
fixed: "a { color: pink;\ntop: 0; }b { color: red; }",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; } b { color: red; }",
fixed: "a { color: pink;\ntop: 0; }b { color: red; }",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "a { color: pink;\ntop: 0; }\tb { color: red; }",
fixed: "a { color: pink;\ntop: 0; }b { color: red; }",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 10
},
{
code: "@media print { a {\ncolor: pink; }\nb { color: red; }}",
fixed: "@media print { a {\ncolor: pink; }b { color: red; }}",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 15
},
{
code:
"@media print { a {\ncolor: pink; }}\n@media screen { b {\ncolor: red; }}",
fixed:
"@media print { a {\ncolor: pink; }}@media screen { b {\ncolor: red; }}",
message: messages.rejectedAfterMultiLine(),
line: 2,
column: 16
Expand Down
18 changes: 17 additions & 1 deletion lib/rules/block-closing-brace-newline-after/index.js
Expand Up @@ -24,7 +24,7 @@ const messages = ruleMessages(ruleName, {
'Unexpected whitespace after "}" of a multi-line block'
});

const rule = function(expectation, options) {
const rule = function(expectation, options, context) {
const checker = whitespaceChecker("newline", expectation, messages);
return (root, result) => {
const validOptions = validateOptions(
Expand Down Expand Up @@ -99,6 +99,22 @@ const rule = function(expectation, options) {
index: -1,
lineCheckStr: blockString(statement),
err: msg => {
if (context.fix) {
if (expectation.indexOf("always") === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Way not just use:

if (expectation === "always") {

Copy link
Member

Choose a reason for hiding this comment

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

@gucong3000 It matches always, always-single-line, always-multi-line.

const index = nodeToCheck.raws.before.search(/\r?\n/);
if (index >= 0) {
nodeToCheck.raws.before = nodeToCheck.raws.before.slice(index);
} else {
nodeToCheck.raws.before =
context.newline + nodeToCheck.raws.before;
}
return;
} else if (expectation.indexOf("never") === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Way not just use:

if (expectation === "never") {

Copy link
Member

Choose a reason for hiding this comment

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

@gucong3000 It matches never-single-line, never-multi-line.

nodeToCheck.raws.before = "";
return;
}
}

report({
message: msg,
node: statement,
Expand Down