Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint入门 #126

Open
LiuYueKai opened this issue Aug 28, 2016 · 0 comments
Open

ESLint入门 #126

LiuYueKai opened this issue Aug 28, 2016 · 0 comments

Comments

@LiuYueKai
Copy link
Contributor

简介

ESLint 是用来检查我们写的 JavaScript 代码是否满足指定规则的静态代码检查工具。

通过用 ESLint 来检查一些规则,我们可以:

  • 统一代码风格规则。
  • 减少错误。
  • 提高代码质量。

安装

npm install  --save-dev eslint

使用方法

如果项目种还没有配置文件(.eslintrc)的话,可以通过指定--init参数来生成一个新的配置文件:

eslint --init

配置

在生成的配置文件(.eslintrc)文件中配置一些规则。

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true
  },
  "parserOptions": {
    "ecmaVersion": 6
  },
  "rules": {
    "indent": ["error", 2],
    "no-mixed-spaces-and-tabs": "error"
    "camelcase": "error",
    "eqeqeq": "warn",
    "curly": "error",
    "no-undef": "error",
    "no-unused-vars": "warn",
    "max-params": "warn"
  }
}
  • env 指定代码运行的环境。

  • parserOptions 指定 JavaScript 相关的选项。ecmaVersion 指定用哪个 ECMAScript 的版本,默认是 3 和 5。

  • rules 指定具体检查的规则。默认情况下,如果不设置检查的规则,ESLint 不会检查任何规则。

    规则的错误等级有三种:
    0:关闭规则。
    1:打开规则,并且作为一个警告(不影响exit code)。
    2:打开规则,并且作为一个错误(exit code将会是1)。

更多规则说明详见官网

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant