2015-11-03 884 views
0

我需要为ESLint编写自定义规则来检查某人是否使用某些代码结构。我是ESLint的新手,在注册规则时遇到了一些问题,所以我实际上可以在.eslintrc配置文件中使用它。规则本身看起来如下:ESLint无法识别的自定义规则

一些定制-rule.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。我认为这是因为我没有正确要求规则。

吞气任务如下:

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吗?提前致谢!

+0

以下是信息:http://eslint.org/docs/developer-guide/working-with-plugins – Gyandeep

回答

1

目前有两种方法向ESLint注册规则,您可以使用--rulesdir命令行标志并将其传递到您的自定义规则所在的目录。如果你这样做,该目录中的所有规则将自动注册到ESLint。但是,这种方式已被弃用。更好的方法是创建一个plugin,这是一种通过NPM模块分发规则包的方法。你可以找到更多关于创建插件here的信息。