Skip to content

fix: correct regex operator precedence in CSS_LENGTH_UNIT_REGEX#7068

Merged
PavelVanecek merged 1 commit intorecharts:mainfrom
haritamar:fix/css-length-unit-regex-precedence
Mar 1, 2026
Merged

fix: correct regex operator precedence in CSS_LENGTH_UNIT_REGEX#7068
PavelVanecek merged 1 commit intorecharts:mainfrom
haritamar:fix/css-length-unit-regex-precedence

Conversation

@haritamar
Copy link
Contributor

@haritamar haritamar commented Feb 28, 2026

Summary

CSS_LENGTH_UNIT_REGEX in src/util/ReduceCSSCalc.ts has a regex operator precedence bug.

The current pattern:

/^px|cm|vh|vw|em|rem|%|mm|in|pt|pc|ex|ch|vmin|vmax|Q$/

Due to | (alternation) having lower precedence than ^ and $, this only anchors px at the start and Q at the end — the middle alternatives are unanchored.

Fix

Wrap the alternation in a non-capturing group so the anchors apply to the entire expression:

/^(px|cm|vh|vw|em|rem|%|mm|in|pt|pc|ex|ch|vmin|vmax|Q)$/

Impact

In practice this doesn't cause runtime issues because the regex is only tested against short, pre-parsed CSS unit strings. However, the current pattern triggers misleading-regex-precedence warnings in static analysis tools (ESLint, SonarQube), which propagate to downstream consumers of recharts.

Ref: elementary-data/elementary#2036

Made with Cursor

Summary by CodeRabbit

Bug Fixes

  • Improved CSS unit matching in calculations by fixing unit validation logic. CSS length units are now accurately recognized within complex CSS expressions, preventing misinterpretation that could occur from partial matches. This ensures more reliable and correct CSS value processing across the application.

The alternation `^px|cm|...|Q$` only anchored `px` at the start and
`Q` at the end. Wrapping in a group `^(px|cm|...|Q)$` ensures the
full match is properly anchored.

Made-with: Cursor
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 28, 2026

Walkthrough

Modified the CSS_LENGTH_UNIT_REGEX pattern in the ReduceCSSCalc utility to enforce full-string matching by adding start and end anchors with grouped alternation. This prevents unintended partial matches within longer strings while maintaining all existing functional logic.

Changes

Cohort / File(s) Summary
CSS Regex Enhancement
src/util/ReduceCSSCalc.ts
Updated CSS_LENGTH_UNIT_REGEX to enforce full-string matching with anchors and grouped alternation, preventing partial matches in longer strings without altering unit conversion or validation logic.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: correct regex operator precedence in CSS_LENGTH_UNIT_REGEX' clearly and specifically describes the main change—correcting a regex operator precedence issue in a specific regex pattern.
Description check ✅ Passed The description provides a clear explanation of the bug, the fix, and the impact, but several required template sections (Related Issue link, How Has This Been Tested?, Screenshots, and Checklist items) are not completed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/util/ReduceCSSCalc.ts (1)

5-5: Please add/confirm a regression test for exact unit-only matches.

This change should be covered with cases like valid (px, vmin, %) and invalid (apx, pxx, xQ) to lock in the precedence fix.

As per coding guidelines, "src/**/*.{ts,tsx}: Aim for 100% unit test code coverage when writing new code" and "src/**/util/**/*.{ts,tsx}: Extract pure helper functions for data processing and unit test them".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/util/ReduceCSSCalc.ts` at line 5, Add a regression unit test for the
CSS_LENGTH_UNIT_REGEX in ReduceCSSCalc that asserts exact unit-only matches and
rejects similar but invalid strings: verify valid matches like "px", "vmin", "%"
and invalid non-matches like "apx", "pxx", "xQ"; use the same matching logic the
code uses (e.g., RegExp.prototype.test or full-string match) against the
exported CSS_LENGTH_UNIT_REGEX from ReduceCSSCalc and include these cases so the
precedence/anchoring fix is locked in.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/util/ReduceCSSCalc.ts`:
- Line 5: Add a regression unit test for the CSS_LENGTH_UNIT_REGEX in
ReduceCSSCalc that asserts exact unit-only matches and rejects similar but
invalid strings: verify valid matches like "px", "vmin", "%" and invalid
non-matches like "apx", "pxx", "xQ"; use the same matching logic the code uses
(e.g., RegExp.prototype.test or full-string match) against the exported
CSS_LENGTH_UNIT_REGEX from ReduceCSSCalc and include these cases so the
precedence/anchoring fix is locked in.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a5333dd and 994b2fb.

📒 Files selected for processing (1)
  • src/util/ReduceCSSCalc.ts

@codecov
Copy link

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.20%. Comparing base (a5333dd) to head (994b2fb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7068   +/-   ##
=======================================
  Coverage   90.20%   90.20%           
=======================================
  Files         526      526           
  Lines       39650    39650           
  Branches     5442     5442           
=======================================
  Hits        35768    35768           
  Misses       3873     3873           
  Partials        9        9           

☔ 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.

@PavelVanecek PavelVanecek merged commit 758c6d4 into recharts:main Mar 1, 2026
41 checks passed
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