2015-11-03 886 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的信息。