treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

request: ESLint support

Open tennox opened this issue 9 months ago • 2 comments

(Originally requested in upstream: https://github.com/numtide/treefmt/discussions/537)

Would be awesome to have eslint integration - especially with the new formatter rules: https://eslint.style/

I use https://github.com/antfu/eslint-config for most of my JS projects 👍

tennox avatar Mar 11 '25 07:03 tennox

A simple example:

[formatter.eslint]
command = "eslint"
options = ["--fix", "--quiet"]
includes = [ "*.js", "*.jsx", "*.ts", "*.tsx" ] # But I'm not sure what to put here, as eslint is able to format MANY files 🤔
excludes = []

tennox avatar Mar 18 '25 09:03 tennox

It's a work project so I can't show you the whole flake, but here's the relevant part of my custom formatter config for ESLint, somewhat complicated to handle the fact that I'm using a monorepo with different configurations in different directories.

treefmt.config = {
  settings = {
    formatter = {
      "eslint" = {
        command = lib.getExe pkgs.bash;
        options = [
          "-euc"
          ''
            cd "$(dirname "$1")"
            ${lib.getExe' pkgs.eslint "eslint"} --fix --quiet "$(basename "$1")"
          ''
          "--"
        ];
        includes = [
          "*.js"
          "*.mjs"
          "*.cjs"
          "*.jsx"
          "*.mjsx"
          "*.ts"
          "*.tsx"
          "*.mtsx"
        ];
        excludes = [];
      };
    };
  };
};

typedrat avatar Mar 28 '25 22:03 typedrat