eslint9配置

eslint9主要做了如下修改:

  • 改用扁平模式的配置文件 eslint.config.js,弃用原有的 .eslintrc 以及 .eslintignore。
  • 弃用了格式化相关的规则,例如缩进、空格、空行、单双引号之类规则。
    在这里插入图片描述

扁平模式下,所有配置都写在一个统一配置文件中:eslint.config.js 或 eslint.config.cjs 或 eslint.config.mjs

  • .mjs 用 ES Module 方式导出
  • .cjs 用 commonjs 方式导出
  • .js 则取决于 package.json 中的 type 字段,设为 module 就是前者,设为 commonjs 就是后者,默认也是后者。

使用

执行 npx eslint --init 后默认创建 eslint.config.js 内容如下

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import pluginVue from 'eslint-plugin-vue';

/** @type {import('eslint').Linter.Config[]} */
export default [
	{ files: ['**/*.{js,mjs,cjs,ts,vue}'] },

	{ languageOptions: { globals: globals.browser } },
	pluginJs.configs.recommended,
	...tseslint.configs.recommended,
	...pluginVue.configs['flat/essential'],
	{
		files: ['**/*.vue'],
		languageOptions: { parserOptions: { parser: tseslint.parser } }
	},
];

进行配置,如合并 prettier

npm i prettier eslint-config-prettier -D

创建 .prettierrc.js

module.exports = {
	printWidth: 120, //单行长度
	tabWidth: 4, //缩进长度
	useTabs: true, //使用空格代替tab缩进
	semi: true, //句末使用分号
	singleQuote: true, //使用单引号
	endOfLine: 'auto',
	trailingComma: 'none' // 对象最后一个属性末尾是否要逗号
};

eslint.config.js

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import pluginVue from 'eslint-plugin-vue';
import eslintConfigPrettier from 'eslint-config-prettier'; // 引入插件

/** @type {import('eslint').Linter.Config[]} */
export default [
	{ files: ['**/*.{js,mjs,cjs,ts,vue}'] },

	{ languageOptions: { globals: globals.browser } },
	pluginJs.configs.recommended,
	...tseslint.configs.recommended,
	...pluginVue.configs['flat/essential'],
	{
		files: ['**/*.vue'],
		languageOptions: { parserOptions: { parser: tseslint.parser } }
	},
	eslintConfigPrettier // prettier
];
要在VSCode中安装并配置ESLint插件,请按照以下步骤进行操作: 1. 打开VSCode,点击左侧的扩展图标(四个方块)或按下快捷键`Ctrl+Shift+X`来打开扩展面板。 2. 在搜索栏中输入"ESLint"并按下回车键,找到ESLint插件并点击安装按钮进行安装。 3. 安装完成后,点击"重新加载"按钮来重新加载VSCode。 4. 接下来,你需要在你的项目中安装ESLint。在终端中进入你的项目根目录,并执行以下命令: ``` npm install eslint --save-dev ``` 或者如果你使用的是Yarn: ``` yarn add eslint --dev ``` 5. 安装完成后,在终端中执行以下命令来初始化ESLint配置文件: ``` npx eslint --init ``` 这将引导你完成一个交互式的配置过程。你可以根据自己的需求选择一些配置选项,例如选择ESLint的规则(Airbnb、Standard等),或者手动配置自定义规则。 6. 完成配置后,ESLint将会生成一个`.eslintrc`文件,该文件存放着ESLint的规则配置。 7. 回到VSCode,点击左下角的设置图标(齿轮图标),选择"首选项"->"设置"来打开用户设置。 8. 在用户设置中搜索"eslint.autoFixOnSave"并勾选该选项,这将在保存文件时自动修复ESLint错误。 9. 在用户设置中搜索"eslint.validate",找到"ESLint: Validate"选项,并在其值中添加以下内容: ``` "javascript", "javascriptreact", "typescript", "typescriptreact" ``` 这将使ESLintJavaScriptJavaScript React、TypeScript和TypeScript React文件进行验证。 现在,你已经成功安装并配置ESLint插件。在保存文件时,ESLint将会自动运行,并根据配置的规则进行验证和修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田本初

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值