heybourn / headwind

An opinionated Tailwind CSS class sorter built for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=heybourn.headwind
MIT License
1.38k stars 46 forks source link

Add ability use multiple regexes to match multiple strings #111

Closed petertriho closed 3 years ago

petertriho commented 3 years ago

Problem: difficult to match an arbitary number of strings e.g. className={clsx("flex items-center", error ? "bg-color-red-500 flex-col" : "bg-color-gray-100 flex-row")}. Current solution can only match the first string flex items-center

Solution: Adds the ability for headwind.classRegex values to be either a string regex (like it is currently, remains backwards compatible) or arrays of string regexes.

With arrays of regexes + recursion, we can use 2 regexes (1st regex matches className value and 2nd regex matches strings value inside the 1st regex values) e.g. 1st regex matches {clsx("flex items-center", error ? "bg-color-red-500 flex-col" : "bg-color-gray-100 flex-row")} and 2nd regex matches flex items-center, bg-color-red-500 flex-col and bg-color-gray-100 flex-row. The solution uses recursion so it theoretically could work any sized regex array.

Regex probably needs some work so that it'll work with more twin.macro scenarios but I believe I've covered most className scenarios

praveenperera commented 3 years ago

Hey @petertriho thanks this looks good, let me know when you want me to review

petertriho commented 3 years ago

@praveenperera good to go

praveenperera commented 3 years ago

This looks really good, good explanation too. Merged