Repository navigation
fix(enforces-canonical-classname): keep canonical results aligned when a batch collapses two classnames - #491
Open
nimbly1 wants to merge 1 commit into
Conversation
…n a batch collapses two classnames
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.
Fixes #490.
Root cause
The
canonicalize-candidatesoperation intailwind-api.mjsmapped the compiler's results back onto the batch by position:canonicalizeCandidatescollects its results in aSet, so when two classnames of a batch share a canonical form (hover:-mt-[0.25em]andhover:mt-[-0.25em]both resolve tohover:mt-[-0.25em]), the result is shorter than the input and every classname after the collapse is mapped onto a neighbour's canonical form. The result can also be reordered by the compiler in its regrouping path, so positional alignment is unsafe in general.Two visible consequences:
enforces-canonical-classnamereports classnames that cannot be written as the suggested form — with the issue's repro,p-4is reported asmd:px-6and--fixmangles the class list intomd:px-6 grid md:px-6 md:px-6.canonicalizeCandidatesWorker(src/utils/tailwindcss-api/index.ts) caches each classname after the first batch, so the misaligned value is remembered for the rest of the lint run (p-4→md:px-6permanently).Fix
Resolve one classname at a time inside the worker, which is the only mapping that makes no assumption about the shape of the compiler's output:
The worker round trip stays one per node, and the compiler's per-call setup is all memoized map lookups, so the added cost is a handful of
Map.getper classname; the caller's per-classname cache means each classname is resolved once per config either way.Tests
canonicalize-candidates.spec.ts: a batch where two classnames share a canonical form stays positionally aligned.enforces-canonical-classname.spec.ts:-mt-[0.25em] grid mt-[-0.25em] md:px-6yields the single legitimate report and the fix preserves the already-canonical spelling instead of shiftingmd:px-6onto it.Credit for the diagnosis and the first fix goes to @MaisonnatM (see their sibling-plugin PR) — thank you!