欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

eslint入门

发布时间:2025/3/21 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 eslint入门 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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个空格。

总结

以上是生活随笔为你收集整理的eslint入门的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。