'use strict';

/** @typedef {import('postcss').Rule} Rule */
/** @typedef {import('postcss').AtRule} AtRule */

/**
 * @param {Rule | AtRule} statement
 * @param {{
 * 	noRawBefore?: boolean
 * }} options
 *
 * @returns {string}
 */
module.exports = function (statement, options = {}) {
	let result = '';
	/** @type {Rule | undefined} */
	let rule; /*?: postcss$rule*/
	/** @type {AtRule | undefined} */
	let atRule; /*?: postcss$atRule*/

	if (statement.type === 'rule') {
		rule = statement;
	}

	if (statement.type === 'atrule') {
		atRule = statement;
	}

	if (!rule && !atRule) {
		return result;
	}

	const before = statement.raws.before || '';

	if (!options.noRawBefore) {
		result += before;
	}

	if (rule) {
		result += rule.selector;
	}

	if (atRule) {
		result += `@${atRule.name}${atRule.raws.afterName || ''}${atRule.params}`;
	}

	const between = statement.raws.between;

	if (between !== undefined) {
		result += between;
	}

	return result;
};

@@ -153,456 +153,145 @@
  * @
-param %7B%7B%0A * %09noRawBefore?: boolean%0A * %7D%7D options%0A *%0A * @returns %7Bstring%7D%0A */%0Amodule.exports = function (statement, options = %7B%7D) %7B%0A%09let result = '';%0A%09/** @type %7BRule %7C undefined%7D */%0A%09let rule; /*?: postcss$rule*/%0A%09/** @type %7BAtRule %7C undefined%7D */%0A%09let atRule; /*?: postcss$atRule*/%0A%0A%09if (statement.type === 'rule') %7B%0A%09%09rule = statement;%0A%09%7D%0A%0A%09if (statement.type === 'atrule') %7B%0A%09%09atRule = statement;%0A%09%7D%0A%0A%09if (!rule && !atRule) %7B%0A%09%09return
+returns %7Bstring%7D%0A */%0Amodule.exports = function beforeBlockString(statement, %7B noRawBefore %7D = %7B noRawBefore: false %7D) %7B%0A%09let
  result
-;%0A%09%7D
+ = '';
 %0A%0A%09c
@@ -344,16 +344,8 @@
 f (!
-options.
 noRa
@@ -384,20 +384,50 @@
 %7D%0A%0A%09
-if (rule) %7B%0A
+switch (statement.type) %7B%0A%09%09case 'rule':%0A%09
 %09%09re
@@ -434,20 +434,25 @@
 sult += 
-rule
+statement
 .selecto
@@ -459,26 +459,35 @@
 r;%0A%09
-%7D%0A%0A%09if (atRule) %7B%0A
+%09%09break;%0A%09%09case 'atrule':%0A%09
 %09%09re
@@ -502,28 +502,34 @@
 %60@$%7B
-atRule.name%7D$%7BatRule
+statement.name%7D$%7Bstatement
 .raw
@@ -552,14 +552,17 @@
 '%7D$%7B
-atRule
+statement
 .par
@@ -573,106 +573,86 @@
 %60;%0A%09
-%7D%0A%0A%09const between = statement.raws.between;%0A%0A%09if (between !== undefined) %7B%0A%09%09result += between;%0A%09%7D
+%09%09break;%0A%09%09default:%0A%09%09%09return '';%0A%09%7D%0A%0A%09result += statement.raws.between %7C%7C '';
 %0A%0A%09r

Refactor `lib/utils/beforeBlockString.js` (#5425)