Skip to main content

eslint

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

配置

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-line

tslint

  • @typescript-eslint/no-redeclare
    • ignoreDeclarationMerge
      • interface + interface
      • namespace + namespace
      • class + interface
      • class + namespace
      • class + interface + namespace
      • function + namespace
      • enum + namespace
      • 没有 const + interface - utils 合集
  • @typescript-eslint/no-extraneous-class
    • allowStaticOnly
      • 方便暴露 utils 合集

FAQ

The file does not match your project config

.eslintrc.js
module.exports = {
ignorePatterns: ['.eslintrc.js'],
};

eslint decorators consistent-type-imports

  • tsconfig.json emitDecoratorMetadata=true

The keyword 'import' is reserved

{
"parserOptions": {
"sourceType": "module"
}
}