Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/rules/no-empty-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @import { CSSRuleDefinition } from "../types.js"
* @typedef {"emptyBlock"} NoEmptyBlocksMessageIds
* @typedef {"emptyBlock" | "removeRule" | "convertToStatement"} NoEmptyBlocksMessageIds
* @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: NoEmptyBlocksMessageIds }>} NoEmptyBlocksRuleDefinition
*/

Expand All @@ -21,6 +21,8 @@ export default /** @satisfies {NoEmptyBlocksRuleDefinition} */ ({
meta: {
type: "problem",

hasSuggestions: true,

docs: {
description: "Disallow empty blocks",
recommended: true,
Expand All @@ -29,16 +31,38 @@ export default /** @satisfies {NoEmptyBlocksRuleDefinition} */ ({

messages: {
emptyBlock: "Unexpected empty block found.",
removeRule: "Remove the empty rule.",
convertToStatement: "Convert to layer statement.",
},
},

create(context) {
return {
Block(node) {
if (node.children.length === 0) {
const parent = context.sourceCode.getParent(node);
const isNamedAtLayer =
parent.type === "Atrule" &&
parent.name === "layer" &&
parent.prelude;

context.report({
loc: node.loc,
messageId: "emptyBlock",
suggest: isNamedAtLayer
? [
{
messageId: "convertToStatement",
fix: fixer =>
fixer.replaceText(node, ";"),
},
]
: [
{
messageId: "removeRule",
fix: fixer => fixer.remove(parent),
},
],
});
}
},
Expand Down
143 changes: 143 additions & 0 deletions tests/rules/no-empty-blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ruleTester.run("no-empty-blocks", rule, {
column: 3,
endLine: 1,
endColumn: 6,
suggestions: [{ messageId: "removeRule", output: "" }],
},
],
},
Expand All @@ -46,6 +47,7 @@ ruleTester.run("no-empty-blocks", rule, {
column: 3,
endLine: 1,
endColumn: 20,
suggestions: [{ messageId: "removeRule", output: "" }],
},
],
},
Expand All @@ -58,6 +60,7 @@ ruleTester.run("no-empty-blocks", rule, {
column: 3,
endLine: 2,
endColumn: 2,
suggestions: [{ messageId: "removeRule", output: "" }],
},
],
},
Expand All @@ -70,6 +73,7 @@ ruleTester.run("no-empty-blocks", rule, {
column: 3,
endLine: 2,
endColumn: 3,
suggestions: [{ messageId: "removeRule", output: "" }],
},
],
},
Expand All @@ -82,6 +86,12 @@ ruleTester.run("no-empty-blocks", rule, {
column: 14,
endLine: 1,
endColumn: 17,
suggestions: [
{
messageId: "removeRule",
output: "",
},
],
},
],
},
Expand All @@ -94,13 +104,146 @@ ruleTester.run("no-empty-blocks", rule, {
column: 3,
endLine: 1,
endColumn: 6,
suggestions: [
{
messageId: "removeRule",
output: "\n@media print { \nb { } \n}", // only a{} was removed
},
],
},
{
messageId: "emptyBlock",
line: 3,
column: 3,
endLine: 3,
endColumn: 6,
suggestions: [
{
messageId: "removeRule",
output: "a { }\n@media print { \n \n}", // only b{} was removed
},
],
},
],
},
{
code: "@layer defaults { \n }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 17,
endLine: 2,
endColumn: 3,
suggestions: [
{
messageId: "convertToStatement",
output: "@layer defaults ;",
},
],
},
],
},
{
code: "@layer a, b { \n }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 13,
endLine: 2,
endColumn: 3,
suggestions: [
{
messageId: "convertToStatement",
output: "@layer a, b ;",
},
],
},
],
},
{
code: "@layer { \n }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 8,
endLine: 2,
endColumn: 3,
suggestions: [
{
messageId: "removeRule",
output: "",
},
],
},
],
},
{
code: "@supports not selector(h2 > p) { }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 32,
endLine: 1,
endColumn: 35,
suggestions: [
{
messageId: "removeRule",
output: "",
},
],
},
],
},
{
code: "@keyframes a { 0% {} 100% {} }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 19,
endLine: 1,
endColumn: 21,
suggestions: [
{
messageId: "removeRule",
output: "@keyframes a { 100% {} }",
},
],
},
{
messageId: "emptyBlock",
line: 1,
column: 27,
endLine: 1,
endColumn: 29,
suggestions: [
{
messageId: "removeRule",
output: "@keyframes a { 0% {} }",
},
],
},
],
},
{
code: "@keyframes a { }",
errors: [
{
messageId: "emptyBlock",
line: 1,
column: 14,
endLine: 1,
endColumn: 17,
suggestions: [
{
messageId: "removeRule",
output: "",
},
],
},
],
},
Expand Down
Loading