当前位置:
首页 >
eslint入门
发布时间:2025/3/21
49
豆豆
ESLint的使用
ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码。
首先,安装 ESLint。
$ npm i -g eslint然后,安装 Airbnb 语法规则,以及 import、a11y、react 插件。
$ npm i -g eslint-config-airbnb $ npm i -g eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react最后,在项目的根目录下新建一个.eslintrc文件,配置ESLint。
{"extends": "eslint-config-airbnb" }现在就可以检查,当前项目的代码是否符合预设的规则。
index.js文件的代码如下。
var unusued = 'I have no purpose!';function greet() {var message = 'Hello, World!';alert(message); }greet();使用 ESLint 检查这个文件,就会报出错误。
$ eslint index.js index.js1:1 error Unexpected var, use let or const instead no-var1:5 error unusued is defined but never used no-unused-vars4:5 error Expected indentation of 2 characters but found 4 indent4:5 error Unexpected var, use let or const instead no-var5:5 error Expected indentation of 2 characters but found 4 indent✖ 5 problems (5 errors, 0 warnings)上面代码说明,原文件有五个错误,其中两个是不应该使用var命令,而要使用let或const;一个是定义了变量,却没有使用;另外两个是行首缩进为4个空格,而不是规定的2个空格。
总结
- 上一篇: Go语言入门——dep入门
- 下一篇: Eslint的使用