我需要为 ESLint 编写一个自定义规则来检查是否有人使用某些代码结构。我是 ESLint 的新手,在注册规则时遇到了一些问题,因此我可以在.eslintrc
配置文件中实际使用它。规则本身如下所示:
一些自定义规则.js
'use strict';
module.exports = function(context) {
function applyRule(node) {
// some logic
}
return {
"Identifier": applyRule
}
};
module.exports.schema = [];
然后我有以下配置文件:
.eslintrc
{
"ecmaFeatures": {
"blockBindings": true,
"forOf": true,
"modules": true,
"arrowFunctions": true,
"classes": true
},
"rules": {
"semi": 2,
"some-custom-rule": 2
},
"env": {
"es6": true,
"browser": true
}
}
当我尝试使用 Gulp 任务执行 ESLint 时,会弹出以下错误:1:1 error Definition for rule 'some-custom-rule' was not found
. 我认为这是因为我没有正确要求该规则。
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());
});
如何使用我的自定义规则?我必须将它传递给 ESLint 还是什么?提前致谢!