Home / File/ constant-fold-declaration.test.ts — tailwindcss Source File

constant-fold-declaration.test.ts — tailwindcss Source File

Architecture documentation for constant-fold-declaration.test.ts, a typescript file in the tailwindcss codebase. 3 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5["constant-fold-declaration.test.ts"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6["constant-fold-declaration.ts"]
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5 --> b567fa55_8905_40fc_b0c8_f7b1041653f6
  9ab273b1_6701_5494_7f88_e2e72f74ddf7["constantFoldDeclaration"]
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5 --> 9ab273b1_6701_5494_7f88_e2e72f74ddf7
  696bd648_5f24_1b59_8e8b_7a97a692869e["vitest"]
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5 --> 696bd648_5f24_1b59_8e8b_7a97a692869e
  style 0f07a507_2f0b_3d0a_c082_f5e82173e2d5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { expect, it } from 'vitest'
import { constantFoldDeclaration } from './constant-fold-declaration'

it.each([
  // Simple expression
  ['calc(1 + 1)', '2'],
  ['calc(3 - 2)', '1'],
  ['calc(2 * 3)', '6'],
  ['calc(8 / 2)', '4'],

  // Nested
  ['calc(1 + calc(1 + 1))', '3'],
  ['calc(3 - calc(1 + 2))', '0'],
  ['calc(2 * calc(1 + 3))', '8'],
  ['calc(8 / calc(2 + 2))', '2'],
  ['calc(1 + (1 + 1))', '3'],
  ['calc(3 - (1 + 2))', '0'],
  ['calc(2 * (1 + 3))', '8'],
  ['calc(8 / (2 + 2))', '2'],

  // With units
  ['calc(1rem * 2)', '2rem'],
  ['calc(2rem - 0.5rem)', '1.5rem'],
  ['calc(3rem * 6)', '18rem'],
  ['calc(5rem / 2)', '2.5rem'],

  // Nested partial evaluation
  ['calc(calc(1 + 2) + 2rem)', 'calc(3 + 2rem)'],

  // Evaluation only handles two operands right now, this can change in the future
  ['calc(1 + 2 + 3)', 'calc(1 + 2 + 3)'],
])('should constant fold `%s` into `%s`', (input, expected) => {
  expect(constantFoldDeclaration(input)).toBe(expected)
})

it.each([
  ['calc(1rem * 2%)'],
  ['calc(1rem * 2px)'],
  ['calc(2rem - 6)'],
  ['calc(3rem * 3dvw)'],
  ['calc(3rem * 2dvh)'],
  ['calc(5rem / 17px)'],
])('should not constant fold different units `%s`', (input) => {
  expect(constantFoldDeclaration(input)).toBe(input)
})

it.each([
  ['calc(0 * 100vw)'],
  ['calc(0 * calc(1 * 2))'],
  ['calc(0 * var(--foo))'],
  ['calc(0 * calc(var(--spacing) * 32))'],

  ['calc(100vw * 0)'],
  ['calc(calc(1 * 2) * 0)'],
  ['calc(var(--foo) * 0)'],
  ['calc(calc(var(--spacing, 0.25rem) * 32) * 0)'],
  ['calc(var(--spacing, 0.25rem) * -0)'],
  ['calc(-0px * -1)'],

  // Zeroes
  ['0px'],
  ['0rem'],
  ['0em'],
  ['0dvh'],
  ['-0'],
  ['+0'],
  ['-0.0rem'],
  ['+0.00rem'],
])('should constant fold `%s` to `0`', (input) => {
  expect(constantFoldDeclaration(input)).toBe('0')
})

it.each([
  ['0deg', '0deg'],
  ['0rad', '0deg'],
  ['0%', '0%'],
  ['0turn', '0deg'],
  ['0fr', '0fr'],
  ['0ms', '0s'],
  ['0s', '0s'],
  ['-0.0deg', '0deg'],
  ['-0.0rad', '0deg'],
  ['-0.0%', '0%'],
  ['-0.0turn', '0deg'],
  ['-0.0fr', '0fr'],
  ['-0.0ms', '0s'],
  ['-0.0s', '0s'],
])('should not fold non-foldable units to `0`. Constant fold `%s` into `%s`', (input, expected) => {
  expect(constantFoldDeclaration(input)).toBe(expected)
})

it('should not constant fold when dividing by `0`', () => {
  expect(constantFoldDeclaration('calc(123rem / 0)')).toBe('calc(123rem / 0)')
})

Domain

Frequently Asked Questions

What does constant-fold-declaration.test.ts do?
constant-fold-declaration.test.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the OxideEngine domain.
What does constant-fold-declaration.test.ts depend on?
constant-fold-declaration.test.ts imports 3 module(s): constant-fold-declaration.ts, constantFoldDeclaration, vitest.
Where is constant-fold-declaration.test.ts in the architecture?
constant-fold-declaration.test.ts is located at packages/tailwindcss/src/constant-fold-declaration.test.ts (domain: OxideEngine, directory: packages/tailwindcss/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free