eslint-plugin-tailwindcss icon indicating copy to clipboard operation
eslint-plugin-tailwindcss copied to clipboard

[BUG] Support eslint's new config file (flat) for recommended rules

Open beorn opened this issue 1 year ago • 17 comments

When using eslint's new config file format, such as this eslint.config.js:

import tw from "eslint-plugin-tailwindcss"

export default [
  tw.configs.recommended
]

Errors out:

Oops! Something went wrong! :( ESLint: 8.50.0 A config object has a "plugins" key defined as an array of strings. Flat config requires "plugins" to be an object in this form: { plugins: { tailwindcss: pluginObject } } Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#importing-plugins-and-custom-parsers If you're using a shareable config that you cannot rewrite in flat config format, then use the compatibility utility: https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config

Is there a way to add recommended tailwind rules using ESlint's new config format?

beorn avatar Sep 28 '23 21:09 beorn

Hi @beorn The plugin doesn't support the new config format yet. Do you use other 3rd party eslint linter which support it so that I can see how to implement it ?

francoismassart avatar Oct 02 '23 13:10 francoismassart

Hi, @beorn According to the documentation, I tried the following and it worked.

npm install @eslint/eslintrc --save-dev

eslint.config.js:

import { FlatCompat } from '@eslint/eslintrc'

const compat = new FlatCompat()

export default [
  // mimic ESLintRC-style extends
  ...compat.extends({
    extends: ['plugin:tailwindcss/recommended'],
    rules: {
      'tailwindcss/no-custom-classname': 'off',
      'tailwindcss/migration-from-tailwind-2': 'off',
    },
  }),
]

Sun-ZhenXing avatar Oct 04 '23 12:10 Sun-ZhenXing

Hi @francoismassart The @antfu/eslint-config widely used in vue or nuxt is flat style, maybe you can refer to how he does the convert? It would be great to be able to combine them!

awdr74100 avatar Oct 06 '23 20:10 awdr74100

@awdr74100, this is my temporary method.

https://github.com/Sun-ZhenXing/algo-code-mgr/blob/main/eslint.config.js

Sun-ZhenXing avatar Oct 07 '23 01:10 Sun-ZhenXing

@Sun-ZhenXing Yes, thanks, it seems like using FlatCompat.compat is the best way currently. Ideally there should be some mention of this in the docs and/or useful error/warning messages when you do it wrong.

beorn avatar Oct 07 '23 03:10 beorn

A migration guide has been added to eslint's docs: https://eslint.org/docs/latest/extend/plugin-migration-flat-config

thenbe avatar Nov 04 '23 05:11 thenbe

We only use project that use eslint flat config at this point, any updates to this being supported?

itpropro avatar Feb 26 '24 16:02 itpropro

@itpropro you can use this workaround: https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/280#issuecomment-1746781848. There is no need to wait till all plugins have migrated to flat config.

kachkaev avatar Feb 26 '24 16:02 kachkaev

Be careful you need to use ...compat.config({...}) and not ...compat.extends({...})

irg1008 avatar Mar 22 '24 12:03 irg1008

@Sun-ZhenXing I can't seem to get @antfu/eslint-config to work with tailwind class sorting in .vue files. I've tried everything, is there anything special you did?

tylerolson avatar Apr 01 '24 00:04 tylerolson

Here's my config, using antfu's v2.11 eslint config

// https://github.com/antfu/eslint-config#customization
import antfu from "@antfu/eslint-config";

// workaround for flat config not being supported yet by eslint-plugin-tailwindcss
// https://github.com/francoismassart/eslint-plugin-tailwindcss/issues/280
import { FlatCompat } from "@eslint/eslintrc";

const compat = new FlatCompat();

export default antfu(
  {
    // Customize the stylistic rules
    stylistic: {
      quotes: "double", // or 'double'
      semi: true,
    },
    rules: {
      // Changing the default, we don't like this because when you rename ie. a field then things break
      "object-shorthand": ["warn", "never"],

      // We like our curly braces. Always.
      "curly": ["warn", "all"],

      // Keep line length below 120 characters
      "max-len": ["error", { code: 120 }],

      // Experimenting too much for now.
      // TODO evaluate if we need to enable this again
      "no-console": "warn",
    },
    formatters: {
      graphql: "prettier",
      css: "prettier",
      markdown: "prettier",
      html: "prettier",
      prettierOptions: {
      },
    },
  },
  {
    files: ["*.json"],
    rules: {
      "max-len": "off",
    },
  },
  ...compat.config({
    // https://github.com/francoismassart/eslint-plugin-tailwindcss
    extends: ["plugin:tailwindcss/recommended"],
    rules: {
      "tailwindcss/no-custom-classname": "off",
    },
  }),
  {
    ignores: ["**/generated.*", "**/locales/generated/**"],
  },
);

DavidDeSloovere avatar Apr 01 '24 07:04 DavidDeSloovere

Tailwindcss errors/warnings appear when I manually run eslint . but do not appear in vscode no matter what I try. General eslint and vue errors appear. This means that tailwind auto sorting is only working when I manually run eslint . --fix

My package.json has these scripts:

"lint": "eslint .",
"lint:debug": "eslint . --debug",
"lint:fix": "eslint . --fix"

Seems to be a vscode errors, Ive tried literally everything.

Edit:

It seems I am finding an issue between the Eslint Vscode extention and using this plugin in flat style. I am able to get it fully working using old .cjs without @antfu/eslint-config, however the Vscode extension seems to completely ignores that plugin, formatting everything else.

Screenshot 2024-04-02 at 12 50 35 AM Screenshot 2024-04-02 at 12 50 51 AM

tylerolson avatar Apr 02 '24 04:04 tylerolson

I have this, and many other changes, in .vscode/settings.json - maybe that's the missing piece

{
  /****************************************
   * Ant Fu eslint
   ****************************************/
  // https://github.com/antfu/eslint-config#vs-code-support-auto-fix
  // Enable the ESlint flat config support
  "eslint.experimental.useFlatConfig": true,
  ..........
}

DavidDeSloovere avatar Apr 02 '24 08:04 DavidDeSloovere

Still can't get eslint-plugin-tailwindcss to show up in vscode. Not sure if I should keep trying, make a new issue, or be stuck with prettier.

tylerolson avatar Apr 06 '24 10:04 tylerolson

@tylerolson it definitely works in VS Code via compat as already confirmed by several others in this thread.

It's hard to help you without seeing the full code of your configuration. Please submit a minimal repro.

romansp avatar Apr 06 '24 18:04 romansp

Still can't get eslint-plugin-tailwindcss to show up in vscode. Not sure if I should keep trying, make a new issue, or be stuck with prettier.

I was using eslint@^9.0.0 with FlatCompat when I encountered the same issue. I reverted to eslint@^8.0.0 (v8.57.0; with FlatCompat) and it worked fine so it seems to be an issue that occurred with the latest eslint version.

vinavinas avatar Apr 21 '24 06:04 vinavinas

Just some stranger on the internet here to say thanks to all of you above - this is such a niche issue ("I want to sort my TailwindCSS classes with Antfu, but Antfu and Tailwind had a public falling out so there's no official support and now I'm down into a github issue")

And it works! This is what makes the internet great. Thanks everyone!

grokpot avatar Apr 21 '24 08:04 grokpot