From 262943880f2b56d4ce3dd6ec5238376b28bfdb10 Mon Sep 17 00:00:00 2001 From: Ido Rosenthal Date: Wed, 12 Jun 2024 09:57:13 +0300 Subject: [PATCH] refactor: extract atrule separation vars --- packages/code-formatter/src/format-css.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/code-formatter/src/format-css.ts b/packages/code-formatter/src/format-css.ts index a0408899a..ab6cae1a9 100644 --- a/packages/code-formatter/src/format-css.ts +++ b/packages/code-formatter/src/format-css.ts @@ -172,19 +172,21 @@ function formatAst(ast: AnyNode, index: number, options: FormatOptions) { ast.value ||= ' '; // minimal space } } else if (ast.type === 'atrule') { - const prevType = ast.prev()?.type; + const prevNode = ast.prev(); + const prevType = prevNode?.type; + const childrenLen = ast.nodes?.length ?? -1; + const hasNestedChildren = childrenLen === -1; const hasCommentBefore = prevType === 'comment'; const hasRuleBefore = prevType === 'rule'; - /* The postcss type does not represent the reality there are atRules without nodes */ - const childrenLen = ast.nodes?.length ?? -1; const separation = - (childrenLen === -1 && !hasRuleBefore) || hasCommentBefore ? 0 : linesBetween; + hasCommentBefore || (hasNestedChildren && !hasRuleBefore) ? 0 : linesBetween; ast.raws.before = index !== 0 || indentLevel > 0 ? NL.repeat(index !== 0 ? separation + 1 : 1) + indent.repeat(indentLevel) : ''; + ast.raws.after = childrenLen ? NL + indent.repeat(indentLevel) : ''; ast.raws.afterName = ast.params.length ? enforceOneSpaceAround(ast.raws.afterName || '')