module.exports = { root: true, parser: '@typescript-eslint/parser', // Specifies the ESLint parser extends: [ 'plugin:react/recommended', // Uses the recommended rules from @eslint-plugin-react 'plugin:@typescript-eslint/recommended', // Uses the recommended rules from @typescript-eslint/eslint-plugin 'prettier', ], parserOptions: { ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features sourceType: 'module', // Allows for the use of imports ecmaFeatures: { jsx: true, // Allows for the parsing of JSX }, project: './tsconfig.json', }, plugins: ['react-hooks', 'prettier'], rules: { '@typescript-eslint/no-unused-expressions': 'off', // 'no-console': 1, 'no-mixed-spaces-and-tabs': 2, 'no-with': 2, 'react/jsx-closing-bracket-location': 1, 'react-hooks/rules-of-hooks': 'error', // 'react-hooks/exhaustive-deps': 'warn' // 禁止使用Var,使用let或const替代 'no-var': 2, // 条件判断语句的body必须用花括号包裹,只有一条语句时,禁止花括号包裹。(有争议) curly: [2, 'all'], // 强制结尾必须有分号 semi: 2, // 强制使用单引号 quotes: [2, 'single'], // 禁止使用==、!=,使用===、!==代替。null可以不遵守该规定 eqeqeq: [ 'error', 'always', { null: 'ignore', }, ], // 禁用行尾多余空格 'no-trailing-spaces': 2, // 强制在一元操作符前后使用一致的空格 'space-unary-ops': [ 2, { nonwords: false, }, ], // 要求文件末尾存在一行空行 'eol-last': 2, // 要求命名函数左括号前不能空格,匿名函数不做控制 'space-before-function-paren': [ 2, { anonymous: 'ignore', named: 'never', }, ], // 要求空行不能超过两行 'no-multiple-empty-lines': 2, // 禁止在函数标识符和其调用括号之间有空格,如 bar (); 'func-call-spacing': 2, // 强制在代码块中使用一致的大括号风格。允许块的开括号和闭括号在 同一行 'brace-style': [ 2, '1tbs', { allowSingleLine: true, }, ], // 要求操作符周围有空格 'space-infix-ops': 2, // 强制在关键字前后使用空格 'keyword-spacing': 2, // 禁止圆括号内前后有空格 'space-in-parens': 2, // 强制块语句左花括号前有空格, 如if、function、for、try……catch 'space-before-blocks': 2, // 禁止使用 foo['bar'],必须写成 foo.bar或foo[attrVar] 'dot-notation': 2, // 强制在对象字面量中,冒号前禁止空格,冒号后有空格 'key-spacing': [ 2, { beforeColon: false, afterColon: true, }, ], // 禁止变量申明时用逗号一次申明多个 'one-var': [2, 'never'], // 禁止使用 \ 来换行字符串 'no-multi-str': 2, // 禁止数组内左括号后和右括号前有空格 'array-bracket-spacing': 2, // 禁止使用未定义的变量 'no-undef': 2, // 禁止对关系运算符的左操作数使用否定操作符(!'abc' instanceof 'string') 'no-unsafe-negation': 2, // 要求使用 isNaN() 检查 NaN 'use-isnan': 2, // 强制 typeof 表达式与有效的字符串进行比较 'valid-typeof': 2, // for in 内部必须有 hasOwnProperty 'guard-for-in': 2, // 禁止解构赋值中出现空 {} 或 [] 'no-empty-pattern': 2, // switch 的 case 内必须有 break, return 或 throw,空的 case 除外 'no-fallthrough': 2, // 禁止使用 ~+ 等难以理解的类型转换,仅允许使用 !!, + 'no-implicit-coercion': [2, { allow: ['!!', '+'] }], // 禁止对 function 的参数进行重新赋值 'no-param-reassign': 1, // 禁止在 catch 中仅仅只是把错误 throw 出去 'no-useless-catch': 2, // 必须使用 if (foo === 5) 而不是 if (5 === foo) yoda: 2, // 强制 “for” 循环中更新子句的计数器朝着正确的方向移动 'for-direction': 2, // get必须有return 'getter-return': 2, // 禁止使用异步函数作为 Promise executor 'no-async-promise-executor': 2, // 禁止不必要的括号 'no-extra-parens': ['error', 'functions'], // 强制在代码块中开括号前和闭括号后有空格 'block-spacing': 2, // 强制在逗号前后使用一致的空格 'comma-spacing': 2, // 强制在 JSX 属性中一致地使用双引号 'jsx-quotes': 2, // 阻止扩展运算符及其表达式之间有空格 'rest-spread-spacing': 2, // 禁止模板字符串中的嵌入表达式周围空格的使用 'template-curly-spacing': 2, // 强制在注释中 // 或 /* 使用空格 'spaced-comment': 2, // 禁止使用多个空格,注释除外 'no-multi-spaces': [2, { ignoreEOLComments: true }], // 禁止重复导入 'no-duplicate-imports': 2, // 强制箭头函数的箭头前后使用空格 'arrow-spacing': 2, // 要求使用 const 声明那些声明后不再被修改的变量 'prefer-const': 2, // 禁止出现重复的case标签 'no-duplicate-case': 2, // 强制花括号内换行符的一致性,即花括号换行时属性超过2个则强制要求一行一个属性,只有一个属性时禁止换行 'object-curly-newline': [2, { multiline: true }], // 强制操作符使用一致的换行符风格 'operator-linebreak': [1, 'before'], // 禁止出现空语句块,catch允许为空 'no-empty': [1, { allowEmptyCatch: true }], // 禁止不必要的布尔转换, if(!!bar) {} 'no-extra-boolean-cast': 1, // 禁止不必要的分号 'no-extra-semi': 1, // 禁用稀疏数组 'no-sparse-arrays': 1, // 强制数组方法的回调函数中有 return 语句(forEach除外) 'array-callback-return': [1, { allowImplicit: true }], // 禁止使用debugger 'no-debugger': 1, // 禁止对 catch 子句的参数重新赋值 'no-ex-assign': 1, // 强制在大括号中使用一致的空格 'object-curly-spacing': [1, 'always', { objectsInObjects: false }], // 尾逗号开启 'comma-dangle': [1, 'always-multiline'], 'no-unsafe-finally': 2, // img, br 标签中禁止有 children 'react/void-dom-elements-no-children': 2, // 禁止 jsx 中使用无用的引号 'react/jsx-curly-brace-presence': [2, 'never'], // 组件的名称必须符合 PascalCase 'react/jsx-pascal-case': 2, // style 属性的取值必须是 object 'react/style-prop-object': 2, // 必须使用 <> 而不是 React.Fragment,这样可以不用引入Fragment 'react/jsx-fragments': 2, // 禁止使用数组的索引作为 key 'react/no-array-index-key': 1, // 组件内没有 children 时,必须使用自闭合写法。html忽略 'react/self-closing-comp': [2, { html: false }], // 以下规则待定 // 组件必须有 displayName 属性 'react/display-name': 0, // 组件必须写 propTypes(使用了TS,关闭该推荐规则) 'react/prop-types': 0, // 禁止使用 target="_blank"时没有使用属性rel='noreferrer'的情况 'react/jsx-no-target-blank': 0, // 修复 React 被误报为未使用的变量的问题(仅在开启 no-unused-vars 时有效).ts处理了 'react/jsx-uses-react': 0, // 修复 no-unused-vars 不检查 jsx 的问题. 该问题被ts-eslint的规则检查了,所以这里可以关闭 'react/jsx-uses-vars': 0, // 禁止使用未定义的组件(ts已经限制了) 'react/jsx-no-undef': 0, // 数组中的 jsx 必须有 key 'react/jsx-key': 1, // 导出的函数或类中的 public 方法必须定义输入输出参数的类型 '@typescript-eslint/explicit-module-boundary-types': 0, // 不允许有空函数 原因:有时需要将一个空函数设置为某个项的默认值 '@typescript-eslint/no-empty-function': 0, // 禁止对没有 then 方法的对象使用 await '@typescript-eslint/await-thenable': 0, // 禁止使用 Array 构造函数 '@typescript-eslint/no-array-constructor': 0, // 禁止调用 Promise 时没有处理异常情况 '@typescript-eslint/no-floating-promises': 0, // 禁止对 array 使用 for in 循环 '@typescript-eslint/no-for-in-array': 0, // 避免错误的使用 Promise '@typescript-eslint/no-misused-promises': 0, // 禁止在接口中定义 constructor,或在类中定义 new '@typescript-eslint/no-misused-new': 2, '@typescript-eslint/camelcase': 0, // 要求2个空格的缩进格式 '@typescript-eslint/indent': [ 2, 2, { SwitchCase: 1, }, ], // 使用 optional chaining 替代 && '@typescript-eslint/prefer-optional-chain': 1, // 已定义变量必须使用,这里使用no-unused-vars-experimental规则来检测已定义变量是否使用 // note you must disable the base rule as it can report incorrect errors 'no-unused-vars': 0, '@typescript-eslint/no-unused-vars': 0, '@typescript-eslint/no-unused-vars-experimental': [1, { ignoreArgsIfArgsAfterAreUsed: true }], // 禁止将 this 赋值给其他变量,除非是解构赋值 '@typescript-eslint/no-this-alias': 1, 'react/react-in-jsx-scope': 'off', }, overrides: [ { files: ['*.ts', '*.tsx'], rules: { // 强制显示的声明函数返回类型 '@typescript-eslint/explicit-function-return-type': 0, // 类型断言必须使用 as Type,禁止使用 ,禁止对对象字面量进行类型断言(断言成 any 是允许的) '@typescript-eslint/consistent-type-assertions': [ 'error', { assertionStyle: 'as', objectLiteralTypeAssertions: 'allow', }, ], // 禁止使用指定的类型(TODO: 该配置会导致Function使用报警告) '@typescript-eslint/ban-types': 1, // 禁止给一个在初始化时直接赋值为 number, string 的变量显式的声明类型 '@typescript-eslint/no-inferrable-types': 1, // 禁止使用 // @ts-ignore // @ts-nocheck // @ts-check '@typescript-eslint/ban-ts-comment': 1, // 禁止使用 any '@typescript-eslint/no-explicit-any': 0, // 禁止使用 non-null 断言(感叹号) '@typescript-eslint/no-non-null-assertion': 0, }, }, ], settings: { 'import/resolver': { node: { extensions: ['.js', '.jsx', '.ts', '.tsx'], }, }, react: { version: 'detect', // Tells eslint-plugin-react to automatically detect the version of React to use }, }, env: { browser: true, node: true, jest: true, es6: true, }, globals: { COUNTRY: true, }, }; // see https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb