luxass / eslint-config

πŸ”§ My ESLint Config

Home Page:https://eslint-config.luxass.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@luxass/eslint-config

npm version npm downloads

✨ Features

  • Based on Antfu's ESLint Config
  • Auto fix for formatting (aimed to be used standalone without Prettier)
  • Designed to work with TypeScript, JSX, Vue & Astro out-of-box
  • Lints also for json, yaml, toml, markdown
  • Sorted imports, dangling commas
  • Reasonable defaults, best practices, only one-line of config
  • Opinionated, but very customizable
  • ESLint Flat config, compose easily!
  • Using ESLint Stylistic
  • Respects .gitignore by default
  • Optional formatters support for CSS, HTML, etc.

πŸ“¦ Install

npm install -D eslint @luxass/eslint-config

πŸš€ Usage

With "type": "module" in package.json (recommended):

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass();

With CJS:

// eslint.config.js
const luxass = require("@luxass/eslint-config").default;

module.exports = luxass();

Combined with legacy config:

// eslint.config.js
const luxass = require("@luxass/eslint-config").default;
const { FlatCompat } = require("@eslint/eslintrc");

const compat = new FlatCompat();

module.exports = luxass(
  {
    ignores: [],
  },

  // Legacy config
  ...compat.config({
    extends: [
      "eslint:recommended",
      // Other extends...
    ],
  })

  // Other flat configs...
);

Note that .eslintignore no longer works in Flat config, see customization for more details.

Setup for Visual Studio Code

Install ESLint extension and add the following to your .vscode/settings.json:

// .vscode/settings.json
{
  // will ensure that eslint can use the experimental flat config
  "eslint.experimental.useFlatConfig": true,

  // disable the default formatter
  "prettier.enable": false,
  "editor.formatOnSave": false,

  // auto fix on save
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": "explicit",
    "source.organizeImports": "never"
  },

  // silent the stylistic rules in you IDE, but still auto fix them
  "eslint.rules.customizations": [
    { "rule": "style/*", "severity": "off" },
    { "rule": "*-indent", "severity": "off" },
    { "rule": "*-spacing", "severity": "off" },
    { "rule": "*-spaces", "severity": "off" },
    { "rule": "*-order", "severity": "off" },
    { "rule": "*-dangle", "severity": "off" },
    { "rule": "*-newline", "severity": "off" },
    { "rule": "*quotes", "severity": "off" },
    { "rule": "*semi", "severity": "off" }
  ],

  // The following is optional.
  // It's better to put under project setting `.vscode/settings.json`
  // to avoid conflicts with working with different eslint configs
  // that does not support all formats.
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "vue",
    "html",
    "markdown",
    "json",
    "jsonc",
    "yaml"
  ]
}

Customization

Normally you would only need to import the luxass preset:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass();

you can also configure each config individually:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  stylistic: true,
  typescript: true,
  vue: true,
  react: false,
  astro: true,
  unocss: true,

  // `.eslintignore` is no longer supported in Flat config, use `ignores` instead.
  ignores: [
    "**/fixtures"
  ]
});

The luxass function accepts an arbitrary number of flat configs overrides:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  // configuration points for my config
}, {
  rules: {}
}, {
  rules: {}
});
Advanced Example

We don't recommend using this style in general usages, as there are shared options between configs and might need extra care to make them consistent.

// eslint.config.js
import {
  comments,
  ignores,
  imports,
  javascript,
  jsdoc,
  jsonc,
  markdown,
  node,
  sortPackageJson,
  sortTsconfig,
  stylistic,
  typescript,
  unicorn,
  vue,
  yaml,
} from "@luxass/eslint-config/configs";

import { combine } from "@luxass/eslint-config";

export default combine(
  ignores(),
  javascript(/* Options */),
  comments(),
  node(),
  jsdoc(),
  imports(),
  unicorn(),
  typescript(/* Options */),
  stylistic(),
  vue(),
  jsonc(),
  yaml(),
  markdown(),
);

Check out the configs and factory for more details.

Thanks to sxzz/eslint-config and antfu/eslint-config for the inspiration and references.

Plugins Renaming

Since flat config requires us to explicitly provide the plugin names (instead of mandatory convention from npm package name), we renamed some plugins to make overall scope more consistent and easier to write.

New Prefix Original Prefix Source Plugin
import/* i/* eslint-plugin-i
node/* n/* eslint-plugin-n
yaml/* yml/* eslint-plugin-yml
ts/* @typescript-eslint/* @typescript-eslint/eslint-plugin
style/* @stylistic/* @stylistic/eslint-plugin
test/* vitest/* eslint-plugin-vitest

When you want to override rules, or disable them inline, you need to update to the new prefix:

-// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
+// eslint-disable-next-line ts/consistent-type-definitions
type foo = { bar: 2 }

Rules Overrides

Certain rules would only be enabled in specific files, for example, ts/* rules would only be enabled in .ts files and vue/* rules would only be enabled in .vue files. If you want to override the rules, you need to specify the file extension:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass(
  { vue: true, typescript: true },
  {
    // Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
    files: ["**/*.vue"],
    rules: {
      "vue/operator-linebreak": ["error", "before"],
    },
  },
  {
    // Without `files`, they are general rules for all files
    rules: {
      "style/semi": ["error", "never"],
    },
  }
);

We also provided a overrides options to make it easier:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  overrides: {
    vue: {
      "vue/operator-linebreak": ["error", "before"],
    },
    typescript: {
      "ts/consistent-type-definitions": ["error", "interface"],
    },
    yaml: {},
    // ...
  }
});

Optional Configs

We provide some optional configs for specific use cases, that we don't include their dependencies by default.

Formatters

Warning

Experimental feature, changes might not follow semver.

Use external formatters to format files that ESLint cannot handle yet (.css, .html, etc). Powered by eslint-plugin-format.

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  formatters: {
    /**
     * Format CSS, LESS, SCSS files, also the `<style>` blocks in Vue
     * By default uses Prettier
     */
    css: true,
    /**
     * Format HTML files
     * By default uses Prettier
     */
    html: true,
    /**
     * Format TOML files
     * Currently only supports dprint
     */
    toml: "dprint",
    /**
     * Format Markdown files
     * Supports Prettier and dprint
     * By default uses Prettier
     */
    markdown: "prettier"
  }
});

Running npx eslint should prompt you to install the required dependencies, otherwise, you can install them manually:

npm i -D eslint-plugin-format

React

To enable React support, need to explicitly turn it on:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  react: true,
});

Running npx eslint should prompt you to install the required dependencies, otherwise, you can install them manually:

npm i -D eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-refresh

Next.JS

To enable Next.JS support, need to explicitly turn it on:

Next.JS also enables React support.

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  nextjs: true,
});

Running npx eslint should prompt you to install the required dependencies, otherwise, you can install them manually:

npm i -D eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-refresh @next/eslint-plugin-next

UnoCSS

To enable UnoCSS support, need to explicitly turn it on:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  unocss: true,
});

Running npx eslint should prompt you to install the required dependencies, otherwise, you can install them manually:

npm i -D @unocss/eslint-plugin

TailwindCSS

To enable TailwindCSS support, need to explicitly turn it on:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  tailwindcss: true,
});

Running npx eslint should prompt you to install the required dependencies, otherwise, you can install them manually:

npm i -D eslint-plugin-tailwindcss

Optional Rules

This config also provides some optional plugins/rules for extended usages.

Type Aware Rules

You can optionally enable the type aware rules by passing the options object to the typescript config:

// eslint.config.js
import luxass from "@luxass/eslint-config";

export default luxass({
  typescript: {
    tsconfigPath: "tsconfig.json",
  },
});

Versioning Policy

This project follows Semantic Versioning for releases. However, since this is just a config and involves opinions and many moving parts, we don't treat rules changes as breaking changes.

Changes Considered as Breaking Changes

  • Node.js version requirement changes
  • Huge refactors that might break the config
  • Plugins made major changes that might break the config
  • Changes that might affect most of the codebases

Changes Considered as Non-breaking Changes

  • Enable/disable rules and plugins (that might become stricter)
  • Rules options changes
  • Version bumps of dependencies

πŸ“„ License

Published under MIT License.

About

πŸ”§ My ESLint Config

https://eslint-config.luxass.dev

License:MIT License


Languages

Language:TypeScript 99.6%Language:JavaScript 0.4%