• Stars
    star
    1,605
  • Rank 29,152 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Anthony's ESLint config presets

@antfu/eslint-config

npm code style

  • Single quotes, no semi
  • Auto fix for formatting (aimed to be used standalone without Prettier)
  • Designed to work with TypeScript, Vue out-of-box
  • Lints also for json, yaml, markdown
  • Sorted imports, dangling commas
  • Reasonable defaults, best practices, only one-line of config
  • Respects .gitignore by default
  • ESLint Flat config, compose easily!
  • Using ESLint Stylistic
  • Style principle: Minimal for reading, stable for diff, consistent

Important
The main branch is for v1.0-beta, which rewrites to ESLint Flat config, check #250 for more details.

Usage

Install

pnpm i -D eslint @antfu/eslint-config

Create config file

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

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu()

With CJS:

// eslint.config.js
const antfu = require('@antfu/eslint-config').default

module.exports = antfu()

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

Add script for package.json

For example:

{
  "scripts": {
    "lint": "eslint .",
    "lint:fix": "eslint . --fix"
  }
}

VS Code support (auto fix)

Install VS Code ESLint extension

Add the following settings to your .vscode/settings.json:

{
  // Enable the ESlint flat config support
  "eslint.experimental.useFlatConfig": true,

  // Disable the default formatter, use eslint instead
  "prettier.enable": false,
  "editor.formatOnSave": false,

  // Auto fix
  "editor.codeActionsOnSave": {
    "source.fixAll": true,
    "source.organizeImports": false
  },

  // 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" }
  ],

  // Enable eslint for all supported languages
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "vue",
    "html",
    "markdown",
    "json",
    "jsonc",
    "yaml"
  ]
}

Customization

Since v1.0, we migrated to ESLint Flat config. It provides much better organization and composition.

Normally you only need to import the antfu preset:

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu()

And that's it! Or you can configure each integration individually, for example:

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu({
  // Enable stylistic formatting rules
  // stylistic: true,

  // Or customize the stylistic rules
  stylistic: {
    indent: 2, // 4, or 'tab'
    quotes: 'single', // or 'double'
  },

  // TypeScript and Vue are auto-detected, you can also explicitly enable them:
  typescript: true,
  vue: true,

  // Disable jsonc and yaml support
  jsonc: false,
  yaml: false,

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

The antfu factory function also accepts any number of arbitrary custom config overrides:

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu(
  {
    // Configures for antfu's config
  },

  // From the second arguments they are ESLint Flat Configs
  // you can have multiple configs
  {
    files: ['**/*.ts'],
    rules: {},
  },
  {
    rules: {},
  },
)

Going more advanced, you can also import fine-grained configs and compose them as you wish:

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

export default [
  ...ignores(),
  ...javascript(),
  ...comments(),
  ...node(),
  ...jsdoc(),
  ...imports(),
  ...unicorn(),
  ...typescript(),
  ...stylistic(),
  ...vue(),
  ...jsonc(),
  ...yaml(),
  ...markdown(),
]

Check out the configs and factory for more details.

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

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
test/* no-only-tests/* eslint-plugin-no-only-tests

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 antfu from '@antfu/eslint-config'

export default antfu(
  { 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 an overrides options to make it easier:

// eslint.config.js
import antfu from '@antfu/eslint-config'

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

Type Aware Rules

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

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu({
  typescript: {
    tsconfigPath: 'tsconfig.json',
  },
})

Lint Staged

If you want to apply lint and auto-fix before every commit, you can add the following to your package.json:

{
  "simple-git-hooks": {
    "pre-commit": "pnpm lint-staged"
  },
  "lint-staged": {
    "*": "eslint --fix"
  }
}

and then

npm i -D lint-staged simple-git-hooks

Badge

If you enjoy this code style, and would like to mention it in your project, here is the badge you can use:

[![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)

code style

FAQ

Prettier?

Why I don't use Prettier

How to lint CSS?

This config does NOT lint CSS. I personally use UnoCSS so I don't write CSS. If you still prefer CSS, you can use stylelint for CSS linting.

I prefer XXX...

Sure, you can config and override rules locally in your project to fit your needs. If that still does not work for you, you can always fork this repo and maintain your own.

Check Also

License

MIT License Β© 2019-PRESENT Anthony Fu

More Repositories

1

vitesse

πŸ• Opinionated Vite + Vue Starter Template
TypeScript
8,522
star
2

ni

πŸ’‘ Use the right package manager
TypeScript
4,527
star
3

icones

⚑️ Icon Explorer with Instant searching, powered by Iconify
Vue
4,525
star
4

unplugin-vue-components

πŸ“² On-demand components auto importing for Vue
TypeScript
3,011
star
5

unocss

The instant on-demand atomic CSS engine.
TypeScript
2,990
star
6

unplugin-icons

🀹 Access thousands of icons as components on-demand universally.
TypeScript
2,793
star
7

vitesse-webext

⚑️ WebExtension Vite Starter Template
TypeScript
2,581
star
8

unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup
TypeScript
2,307
star
9

vscode-file-nesting-config

Config of File Nesting for VS Code
JavaScript
2,132
star
10

vue-starport

πŸ›° Shared component across routes with animations
TypeScript
1,744
star
11

vscode-settings

My VS Code settings and extensions
1,739
star
12

vitesse-nuxt3

Vitesse for Nuxt 3 πŸ”πŸ’šβš‘οΈ
TypeScript
1,510
star
13

shikiji

A syntax highlighter based on TextMate grammars. ESM rewrite of shiki, with more features and capabilities.
TypeScript
1,465
star
14

reactivue

πŸ™Š Use Vue Composition API in React components
TypeScript
1,415
star
15

taze

πŸ₯¦ A modern cli tool that keeps your deps fresh
TypeScript
1,233
star
16

vite-ssg

Static site generation for Vue 3 on Vite
TypeScript
1,198
star
17

handle

A Chinese Hanzi variation of Wordle - 汉字 Wordle
TypeScript
1,194
star
18

qrcode-toolkit

Anthony's QR Code Toolkit for AI generated QR Codes
Vue
1,111
star
19

case-police

🚨 Make the case correct, PLEASE!
TypeScript
1,098
star
20

vite-plugin-inspect

Inspect the intermediate state of Vite plugins
Vue
1,009
star
21

use

Things I am using
991
star
22

vitesse-lite

⛺️ Lightweight version of Vitesse
TypeScript
901
star
23

drauu

Headless SVG-based drawboard in browser.
TypeScript
817
star
24

broz

A simple, frameless browser for screenshots
JavaScript
745
star
25

iroiro

Beautiful Colors Lookup in CLI
TypeScript
735
star
26

retypewriter

Replay the steps of your changes at ease.
TypeScript
715
star
27

live-draw

A tool allows you to draw on screen real-time.
C#
706
star
28

sd-webui-qrcode-toolkit

Anthony's QR Toolkit for Stable Diffusion WebUI
JavaScript
640
star
29

changelogithub

Generate changelog for GitHub
TypeScript
613
star
30

vite-plugin-md

Markdown with Vue for Vite
TypeScript
602
star
31

vscode-smart-clicks

Smart selection with double clicks for VS Code.
TypeScript
601
star
32

unplugin-vue2-script-setup

πŸ’‘ Bring `<script setup>` to Vue 2.
TypeScript
567
star
33

sponsorkit

πŸ’– Toolkit for generating sponsors images πŸ˜„
TypeScript
550
star
34

eslint-flat-config-viewer

A visual tool to help you view and understand your ESLint Flat config.
Vue
549
star
35

unconfig

A universal solution for loading configurations.
TypeScript
524
star
36

utils

Collection of common JavaScript / TypeScript utils
TypeScript
493
star
37

100

My 100-day project of exploring design, compform, and new things.
Vue
491
star
38

v-lazy-show

Compile-time directive to lazy initialize v-show for Vue
TypeScript
487
star
39

antfu.me

My personal website
Markdown
463
star
40

raycast-multi-translate

A Raycast extension that translates text to multiple languages at once
TypeScript
454
star
41

vue-reuse-template

Define and reuse Vue template inside the component scope.
TypeScript
440
star
42

vite-node

Vite as Node.js runtime
JavaScript
428
star
43

vscode-browse-lite

πŸš€ An embedded browser in VS Code
TypeScript
425
star
44

vscode-vite

One step faster for Vite in VS Code ⚑️
TypeScript
404
star
45

vscode-theme-vitesse

πŸ• Vitesse theme for VS Code
TypeScript
395
star
46

starter-ts

Starter template for TypeScript library
TypeScript
386
star
47

vue-template-promise

Template as Promise in Vue
TypeScript
373
star
48

vue-global-api

Use Vue Composition API globally
TypeScript
331
star
49

vue-router-better-scroller

Enhanced scroll behavior for Vue Router
TypeScript
327
star
50

qr-scanner-wechat

QR Code scanner in JS with Open CV and WeChat's Algorithm
JavaScript
322
star
51

starter-vscode

Starter template for VS Code Extension
TypeScript
299
star
52

vscode-iconify

πŸ™‚ Iconify IntelliSense for VS Code
TypeScript
294
star
53

1990-script

Make your GitHub history back to 1990
Shell
293
star
54

p

Toolkit for managing multiple promises
TypeScript
284
star
55

wenyan-lang-vscode

文言 Wenyan Lang for VS Code
TypeScript
277
star
56

pnpm-patch-i

A better and interactive pnpm patch
TypeScript
265
star
57

fsxx

File system in zx style
JavaScript
262
star
58

birpc

Message-based two-way remote procedure call.
TypeScript
245
star
59

vue-minesweeper

A tiny minesweeper clone in Vue 3
TypeScript
242
star
60

contribute

Contribution guide for my projects
231
star
61

purge-icons

🎐 Bundles icons on demand
TypeScript
229
star
62

vscode-open-in-github-button

Add a button to go to the GitHub on the status bar.
TypeScript
224
star
63

github-doorcat

😼 Supercharges GitHub navbar for fast navigation [WIP]
TypeScript
214
star
64

vscode-goto-alias

Go to Definition following alias redirections.
TypeScript
211
star
65

refined-github-notifications

UserScript that enhances the GitHub Notifications
JavaScript
208
star
66

eslint-plugin-command

Comment-as-command for one-off codemod with ESLint.
TypeScript
195
star
67

nuxt-server-fn

Server functions in client for Nuxt 3
TypeScript
194
star
68

magic-string-stack

magic-string with the capability of committing changes.
TypeScript
190
star
69

vscode-array-index-inlay

Show array index inlay hints for large arrays for VS Code
TypeScript
185
star
70

eslint-typegen

Generate types from ESLint rule schemas, with auto-completion and type-checking for rule options.
TypeScript
185
star
71

prism-theme-vars

A customizable Prism.js theme using CSS variables
CSS
178
star
72

what-time

What time works for you?
Vue
177
star
73

esbuild-node-loader

Transpile TypeScript to ESM with Node.js loader.
JavaScript
175
star
74

userscript-clean-twitter

Bring back the peace on Twitter
JavaScript
174
star
75

vitesse-nuxt-bridge

πŸ• Vitesse experience for Nuxt 2 and Vue 2
Vue
174
star
76

vscode-icons-carbon

Carbon Visual Studio Code product icon theme
TypeScript
170
star
77

talks

Slides & code for my talks
Vue
170
star
78

fs-spy

Monitoring fs accessing for Node process
TypeScript
166
star
79

vite-plugin-glob

The design experiment for import.meta.glob from Vite.
TypeScript
164
star
80

diff-match-patch-es

ESM and TypeScript rewrite of Google's diff-match-patch for JavaScript
TypeScript
159
star
81

vite-plugin-optimize-persist

Persist dynamically analyzed dependencies optimization
TypeScript
155
star
82

eslint-ts-patch

Support loading eslint.config.mjs and eslint.config.ts as flat config files for ESLint.
JavaScript
155
star
83

markdown-it-github-alerts

Support GitHub-style alerts for markdown-it
TypeScript
153
star
84

regex-doctor

Monitor your RegExp consumption and provide suggestions to improve performance.
TypeScript
152
star
85

v-dollar

jQuery-like Vue Reactivity API
TypeScript
146
star
86

.github

The default community health files for all my repos on GitHub
146
star
87

p5i

p5.js, but with more friendly instance mode APIs
TypeScript
139
star
88

vscode-pnpm-catalog-lens

Show versions inline for PNPM catalog: field
TypeScript
136
star
89

magic-string-extra

Extended magic-string with extra utilities
TypeScript
131
star
90

export-size

Analysis bundle cost for each export of a package
TypeScript
130
star
91

eslint-plugin-format

Format various languages with formatters in ESLint
TypeScript
129
star
92

local-pkg

Get information on local packages.
TypeScript
126
star
93

vite-plugin-restart

Custom files/globs to restart Vite server
TypeScript
124
star
94

pkg-exports

Get exports of an local npm package.
TypeScript
118
star
95

qr-verify

A CLI to verify scannable QR Code in batch
TypeScript
114
star
96

eslint-plugin-antfu

Anthony extended ESLint rules.
TypeScript
114
star
97

install-pkg

Install package programmatically.
TypeScript
114
star
98

deploy-check

WIP, Prevent runtime errors earlier in CI
TypeScript
113
star
99

fast-npm-meta

A lightweight API server to get npm package metadata, resolve the latest versions on server, and batch multiple package resolutions in one request.
TypeScript
110
star
100

markdown-it-mdc

MDC (Markdown Components) syntax for markdown-it.
TypeScript
106
star