Home / Function/ substituteFunctionsInValue() — tailwindcss Function Reference

substituteFunctionsInValue() — tailwindcss Function Reference

Architecture documentation for the substituteFunctionsInValue() function in migrate-theme-to-var.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  91c97086_ce94_1ef6_775d_f4471bc2ca0e["substituteFunctionsInValue()"]
  c36efdeb_7fd2_0935_2c28_bf15095a9dd9["migrate-theme-to-var.ts"]
  91c97086_ce94_1ef6_775d_f4471bc2ca0e -->|defined in| c36efdeb_7fd2_0935_2c28_bf15095a9dd9
  f1c7fb37_4a69_f6f5_b7f4_9f3d8b534c40["createConverter()"]
  f1c7fb37_4a69_f6f5_b7f4_9f3d8b534c40 -->|calls| 91c97086_ce94_1ef6_775d_f4471bc2ca0e
  d6cf80a6_8130_7069_e60d_09156f156b67["toCss()"]
  91c97086_ce94_1ef6_775d_f4471bc2ca0e -->|calls| d6cf80a6_8130_7069_e60d_09156f156b67
  7ca42872_8efd_7e33_88c4_dfb58aa0f681["eventuallyUnquote()"]
  91c97086_ce94_1ef6_775d_f4471bc2ca0e -->|calls| 7ca42872_8efd_7e33_88c4_dfb58aa0f681
  49a8c506_c50e_ed4b_5a0e_0393edae2b6f["parse()"]
  91c97086_ce94_1ef6_775d_f4471bc2ca0e -->|calls| 49a8c506_c50e_ed4b_5a0e_0393edae2b6f
  4982d9ce_98d4_85d9_44af_7cc47b93c482["walk()"]
  91c97086_ce94_1ef6_775d_f4471bc2ca0e -->|calls| 4982d9ce_98d4_85d9_44af_7cc47b93c482
  style 91c97086_ce94_1ef6_775d_f4471bc2ca0e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/template/migrate-theme-to-var.ts lines 172–250

function substituteFunctionsInValue(
  ast: ValueParser.ValueAstNode[],
  handle: (value: string, fallback?: string) => string | null,
) {
  walk(ast, (node, ctx) => {
    if (node.kind === 'function' && node.value === 'theme') {
      if (node.nodes.length < 1) return

      // Ignore whitespace before the first argument
      if (node.nodes[0].kind === 'separator' && node.nodes[0].value.trim() === '') {
        node.nodes.shift()
      }

      let pathNode = node.nodes[0]
      if (pathNode.kind !== 'word') return

      let path = pathNode.value

      // For the theme function arguments, we require all separators to contain
      // comma (`,`), spaces alone should be merged into the previous word to
      // avoid splitting in this case:
      //
      // theme(--color-red-500 / 75%) theme(--color-red-500 / 75%, foo, bar)
      //
      // We only need to do this for the first node, as the fallback values are
      // passed through as-is.
      let skipUntilIndex = 1
      for (let i = skipUntilIndex; i < node.nodes.length; i++) {
        if (node.nodes[i].value.includes(',')) {
          break
        }
        path += ValueParser.toCss([node.nodes[i]])
        skipUntilIndex = i + 1
      }

      path = eventuallyUnquote(path)
      let fallbackValues = node.nodes.slice(skipUntilIndex + 1)

      let replacement =
        fallbackValues.length > 0 ? handle(path, ValueParser.toCss(fallbackValues)) : handle(path)
      if (replacement === null) return

      if (ctx.parent) {
        let idx = ctx.parent.nodes.indexOf(node) - 1
        while (idx !== -1) {
          let previous = ctx.parent.nodes[idx]
          // Skip the space separator
          if (previous.kind === 'separator' && previous.value.trim() === '') {
            idx -= 1
            continue
          }

          // If the previous node is a word and contains an operator, we need to
          // wrap the replacement in parentheses to make the output less
          // ambiguous.
          //
          // Input:
          // - `calc(100dvh-theme(spacing.2))`
          //
          // Output:
          // - `calc(100dvh-(--spacing(2)))`
          //
          // Not:
          // -`calc(100dvh---spacing(2))`
          //
          if (/^[-+*/]$/.test(previous.value.trim())) {
            replacement = `(${replacement})`
          }

          break
        }
      }

      return WalkAction.Replace(ValueParser.parse(replacement))
    }
  })

  return ValueParser.toCss(ast)
}

Subdomains

Called By

Frequently Asked Questions

What does substituteFunctionsInValue() do?
substituteFunctionsInValue() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-theme-to-var.ts.
Where is substituteFunctionsInValue() defined?
substituteFunctionsInValue() is defined in packages/@tailwindcss-upgrade/src/codemods/template/migrate-theme-to-var.ts at line 172.
What does substituteFunctionsInValue() call?
substituteFunctionsInValue() calls 4 function(s): eventuallyUnquote, parse, toCss, walk.
What calls substituteFunctionsInValue()?
substituteFunctionsInValue() is called by 1 function(s): createConverter.

Analyze Your Own Codebase

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

Try Supermodel Free