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

[BUG] Support of Vue v-bind:class #168

Closed
extrem7 opened this issue Sep 23, 2022 · 17 comments · Fixed by #208
Closed

[BUG] Support of Vue v-bind:class #168

extrem7 opened this issue Sep 23, 2022 · 17 comments · Fixed by #208
Labels
bug Something isn't working feedback needed

Comments

@extrem7
Copy link

extrem7 commented Sep 23, 2022

Seems like the plugin doesn't work with Vue v-bind:class functionality.

image

@extrem7 extrem7 added the bug Something isn't working label Sep 23, 2022
@kevinvalk
Copy link

This is a shame because https://tailwindcss.com/blog/automatic-class-sorting-with-prettier does sort classes in :class.

@innocenzi
Copy link

Would be awesome. I tried with the existing options but couldn't manage to find a regex that could work.

@francoismassart
Copy link
Owner

@extrem7, @kevinvalk, @innocenzi, @NielsJanssen, @hrubysi
I'm working on this and I'll share a beta version soon starting only with no-custom-classname rule.

Are you willing to test it out and give feedback using real/complex projects once it is available in the beta channel ?
Instructions will follow 😉

@innocenzi
Copy link

Awesome! I would love to.

@francoismassart
Copy link
Owner

francoismassart commented Feb 1, 2023

Here is a first beta for the no-custom-classname rule only, all rules will follow in the next betas

npm i eslint-plugin-tailwindcss@3.9.0-beta.0 -D

👉 Make sure the no-custom-classname rule is enabled.

I made an invalid test in https://github.com/francoismassart/eslint-plugin-tailwindcss/blob/feat/v-bind/tests/lib/rules/wip.js#L71, take a look at it if you want to know what to expect in terms of usage.

Thank you for your feedback(s)

@francoismassart
Copy link
Owner

FYI: I don't think I'll go as far as checking the value of a variable inside the data() of a SFC to validate its value.

An easy fix for this is using a callee like ctl to "force" eslint-plugin-tailwindcss to validate the arguments as Tailwind CSS classnames...

We use this same logic in our react project when we pass props meant to contain Tailwind CSS classnames (which are not named class or className) .

e.g.

...
<Transition
  as={Fragment}
  enter={ctl('transition duration-100 ease-out')}
  enterFrom={ctl('scale-95 opacity-0')}
  enterTo={ctl('scale-100 opacity-100')}
  leave={ctl('transition duration-75 ease-in')}
  leaveFrom={ctl('scale-100 opacity-100')}
  leaveTo={ctl('scale-95 opacity-0')}
>
...

@innocenzi
Copy link

Hey! So no-custom-classname works well:
image

It's overwhelming though that the whole :class is erroring instead of just the actual line/string that contains the class name.

Seems like other rules are not yet implemented?

@francoismassart
Copy link
Owner

The entire underlining is normal, it could be enhanced for sure but it disappears quickly especially if the error is fixable automatically by the plugin.

As stated in my previous comment, for now only the no-custom-classname is supported but more rules will be published in beta channel soon and I’ll post it in this issue.

@innocenzi
Copy link

Awesome! Thanks for your work. Looking forward to the sorting. ❤️

@francoismassart
Copy link
Owner

Here is a new beta version with all the rules available for v-bind

👉 npm i eslint-plugin-tailwindcss@3.9.0-beta.1

Keep in mind that arrays and objects passed to a :class attribute are parsed by its inner parts "in isolation"...

The plugin won't lint array elements "amongst themselves":
e.g. :class="['p-0', 'p-10', 'm-0']" won't throw a contradicting error for p-0 vs p-10
but :class="['p-0 p-10', 'm-0']" will throw a contradicting error for p-0 vs p-10

I'll keep it in beta for few days at least, please test it out.

Thank you for the feedback.

@innocenzi
Copy link

Hey, just tried that — seems to work perfectly fine for arrays at first sight.

Classname order doesn't seem to work for objects in arrays:
image

It does work for :class with a single object though:
image

francoismassart added a commit that referenced this issue Feb 2, 2023
@francoismassart
Copy link
Owner

@innocenzi 👇
npm i eslint-plugin-tailwindcss@3.9.0-beta.2

should do the trick 🎉

@innocenzi
Copy link

innocenzi commented Feb 2, 2023

Can confirm, does the trick 🎉

Not sure how hard it would be to implement, but it would be great if no-contradicting-classname and enforces-shorthand could work per :class block and not per individual line, just like the current underlining works.

However, I'm really happy with what's currently in 3.9.0-beta.2 — so, thanks again :)

@hrubysi
Copy link

hrubysi commented Feb 2, 2023

It seems to work well for me.
Thank you for this! I hated to use prettier just to be able to sort tailwind classes.

@hrubysi
Copy link

hrubysi commented Feb 3, 2023

@francoismassart I found a bug while using v-bind.

Error:

TypeError: Cannot read properties of null (reading 'name')
Rule: "tailwindcss/classnames-order"

Triggering code:

<slot v-bind="slotData"></slot>

const slotData = {
  vModel: model,
};

@francoismassart
Copy link
Owner

@hrubysi thank you, I’ll fix this and publish a new beta

@francoismassart
Copy link
Owner

@hrubysi 👉 npm i eslint-plugin-tailwindcss3.9.0-beta.3 -D

@francoismassart francoismassart mentioned this issue Feb 20, 2023
4 tasks
francoismassart added a commit that referenced this issue Feb 20, 2023
* chore: add wip v-bind tests for no-custom-classname

* chore: first beta tests for v-bind

* 3.9.0-beta.0

* refactor(no-custom-classname): templateVisitor

* tests(no-custom-classname): v-bind

* feat(no-contradicting-classname): v-bind

* feat(no-arbitrary-value): v-bind

* feat(migration-from-tailwind-2): v-bind

* feat(enforces-shorthand): v-bind

* feat(enforces-negative-arbitrary-values): v-bind

* feat(classnames-order): v-bind

* chore: cleaning temp code

* 3.9.0-beta.1

* fix: #168 (comment)

* 3.9.0-beta.2

* fix: property of null

* 3.9.0-beta.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feedback needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants