Skip to content

Refactor printer functions into factory pattern#189

Open
bartveneman wants to merge 3 commits intomainfrom
claude/export-print-functions-JmWyi
Open

Refactor printer functions into factory pattern#189
bartveneman wants to merge 3 commits intomainfrom
claude/export-print-functions-JmWyi

Conversation

@bartveneman
Copy link
Copy Markdown
Member

Summary

Refactored the CSS formatter to extract printer functions into a factory function (create_printers) that accepts configuration parameters. This enables code reuse and allows both minified and pretty-printed output without duplicating logic.

Key Changes

  • Extracted create_printers factory function: Moved all printer functions (print_operator, print_list, print_value, etc.) into a factory that accepts formatting configuration (optional_space, newline, last_semicolon, minify flag, indent maker, and comment lookup function)
  • Moved utility functions to module level: unquote() and print_string() are now exported at the module level since they don't depend on formatting configuration
  • Created default printer exports: Instantiated create_printers with non-minified defaults (spaces, newlines, tab indentation) and exported all printer functions for direct use
  • Simplified format() function: Now creates a printer instance with the appropriate configuration based on minify and tab_size options, then delegates to print_stylesheet
  • Parameterized formatting variables: Replaced hardcoded OPTIONAL_SPACE, NEWLINE, and LAST_SEMICOLON constants with function parameters throughout the printer functions

Implementation Details

  • The factory pattern allows the same printer logic to work with different formatting rules without conditional checks scattered throughout the code
  • Comment lookup is now abstracted as a callback parameter, enabling the default exports to omit comments (since source CSS isn't available) while the format() function can provide comment extraction
  • The make_indent function is parameterized to support both tab-based and space-based indentation based on configuration

https://claude.ai/code/session_018n5LPJrTpLBwUW6mcdJtkS

claude added 3 commits March 30, 2026 07:49
Extract unquote/print_string as pure top-level exports. Refactor all
other print_* functions into a create_printers() factory that captures
optional_space, newline, last_semicolon, minify, make_indent, and
lookup_comment as closure values. Export pre-configured pretty-print
instances (OPTIONAL_SPACE=' ', NEWLINE='\n') so callers can use them
with just CSSNode or CSSNode[]. The format() function continues to call
create_printers() internally with minify-aware values and a
lookup_comment callback backed by the parsed comment positions.

https://claude.ai/code/session_018n5LPJrTpLBwUW6mcdJtkS
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 97.10145% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.26%. Comparing base (59c00e7) to head (3e2c7b5).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/lib/index.ts 97.10% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #189      +/-   ##
==========================================
- Coverage   96.78%   96.26%   -0.52%     
==========================================
  Files           2        2              
  Lines         342      348       +6     
  Branches      119      119              
==========================================
+ Hits          331      335       +4     
- Misses         10       12       +2     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Bundle Report

Changes will increase total bundle size by 2.13kB (13.57%) ⬆️⚠️, exceeding the configured threshold of 5%.

Bundle name Size Change
formatCss-esm 15.1kB 2.13kB (16.45%) ⬆️⚠️

Affected Assets, Files, and Routes:

view changes for bundle: formatCss-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
index.js 982 bytes 13.38kB 7.92% ⚠️
index.d.ts 1.15kB 1.72kB 203.36% ⚠️

Files in index.js:

  • ./src/lib/index.ts → Total Size: 12.98kB

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