babel-eslint

2018-10-10  本文已影响1908人  冷小谦

babel-eslint allows you to lint ALL valid Babel code with the fantastic ESLint.
babel-eslint可以对所有有效的babel代码进行lint处理。

Why Use babel-eslint

You only need to use babel-eslint if you are using types (Flow) or experimental features not supported in ESLint itself yet. Otherwise try the default parser (you don't have to use it just because you are using Babel).
如果你使用eslint本身不支持的特性,可以使用 babel-eslint。否则可以使用默认解析器而不需要使用babel-eslint。

If there is an issue, first check if it can be reproduced with the regular parser or with the latest versions of eslint and babel-eslint!
如果有问题,先检查是否可以用常规解析器,或者是否是最新版本的eslint、babel-eslint。

How does it work?

ESLint allows custom parsers. This is great but some of the syntax nodes that Babel supports aren't supported by ESLint. When using this plugin, ESLint is monkeypatched and your code is transformed into code that ESLint can understand. All location info such as line numbers, columns is also retained so you can track down errors with ease.
eslint允许常规解析器,有一些babel语法没有被eslint支持。当使用这些插件时,你的代码要转换成eslint能够支持解析的代码格式。所有例如行号、列等本地信息也被保留,所以你可以通过这些本地信息轻易的追踪错误。

Basically babel-eslint exports an index.js that a linter can use. It just needs to export a parse method that takes in a string of code and outputs an AST.
所以,babel-eslint是导出一个解析器可以支持的index.js文件,它仅需要输出一个获取代码字符串和输出数据的解析方法。

Install

Ensure that you have substituted the correct version lock for eslint and babel-eslint into this command:

$ npm install eslint@4.x babel-eslint@8 --save-dev
# or 
$ yarn add eslint@4.x babel-eslint@8 -D

Setup

配置根目录下的文件.eslintrc

{
  "parser": "babel-eslint",
  "rules": {
    "strict": 0
  }
}

规则的严重性(rule severity)

"off"

or 0 - turn the rule off 不验证 "warn"

or 1 - turn the rule on as a warning(doesn’ t affect exit code) 警告 "error"

or 2 - turn the rule on as an error(exit code is 1 when triggered) 错误

( 如有) 规则的选项(additional options)

"quotes": [2, "double"]

configuration:

.eslintrc

{
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEverywhere": false,
    "codeFrame": true
  }
}

总的来说babel-eslint就是将不能被常规linter解析的代码转换为能被常规解析的代码

上一篇下一篇

猜你喜欢

热点阅读