在脚本中使用ESLint

Instead of running ESLint on the command line and passing files to it, I wanted to require() and use it with code from strings. That is because I want to lint and unit-test the code from the book I write in AsciiDoc. (Will post the complete script once it's running properly, some taste)

我不是在命令行上运行ESLint并将文件传递给它,而是要require()并将其与字符串中的代码一起使用。 那是因为我想对我在AsciiDoc中编写的书中的代码进行整理和单元测试。 (一旦正常运行,就会发布完整的脚本,有些味道)

Had to jump through a few hoops, so here it is for the posterity (where posterity = me, next month)

不得不跳了几圈,所以这里是给后代的(其中后代=我,下个月)

安装 (Install)

$ npm install eslint --save-dev

创建一个配置文件 (Create a configuration file)

This one always trips me up. ESLint's binary has an `--init` option now that takes care of this and I tried it, but the generated "standard" file was missing rules, etc, so I abandoned the idea in favor of creating am .eslintrc.json file in my directory with this content:

这总是使我绊倒。 ESLint的二进制文件现在有一个--init选项可以解决这个问题,我尝试了一下,但是生成的“标准”文件缺少规则等,因此我放弃了这个想法,而是在其中创建了.eslintrc.json文件。我的目录中包含以下内容:

{
    "extends": [
        "eslint:recommended"
    ]
}

代码(The code)

const CLIEngine = require('eslint').CLIEngine;
const cli = new CLIEngine({
  parserOptions: {
    ecmaVersion: 6,
  },
  rules: {
    'no-unused-vars': 'off',
  }
});

const report = cli.executeOnText("let foo = 'bar';;").results[0];

if (report.errorCount) {
  console.log(report.messages);
} else {
  console.log('No errors');
}

In action:

实际上:

ESLint script in action

A note about the two config options passed to the constructor:

关于传递给构造函数的两个配置选项的说明:

  • parserOptions lets me use more modern syntax

    parserOptions让我使用更现代的语法

  • rules is just an example of how to zap some rules

    rules只是如何修改某些规则的示例

ESLinting快乐!(Happy ESLinting!)

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/using-eslint-in-a-script/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值