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

Refactor to use PostCSS Container type for more flexibility #5708

Merged
merged 1 commit into from Nov 9, 2021
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
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;
};