Skip to content

语法检测 ​

  • 校检代码是否合法,使代码更加健壮。
  • 官方文档

安装 ​

bash
npm install eslint -D
# 或者
pnpm install eslint -D

# 解决prettier和eslint 冲突插件
pnpm i -D eslint-plugin-prettier eslint-config-prettier

初始化 ​

bash
# 根据提示安装,会生成eslint.config.ts 配置文件
pnpm eslint --init

  • 默认配置 eslint.config.ts
ts
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import pluginVue from 'eslint-plugin-vue';
import { defineConfig } from 'eslint/config';

export default defineConfig([
	{
		files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
		plugins: { js },
		extends: ['js/recommended'],
		languageOptions: { globals: globals.browser },
	},
	tseslint.configs.recommended,
	pluginVue.configs['flat/essential'],
	{ files: ['**/*.vue'], languageOptions: { parserOptions: { parser: tseslint.parser } } },
]);

自定义规则 ​

js
import js from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import pluginVue from 'eslint-plugin-vue';
import prettier from 'eslint-plugin-prettier';
import prettierDisableConfig from 'eslint-config-prettier';
import { defineConfig } from 'eslint/config';

const isProd = process.env.NODE_ENV === 'production';

export default defineConfig([
	// 全局忽略
	{
		ignores: [
			'node_modules/**',
			'dist/**',
			'*.log',
			'.husky/**',
			'.vscode/**',
			'.idea/**',
			'.DS_Store',
		],
	},
	// 基础全局配置:文件范围 + 全局变量
	{
		files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
		plugins: {
			prettier, // 注册 Prettier 插件
		},
		languageOptions: {
			globals: {
				...globals.browser,
				...globals.es2021,
			},
		},
	},

	// JS 官方推荐规则(Flat Config 正确写法:展开配置对象)
	js.configs.recommended,

	// TypeScript 官方推荐规则
	...tseslint.configs.recommended,

	// Vue3 官方推荐规则
	...pluginVue.configs['flat/recommended'],

	// Vue 文件专属:script 块使用 TS 解析器
	{
		files: ['**/*.vue'],
		languageOptions: {
			parserOptions: {
				parser: tseslint.parser,
			},
		},
	},

	// 关闭所有和 Prettier 冲突的规则【必须放在所有规则集之后】
	prettierDisableConfig,

	// 自定义规则
	{
		rules: {
			//  Prettier 格式校验
			'prettier/prettier': 'error',

			// 基础 JS 规则
			'no-console': isProd ? 'error' : 'off',
			'no-debugger': isProd ? 'error' : 'off',
			'no-var': 'error',
			'no-unexpected-multiline': 'error',

			// Vue规则 参考配置:@see(https://eslint.vuejs.org/rules/)
			'vue/no-mutating-props': 'error', // 禁止修改props
			'vue/no-unused-components': 'warn', // 未使用的组件警告
			'vue/attribute-hyphenation': ['error', 'always'], // 属性使用连字符命名

			// TypeScript规则 参考配置:@see(https://typescript-eslint.io/rules/)
			'@typescript-eslint/no-unused-vars': [
				'warn',
				{
					argsIgnorePattern: '^_',
					varsIgnorePattern: '^_',
				},
			], //不允许 定义未使用的变量
			'@typescript-eslint/explicit-module-boundary-types': 'off', // 关闭显式导出类型检查
			'@typescript-eslint/no-explicit-any': 'off', // any类型警告
			'@typescript-eslint/ban-ts-comment': 'warn', // 禁止使用@ts-ignore等注释
		},
	},
]);

配置忽略 ​

  • 在.eslintignore 文件中加入忽略的文件
text
dist
node_modules
src/assets

配置检测命令 ​

在 package.json 中加入执行命令

js
"scripts": {
    "lint": "eslint src",
    "lint-fix": "eslint src --fix"
}