116181541650089863
前言
ESLint用于检查JavaScript代码是否符合规则,由Nicholas C. Zakas在2013年建立。(维基百科)
下载依赖
交互式创建配置文件
手动创建配置文件
使用内置JS配置
下载依赖
1
| npm install -D @eslint/js
|
修改配置文件
规则等级
off、0:关闭规则
warn、1:警告
error、2:报错
no-unused-vars:禁止未使用的变量
no-console:禁止控制台输出
indent:指定缩进方式
quotes:指定引号类型
single:单引号
double:双引号
eslint.config.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import js from "@eslint/js";
export default [ { ignores: [ "node_modules/**", "dist/**", "build/**" ] },
js.configs.recommended,
{ files: ["**/*.js"], languageOptions: { ecmaVersion: "latest", sourceType: "module", globals: { window: "readonly", console: "readonly" } }, rules: { "no-unused-vars": "warn", "no-console": "warn", "indent": ["error", 2], "quotes": ["error", "double"] } } ];
|
使用Prettier配置
下载依赖
1
| npm install -D @eslint/js prettier eslint-config-prettier eslint-plugin-prettier
|
修改配置文件
eslint.config.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import js from "@eslint/js"; import eslintConfigPrettier from "eslint-config-prettier"; import eslintPluginPrettier from "eslint-plugin-prettier";
export default [ { ignores: [ "node_modules/**", "dist/**", "build/**" ] },
js.configs.recommended,
eslintConfigPrettier,
{ files: ["**/*.js"], languageOptions: { ecmaVersion: "latest", sourceType: "module", globals: { window: "readonly", console: "readonly" } }, plugins: { prettier: eslintPluginPrettier }, rules: { "prettier/prettier": "error" } } ];
|
检测代码
检测代码并自动修复代码
1
| npx eslint --fix <file>.js
|
完成
参考文献
哔哩哔哩——许泽鸿