From 9e445435def6f9c2919e523a97947019c495fc76 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:46:24 +0900 Subject: [PATCH] Refactor `block-no-empty` rule --- lib/rules/block-no-empty/index.js | 32 +++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/rules/block-no-empty/index.js b/lib/rules/block-no-empty/index.js index d83120e8da..7e25e23be9 100644 --- a/lib/rules/block-no-empty/index.js +++ b/lib/rules/block-no-empty/index.js @@ -49,25 +49,17 @@ const rule = (primary, secondaryOptions) => { root.walkRules(check); root.walkAtRules(check); + /** @typedef {import('postcss').Rule | import('postcss').AtRule} Statement */ + /** - * @param {import('postcss').Rule | import('postcss').AtRule} statement + * @param {Statement} statement */ function check(statement) { if (!hasBlock(statement)) { return; } - const children = statement.nodes.filter((child) => { - if (isComment(child)) { - if (ignoreComments) return false; - - if (isStylelintCommand(child)) return false; - } - - return true; - }); - - if (children.length > 0) { + if (hasNotableChildren(statement)) { return; } @@ -86,6 +78,22 @@ const rule = (primary, secondaryOptions) => { ruleName, }); } + + /** + * @param {Statement} statement + * @returns {boolean} + */ + function hasNotableChildren(statement) { + return statement.nodes.some((child) => { + if (isComment(child)) { + if (ignoreComments) return false; + + if (isStylelintCommand(child)) return false; + } + + return true; + }); + } }; };