Skip to content

Commit

Permalink
Refactor to use PostCSS Container type for more flexibility (#5708)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Nov 9, 2021
1 parent dd1938c commit b91bc26
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
20 changes: 8 additions & 12 deletions 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 }) {
Expand All @@ -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 || '';
Expand Down
5 changes: 1 addition & 4 deletions lib/utils/blockString.js
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/hasBlock.js
Expand Up @@ -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;
};

0 comments on commit b91bc26

Please sign in to comment.