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 OxideEngine Scanner 8 imports 2 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  b567fa55_8905_40fc_b0c8_f7b1041653f6["constant-fold-declaration.ts"]
  58207dba_8b48_ce0d_c2f9_d350f61e57ee["dimensions.ts"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 58207dba_8b48_ce0d_c2f9_d350f61e57ee
  6eccff72_e0d4_80af_8507_f68219a65aec["dimensions"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 6eccff72_e0d4_80af_8507_f68219a65aec
  516809a4_c70e_60c3_bbb2_8de4c4572510["infer-data-type.ts"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 516809a4_c70e_60c3_bbb2_8de4c4572510
  978cd83c_b489_4dd0_bddf_ab7b6f0970c6["isLength"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 978cd83c_b489_4dd0_bddf_ab7b6f0970c6
  1d3f1613_f144_938f_08f7_49039a46ad49["value-parser.ts"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 1d3f1613_f144_938f_08f7_49039a46ad49
  d1b39b63_c9d5_6c28_0206_0ddc8b895876["walk.ts"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> d1b39b63_c9d5_6c28_0206_0ddc8b895876
  ed78da58_8727_ad98_120c_61f35cea357a["walk"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> ed78da58_8727_ad98_120c_61f35cea357a
  7b34c369_d799_30f1_b751_6e3fd5349f6b["WalkAction"]
  b567fa55_8905_40fc_b0c8_f7b1041653f6 --> 7b34c369_d799_30f1_b751_6e3fd5349f6b
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e --> b567fa55_8905_40fc_b0c8_f7b1041653f6
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5["constant-fold-declaration.test.ts"]
  0f07a507_2f0b_3d0a_c082_f5e82173e2d5 --> b567fa55_8905_40fc_b0c8_f7b1041653f6
  style b567fa55_8905_40fc_b0c8_f7b1041653f6 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 OxideEngine domain, Scanner 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: OxideEngine, subdomain: Scanner, directory: packages/tailwindcss/src).

Analyze Your Own Codebase

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

Try Supermodel Free