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:
实际上:
A note about the two config options passed to the constructor:
关于传递给构造函数的两个配置选项的说明:
parserOptions
lets me use more modern syntaxparserOptions
让我使用更现代的语法rules
is just an example of how to zap some rulesrules
只是如何修改某些规则的示例
ESLinting快乐!(Happy ESLinting!)
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息