【问题标题】:Custom rule not recognized by ESLintESLint 无法识别自定义规则
【发布时间】:2016-02-03 20:14:28
【问题描述】:

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

some-custom-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。我认为这是因为我没有正确要求该规则。

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 还是什么?提前致谢!

【问题讨论】:

标签: javascript gulp eslint


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2020-01-29
    • 1970-01-01
    • 2020-06-30
    • 2023-03-14
    • 1970-01-01
    • 2020-08-15
    • 2019-12-21
    • 2017-02-27
    相关资源
    最近更新 更多