Rules enforcing best practices while using Tailwind CSS
npm install eslint-plugin-tailwindcss!npm latest version !license !downloads
!eslint-plugin-tailwindcss logo
Rules enforcing best practices and consistency using Tailwind CSS.
While you can use the official plugin prettier-plugin-tailwindcss for ordering your classnames...
eslint-plugin-tailwindcss offers more than 5 other rules, that you can benefit from on top of prettier-plugin-tailwindcss. Sounds good ? Keep reading π
While the development of eslint-plugin-tailwindcss for Tailwind CSS v4 is ongoing, you can use the latest version published on the beta channel to get partial support of Tailwind CSS v4.
npm i eslint-plugin-tailwindcss@beta -D
> NB: As we will focus the effort on the full rewrite of the plugin, this version is available "as is" and you may get errors or false positives like for the rule no-contradicting-classname . You can learn more about these issues on GitHub.
You can always disable specific rules if necessary.
This version has been made possible thanks to the work of hyoban and his project tailwind-api-utils.
Learn more about each supported rules by reading their documentation:
- classnames-order: order classnames for consistency and it makes merge conflict a bit easier to resolve
- enforces-negative-arbitrary-values: make sure to use negative arbitrary values classname without the negative classname e.g. -top-[5px] should become top-[-5px]
- enforces-shorthand: merge multiple classnames into shorthand if possible e.g. mx-5 my-5 should become m-5
- migration-from-tailwind-2 for easy upgrade from Tailwind CSS v2 to v3.
Warning: at the moment you should temporary turn off the no-custom-classname rule if you want to see the warning from migration-from-tailwind-2
- no-arbitrary-value: forbid using arbitrary values in classnames (turned off by default)
- no-custom-classname: only allow classnames from Tailwind CSS and the values from the whitelist option
- no-contradicting-classname: e.g. avoid p-2 p-3, different Tailwind CSS classnames (pt-2 & pt-3) but targeting the same property several times for the same variant.
- no-unnecessary-arbitrary-value: e.g. replacing m-[1.25rem] by its configuration based classname m-5
Using ESLint extension for Visual Studio Code, you will get these messages
!detected-errors
You can can the same information on your favorite command line software as well.
eslint-plugin-tailwindcss| π₯° How you can support us? | πͺ They did it! |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Premium Sponsors
Support us by becoming a sponsor.
Become a recurring sponsor |
|
| Current Sponsors
Any amount is appreciated. |
|
| Past sponsors
Even if this is just a one-time thing.
Become a backer |
|
| Contributors
This project can evolve thanks to all the people who contribute.
You are welcome to contribute to this project by reporting issues, feature requests or even opening Pull Requests. | |
| Supporters
Talk about the plugin on your social networks | Share the word on Bluesky or Reach my profile |
- feat: partial support for Tailwind CSS v4 (by hyoban π)
- feat: enforces-shorthand rule now support place-content-/place-items-/place-self-*
- fix: enforces-shorthand does not work for h- & w- when prefixed (by Kamahl19 π)
- docs: so long X
- fix: custom classnames rule with VueJS objects within list (by Ericlm π)
- fix: config loading cache
- fix: Avoid re-parsing unchanged CSS files
- fix: AST expression tests for null expressions (by kesor π)
- fix: no-arbitrary-value rule is too broad
- fix: support tag.div and tag(Component) (by nihalgonsalves π)
- feat: support flat config and ESLint 9 (by kazupon π)
- feat: new rule no-unnecessary-arbitrary-value π
- fix: retro compatibility for older Tailwind CSS (before typescript config)
- fix: composable touch action classnames
- fix: [shadow-md + shadow-[#color]can be used together π€](https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/298)
- fix: tabular-nums and slashed-zero can be used together π€
- fix: size-* based size, spacing, width and height π€
- fix: there is no size-screen π
- fix: edge cases with whitespace in enforces-shorthand(by kachkaev π)
- fix: parsing spreads in function call returns(by egorpavlikhin π)
- feat: support for Tailwind CSS 3.4.0
- ci: add github actions workflow (by nihalgonsalves π)
- fix: [bg-center mark as conflicting with bg-[image:xxx]](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/260)
- feat: support enforcing truncate shorthand (by bezbac π)
- fix: parsing spreads in object expressions (by bezbac π)
- fix: do not handle non-ASCII whitespace as separator (by uhyo π)
- fix: prefix support for named group/peer syntax (by bezbac π)
- feat: support tailwind config in typescript (by quesabe π)
You may have to upgrade your Tailwind CSS version to 3.3.2
|
| ESLint plugin Tailwind CSS
youtube.com/@eslint-plugin-tailwindcss |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
You'll first need to install ESLint:
``
$ npm i -D eslint eslint-plugin-tailwindcss
`
#### .eslintrc
Use .eslintrc.\* file to configure rules in ESLint < v9. See also: https://eslint.org/docs/latest/use/configure/.
`js`
module.exports = {
root: true,
extends: ["plugin:tailwindcss/recommended"],
};
If you would like to know about configuration, Learn more in ESLint docs
#### eslint.config.js
Use eslint.config.js file to configure rules. This is the default in ESLint v9, but can be used starting from ESLint v8.57.0. See also: https://eslint.org/docs/latest/use/configure/configuration-files-new.
`js
import tailwind from "eslint-plugin-tailwindcss";
export default [...tailwind.configs["flat/recommended"]];
`
If you would like to know about configuration, Learn more in ESLint docs
Depending on the languages you are using in your project you must tell which parser will analyze your source files.
Our recommendations:
#### For .eslintrc
- For js[x], react, ts[x]:npm i -D @typescript-eslint/parser
- Install the parser: eslintrc
- Assign it to your files in :`
json5`
overrides: [
{
files: ['.ts', '.tsx', '*.js'],
parser: '@typescript-eslint/parser',
},
],
vue.js
- For :npm i -D vue-eslint-parser
- Install the parser: eslintrc
- Assign it to your files in :`
json5`
overrides: [
{
files: ['*.vue'],
parser: 'vue-eslint-parser',
},
],
HTML
- For and similar:npm i -D @angular-eslint/template-parser
- Install the parser: eslintrc
- Assign it to your files in :`
json5`
overrides: [
{
files: ['.html', '.blade.php'],
parser: '@angular-eslint/template-parser',
},
],
> We removed the default parsers which were added to v3.8.2 because it created negative impact on dependencies resolution, bundle size increase and possible conflicts with existing configurations.
#### For eslint.config.js
- For js[x], ts[x]:
- Install the parser: npm i -D @eslint/js typescript-eslinteslint.config.js
- Assign it to your files in :
`js
import js from "@eslint/js";
import ts from "typescript-eslint";
import tailwind from "eslint-plugin-tailwindcss";
export default [
// add eslint built-in
js.configs.recommended,
// add typescript-eslint flat config simply`
// if you would like use more another configuration,
// see the section: https://typescript-eslint.io/getting-started#details
...ts.configs.recommended,
...tailwind.configs["flat/recommended"],
];
- For vue.js:
- Install the parser: npm i -D eslint-plugin-vueeslint.config.js
- Assign it to your files in :
`js
import vue from "eslint-plugin-vue";
import tailwind from "eslint-plugin-tailwindcss";
export default [
// add eslint-plugin-vue flat config simply`
// if you would like use more another configuration,
// see the section: https://eslint.vuejs.org/user-guide/#bundle-configurations-eslint-config-js
...vue.configs["flat/recommended"],
...tailwind.configs["flat/recommended"],
];
In your package.json add one or more script(s) to run eslint targeting your source files:
`json5`
"scripts": {
"lint": "eslint ./src",
"lint:debug": "eslint ./src --debug",
"lint:fix": "eslint ./src --fix"
},
npm run lint can do the job on demand but you can also get live feedback using VS Code ESLint extension, just make sure you restart VS Code as it can be required for the plugin to work as expected.
The rules accept settings meant to fit your own choices, make sure to read the documentation of each rule.
Most rules share the same settings, instead of duplicating the options all over the place...
You should define the shared settings that will be shared across all the plugin rules.
All these settings already have nice default values that are explained in the documentation.
#### For .eslintrc
FYI, here are the default values:
`json5loadConfig()
{
settings: {
tailwindcss: {
// These are the default values but feel free to customize
callees: ["classnames", "clsx", "ctl"],
config: "tailwind.config.js", // returned from utility if not providedbg-blue
cssFiles: [
"*/.css",
"!**/node_modules",
"!*/.",
"!**/dist",
"!**/build",
],
cssFilesRefreshRate: 5_000,
removeDuplicates: true,
skipClassAttribute: false,
whitelist: [],
tags: [], // can be set to e.g. ['tw'] for use in twtwin.macro
classRegex: "^class(Name)?$", // can be modified to support custom attributes. E.g. "^tw$" for `
},
},
}
#### For eslint.config.js
`js
import tailwind from "eslint-plugin-tailwindcss";
export default [
...tailwind.configs["flat/recommended"],
{
settings: {
tailwindcss: {
// These are the default values but feel free to customize
callees: ["classnames", "clsx", "ctl"],
config: "tailwind.config.js", // returned from loadConfig() utility if not providedbg-blue
cssFiles: [
"*/.css",
"!**/node_modules",
"!*/.",
"!**/dist",
"!**/build",
],
cssFilesRefreshRate: 5_000,
removeDuplicates: true,
skipClassAttribute: false,
whitelist: [],
tags: [], // can be set to e.g. ['tw'] for use in twtwin.macro
classRegex: "^class(Name)?$", // can be modified to support custom attributes. E.g. "^tw$" for `
},
},
},
];
The plugin will look for each setting in this order and stops searching as soon as it finds the settings:
1. In the rule option argument (rule level)
2. In the shared settings (plugin level)
3. Default value of the requested setting (plugin level)...
- validate-modifiers: I don't know if possible, but I'd like to make sure all the modifiers prefixes of a classname are valid e.g. yolo:bg-red should throw an error...
- no-redundant-variant: e.g. avoid mx-5 sm:mx-5, no need to redefine mx in sm: variant as it uses the same value (5)
- only-valid-arbitrary-values:
- e.g. avoid top-[42], only 0 value can be unitless.text-[rgba(10%,20%,30,50%)]
- e.g. avoid , can't mix % and 0-255`.