Skip to content

Fix the variable check of arbitrary values in canonicalization - #20760

Open
RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-escape-var-names-in-canonicalization
Open

RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-escape-var-names-in-canonicalization

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

Summary

Before canonicalization replaces an arbitrary value that contains a var(…) with a theme value, it checks that the replacement uses the same variables. That check in allVariablesAreUsed() had two problems:

  1. An empty var() threw a TypeError. The check read node.nodes[0].value without a guard, so a candidate like p-[var(--spacing-2,var())] crashed the whole canonicalizeCandidates(…) call:

    TypeError: Cannot read properties of undefined (reading 'value')
    

    This input is not valid CSS, but it can easily show up while typing with auto-closing brackets (var( becomes var()), and a single class like this breaks the call for every other class.

  2. Theme variables with escaped characters were never matched. The variable name went into a RegExp without escaping. The backslash of an escaped name, like --spacing-1\.5, became a regex escape, so the pattern looked for --spacing-1.5 while the CSS of the replacement contains --spacing-1\.5. As a result:

    p-[var(--spacing-2)]            → p-2
    p-[var(--spacing-1\.5)]         → p-(--spacing-1\.5)            (expected: p-1.5)
    [padding:var(--spacing-1\.5)]   → [padding:var(--spacing-1\.5)] (expected: p-1.5)
    

The check now uses exact string matches for var(<name>) and var(<name>,, the same two forms that the old pattern accepted, so no RegExp is needed. An empty var() counts as a variable that the replacement doesn't use, so the candidate is kept as-is instead of throwing.

I found no input that makes the old RegExp throw a SyntaxError in practice. A name with special characters must escape them, and the escape turns them into literal characters in the pattern. So the visible effect of the missing escape was only the missed canonicalization above.

The prefix strategy in the tests rewrites --foo to --tw-foo. It now also handles variable names with escaped characters, so the new tests run in all four strategies.

Test plan

  • Added tests to canonicalize-candidates.test.ts, next to the existing test for variables from a different namespace. They cover escaped variable names and an empty var(), for @theme and @theme inline, in all four strategies. All 32 cases fail without the fix, and pass with it.
  • pnpm vitest run in packages/tailwindcss: 5549 tests pass.
  • pnpm vitest run --project @tailwindcss/upgrade: 470 tests pass.
  • pnpm run build, tsc --noEmit in packages/tailwindcss, and pnpm changelog check pass.

Before an arbitrary value with a `var(…)` is replaced by a theme value,
canonicalization checks that the replacement uses the same variables.
That check had two problems:

- An empty `var()` threw a `TypeError`, e.g. for
  `p-[var(--spacing-2,var())]`. Such a class can show up while typing
  with auto-closing brackets, and the error broke the whole
  `canonicalizeCandidates(…)` call.
- The variable name went into a `RegExp` without escaping. A backslash
  of an escaped name, like `--spacing-1\.5`, became a regex escape, so
  the name never matched the CSS of the replacement, and
  `p-[var(--spacing-1\.5)]` was not canonicalized to `p-1.5`.

The check now uses exact string matches, with the same `var(<name>)`
and `var(<name>,` forms as the old pattern, and treats an empty `var()`
as a variable that the replacement doesn't use.

The `prefix` strategy of the tests now also prefixes variable names
with escaped characters.
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 25, 2026 12:39
@RobinMalfait RobinMalfait self-assigned this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant