Skip to content

Commit 22cafcf

Browse files
Bump precision of evaluated calc expressions (#1449)
Fixes #1446
1 parent 30df247 commit 22cafcf

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

packages/tailwindcss-language-server/tests/completions/completions.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ withFixture('v4/basic', (c) => {
602602
'```css',
603603
'.text-sm {',
604604
' font-size: var(--text-sm) /* 0.875rem = 8.75px */;',
605-
' line-height: var(--tw-leading, var(--text-sm--line-height) /* calc(1.25 / 0.875) ≈ 1.4286 */);',
605+
' line-height: var(--tw-leading, var(--text-sm--line-height) /* calc(1.25 / 0.875) ≈ 1.428571 */);',
606606
'}',
607607
'```',
608608
].join('\n'),

packages/tailwindcss-language-service/src/util/rewriting/calc.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ export function evaluateExpression(str: string): string | null {
1313
randomSeed: 1,
1414

1515
// Limit precision to keep values environment independent
16-
precision: 4,
16+
//
17+
// This is mostly to limit displayed numbers to a reasonable length. Ideally
18+
// we could perform any calcs with "infinite" precision and only then round
19+
// numbers back for display purposes.
20+
//
21+
// This is short of the 7 digits that 32-bit floats provide. JS does store
22+
// numbers as doubles so maybe this can be bumped to like 12–15?
23+
precision: 6,
1724
})
1825

1926
// The result array is the same shape as the original so we're guaranteed to

packages/tailwindcss-language-service/src/util/rewriting/index.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,18 @@ test('Evaluating CSS calc expressions', () => {
173173
)
174174

175175
expect(replaceCssCalc('calc(1.25 / 0.875)', (node) => evaluateExpression(node.value))).toBe(
176-
'1.4286',
176+
'1.428571',
177177
)
178178

179179
expect(replaceCssCalc('calc(1/4 * 100%)', (node) => evaluateExpression(node.value))).toBe('25%')
180+
181+
expect(replaceCssCalc('calc(0.12345rem * 0.5)', (node) => evaluateExpression(node.value))).toBe(
182+
'0.061725rem',
183+
)
184+
185+
expect(
186+
replaceCssCalc('calc(0.12345789rem * 0.5)', (node) => evaluateExpression(node.value)),
187+
).toBe('0.061729rem')
180188
})
181189

182190
test('Inlining calc expressions using the design system', () => {

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Suggest default variant values when they also support arbitrary values ([#1439](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1439))
77
- Show color swatches for OKLCH colors with units in all positions ([#1442](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1442))
88
- Fix incorrect diagnostic for `--theme(--some-var inline)` ([#1443](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1443))
9+
- Bump precision of evaluated calc expressions ([#1449](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1449))
910

1011
## 0.14.26
1112

0 commit comments

Comments
 (0)