Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-better-tailwindcss

schoero157.6kMIT3.7.5TypeScript support: included

auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.

eslint, eslint-plugin, stylistic, formatting, tailwind, readable, readability, horizontal, scrolling, multiline, multi, newline, line, break, linebreak, wrap, template, literal, jsx, html, astro, svelte, vue, react, qwik, solid, template-literal, template-literals, tailwindcss, tailwind-css, tailwind-classes

readme

<picture> <source media="(prefers-color-scheme: dark)" srcset="./assets/eslint-plugin-better-tailwindcss-logo-dark.svg"> <source media="(prefers-color-scheme: light)" srcset="./assets/eslint-plugin-better-tailwindcss-logo-light.svg"> eslint-plugin-better-tailwindcss logo </picture>

eslint-plugin-better-tailwindcss



<picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/license/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/github/license/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture> <picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/npm/v/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/npm/v/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture> <picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/issues/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/github/issues/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture> <picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/npm/dt/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/npm/dt/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture> <picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/stars/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/github/stars/schoero/eslint-plugin-better-tailwindcss?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture> <picture><source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/sponsors/schoero?style=flat-square&labelColor=32363B&color=ffffff" /><source media="(prefers-color-scheme: light)" srcset="hhttps://img.shields.io/github/sponsors/schoero?style=flat-square&labelColor=EBEEF2&color=000000" />eslint-plugin-better-tailwindcss logo</picture>


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



eslint-plugin-better-tailwindcss example


<picture> <source media="(prefers-color-scheme: dark)" srcset="./assets/sponsor-dark.svg"> <source media="(prefers-color-scheme: light)" srcset="./assets/sponsor-light.svg"> eslint-plugin-better-tailwindcss logo </picture>

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
enforce-consistent-line-wrapping Enforce consistent line wrapping for tailwind classes.
enforce-consistent-class-order Enforce a consistent order for tailwind classes.
enforce-consistent-variable-syntax Enforce consistent variable syntax. | ✔
enforce-consistent-important-position Enforce consistent position of the important modifier. | ✔
enforce-shorthand-classes Enforce shorthand class names. | ✔
no-duplicate-classes Remove duplicate classes.
no-deprecated-classes Remove deprecated classes. | ✔ | ✔
no-unnecessary-whitespace Disallow unnecessary whitespace in tailwind 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"
  }
}


changelog

Changelog

v3.7.5

compare changes

Fixes

  • Matching object values with immediate indexed access (#212)

v3.7.4

compare changes

Fixes

  • Error in no-conflicting-classes when used in tailwindcss 3 (#205)
  • Invalid config warning when config was actually found (#206)
  • Differentiate shorthands for the same classes with different variants (#207)

v3.7.3

compare changes

Fixes

  • Invalid fix for multiple vars in enforce-consistent-variable-syntax (#200)

v3.7.2

compare changes

Fixes

  • Error when no tsconfig is available (#195)

Refactors

  • Refine cache (#196)

v3.7.1

compare changes

Fixes

  • no-unnecessary-whitespace false positive on empty string (#191)
  • Don't convert variable definitions (#192)

Chore

  • Update dependencies (#193)

v3.7.0

compare changes

Features

  • Support tsconfig paths (#185)

Refactors

  • Exact unnecessary whitespace fixes (#184)

v3.6.3

compare changes

Fixes

Refactors

  • Add missing deprecations (#181)
  • Variable syntax tailwindcss3 shorthand (#183)

v3.6.2

compare changes

Fixes

  • Fixes crash when importing css files via tsconfig path alias and detectComponentClasses enabled (#178)
  • Fixes component classes not getting updated when inside an imported file (#178)
  • Disallow extra properties in rule options (#180)

❤️ Contributors

v3.6.1

compare changes

Fixes

  • Recursively reading imports (#175)

v3.6.0

compare changes

Features

  • New rule enforce-consistent-important-position (#167)
  • New rule no-deprecated-classes (#169)

Fixes

  • Support starting important in enforce-shorthand-classes (#164)
  • Error position (a55a6cc)

v3.5.2

compare changes

Fixes

  • Tailwind 3 shorthand classes with important modifier (#162)

v3.5.1

compare changes

Fixes

  • False reports of shorthand classes (c5f14ab)

v3.5.0

compare changes

Features

  • New Rule: Enforce shorthand classes (#153)

Fixes

  • Bump tailwindcss peer dependency (#157)
  • Regex deprecation warning (#161)

v3.4.4

compare changes

Fixes

  • Altering variant order in tailwindcss cache (#151)

Documentation

  • Add example for arbitrary values (ef6faa2)

v3.4.3

compare changes

Fixes

  • Prevent removal of whitespace between template literals (#147)
  • Extract class variants via tailwind (#146)

v3.4.2

compare changes

Fixes

  • Template literals resulting in undefined path in getESObjectPath causing false positives (#142)

❤️ Contributors

v3.4.1

compare changes

Fixes

  • Detect conflicts with multiple properties (#137)

v3.4.0

compare changes

Features

  • Add customizable autofix option to no-restricted-classes (#133)

Refactors

  • Rename rules for better consistency (#134)

    • better-tailwindcss/multiline -> better-tailwindcss/enforce-consistent-line-wrapping
    • better-tailwindcss/sort-classes -> better-tailwindcss/enforce-consistent-class-order

    The old names will still work for now, but will be removed in the next major version.

v3.3.1

compare changes

Fixes

  • Prevent variable matchers from crossing arrow function boundaries (#131)
  • Sorting order with unregistered class with variant (#132)

v3.3.0

compare changes

Features

  • No-restricted-classes rule to support custom error messages (#129)

Fixes

v3.2.1

compare changes

Fixes

  • Don't report inside member expressions (#120)

v3.2.0

compare changes

Features

  • Auto detect custom component layer classes (#111)
  • Ignore prefix in groups (#110)
  • Support prefixed groups and tags (#115)

Fixes

  • Add additional tailwind variants matchers (#116)

v3.1.0

compare changes

Features

  • Add support for astro syntactic sugar (#103)
  • New rule enforce consistent variable syntax (#101)

Fixes

  • Remove name property (#105)

v3.0.0

compare changes

This version adds 3 new correctness rules to the plugin. To better reflect the new scope of the plugin it was renamed from eslint-plugin-readable-tailwind to eslint-plugin-better-tailwindcss. https://github.com/schoero/eslint-plugin-readable-tailwind/issues/86#issuecomment-2855845766

The predefined configs also have been renamed to better reflect their scope.

Features

⚠️ Breaking changes

  • Plugin renamed to eslint-plugin-better-tailwindcss
  • Deprecate Regex matchers to simplify the configuration. (#98)
    Regex matchers were an early attempt to make the plugin more flexible. However, they were quickly replaced with Matchers which work on the Abstract Syntax Tree and are far more powerful. Support for Regex matchers will be removed in the next major version.

  • warning and error configs have been removed. Use recommended-warn or recommended-error instead. (#99)

Migration

  1. Replace eslint-plugin-readable-tailwind with eslint-plugin-better-tailwindcss:

    npm uninstall eslint-plugin-readable-tailwind
    npm i -D eslint-plugin-better-tailwindcss
  2. Update the imports in your config:

    - import eslintPluginReadableTailwind from "eslint-plugin-readable-tailwind"; 
    + import eslintPluginBetterTailwindcss from "eslint-plugin-better-tailwindcss";
  3. Migrate to the new configs

    rules: {
        // enable all recommended rules to warn
    -    ...eslintPluginReadableTailwind.configs.warning.rules,
    +   ...eslintPluginBetterTailwindcss.configs["recommended-warn"].rules,
        // enable all recommended rules to error
    -    ...eslintPluginReadableTailwind.configs.error.rules,
    +    ...eslintPluginBetterTailwindcss.configs["recommended-error"].rules,
    
       // or configure rules individually
    -    "readable-tailwind/multiline": ["warn", { printWidth: 100 }]
    +    "better-tailwindcss/multiline": ["warn", { printWidth: 100 }] 
     }

v2.1.2

compare changes

Fixes

  • Multiline quotes (#96)

Refactors

  • Report error for each duplicate class instead of the whole class string (#91)

v2.1.1

compare changes

Fixes

  • Unnecessarily escaped quotes in autofixed classes (#88)

v2.1.0

compare changes

Features

  • Experimental angular support. (#85)

Fixes

  • Keep carriage return in es literals when used with vue parser (#84)

v2.0.1

compare changes

Fixes

  • Keep original newline characters (a564783)

Refactors

  • Display warning if plugin is misconfigured (7c532cd)

Documentation

  • Update quick start guide (e570981)

v2.0.0

Adds tailwindcss v4 support while keeping support for tailwindcss v3. (#78)

This version contains breaking changes. Most notably support for Node.js < 20 had to be dropped. The other breaking changes are mostly just changes of the default config, that may cause linting errors.

Migration

  • If you use tailwindcss v4, you should specify the entryPoint of the css based tailwind configuration file for the sort-classes rule or in the settings.
  • If you have customized the classAttributes option for any of the rules or via the settings, rename the option to attributes
  • If you have customized attributes, callees, variables, or tags, escape any reserved characters for regular expressions in the name as the name is now evaluated as a regular expression.

    For example:

     {
       variables: [
    -    "$MyVariable"
    +    "\\$MyVariable"
       ]
     }

Changes

  • Reload tailwind config automatically if a change is detected.
  • Options now correctly override settings (#66)

⚠️ Breaking Changes

  • ⚠️ Drop support for Node.js < 20 due to incompatibility of worker threads.
  • ⚠️ Add support for tailwindcss v4 (#25)

    • The official class ordering seems to have changed slightly.
    • The improved sorting order will no longer sort variants alphabetically, instead it just makes sure that identical variants are grouped together.
  • ⚠️ Regex names (#63)

    • "Names" can now be regular expressions. This is a breaking change, if you have names configured that contain reserved characters in regular expressions like $.
  • ⚠️ Enable no-duplicate-classes by default (#67)
  • ⚠️ Change default multiline grouping to newLine (#68)
  • ⚠️ Rename classAttributes to attributes (#69)

v1.9.1

compare changes

Fixes

  • Lint className in render functions inside object (#75)

v1.9.0

compare changes

Features

  • Template literal tags (#65)

v1.8.2

compare changes

Fixes

  • Fixing loop when lines wrap on two lines immediately but was theoretically short enough to not wrap (#61)

v1.8.1

compare changes

Refactors

  • Improve display of linting errors (#60)

v1.8.0

compare changes

Features

  • Add support to globally configure shared options across all rules via the settings object (#56)

v1.7.0

compare changes

Features

  • New option preferSingleLine (#54)

v1.6.1

compare changes

Fixes

  • Group type never not working with expressions (#53)

v1.6.0

compare changes

Features

  • New rule no-duplicate-classes (#49)
    This rule will be enabled by default in v2.0.0. If you want to enable it now, please refer to the rule documentation.
    You can suggest additional rules in the discussions.

Refactors

v1.5.3

compare changes

Refactors

  • Insertion of unnecessary escape characters (#47)

v1.5.2

compare changes

Fixes

  • Remove unnecessary plugin import in shared config (#44)
  • Support svelte shorthand syntax (#43)

v1.5.1

compare changes

Fixes

  • Commonjs build (#39)

v1.5.0

compare changes

Features

  • Vue bound classes (#31)

Fixes

  • Change quotes in multiline arrays (#32)
  • Escape nested quotes (#33)
  • Allow call expressions as object values (#34)
  • Attributes are no longer case sensitive (#35)
  • Warn in html matchers (#36)
  • Don't treat escape characters as whitespace (6aa74f8)

Refactors

  • Simplify build system (#26, #29)

v1.4.0

compare changes

Features

  • Matchers (#28)

v1.3.2

compare changes

Fixes

  • Remove unnecessary newline after single sticky class (#23)
  • Prevent inserting new line if the first class is already too long (#24)

Tests

  • Simplify testing (#22)

v1.3.1

compare changes

Fixes

  • Accept tabs (#21)

v1.3.0

compare changes

Features

  • Add eslint 9 support (#19)

Chore

v1.2.5

compare changes

Performance

  • Cache tailwind config and context (#16)

Fixes

  • Resolving tailwind config (#15)

v1.2.4

compare changes

Fixes

  • Sticky expressions (#13)

v1.2.3

compare changes

Fixes

  • Remove unnecessary trailing spaces in multiline strings (#12)
  • False positives when using crlf (#11)

v1.2.2

compare changes

Fixes

  • False positives of unnecessary whitespace around template literal elements (#9)

v1.2.1

compare changes

Fixes

  • Don't wrap empty attributes (#8)

v1.2.0

compare changes

Features

  • Lint variables (#7)

Fixes

  • Apply nested regex only to container groups (#6)

v1.1.1

compare changes

Fixes

  • Invalid collapsing with template literal expressions (adfafbf)

v1.1.0

compare changes

Features

  • Collapse unnecessary newlines (#4)
  • Regex as callees (#3)