Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort classes using position of first matching rule #11504

Merged
merged 5 commits into from Jun 29, 2023

Conversation

thecrypticace
Copy link
Contributor

Fixes tailwindlabs/prettier-plugin-tailwindcss#174

Given a list of components like this:

addComponents({
  '.foo': { display: 'block' },
  '.foo-1': { display: 'block' },
  '.bar': { display: 'block' },

  // This rule matches both the utility `foo` and `bar`
  // But when sorting `foo` — we've already got a
  // position for `foo` so we should use it
  '.bar .foo': { display: 'block' },
})

Note that the last rule matches both the utilities foo and bar.

When sorting lists of classes we would iterate over all generated rules. Given that last rule matches both foo and bar it would win for any instance of foo used. This means that the utility foo would have a much "later" position than expected. For example, foo-1 foo would be sorted and stay as-is instead of becoming the expected foo foo-1.

Now, given that we cache rules, the internal utility we used to match against them could be overwritten. As such, once you used bar the utility was permanently changed and no longer matched the utility foo causing the sort order to change for any list of classes after the one that used the utility bar.

All that to say, given this HTML:

We would see the following HTML file as having sorted classes:

<span class="foo-1 foo"></span>
<span class="bar"></span>
<span class="foo foo-2"></span>

When in reality it should be this:

<span class="foo foo-1"></span>
<span class="bar"></span>
<span class="foo foo-2"></span>

This primarily affects components and utilities which contain multiple matched classes
@thecrypticace thecrypticace merged commit 40cb82c into master Jun 29, 2023
10 checks passed
@thecrypticace thecrypticace deleted the fix/sorting-with-multiple-matching-rules branch June 29, 2023 13:49
thecrypticace added a commit that referenced this pull request Jul 13, 2023
* Refactor

* Sort based on first occurence of a candidate

This primarily affects components and utilities which contain multiple matched classes

* Simplify

* Update changelog

* Update
thecrypticace added a commit that referenced this pull request Jul 13, 2023
* Refactor

* Sort based on first occurence of a candidate

This primarily affects components and utilities which contain multiple matched classes

* Simplify

* Update changelog

* Update
thecrypticace added a commit that referenced this pull request Jul 13, 2023
* Refactor

* Sort based on first occurence of a candidate

This primarily affects components and utilities which contain multiple matched classes

* Simplify

* Update changelog

* Update
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.

prettier path/to/file output differs from prettier .
1 participant