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

constant-fold-declaration.ts — tailwindcss Source File

Architecture documentation for constant-fold-declaration.ts, a typescript file in the tailwindcss codebase. 8 imports, 2 dependents.

File typescript Oxide Extractor 8 imports 2 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b["constant-fold-declaration.ts"]
  27d3cdf1_e27d_4437_413c_da251dc63276["dimensions.ts"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 27d3cdf1_e27d_4437_413c_da251dc63276
  06ffc038_18ab_2fb1_266d_adc31a72e8a6["dimensions"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 06ffc038_18ab_2fb1_266d_adc31a72e8a6
  43fe4735_10e8_ed1d_6f4f_74b2008c1b00["infer-data-type.ts"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 43fe4735_10e8_ed1d_6f4f_74b2008c1b00
  1892fde4_0ab4_2e7a_38c4_cf431005a033["isLength"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 1892fde4_0ab4_2e7a_38c4_cf431005a033
  d9175aea_5971_a6c1_773d_004ce3789372["value-parser.ts"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> d9175aea_5971_a6c1_773d_004ce3789372
  1b8f1c54_b1e9_e18d_0719_b7ad92808185["walk.ts"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 1b8f1c54_b1e9_e18d_0719_b7ad92808185
  4982d9ce_98d4_85d9_44af_7cc47b93c482["walk"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 4982d9ce_98d4_85d9_44af_7cc47b93c482
  47187d1b_a6f7_f734_0752_446b87b5cd9e["WalkAction"]
  28f23724_d31a_a8d3_3dd6_07e505ec5b7b --> 47187d1b_a6f7_f734_0752_446b87b5cd9e
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0 --> 28f23724_d31a_a8d3_3dd6_07e505ec5b7b
  624feab8_f098_5b87_977f_8a6f50730080["constant-fold-declaration.test.ts"]
  624feab8_f098_5b87_977f_8a6f50730080 --> 28f23724_d31a_a8d3_3dd6_07e505ec5b7b
  style 28f23724_d31a_a8d3_3dd6_07e505ec5b7b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { dimensions } from './utils/dimensions'
import { isLength } from './utils/infer-data-type'
import * as ValueParser from './value-parser'
import { walk, WalkAction } from './walk'

// Assumption: We already assume that we receive somewhat valid `calc()`
// expressions. So we will see `calc(1 + 1)` and not `calc(1+1)`
export function constantFoldDeclaration(input: string, rem: number | null = null): string {
  let folded = false
  let valueAst = ValueParser.parse(input)

  walk(valueAst, {
    exit(valueNode) {
      // Canonicalize dimensions to their simplest form. This includes:
      // - Convert `-0`, `+0`, `0.0`, … to `0`
      // - Convert `-0px`, `+0em`, `0.0rem`, … to `0`
      // - Convert units to an equivalent unit
      if (
        valueNode.kind === 'word' &&
        valueNode.value !== '0' // Already `0`, nothing to do
      ) {
        let canonical = canonicalizeDimension(valueNode.value, rem)
        if (canonical === null) return // Couldn't be canonicalized, nothing to do
        if (canonical === valueNode.value) return // Already in canonical form, nothing to do

        folded = true
        return WalkAction.ReplaceSkip(ValueParser.word(canonical))
      }

      // Constant fold `calc()` expressions with two operands and one operator
      else if (
        valueNode.kind === 'function' &&
        (valueNode.value === 'calc' || valueNode.value === '')
      ) {
        // [
        //   { kind: 'word', value: '0.25rem' },            0
        //   { kind: 'separator', value: ' ' },             1
        //   { kind: 'word', value: '*' },                  2
        //   { kind: 'separator', value: ' ' },             3
        //   { kind: 'word', value: '256' }                 4
        // ]
        if (valueNode.nodes.length !== 5) return

        let lhs = dimensions.get(valueNode.nodes[0].value)
        let operator = valueNode.nodes[2].value
        let rhs = dimensions.get(valueNode.nodes[4].value)

        // Nullify entire expression when multiplying by `0`, e.g.: `calc(0 * 100vw)` -> `0`
        //
        // TODO: Ensure it's safe to do so based on the data types?
        if (
          operator === '*' &&
          ((lhs?.[0] === 0 && lhs?.[1] === null) || // 0 * something
            (rhs?.[0] === 0 && rhs?.[1] === null)) // something * 0
        ) {
          folded = true
          return WalkAction.ReplaceSkip(ValueParser.word('0'))
        }

        // We're not dealing with dimensions, so we can't fold this
// ... (92 more lines)

Domain

Subdomains

Frequently Asked Questions

What does constant-fold-declaration.ts do?
constant-fold-declaration.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the Oxide domain, Extractor subdomain.
What functions are defined in constant-fold-declaration.ts?
constant-fold-declaration.ts defines 2 function(s): canonicalizeDimension, constantFoldDeclaration.
What does constant-fold-declaration.ts depend on?
constant-fold-declaration.ts imports 8 module(s): WalkAction, dimensions, dimensions.ts, infer-data-type.ts, isLength, value-parser.ts, walk, walk.ts.
What files import constant-fold-declaration.ts?
constant-fold-declaration.ts is imported by 2 file(s): canonicalize-candidates.ts, constant-fold-declaration.test.ts.
Where is constant-fold-declaration.ts in the architecture?
constant-fold-declaration.ts is located at packages/tailwindcss/src/constant-fold-declaration.ts (domain: Oxide, subdomain: Extractor, directory: packages/tailwindcss/src).

Analyze Your Own Codebase

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

Try Supermodel Free