Skip to content

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

License

Notifications You must be signed in to change notification settings

schoero/eslint-plugin-better-tailwindcss

Repository files navigation

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 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, Angular, HTML or plain JavaScript or TypeScript.



eslint-plugin-better-tailwindcss example


eslint-plugin-better-tailwindcss logo

Buy me a coffee | GitHub Sponsors

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

npm i -D eslint-plugin-better-tailwindcss

Quick start

  1. Follow the parsers section below to learn how to configure the plugin for your specific requirements.

  2. Configure the plugin to be able to read your tailwind configuration via settings or for each rule separately.

    // eslint.config.js
    {
      //...
      "settings": {
        "better-tailwindcss": {
          // tailwindcss 4: the path to the entry file of the css based tailwind config (eg: `src/global.css`)
          "entryPoint": "src/global.css",
          // tailwindcss 3: the path to the tailwind config file (eg: `tailwind.config.js`)
          "tailwindConfig": "tailwind.config.js"
        }
      }
    }
  3. Configure your editor to conveniently auto-fix on save.



Parsers

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



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.

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
multiline Enforce consistent line wrapping for tailwind classes.
no-unnecessary-whitespace Disallow unnecessary whitespace in tailwind classes.
sort-classes Enforce a consistent order for tailwind classes.
no-duplicate-classes Remove duplicate classes.

Correctness rules

Name Description tw3 tw4 recommended autofix
no-unregistered-classes Report classes not registered with tailwindcss.
no-conflicting-classes Report classes that produce conflicting styles.
no-restricted-classes Disallow restricted classes.


Utilities

This plugin works out of the box with most popular tailwind utilities:



Advanced configuration

If an utility is not supported by default, or you want to customize the configuration, you can define which string literals should be linted for each rule. See the Advanced configuration guide 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. If you have installed the VSCode ESLint plugin, you can configure it to automatically fix the classes on save by adding the following options to your .vscode/settings.json:

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


About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors 5

Languages