From 7e4a664808e295bf547868ec6d1e5aafd4512ba7 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Tue, 9 Nov 2021 08:58:03 +0900 Subject: [PATCH] Refactor to use PostCSS `Container` type for more flexibility --- lib/utils/beforeBlockString.js | 20 ++++++++------------ lib/utils/blockString.js | 5 +---- lib/utils/hasBlock.js | 4 ++-- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/utils/beforeBlockString.js b/lib/utils/beforeBlockString.js index b17b856abb..3ae78306b9 100644 --- a/lib/utils/beforeBlockString.js +++ b/lib/utils/beforeBlockString.js @@ -1,10 +1,9 @@ 'use strict'; -/** @typedef {import('postcss').Rule} Rule */ -/** @typedef {import('postcss').AtRule} AtRule */ +const { isAtRule, isRule } = require('./typeGuards'); /** - * @param {Rule | AtRule} statement + * @param {import('postcss').Container} statement * @returns {string} */ module.exports = function beforeBlockString(statement, { noRawBefore } = { noRawBefore: false }) { @@ -16,15 +15,12 @@ module.exports = function beforeBlockString(statement, { noRawBefore } = { noRaw result += before; } - switch (statement.type) { - case 'rule': - result += statement.selector; - break; - case 'atrule': - result += `@${statement.name}${statement.raws.afterName || ''}${statement.params}`; - break; - default: - return ''; + if (isRule(statement)) { + result += statement.selector; + } else if (isAtRule(statement)) { + result += `@${statement.name}${statement.raws.afterName || ''}${statement.params}`; + } else { + return ''; } result += statement.raws.between || ''; diff --git a/lib/utils/blockString.js b/lib/utils/blockString.js index 5a96442513..6ebd81a411 100644 --- a/lib/utils/blockString.js +++ b/lib/utils/blockString.js @@ -4,15 +4,12 @@ const beforeBlockString = require('./beforeBlockString'); const hasBlock = require('./hasBlock'); const rawNodeString = require('./rawNodeString'); -/** @typedef {import('postcss').Rule} Rule */ -/** @typedef {import('postcss').AtRule} AtRule */ - /** * Return a CSS statement's block -- the string that starts and `{` and ends with `}`. * * If the statement has no block (e.g. `@import url(foo.css);`), returns an empty string. * - * @param {Rule | AtRule} statement - postcss rule or at-rule node + * @param {import('postcss').Container} statement * @returns {string} */ module.exports = function blockString(statement) { diff --git a/lib/utils/hasBlock.js b/lib/utils/hasBlock.js index 0873ec3a29..09e3b1f217 100644 --- a/lib/utils/hasBlock.js +++ b/lib/utils/hasBlock.js @@ -3,9 +3,9 @@ /** * Check if a statement has an block (empty or otherwise). * - * @param {import('postcss').Rule | import('postcss').AtRule} statement - postcss rule or at-rule node + * @param {import('postcss').Container} statement * @return {boolean} True if `statement` has a block (empty or otherwise) */ -module.exports = function (statement) { +module.exports = function hasBlock(statement) { return statement.nodes !== undefined; };