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 for at-if-else-closing-brace-space-after #228

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
42 changes: 42 additions & 0 deletions src/rules/at-else-closing-brace-space-after/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ testRule(rule, {
ruleName,
config: ["always-intermediate"],
syntax: "scss",
fix: true,

accept: [
{
Expand Down Expand Up @@ -79,6 +80,11 @@ testRule(rule, {
@if ($x == 1) { } @else ($x ==2) {

}@else {}
}`,
fixed: `a {
@if ($x == 1) { } @else ($x ==2) {

} @else {}
}`,
description:
"always-intermediate (an intermediate @else, no space after).",
Expand All @@ -91,6 +97,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) { } @else ($x ==2) {

} @else { }
}`,
description:
"always-intermediate (an intermediate @else, newline after).",
Expand All @@ -103,6 +114,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) { } @else ($x ==2) {

} @else { }
}`,
description:
"always-intermediate (an intermediate @else, a space and an newline after).",
Expand All @@ -114,6 +130,11 @@ testRule(rule, {
@if ($x == 1) { } @else ($x ==2) {

} @else { }
}`,
fixed: `a {
@if ($x == 1) { } @else ($x ==2) {

} @else { }
}`,
description:
"always-intermediate (an intermediate @else, multiple spaces after).",
Expand All @@ -128,6 +149,7 @@ testRule(rule, {
ruleName,
config: ["never-intermediate"],
syntax: "scss",
fix: true,

accept: [
{
Expand Down Expand Up @@ -193,6 +215,11 @@ testRule(rule, {
@if ($x == 1) {} @else ($x ==2) {

} @else {}
}`,
fixed: `a {
@if ($x == 1) {} @else ($x ==2) {

}@else {}
}`,
description:
"never-intermediate (an intermediate @else, has space after).",
Expand All @@ -205,6 +232,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {} @else ($x ==2) {

}@else { }
}`,
description: "never-intermediate (an intermediate @else, newline after).",
message: messages.rejected,
Expand All @@ -216,6 +248,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {} @else ($x ==2) {

}@else { }
}`,
description:
"never-intermediate (an intermediate @else, a space and a newline after).",
Expand All @@ -227,6 +264,11 @@ testRule(rule, {
@if ($x == 1) {

} @else ($x ==2) {} @else { }
}`,
fixed: `a {
@if ($x == 1) {

} @else ($x ==2) {}@else { }
}`,
description:
"never-intermediate (an intermediate @else, multiple spaces after).",
Expand Down
5 changes: 3 additions & 2 deletions src/rules/at-else-closing-brace-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const messages = utils.ruleMessages(ruleName, {
rejected: 'Unexpected space after "}" of @else statement'
});

export default function(expectation) {
export default function(expectation, _, context) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
actual: expectation,
Expand All @@ -25,7 +25,8 @@ export default function(expectation) {
ruleName,
atRuleName: "else",
expectation,
messages
messages,
context
});
};
}
42 changes: 42 additions & 0 deletions src/rules/at-if-closing-brace-space-after/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ testRule(rule, {
ruleName,
config: ["always-intermediate"],
syntax: "scss",
fix: true,

accept: [
{
Expand Down Expand Up @@ -68,6 +69,11 @@ testRule(rule, {
@if ($x == 1) {

}@else {}
}`,
fixed: `a {
@if ($x == 1) {

} @else {}
}`,
description: "always-intermediate (has @else, no space after).",
message: messages.expected,
Expand All @@ -79,6 +85,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {

} @else { }
}`,
description: "always-intermediate (has @else, newline after).",
message: messages.expected,
Expand All @@ -90,6 +101,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {

} @else { }
}`,
description:
"always-intermediate (has @else, a space and an newline after).",
Expand All @@ -101,6 +117,11 @@ testRule(rule, {
@if ($x == 1) {

} @else { }
}`,
fixed: `a {
@if ($x == 1) {

} @else { }
}`,
description: "always-intermediate (has @else, multiple spaces after).",
message: messages.expected,
Expand All @@ -114,6 +135,7 @@ testRule(rule, {
ruleName,
config: ["never-intermediate"],
syntax: "scss",
fix: true,

accept: [
{
Expand Down Expand Up @@ -176,6 +198,11 @@ testRule(rule, {
@if ($x == 1) {

} @else {}
}`,
fixed: `a {
@if ($x == 1) {

}@else {}
}`,
description: "never-intermediate (has @else, has space after).",
message: messages.rejected,
Expand All @@ -187,6 +214,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {

}@else { }
}`,
description: "never-intermediate (has @else, newline after).",
message: messages.rejected,
Expand All @@ -198,6 +230,11 @@ testRule(rule, {

}
@else { }
}`,
fixed: `a {
@if ($x == 1) {

}@else { }
}`,
description:
"never-intermediate (has @else, a space and a newline after).",
Expand All @@ -209,6 +246,11 @@ testRule(rule, {
@if ($x == 1) {

} @else { }
}`,
fixed: `a {
@if ($x == 1) {

}@else { }
}`,
description: "never-intermediate (has @else, multiple spaces after).",
message: messages.rejected,
Expand Down
19 changes: 13 additions & 6 deletions src/rules/at-if-closing-brace-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const messages = utils.ruleMessages(ruleName, {
rejected: 'Unexpected space after "}" of @if statement'
});

export default function(expectation) {
export default function(expectation, _, context) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
actual: expectation,
Expand All @@ -24,7 +24,8 @@ export default function(expectation) {
ruleName,
atRuleName: "if",
expectation,
messages
messages,
context
});
};
}
Expand All @@ -47,9 +48,15 @@ export function sassConditionalBraceSpaceAfterChecker({
ruleName,
atRuleName,
expectation,
messages
messages,
context
}) {
function complain(node, message, index) {
function complain(node, message, index, fixValue) {
if (context.fix) {
node.next().raws.before = fixValue;
return;
}

utils.report({
result,
ruleName,
Expand All @@ -74,10 +81,10 @@ export function sassConditionalBraceSpaceAfterChecker({
if (nextNode && nextNode.type === "atrule" && nextNode.name === "else") {
// A single space is needed
if (expectation === "always-intermediate" && !hasSpaceAfter) {
complain(atrule, messages.expected, reportIndex);
complain(atrule, messages.expected, reportIndex, " ");
} else if (expectation === "never-intermediate" && hasWhiteSpaceAfter) {
// No whitespace is needed
complain(atrule, messages.rejected, reportIndex);
complain(atrule, messages.rejected, reportIndex, "");
}
}
});
Expand Down