Repository navigation
Fix the variable check of arbitrary values in canonicalization - #20760
Open
RobinMalfait wants to merge 2 commits into
Open
RobinMalfait wants to merge 2 commits into
RobinMalfait wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inallVariablesAreUsed()had two problems:An empty
var()threw aTypeError. The check readnode.nodes[0].valuewithout a guard, so a candidate likep-[var(--spacing-2,var())]crashed the wholecanonicalizeCandidates(…)call:This input is not valid CSS, but it can easily show up while typing with auto-closing brackets (
var(becomesvar()), and a single class like this breaks the call for every other class.Theme variables with escaped characters were never matched. The variable name went into a
RegExpwithout escaping. The backslash of an escaped name, like--spacing-1\.5, became a regex escape, so the pattern looked for--spacing-1.5while the CSS of the replacement contains--spacing-1\.5. As a result:The check now uses exact string matches for
var(<name>)andvar(<name>,, the same two forms that the old pattern accepted, so noRegExpis needed. An emptyvar()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
RegExpthrow aSyntaxErrorin 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
prefixstrategy in the tests rewrites--footo--tw-foo. It now also handles variable names with escaped characters, so the new tests run in all four strategies.Test plan
canonicalize-candidates.test.ts, next to the existing test for variables from a different namespace. They cover escaped variable names and an emptyvar(), for@themeand@theme inline, in all four strategies. All 32 cases fail without the fix, and pass with it.pnpm vitest runinpackages/tailwindcss: 5549 tests pass.pnpm vitest run --project @tailwindcss/upgrade: 470 tests pass.pnpm run build,tsc --noEmitinpackages/tailwindcss, andpnpm changelog checkpass.