简体   繁体   English

monorepo 中的自定义 Eslint 规则

[英]Custom Eslint rules in monorepo

I have a monorepo with a server and frontend.我有一个带有服务器和前端的 monorepo。 I want to incorporate a set of custom eslint rules into the frontend component of my repo.我想将一组自定义 eslint 规则合并到我的 repo 的前端组件中。 I cannot find a way to tell eslint that there are custom rules in a specific folder within my frontend directory.我找不到告诉 eslint 在我的前端目录中的特定文件夹中有自定义规则的方法。 I don't want to create a new external repo and publish this in order to import the rules.我不想创建新的外部存储库并发布它以导入规则。 So far I think the way to do what I want is to create a sub-package within the main repo (with its own package.json).到目前为止,我认为做我想做的事情的方法是在主仓库中创建一个子包(有它自己的 package.json)。

In my root package.json I have "eslint-plugin-atlas": "file:./src/main/javascript/build/eslint",在我的根 package.json 我有"eslint-plugin-atlas": "file:./src/main/javascript/build/eslint",

In my root.eslintrc I have "plugin:atlas/recommended"在我的 root.eslintrc 我有"plugin:atlas/recommended"

In the directory specified in package.json, in an index.js, I have the below code;在 package.json 指定的目录中,在 index.js 中,我有以下代码;

'use strict';

module.exports = {
    rules: {
        'tsx-qualified-class-state-definition': {
            meta: {
                docs: {
                    description: 'A test rule',
                    category: 'Best Practices',
                    recommended: true,
                },
            },
            create: function(context) {
                console.log(context, 'THIS HAS BEEN HIT');
                return {};
            },
        },
    },
    configs: {
        recommended: {
            rules: {
                'tsx-qualified-class-state-definition': 'error',
            },
        },
    },
};

Obviously the above lint rule won't produce proper linting, but I'd expect to see the console statement to be output.显然,上述 lint 规则不会产生正确的 linting,但我希望看到控制台语句是 output。 When my intellij IDE trys to read the config I get the error;当我的 intellij IDE 尝试读取配置时出现错误;

Error: Failed to load config "plugin:atlas/recommended" to extend from.
Referenced from: F:\dev\workspace\lepton\src\main\javascript\build\.eslintrc.json
    at configMissingError (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:233:9)
    at ConfigArrayFactory._loadExtendedPluginConfig (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:674:31)
    at ConfigArrayFactory._loadExtends (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:607:29)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:545:25)
    at _normalizeObjectConfigDataBody.next (<anonymous>)
    at ConfigArrayFactory._normalizeObjectConfigData (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:491:20)
    at _normalizeObjectConfigData.next (<anonymous>)
    at createConfigArray (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:307:25)
    at ConfigArrayFactory.loadFile (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\config-array-factory.js:382:16)
    at createCLIConfigArray (F:\dev\workspace\lepton\node_modules\eslint\lib\cli-engine\cascading-config-array-factory.js:140:35)

When I run eslint I get errors Definition for rule 'tsx-qualified-class-state-definition' was not found tsx-qualified-class-state-definition当我运行 eslint 时,我收到错误 找不到Definition for rule 'tsx-qualified-class-state-definition' was not found tsx-qualified-class-state-definition

Not a solution built into eslint, but this can be done with the following eslint plugin;不是 eslint 内置的解决方案,但这可以通过以下 eslint 插件来完成;

npmjs.com/eslint-plugin-rulesdir npmjs.com/eslint-plugin-rulesdir

Correct me if I'm wrong, but isn't this kind of an anti-pattern?如果我错了,请纠正我,但这不是一种反模式吗? I think you're supposed to have configs in each package directory.我认为您应该在每个 package 目录中都有配置。

/
 - package.json
   /packages
       /pkg-1
         - .eslintrc.json
       /pkg-2
         - .eslintrc.json

See here for a bit more info, kind of a broader post, but should give you some direction.有关更多信息,请参见此处,这是一篇更广泛的帖子,但应该会给您一些指导。

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

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