eslint 9.x 升级或使用指南,eslint.config.js 配置,包含 react、typescript、prettier 等常用配置升级迁移
前言
Breaking Changes
不再支持 Node v19.x
和 LTS18.18.0
版本
Flat
配置文件取代 eslintrc
配置
eslint.config.js
const eslint = require('@eslint/eslint');
module.exports = [
eslint.configs.recommended,
// your config
{
name: 'custom-lint-config',
files: ['*.js'],
rules: {
'no-undef': 0,
},
},
];
import eslint from '@eslint/eslint';
export default [
eslint.configs.recommended,
// your config
{
name: 'custom-lint-config',
files: ['*.js'],
rules: {
'no-undef': 0,
},
},
];
新 Rule:no-useless-assignment
let id = 1234; // 1234 is never used
id = calculateId();
可无参数运行 eslint 命令
npx eslint
基本使用
pnpm add eslint @eslint/js -D
import eslint from '@eslint/js';
const flatConfig = [
{
name: 'some global cofig here',
languageOptions: {
globals: {
// ...
},
},
rules: {
'no-unused-vars': 0,
},
},
{
name: 'some user comfig here',
files: ['src/**.{ts, tsx}'],
rules: {
// ...
},
},
// global files
{
files: ['src/**.{ts, tsx}'],
},
// global ignores
{
ignores: ['dist'],
},
];
export default [
eslint.configs.recommended,
// ... your config
...flatConfig,
];
相关配置迁移
typescript-eslint
使用 typescript-eslint
推荐配置
https://typescript-eslint.io/getting-started/ > https://github.com/typescript-eslint/typescript-eslint
pnpm add typescript typescript-eslint -D
import eslint from '@eslint/js';
import tsEslint from 'typescript-eslint';
const flatConfig = [
{
name: 'some comfig here',
rules: {
// ...
},
},
// global rules
{
rules: {
'@typescript-eslint/ban-types': 2,
},
},
];
export default tsEslint.config(
eslint.configs.recommended,
...flatConfig,
...tsEslint.configs.recommended,
);
使用 @typescript-eslint/parser
自定义
pnpm add @typescript-eslint/eslint-plugin @typescript-eslint/parser -D
import eslint from '@eslint/js';
import tsEslintPlugin from '@typescript-eslint/eslint-plugin';
import tsEslintParser from '@typescript-eslint/parser';
const flatConfig = [
// ....
];
const customTsFlatConfig = [
{
// any string
name: 'typescript-eslint/base',
languageOptions: {
parser: tsEslintParser,
sourceType: 'module',
},
files: ['**/*.{ts,tsx}'],
// custom rules or use recommended rules
rules: {
...tsEslintPlugin.configs.recommended.rules,
'@typescript-eslint/ban-types': 2,
'@typescript-eslint/no-confusing-non-null-assertion': 2,
},
plugins: {
// ts 语法特有的规则,例如泛型
'@typescript-eslint': tsEslintPlugin,
},
},
];
export default [eslint.configs.recommended, ...flatConfig, ...customTsFlatConfig];
@babel/eslint-parser
pnpm add @babel/eslint-parser @babel/preset-env -D
import eslint from '@eslint/js';
import tsEslintPlugin from '@typescript-eslint/eslint-plugin';
import tsEslintParser from '@typescript-eslint/parser';
import babelParser from '@babel/eslint-parser';
const flatConfig = [
// ....
];
const customTsFlatConfig = [
// ...
];
const bableConfig = {
name: 'babel-parser',
languageOptions: {
parser: babelParser,
parserOptions: {
babelOptions: {
babelrc: false,
configFile: false,
browserslistConfigFile: false,
presets: ['@babel/preset-env'],
},
requireConfigFile: false,
},
},
};
export default [eslint.configs.recommended, ...flatConfig, bableConfig, ...customTsFlatConfig];
eslint-plugin-react
、eslint-plugin-react-hooks
https://github.com/jsx-eslint/eslint-plugin-react > https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks
pnpm add eslint-plugin-react eslint-plugin-react-hooks globals -D
import eslint from '@eslint/js';
import tsEslintPlugin from '@typescript-eslint/eslint-plugin';
import tsEslintParser from '@typescript-eslint/parser';
import babelParser from '@babel/eslint-parser';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import globals from 'globals';
const flatConfig = [
// ....
];
const customTsFlatConfig = [
// ...
];
const bableConfig = {
// ...
};
const reactConfig = {
name: 'react-eslint',
files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
plugins: {
react: reactPlugin,
'react-hooks': reactHooksPlugin,
},
languageOptions: {
...reactPlugin.configs.recommended.languageOptions,
// parserOptions: {
// ecmaFeatures: {
// jsx: true,
// },
// },
globals: {
...globals.es2022,
...globals.browser,
...globals.node,
},
},
rules: {
...reactPlugin.configs.recommended.rules,
'react/react-in-jsx-scope': 0,
},
settings: {
react: {
// 需要显示安装 react
version: 'detect',
},
},
};
export default [
eslint.configs.recommended,
...flatConfig,
bableConfig,
reactConfig,
...customTsFlatConfig,
];
import reactPlugin from 'eslint-plugin-react';
export default [reactPlugin.configs.recommended];
eslint-plugin-prettier
https://github.com/prettier/eslint-plugin-prettier/tree/master
pnpm add eslint-plugin-prettier eslint-config-prettier -D
import eslint from '@eslint/js';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
export default [eslint.configs.recommended, eslintPluginPrettierRecommended];
ESLint Config Inspector
[{"url":"https://image.baidu.com/search/down?url=https://gzw.sinaimg.cn/mw2000/0085UwQ9gy1hqyany1mx0j31dm11a7d9.jpg","alt":""},{"url":"https://image.baidu.com/search/down?url=https://gzw.sinaimg.cn/mw2000/0085UwQ9gy1hqyanyktc3j31ks17kgx5.jpg","alt":""}]
总结
import eslint from '@eslint/js';
import tsEslintPlugin from '@typescript-eslint/eslint-plugin';
import tsEslintParser from '@typescript-eslint/parser';
import tsEslint from 'typescript-eslint';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import babelParser from '@babel/eslint-parser';
import globals from 'globals';
const customTsFlatConfig = [
{
name: 'typescript-eslint/base',
languageOptions: {
parser: tsEslintParser,
sourceType: 'module',
},
files: ['**/*.{ts,tsx}'],
rules: {
...tsEslintPlugin.configs.recommended.rules,
'@typescript-eslint/ban-types': 2,
'@typescript-eslint/no-confusing-non-null-assertion': 2,
},
plugins: {
// ts 语法特有的规则,例如泛型
'@typescript-eslint': tsEslintPlugin,
},
},
];
const flatConfig = [
// 全局生效的规则
{
name: 'global config',
languageOptions: {
globals: {
...globals.es2022,
...globals.browser,
...globals.node,
},
parserOptions: {
warnOnUnsupportedTypeScriptVersion: false,
},
},
rules: {
'no-dupe-class-members': 0,
'no-redeclare': 0,
'no-undef': 0,
'no-unused-vars': 0,
},
},
{
name: 'react-eslint',
files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
plugins: {
react: reactPlugin,
'react-hooks': reactHooksPlugin,
},
languageOptions: {
...reactPlugin.configs.recommended.languageOptions,
// parserOptions: {
// ecmaFeatures: {
// jsx: true,
// },
// },
},
rules: {
...reactPlugin.configs.recommended.rules,
'react/react-in-jsx-scope': 0,
},
settings: {
react: {
// 需要显示安装 react
version: 'detect',
},
},
},
{
name: 'babel-parser',
languageOptions: {
parser: babelParser,
parserOptions: {
babelOptions: {
babelrc: false,
configFile: false,
browserslistConfigFile: false,
presets: ['@babel/preset-env'],
},
requireConfigFile: false,
},
},
},
{
ignores: ['dist'],
}
];
// export default tsEslint.config(
// eslint.configs.recommended,
// eslintPluginPrettierRecommended,
// ...flatConfig,
// ...tsEslint.configs.recommended,
// );
export default [
eslint.configs.recommended,
eslintPluginPrettierRecommended,
...flatConfig,
...customTsFlatConfig,
];
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 kshao-blog-前端知识记录!
评论