feat: add Tailwind class name sorting (#552)#795
Open
todor-a wants to merge 2 commits into
Open
Conversation
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 #552. Adds opt-in Tailwind class sorting aligned with prettier-plugin-tailwindcss.
Problem
Tailwind users often rely on Prettier's Tailwind plugin to keep utility classes in a stable recommended order.
Currently dprint-plugin-typescript preserves the input order, so projects that use dprint cannot opt into that Tailwind ordering for JSX class attributes or common class helper calls.
Fix
Add
jsx.sortClassNameswith a newtailwindvalue. The default remainsmaintain.When enabled, the formatter sorts Tailwind class lists in:
classandclassNamestring attributesclassName={...}expression containersjsx.sortClassNames.functions, for examplecn,classnames, ortwThe sorter follows the same broad behavior as prettier-plugin-tailwindcss for the covered cases: custom classes stay first, whitespace is collapsed, duplicate classes are removed, and variants sort after base utilities.
Behavior
<div className="p-4 flex" /><div className="p-4 flex" />jsx.sortClassNames: tailwind<div className="flex p-4" />className={cn("p-4 flex")}jsx.sortClassNames: tailwindcn("p-4 flex")jsx.sortClassNames.functions: ["cn"]cn("flex p-4")tw`p-4 flex`jsx.sortClassNames.functions: ["tw"]tw`flex p-4`Spec and docs updates
New JSX attribute specs cover:
classandclassNameclassName={...}expression containersDocs updates cover:
jsx.sortClassNamesandjsx.sortClassNames.functionsCross-referenced against prettier-plugin-tailwindcss docs/source and Oxc's Tailwind sorting tests.
cargo testpasses.