Skip to content

Add support for formatting CSS if() conditional value functions - #242

Merged
bartveneman merged 8 commits into
mainfrom
claude/css-parser-if-else-format-bv4uvi
Aug 16, 2026
Merged

Add support for formatting CSS if() conditional value functions#242
bartveneman merged 8 commits into
mainfrom
claude/css-parser-if-else-format-bv4uvi

Conversation

@bartveneman

Copy link
Copy Markdown
Member

Summary

This PR adds comprehensive support for formatting CSS if() conditional value functions, which allow declaring different values based on media queries, feature support, or custom properties.

Key Changes

  • New type support: Added imports for IfBranch and Function types to handle the structure of if() functions
  • Indentation helper: Created print_indent() function to centralize indentation logic, respecting both tab characters and tab_size settings
  • Condition formatting: Implemented print_if_condition() to format condition functions (media(), supports(), style()) and the else keyword, normalizing their prelude formatting
  • Branch formatting: Added print_if_branch() to format individual condition: value branches with proper spacing
  • If function printing: Implemented print_if() to handle multi-line formatting of if() functions with one branch per line in pretty-print mode, or minified to a single line
  • If function detection: Added is_if_function() helper to distinguish if() functions from regular functions
  • Enhanced value formatting: Updated print_list() to accept depth, minify, and tab_size parameters to properly handle nested if() functions with correct indentation
  • Declaration depth tracking: Modified format_value() and format_declaration() to accept and pass depth information for proper indentation of multi-line values
  • Comprehensive tests: Added 8 test cases covering formatting, minification, nested conditions, empty values, and indentation with custom tab sizes

Implementation Details

  • if() functions are formatted with one condition/value branch per line in pretty-print mode, with proper indentation relative to the declaration depth
  • Condition function names and the if keyword itself are lowercased
  • Condition prelude formatting (e.g., media queries, supports conditions) is normalized using the existing format_atrule_prelude() function
  • Minified output collapses if() onto a single line with semicolon-separated branches
  • Nested if() functions are properly indented one level deeper than their parent
  • Empty branch values (e.g., style(--y: 1):;) are supported

https://claude.ai/code/session_01LPK5x1eiNibAUJ5d5gc98N

claude added 2 commits August 16, 2026 11:22
Prints each if() condition/value branch on its own line, indented one
level deeper than the declaration, with a closing paren back at the
declaration's level — matching the style requested in #219. Nested
if() and minify both work: minify collapses everything onto one line,
joined by semicolons.

Condition functions (style()/supports()/media()) are normalized
through the existing string-based @supports/@media prelude formatter,
since if()'s condition grammar is the same grammar — only the
function name itself needed separate lowercasing, since that
formatter doesn't touch it.

Depends on the not-yet-merged css-parser#253 (adds IfBranch/is_if_branch),
pinned here to its pkg.pr.new preview build. pnpm-lock.yaml is left
untouched because pkg.pr.new was unreachable from this sandbox's egress
policy (403) — regenerate the lockfile in an environment that can reach
it, or swap the dependency for a released version once css-parser
publishes if() support.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LPK5x1eiNibAUJ5d5gc98N
print_if only ever calls it after already short-circuiting on minify,
so threading minify through was dead weight. Renamed to print_indent
to match this file's print_* convention and avoid a same-name collision
with format()'s local indent() wrapper, which still does its own
minify check and delegates.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LPK5x1eiNibAUJ5d5gc98N
@bartveneman
bartveneman marked this pull request as draft August 16, 2026 11:51

This comment was marked as outdated.

@github-actions

github-actions Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

⚠️ Package Size Increase

📦 Package 📏 Base Size 📏 Source Size 📈 Size Change
@projectwallace/format-css 8.7 kB 9.6 kB +856 B

@bartveneman
bartveneman marked this pull request as ready for review August 16, 2026 13:54
@bartveneman
bartveneman merged commit 9aa010d into main Aug 16, 2026
9 checks passed
@bartveneman
bartveneman deleted the claude/css-parser-if-else-format-bv4uvi branch August 16, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants