eslint-config 配置
JavaScript

README.md

eslint-config 配置

安装

$ npm install eslint-config-lei eslint-plugin-promise --save-dev

说明:由于本配置使用了promise插件,因此需要同时安装eslint-plugin-promise模块。

使用方法

配置文件

在项目根目录下新建文件.eslintrc.js

仅包含 ES6 语法时:

module.exports = {
  extends: 'lei',
};

包含 ES7 语法时:

module.exports = {
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: false,
  },
  extends: 'lei',
};

在基于mocha框架的单元测试中使用,在test目录下新建文件.eslintrc.js

module.exports = {
  extends: 'lei/mocha',
};

使用

执行以下命令即可:

$ eslint dir/**.js

如果需要自动格式化代码,在执行时添加--fix选项:

$ eslint dir/**.js --fix

配置文件

  • lei - 默认的配置,基于Node.js/ES6
  • lei/mocha - mocha测试环境
  • lei/wechat - 微信小程序环境

常见问题

1、如果在使用babel-eslint时报错,可能是该模块的 Bug,目前可以通过以下方法解决:

module.exports = {
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: false,
  },
  extends: 'lei',
  rules: {
    // 关闭以下规则
    'generator-star-spacing': 'off',
    'require-yield': 'off',
  },
};

2、在使用过程中,可能会遇到一些例外情况,比如需要更改参数对象的属性,可以通过eslint-disable-next来临时关闭对下一行的检查:

// eslint-disable-next-line no-param-reassign
param.xxx = 'ok';

注意:任何时候请勿使用eslint-disable来关闭eslint的检查,如果该备注不能与eslint-enable成对出现将会导致余下的程序不能正常获得检查

3、检查 HTML 文件内的 script 标签:

需要执行以下命令安装eslint-plugin-html插件:

$ npm install eslint-plugin-html --save-dev

并修改文件.eslintrc.js,载入eslint-plugin-html插件:

{
  plugins: [ 'html' ],
}

在 HTML 文件中,<script>标签需要包含type="text/javascript",比如:

<script type="text/javascript">
  var a = 10;
</script>

由于eslint命令默认只会检查.js后缀的文件,所以需要在执行命令时指定.html后缀,比如:

$ eslint --ext .js,.html . --fix

相关链接

License

The MIT License (MIT)

Copyright (c) 2016 Zongmin Lei <leizongmin@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.