Skip to content

Commit

Permalink
Add autofix for at-if/else-closing-brace-newline-after (#229)
Browse files Browse the repository at this point in the history
* Add autofix

* Add space at last @else in chain & avoid indenting

* Fix ut

* Add disableFix option
  • Loading branch information
OriR authored and kristerkari committed Mar 31, 2018
1 parent c9aaa2c commit d1476d6
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 16 deletions.
168 changes: 168 additions & 0 deletions src/rules/at-else-closing-brace-newline-after/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ testRule(rule, {
ruleName,
config: ["always-last-in-chain"],
syntax: "scss",
fix: true,

accept: [
{
Expand Down Expand Up @@ -67,6 +68,155 @@ testRule(rule, {
} @else {
} width: 10px;
}`,
fixed: `a {
@if ($x == 1) {
} @else {
}
width: 10px;
}`,
description:
"always-last-in-chain (has decl on the same line as its closing brace).",
message: messages.expected,
line: 6
},
{
code: `a {
@if ($x == 1) {
} @else if { }
@else { }
}`,
fixed: `a {
@if ($x == 1) {
} @else if { } @else { }
}`,
description: "always-last-in-chain (has following @else, newline after).",
message: messages.rejected,
line: 4
},
{
code: `a {
@if ($x == 1) {
} @else { }
@else { }
}`,
fixed: `a {
@if ($x == 1) {
} @else { } @else { }
}`,
description:
"always-last-in-chain (has following @else, empty line after).",
message: messages.rejected,
line: 4
},
{
code: `a {
@if ($x == 1) {
} @else {} @include x;
}`,
fixed: `a {
@if ($x == 1) {
} @else {}
@include x;
}`,
description:
"always-last-in-chain (followed by non-@else at-rule, no newline after).",
message: messages.expected,
line: 4
}
]
});

testRule(rule, {
ruleName,
config: [
"always-last-in-chain",
{
disableFix: true
}
],
syntax: "scss",
fix: true,

accept: [
{
code: `a {
@if ($x == 1) {} @else {}
width: 10px;
}`,
description:
"always-last-in-chain (no following @else, has newline after)."
},
{
code: `a {
@if ($x == 1) {} @else {}
width: 10px;
}`,
description:
"always-last-in-chain (no following @else, has blank line after)."
},
{
code: `a {
@if ($x == 1) {
} @else if {
} @else {}
width: 10px;
}`,
description:
"always-last-in-chain (has following @else, this one with no newline after, the following has it)."
},
{
code: `a {
@if ($x == 1) { } @else if { } @else { }
width: 10px;
}`,
description:
"always-last-in-chain (has following @else, single-line, this one with no newline after, the following has it)."
},
{
code: `a {
@if ($x == 1) { } @else { }
@include x;
}`,
description:
"always-last-in-chain (followed by non-@else at-rule, single-line, empty line after)."
},
{
code: "@if ($x == 1) {} @else { }",
description: "always-last-in-chain (single line, nothing after)."
}
],

reject: [
{
code: `a {
@if ($x == 1) {
} @else {
} width: 10px;
}`,
fixed: `a {
@if ($x == 1) {
} @else {
} width: 10px;
}`,
description:
Expand All @@ -78,6 +228,12 @@ testRule(rule, {
code: `a {
@if ($x == 1) {
} @else if { }
@else { }
}`,
fixed: `a {
@if ($x == 1) {
} @else if { }
@else { }
}`,
Expand All @@ -91,6 +247,13 @@ testRule(rule, {
} @else { }
@else { }
}`,
fixed: `a {
@if ($x == 1) {
} @else { }
@else { }
}`,
description:
Expand All @@ -102,6 +265,11 @@ testRule(rule, {
code: `a {
@if ($x == 1) {
} @else {} @include x;
}`,
fixed: `a {
@if ($x == 1) {
} @else {} @include x;
}`,
description:
Expand Down
26 changes: 20 additions & 6 deletions src/rules/at-else-closing-brace-newline-after/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { namespace } from "../../utils";
import { utils } from "stylelint";
import { isBoolean } from "lodash";

export const ruleName = namespace("at-else-closing-brace-newline-after");

Expand All @@ -10,12 +11,23 @@ export const messages = utils.ruleMessages(ruleName, {

import { sassConditionalBraceNLAfterChecker } from "../at-if-closing-brace-newline-after";

export default function(expectation) {
export default function(expectation, options, context) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
actual: expectation,
possible: ["always-last-in-chain"]
});
const validOptions = utils.validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ["always-last-in-chain"]
},
{
actual: options,
possible: {
disableFix: isBoolean
},
optional: true
}
);
if (!validOptions) {
return;
}
Expand All @@ -26,7 +38,9 @@ export default function(expectation) {
ruleName,
atRuleName: "else",
expectation,
messages
messages,
context,
options
});
};
}
Loading

0 comments on commit d1476d6

Please sign in to comment.