[英]Custom rule not recognized by ESLint
I need to write a custom rule for ESLint to check if someone uses certain code constructs. 我需要为ESLint编写一个自定义规则,以检查是否有人使用某些代码构造。 I am new to ESLint and am having some problems registering the rule so I can actually use it in the
.eslintrc
configuration file. 我是ESLint的新手,在注册规则时遇到一些问题,因此我可以在
.eslintrc
配置文件中实际使用它。 The rule itself looks as follows: 规则本身如下所示:
some-custom-rule.js some-custom-rule.js
'use strict';
module.exports = function(context) {
function applyRule(node) {
// some logic
}
return {
"Identifier": applyRule
}
};
module.exports.schema = [];
Then I have the following configuration file: 然后,我有以下配置文件:
.eslintrc .eslintrc
{
"ecmaFeatures": {
"blockBindings": true,
"forOf": true,
"modules": true,
"arrowFunctions": true,
"classes": true
},
"rules": {
"semi": 2,
"some-custom-rule": 2
},
"env": {
"es6": true,
"browser": true
}
}
When I try to execute ESLint with a Gulp task, the following errors pops up: 1:1 error Definition for rule 'some-custom-rule' was not found
. 当我尝试使用Gulp任务执行ESLint时,会弹出以下错误:
1:1 error Definition for rule 'some-custom-rule' was not found
。 I assume this is because I didn't require the rule properly. 我认为这是因为我没有正确地要求该规则。
The Gulp task is as follows: Gulp任务如下:
var gulp = require('gulp');
var path = require('path');
var conf = require('./conf');
var eslint = require('gulp-eslint');
var rule = require('./rules/some-custom-rule');
gulp.task('eslint', function () {
return gulp.src(path.join(conf.paths.src, '**/*.js'))
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
How can I make use of my custom rule? 如何使用自定义规则? Do I have to pass it to ESLint or something?
我是否必须将其传递给ESLint或其他? Thanks in advance!
提前致谢!
There are currently two ways to register rules with ESLint, you can use --rulesdir
command-line flag and pass it a directory where your custom rules are located. 当前有两种向ESLint注册规则的方法,可以使用
--rulesdir
命令行标志,并将其传递给自定义规则所在的目录。 If you do that, all of the rules in that directory will be automatically registered with ESLint. 如果这样做,该目录中的所有规则将自动向ESLint注册。 This way, however, is deprecated.
但是,不建议使用这种方式。 A better way would be to create a
plugin
, which is a way to distribute bundle of rules through NPM module. 更好的方法是创建一个
plugin
,这是一种通过NPM模块分发规则束的方法。 You can find more information about creating a plugin here . 您可以在此处找到有关创建插件的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.