Skip to content

Commit

Permalink
refactor: extract atrule separation vars
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros committed Jun 12, 2024
1 parent a7facdc commit 2629438
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/code-formatter/src/format-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '')
Expand Down

0 comments on commit 2629438

Please sign in to comment.