简体   繁体   English

插件内所有规则的 ESLint 共享配置

[英]ESLint shared configuration for all rules within a plugin

I've created a es lint plugin that provides me with a rule which checks that provided import is not in a dynamic list of blacklisted imports.我创建了一个 es lint 插件,它为我提供了一个规则,该规则检查提供的导入不在黑名单导入的动态列表中。

The list is loaded from a JSON file.该列表是从 JSON 文件加载的。

Currently I am loading this file in the create method of a rule, but the rule is being recreated for each file, which in turn makes the linter slow.目前我在规则的create方法中加载这个文件,但是正在为每个文件重新创建规则,这反过来又使 linter 变慢。

I've found the following text in the ESLint documentation, but no other mention of a way to share some global object within an invocation of the linter.我在 ESLint 文档中找到了以下文本,但没有提到在调用 linter 中共享某些全局对象的方法。

https://eslint.org/docs/latest/user-guide/configuring/configuration-files#adding-shared-settings https://eslint.org/docs/latest/user-guide/configuring/configuration-files#adding-shared-settings

ESLint supports adding shared settings into configuration files. ESLint 支持将共享设置添加到配置文件中。 Plugins use settings to specify information that should be shared across all of its rules.插件使用设置来指定应该在其所有规则之间共享的信息。 You can add settings object to ESLint configuration file and it will be supplied to every rule being executed.您可以将设置对象添加到 ESLint 配置文件中,它将提供给正在执行的每个规则。 This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.如果您要添加自定义规则并希望它们能够访问相同的信息并且易于配置,这可能会很有用。

Is this supported?这支持吗? I've tried exporting a settings object in the index.js of the plugin, but it is not being picked up at all.我已经尝试在插件的 index.js 中导出一个设置对象,但它根本没有被拾取。

module.exports.settings = {
    "sharedData": "Hello"
}

Looks like I already have a workaround, but wondering if someone will come up with a better solution.看起来我已经有了解决方法,但想知道是否有人会提出更好的解决方案。

What I did in the end was to load the configuration in the index of the rule itself, which is loaded at the start of ESLint.我最后做的是将配置加载到规则本身的索引中,该索引是在 ESLint 开始时加载的。

Instead of depending on context.getCwd() I've fallen back to _dirname.而不是依赖于context.getCwd()我已经退回到 _dirname。 Which proved to be enough for my use case.事实证明,这对我的用例来说已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM