Skip to content

Commit 12833aa

Browse files
authored
Fix canonicalization bug where we end up with a high precision number (#20221)
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
1 parent 97a5b3a commit 12833aa

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
- Ensure transitions between `inset-shadow-none` and other inset shadows work correctly ([#20208](https://github.com/tailwindlabs/tailwindcss/pull/20208))
3434
- Ensure explicitly referenced `@source` directories are scanned even when ignored by git ([#20214](https://github.com/tailwindlabs/tailwindcss/pull/20214))
3535
- Ensure `@source` globs ending in `**/*` preserve dynamic path segments to avoid scanning too many files ([#20217](https://github.com/tailwindlabs/tailwindcss/pull/20217))
36+
- Canonicalization: don't fold `calc(…)` divisions when the result would require high precision (e.g. `w-[calc(100%/3.5)]` → `w-[calc(100%/3.5)]`, not `w-[28.571428571428573%]`) ([#20221](https://github.com/tailwindlabs/tailwindcss/pull/20221))
3637

3738
### Changed
3839

‎packages/tailwindcss/src/canonicalize-candidates.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,17 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
12441244

12451245
await expectCanonicalization(input, candidate, expected)
12461246
})
1247+
1248+
// https://github.com/tailwindlabs/tailwindcss-intellisense/issues/1591
1249+
test.each([
1250+
['w-[calc(100%/3.5)]', 'w-[calc(100%/3.5)]'], // Not w-[28.571428571428573%]
1251+
])(testName, { timeout }, async (candidate, expected) => {
1252+
let input = css`
1253+
@import 'tailwindcss';
1254+
`
1255+
1256+
await expectCanonicalization(input, candidate, expected)
1257+
})
12471258
})
12481259

12491260
describe('theme to var', () => {

‎packages/tailwindcss/src/constant-fold-declaration.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,11 @@ it.each([
127127
it('should not constant fold when dividing by `0`', () => {
128128
expect(constantFoldDeclaration('calc(123rem / 0)')).toBe('calc(123rem / 0)')
129129
})
130+
131+
it('should not constant fold when a computation has a high-precision result', () => {
132+
expect(constantFoldDeclaration('calc(100% / 3.5)')).toBe('calc(100% / 3.5)')
133+
})
134+
135+
it('should constant fold division results with floating-point error after scaling', () => {
136+
expect(constantFoldDeclaration('calc(29% / 100)')).toBe('0.29%')
137+
})

‎packages/tailwindcss/src/constant-fold-declaration.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,19 @@ export function constantFoldDeclarationAst(
286286
((lhs[1] === null && rhs[1] === null) || // Unitless / Unitless, e.g.: `8 / 2`
287287
(lhs[1] !== null && rhs[1] === null)) // Unit / Unitless, e.g.: `1rem / 2`
288288
) {
289+
let computed = lhs[0] / rhs[0]
290+
291+
// Only fold with .xx precision, anything beyond that might be a bit too much.
292+
//
293+
// E.g. 100% / 3.5 = 28.571428571428573%, which is correct but not
294+
// as user friendly. Especially when going from
295+
// `w-[calc(100%/3.5)]` → `w-[28.571428571428573%]`
296+
if (Math.round(computed * 100) / 100 !== computed) {
297+
break
298+
}
299+
289300
folded = true
290-
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] / rhs[0]}${lhs[1] ?? ''}`))
301+
return WalkAction.ReplaceSkip(ValueParser.word(`${computed}${lhs[1] ?? ''}`))
291302
}
292303
break
293304
}

0 commit comments

Comments
 (0)