An open API service indexing awesome lists of open source software.

https://github.com/schoero/eslint-plugin-better-tailwindcss

ESLint plugin to help you write better tailwindcss by improving readability with formatting rules and enforcing best practices with linting rules.
https://github.com/schoero/eslint-plugin-better-tailwindcss

auto-wrap eslint-plugin line-break multiline tailwindcss

Last synced: 4 months ago
JSON representation

ESLint plugin to help you write better tailwindcss by improving readability with formatting rules and enforcing best practices with linting rules.

Awesome Lists containing this project

README

          





eslint-plugin-better-tailwindcss logo

eslint-plugin-better-tailwindcss





eslint-plugin-better-tailwindcss logo
eslint-plugin-better-tailwindcss logo
eslint-plugin-better-tailwindcss logo
eslint-plugin-better-tailwindcss logo
eslint-plugin-better-tailwindcss logo
eslint-plugin-better-tailwindcss logo




ESLint/Oxlint plugin with formatting and linting rules to help you write cleaner, more maintainable Tailwind CSS.

The formatting rules focus on improving readability by automatically breaking up long Tailwind class strings into multiple lines and sorting/grouping them in a logical order. The linting rules enforce best practices and catch potential issues, ensuring that you're writing valid Tailwind CSS.

This plugin supports a wide range of projects, including React, Solid.js, Qwik, Svelte, Vue, Astro, Angular, HTML or plain JavaScript or TypeScript.





eslint-plugin-better-tailwindcss example








eslint-plugin-better-tailwindcss logo








[Buy me a coffee](https://buymeacoffee.com/schoero) | [GitHub Sponsors](https://github.com/sponsors/schoero)

Help support this project.
If you or your company benefit from this project, please consider becoming a sponsor or making a one-time donation.
Your contribution will help me to maintain and develop the project.




## Installation

```sh
npm i -D eslint-plugin-better-tailwindcss
```


## Quick start

Depending on the flavor you are using, you need to install and configure the corresponding parser:

- React: [.jsx](docs/parsers/jsx.md) · [.tsx](docs/parsers/tsx.md)
- SolidJS: [.jsx](docs/parsers/jsx.md) · [.tsx](docs/parsers/tsx.md)
- Qwik: [.jsx](docs/parsers/jsx.md) · [.tsx](docs/parsers/tsx.md)
- Svelte: [.svelte](docs/parsers/svelte.md)
- Vue: [.vue](docs/parsers/vue.md)
- Astro: [.astro](docs/parsers/astro.md)
- Angular: [.html, .ts](docs/parsers/angular.md)
- HTML: [.html](docs/parsers/html.md)
- CSS: [.css](docs/parsers/css.md)
- JavaScript: [.js](docs/parsers/javascript.md)
- TypeScript: [.ts](docs/parsers/typescript.md)




### Rules

The rules are categorized into two types: `stylistic` and `correctness`.

#### Configs

The plugin offers three recommended configurations to help you get started quickly:

- `stylistic`: Enforces stylistic rules for tailwind classes.
- `correctness`: Enforces correctness rules for tailwind classes.
- `recommended`: Enforces both stylistic and correctness rules.

By default:

- `stylistic` rules are reported as warnings
- `correctness` rules are reported as errors

You can change the severity by adding a suffix to the config name:

- Use `-error` to report all rules as errors
- Use `-warn` to report all rules as warnings

For example, `recommended-warn` will report every rule as a warning and `stylistic-error` will report the formatting rules as errors.

If you still use the old .eslintrc configuration format, you can prefix the config names with `legacy-`.

For example, `legacy-recommended` or `legacy-correctness-warn`.

The table below lists all available rules, the Tailwind CSS versions they support, and whether they are enabled by default in each recommended configuration:




#### Stylistic rules

| Name | Description | `tw3` | `tw4` | `recommended` | autofix |
| :--- | :--- | :---: | :---: | :---: | :---: |
| [enforce-consistent-line-wrapping](docs/rules/enforce-consistent-line-wrapping.md) | Enforce consistent line wrapping for tailwind classes. | ✔ | ✔ | ✔ | ✔ |
| [enforce-consistent-class-order](docs/rules/enforce-consistent-class-order.md) | Enforce a consistent order for tailwind classes. | ✔ | ✔ | ✔ | ✔ |
| [enforce-consistent-variable-syntax](docs/rules/enforce-consistent-variable-syntax.md) | Enforce consistent variable syntax. | ✔ | ✔ | | ✔ |
| [enforce-consistent-important-position](docs/rules/enforce-consistent-important-position.md) | Enforce consistent position of the important modifier. | ✔ | ✔ | | ✔ |
| [enforce-shorthand-classes](docs/rules/enforce-shorthand-classes.md) | Enforce shorthand class names. | ✔ | ✔ | | ✔ |
| [enforce-canonical-classes](docs/rules/enforce-canonical-classes.md) | Enforce canonical class names. | | ✔ | ✔ | ✔ |
| [no-duplicate-classes](docs/rules/no-duplicate-classes.md) | Remove duplicate classes. | ✔ | ✔ | ✔ | ✔ |
| [no-deprecated-classes](docs/rules/no-deprecated-classes.md) | Remove deprecated classes. | | ✔ | ✔ | ✔ |
| [no-unnecessary-whitespace](docs/rules/no-unnecessary-whitespace.md) | Disallow unnecessary whitespace in tailwind classes. | ✔ | ✔ | ✔ | ✔ |

#### Correctness rules

| Name | Description | `tw3` | `tw4` | `recommended` | autofix |
| :--- | :--- | :---: | :---: | :---: | :---: |
| [no-unknown-classes](docs/rules/no-unknown-classes.md) | Report classes not registered with Tailwind CSS. | ✔ | ✔ | ✔ | |
| [no-conflicting-classes](docs/rules/no-conflicting-classes.md) | Report classes that produce conflicting styles. | | ✔ | ✔ | |
| [no-restricted-classes](docs/rules/no-restricted-classes.md) | Disallow restricted classes. | ✔ | ✔ | | ✔ |




### Utilities

This plugin is pre-configured to lint tailwind classes for the most popular utilities:

- [tailwind merge](https://github.com/dcastil/tailwind-merge): `twMerge` · `twJoin`
- [class variance authority](https://github.com/joe-bell/cva): `cva`
- [tailwind variants](https://github.com/nextui-org/tailwind-variants?tab=readme-ov-file): `tv`
- [shadcn](https://ui.shadcn.com/docs/installation/manual): `cn`
- [classcat](https://github.com/jorgebucaran/classcat): `cc`
- [class list builder](https://github.com/crswll/clb): `clb`
- [clsx](https://github.com/lukeed/clsx): `clsx`
- [cnbuilder](https://github.com/xobotyi/cnbuilder): `cnb`
- [classnames template literals](https://github.com/netlify/classnames-template-literals): `ctl`
- [obj str](https://github.com/lukeed/obj-str): `objstr`




### Advanced configuration

If an utility is not supported by default, or you want to customize the configuration, you can define which [attributes](./docs/configuration/advanced.md#attribute), [callees](./docs/configuration/advanced.md#callee), [variables](./docs/configuration/advanced.md#variable), and [tags](./docs/configuration/advanced.md#tag) should get linted.
See the [Advanced configuration guide](./docs/configuration/advanced.md) to learn how to override or extend the default settings.




### Editor configuration

#### VSCode

##### Auto-fix on save

Most rules are intended to automatically fix the tailwind classes using VSCode extensions.

###### ESLint

For ESLint, you can install the [VSCode ESLint plugin](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and configure it to automatically fix the classes on save by adding the following options to your `.vscode/settings.json`:

```jsonc
{
// enable VSCode to fix tailwind classes on save
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
```

###### Oxlint

For Oxlint, you can install the [VSCode Oxc plugin](https://marketplace.visualstudio.com/items?itemName=oxc.oxc-vscode) and configure it to automatically fix the classes on save by adding the following options to your `.vscode/settings.json`:

```jsonc
{
// enable VSCode to fix tailwind classes on save
"editor.codeActionsOnSave": {
"source.fixAll.oxc": "explicit"
}
}
```