eslint
约 2 分钟阅读
npx eslint "src/**" --ext .ts --ext .tsx
TIMING=1 npx eslint src # 输出时间,排查慢的插件
# fix single rule
# eslint --fix --rule 'quotes: [2, double]' .
eslint --no-eslintrc --parser @typescript-eslint/parser --fix --rule 'prefer-const: 1' src/**/*.ts
eslint --env-info- eslint-plugin-import
- eslint-plugin-react
- eslint-plugin-jsx-a11y
- react/react-in-jsx-scope
- 不要求
import React from 'react' - ESBuild 还不支持 jsx - 因此需要
import React- esbuild#334GitHub Issueevanw/esbuild#334
- 不要求
- 参考
- eslint.org
- typescript-eslintGitHubtypescript-eslint/typescript-eslint
- vercel/next.js/packages/eslint-config-nextGitHub 文件vercel/next.js/packages/eslint-config-next
- NextJS
- xojs/eslint-config-xo-typescriptGitHubxojs/eslint-config-xo-typescript
- standard/eslint-config-standard-with-typescriptGitHubstandard/eslint-config-standard-with-typescript
配置
- .eslintrc.{js,yml,json,cjs}
- eslint.config.js
- ESM
- 参考
- ESLINT_USE_FLAT_CONFIG=true 不搜索 eslint.config.js
module.exports = {
root: true,
// 扩展现有配置 - 可以是绝对路径,可以是包/模块
// 缩写逻辑:
// custom -> eslint-config-custom
// @wener -> @wener/eslint-config
extends: ['@wener/eslint-config-custom'],
settings: {
next: {
rootDir: ['apps/*/'],
},
},
};disable/false positive
// 忽略单个规则
/*eslint eqeqeq:0*/
// 忽略单行
// eslint-disable-next-line
// 忽略范围
/*eslint-disable */
alert('foo');
/*eslint-enable */
// 忽略当前
a == a; // eslint-disable-linetslint
- @typescript-eslint/no-redeclare
- ignoreDeclarationMerge
- interface + interface
- namespace + namespace
- class + interface
- class + namespace
- class + interface + namespace
- function + namespace
- enum + namespace
- 没有 const + interface - utils 合集
- ignoreDeclarationMerge
- @typescript-eslint/no-extraneous-class
- allowStaticOnly
- 方便暴露 utils 合集
- allowStaticOnly
FAQ
The file does not match your project config
module.exports = {
ignorePatterns: ['.eslintrc.js'],
};eslint decorators consistent-type-imports
- tsconfig.json emitDecoratorMetadata=true
The keyword 'import' is reserved
{
"parserOptions": {
"sourceType": "module"
}
}\n## Reference Snippets\n
npx eslint --inityarn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
yarn add eslint-config-prettier eslint-plugin-jest eslint-plugin-react eslint-plugin-react-hooks依赖包eslintnpm 包eslintNPM@typescript-eslint/parsernpm 包@typescript-eslint/parserNPM@typescript-eslint/eslint-pluginnpm 包@typescript-eslint/eslint-pluginNPMeslint-config-prettiernpm 包eslint-config-prettierNPMeslint-plugin-jestnpm 包eslint-plugin-jestNPMeslint-plugin-reactnpm 包eslint-plugin-reactNPMeslint-plugin-react-hooksnpm 包eslint-plugin-react-hooksNPM
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
};关联信息
反向链接、本文链接的其他页面和外部资料。
反向链接
- Web Dev FAQ笔记 · eslint
| abbr. | stand for | · | iife | Immediately Invoked Function Expression | · 在开发时 assert 错误 · invariant · 早期来自 React, flux
参考资料
GitHub6 条
- evanw/esbuild#334github.com/evanw/esbuild/issues/334
- standard/eslint-config-standard-with-typescriptgithub.com/standard/eslint-config-standard-with-typescript
- standard/eslint-config-standard-with-typescript/src/index.tsgithub.com/standard/eslint-config-standard-with-typescript/blob/master/src/index.ts
- typescript-eslint/typescript-eslintgithub.com/typescript-eslint/typescript-eslint
- vercel/next.js/packages/eslint-config-nextgithub.com/vercel/next.js/tree/canary/packages/eslint-config-next
- xojs/eslint-config-xo-typescriptgithub.com/xojs/eslint-config-xo-typescript
其他外链5 条
- eslint.orgeslint.org/
- eslint.org/docs/latest/ruleseslint.org/docs/latest/rules
- eslint.org/docs/latest/use/configure/configuration-fileseslint.org/docs/latest/use/configure/configuration-files
- eslint.org/docs/latest/use/configure/configuration-files-neweslint.org/docs/latest/use/configure/configuration-files-new
- typescript-eslint.io/rulestypescript-eslint.io/rules