Extract regex patterns to constants for better maintainability#224
Merged
Conversation
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).
Bundle ReportChanges will increase total bundle size by 593 bytes (3.28%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: formatCss-esmAssets Changed:
Files in
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 boundariesDATA_URL_RE- detects data URLsFONT_SLASH_RE- matches whitespace around forward slashes in font declarationsATRULE_COLON_COMMA_RE- matches colons and commas in at-rulesATRULE_PAREN_TEXT_RE- matches closing parenthesis followed by textATRULE_KEYWORD_PAREN_RE- matches media/supports keywords before parenthesesATRULE_ARROW_COMPARE_RE- matches comparison operators (=>, >=, <=)ATRULE_COMPARE_RE- matches unspaced comparison operatorsATRULE_COMPARE_SPACED_RE- matches comparison operators with surrounding whitespaceATRULE_WHITESPACE_RE- matches multiple whitespace charactersATRULE_COLON_COMMA_SPACE_RE- matches colons/commas followed by spaceATRULE_CALC_RE- matches calc() function expressionsATRULE_FN_NAME_RE- matches function names to be lowercasedUpdated all
replaceAll()calls inunquote(),print_url(),format_declaration(), andformat_atrule_prelude()to use the new constants instead of inline regex literalsBenefits
https://claude.ai/code/session_015ghHBJsYCroWnc3eMioBsy