为什么我的 eslint 插件显示未找到规则的定义?

20

我的插件

@bluelovers/eslint-plugin https://github.com/bluelovers/ws-node-bluelovers/tree/master/packages/eslint-plugin

我的基础配置 https://github.com/bluelovers/ws-node-bluelovers/blob/master/packages/eslintrc/.eslintrc.json

运行时用户配置

{
  "extends": [
    "bluelovers"
  ]
}

我可以在用户仓库中输入。
eslint --print-config .

这可以显示配置并且没有错误,我也能在列表中看到我的插件配置

   "@bluelovers/no-irregular-whitespace": [
      "error",
      {
        "skipComments": true,
        "skipStrings": false,
        "skipTemplates": false,
        "skipRegExps": false,
        "ignores": [
          " "
        ]
      }
    ],

但是当我输入 eslint index.ts 时,它显示错误。

   1:1   error    Definition for rule '@bluelovers/no-irregular-whitespace' was not found  @bluelovers/no-irregular-whitespace

index.ts

export const r = /[ \t\uFEFF\xA0 ]+$/;

const IRREGULAR_WHITESPACE = /[\f\v  ᠎           ​   ]+/mgu;

我该怎样解决这个问题?

10
至少在我的情况下,这不是一个配置问题,只需简单地重新启动VS Code即可解决。因此,在您开始查看十几个eslint插件的文档之前,您可能想尝试一下这种方法。我会把这个提示留在这里。 - tillsanders
@bluelovers你有没有找到这个问题的答案?我看到你已经从项目中移除了eslintrc。我也遇到了同样的问题,卡在这里了。 - Ashwin
2个回答

10
我认为关键的缺失部分是在配置文件中没有“plugins”部分。
你为什么要从“bluelovers”扩展?你是否已发布一个共享配置?看起来你正在开发一个插件,而不是一个配置。
然后,您使用了规则“@bluelovers/no-irregular-whitespace”,其中包含前导符号“@”。
如果您的插件已发布为“@bluelovers/eslint-plugin”,您应尝试类似以下方式:
{
    "plugins": ["@bluelovers"],
    "extends": ["@bluelovers"], // Assuming you have @bluelovers/eslint-config published, otherwise this might look different or could be skipped
    "rules": {
        "@bluelovers/no-irregular-whitespace": ["error"]
    }
}

1
我同时拥有插件和配置文件,且插件字段已在配置文件中设置。 - bluelovers
eslint-config-bluelovers,@bluelovers/eslint-plugin - bluelovers

7
这意味着 eslint 找不到你的 index.js 中的规则。尝试查看你的 index.js 导出内容。它们应该像这样:
module.exports = {
  rules: {
    [myRuleName]: myRule
  }
};

而不是这样:

exports.default = {
  rules: {
    [myRuleName]: myRule
  }
};


如果您使用的是 TypeScript,请将您在 index.ts 文件中的导出更改为 export = {...},而不是 export default {...}

2
这个答案已经过时了吗?文档提供了一个规则的示例,但是关键字“rules”在那篇文章中并没有出现。 - O. R. Mapper
那个链接没有提到“rules”,这让人感到困惑,而这个链接专门讲解如何使用插件,其中涉及到了“rules”。链接地址为:https://eslint.org/docs/developer-guide/working-with-plugins。 - brooklynDadCore
如果你的项目没有index.js文件怎么办? - Suncat2000
不确定 index.js 是从哪里来的。据我所知,.eslintrc.js 应该是 eslint 配置文件的文件名。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接