Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix canonicalization bug where we end up with a high precision number#20221

Merged
RobinMalfait merged 4 commits into
mainfrom
fix/issue-intellisense-1591
Jun 11, 2026
Merged

Fix canonicalization bug where we end up with a high precision number#20221
RobinMalfait merged 4 commits into
mainfrom
fix/issue-intellisense-1591

Conversation

@RobinMalfait

@RobinMalfait RobinMalfait commented Jun 11, 2026

Copy link
Copy Markdown
Member

This PR fixes a bug where we suggest a canonicalization with a high precision number.

E.g.:

w-[calc(100%/3.5)]w-[28.571428571428573%]

While this is technically correct, it's also not as user friendly. This PR solves this issue by checking whether the result has a precision of .xx at most. If that's not the case, then we keep the original expression.

Fixes: tailwindlabs/tailwindcss-intellisense#1591

Test plan

  1. Added an a regression test for this situation
  2. Existing tests pass

100% / 3.5 = 28.571428571428573%, which is correct but not as user friendly.

Especially when going from `w-[calc(100%/3.5)]` → `w-[28.571428571428573%]`
@RobinMalfait RobinMalfait requested a review from a team as a code owner June 11, 2026 22:01
@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

The change is narrowly scoped to division folding in constant-fold-declaration.ts, uses Math.round correctly to avoid IEEE 754 edge cases, and is covered by targeted regression tests. Safe to merge.

The precision guard correctly uses Math.round, which handles floating-point representations where multiplying by 100 lands just below an integer. The logic is straightforward, the existing test suite continues to pass, and two new tests cover both the high-precision bail-out and the normal-precision fold path.

No files require special attention.

Reviews (2): Last reviewed commit: "update changelog" | Re-trigger Greptile

Comment thread packages/tailwindcss/src/constant-fold-declaration.ts Outdated
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c7cd862f-6985-4f46-93b2-6e0c877c3c49

📥 Commits

Reviewing files that changed from the base of the PR and between 95ee4a4 and 4ce3180.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/tailwindcss/src/constant-fold-declaration.test.ts
  • packages/tailwindcss/src/constant-fold-declaration.ts
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/tailwindcss/src/constant-fold-declaration.ts
  • packages/tailwindcss/src/constant-fold-declaration.test.ts

Walkthrough

This PR prevents high-precision division results in calc() expressions from being constant-folded. The implementation adds a precision gate to the division folding path in constantFoldDeclarationAst, ensuring that quotients matching their 2-decimal truncation are folded, while non-terminating or rounding-heavy fractions like 100% / 3.5 remain unchanged. A unit test verifies the core behavior, and an integration test confirms the fix propagates through canonicalization, addressing issue #1591.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a canonicalization bug that produces high-precision numbers from calc expressions.
Description check ✅ Passed The description is directly related to the changeset, providing a concrete example of the bug, explaining the fix logic, and referencing the linked issue.
Linked Issues check ✅ Passed The PR fully addresses the objective from #1591 by implementing precision checking to prevent canonicalization of calc expressions into high-precision numeric values.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: test additions for regression coverage and implementation of precision guards in constant-folding logic.

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


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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/tailwindcss/src/constant-fold-declaration.test.ts (1)

131-133: 💤 Low value

Test correctly validates the fix.

The test verifies that calc(100% / 3.5) is not folded to a high-precision result. Consider adding a few more test cases to strengthen coverage:

  • High-precision cases: calc(10 / 3), calc(100% / 7)
  • Confirm 2-decimal folding still works: calc(100% / 8)12.5%, calc(100% / 4)25%

However, as a focused regression test for issue #1591, this is sufficient.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tailwindcss/src/constant-fold-declaration.test.ts` around lines 131
- 133, Add additional unit cases around constantFoldDeclaration to strengthen
coverage: include high-precision no-fold examples such as 'calc(10 / 3)' and
'calc(100% / 7)' to ensure they are returned unchanged, and add assertions that
2-decimal or exact folding still occurs (e.g., 'calc(100% / 8)' -> '12.5%' and
'calc(100% / 4)' -> '25%') so both non-folding high-precision and expected
folding behaviors are validated alongside the existing 'calc(100% / 3.5)' test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/tailwindcss/src/constant-fold-declaration.test.ts`:
- Around line 131-133: Add additional unit cases around constantFoldDeclaration
to strengthen coverage: include high-precision no-fold examples such as 'calc(10
/ 3)' and 'calc(100% / 7)' to ensure they are returned unchanged, and add
assertions that 2-decimal or exact folding still occurs (e.g., 'calc(100% / 8)'
-> '12.5%' and 'calc(100% / 4)' -> '25%') so both non-folding high-precision and
expected folding behaviors are validated alongside the existing 'calc(100% /
3.5)' test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bbd63301-1104-40e4-8dc7-5bd569bbf7e4

📥 Commits

Reviewing files that changed from the base of the PR and between 97a5b3a and 95ee4a4.

📒 Files selected for processing (3)
  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/constant-fold-declaration.test.ts
  • packages/tailwindcss/src/constant-fold-declaration.ts

@RobinMalfait RobinMalfait merged commit 12833aa into main Jun 11, 2026
9 checks passed
@RobinMalfait RobinMalfait deleted the fix/issue-intellisense-1591 branch June 11, 2026 22:23
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.

Interesting suggestCanonicalClasses suggestion for calc

1 participant