Skip to content

Extract regex patterns to constants for better maintainability#224

Merged
bartveneman merged 3 commits into
mainfrom
claude/formatter-performance-analysis-ttf5ss
Jul 10, 2026
Merged

Extract regex patterns to constants for better maintainability#224
bartveneman merged 3 commits into
mainfrom
claude/formatter-performance-analysis-ttf5ss

Conversation

@bartveneman

Copy link
Copy Markdown
Member

Summary

Refactored regex patterns used throughout the formatting logic into named constants defined at the module level. This improves code maintainability, readability, and performance by avoiding repeated regex literal creation.

Key Changes

  • Extracted 15 regex patterns into module-level constants with descriptive names:

    • UNQUOTE_RE - removes quotes from string boundaries
    • DATA_URL_RE - detects data URLs
    • FONT_SLASH_RE - matches whitespace around forward slashes in font declarations
    • ATRULE_COLON_COMMA_RE - matches colons and commas in at-rules
    • ATRULE_PAREN_TEXT_RE - matches closing parenthesis followed by text
    • ATRULE_KEYWORD_PAREN_RE - matches media/supports keywords before parentheses
    • ATRULE_ARROW_COMPARE_RE - matches comparison operators (=>, >=, <=)
    • ATRULE_COMPARE_RE - matches unspaced comparison operators
    • ATRULE_COMPARE_SPACED_RE - matches comparison operators with surrounding whitespace
    • ATRULE_WHITESPACE_RE - matches multiple whitespace characters
    • ATRULE_COLON_COMMA_SPACE_RE - matches colons/commas followed by space
    • ATRULE_CALC_RE - matches calc() function expressions
    • ATRULE_FN_NAME_RE - matches function names to be lowercased
  • Updated all replaceAll() calls in unquote(), print_url(), format_declaration(), and format_atrule_prelude() to use the new constants instead of inline regex literals

Benefits

  • Improved code readability with self-documenting constant names
  • Better performance by compiling regex patterns once at module load time
  • Easier to maintain and update regex patterns in a centralized location
  • Reduced code duplication

https://claude.ai/code/session_015ghHBJsYCroWnc3eMioBsy

Regexes in unquote(), print_url(), format_declaration(), and
format_atrule_prelude() were declared as literals inside hot functions,
causing repeated RegExp allocation on every call. Hoisting them to
top-level constants avoids that overhead; format_atrule_prelude() in
particular runs 9 regex passes per at-rule prelude.

No behavior change (verified identical output across comment/atrule/url
test cases and the full test suite).
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 593 bytes (3.28%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
formatCss-esm 15.91kB 593 bytes (3.87%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: formatCss-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
index.js 593 bytes 14.24kB 4.35%

Files in index.js:

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

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.07%. Comparing base (f0ede7e) to head (64f4006).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #224      +/-   ##
==========================================
+ Coverage   96.98%   97.07%   +0.08%     
==========================================
  Files           2        2              
  Lines         365      376      +11     
  Branches      136      136              
==========================================
+ Hits          354      365      +11     
  Misses         10       10              
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 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.

claude added 2 commits July 10, 2026 17:51
…efer-number-coercion)

Unrelated to the regex-hoisting change, but blocking CI on this PR.
Number() breaks formatting that parseFloat's lenient trailing-character
handling relied on. Suppress the linter rule instead of changing behavior.
@bartveneman bartveneman merged commit 205b372 into main Jul 10, 2026
7 of 8 checks passed
@bartveneman bartveneman deleted the claude/formatter-performance-analysis-ttf5ss branch July 10, 2026 18:14
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