Eslint,防止从同一包中导入

huangapple go评论50阅读模式
英文:

Eslint, prevent to import from the same package

问题

可以告诉eslint避免从同一包中导入吗?

我在一个monorepo中,并且希望阻止开发人员以及某些IDE的自动导入从同一包中导入。

例如:

// 在@lib/utils内部
import { convertNames } from @lib/utils

我希望强制开发人员改用以下方法:

// 在@lib/utils内部
import {convertNames} from '../../someWhere' 

我尝试使用eslint-plugin-importno-restricted-imports,但没有成功。

非常感谢任何建议。

英文:

Is it possible to say to eslint to avoid to import from the same package?

I'm in a monorepo and I would like to block the dev and for sure the auto-import of some IDEs to import from the same package.

e.g.

// inside the @lib/utils
import { convertNames } from @lib/utils

I'd like to force the Devs to use this approach instead

// inside the @lib/utils
import {convertNames} from '../../someWhere' 

I tried with eslint-plugin-import and no-restricted-imports without success.

any suggestion is really appreciated

答案1

得分: 1

这是我会这样做的方式:

  1. 为整个项目创建一个 ESLint 配置,优先使用路径别名。
  2. 为每个路径别名文件夹创建一个覆盖配置,以避免使用路径别名并鼓励使用相对路径。

如果您在 .eslintrc 中使用 JavaScript 文件,甚至可以以编程方式执行此操作。

手动配置(需要手动添加新的路径)

// https://json.schemastore.org/eslintrc
module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
  },
  env: {
    es6: true,
    node: true,
  },
  globals: {
    MyGlobal: true,
  },
  overrides: [
    {
      files: ["*.ts", "*.js"],
      parserOptions: {
        project: ["tsconfig.json"],
        createDefaultProgram: true,
      },
      rules: {
        "no-restricted-imports": [
          "error",
          {
            patterns: [
              {
                group: ["*someWhere"],
                message: "Please use our path alias `@lib/utils` instead.",
              }
            ],
          },
        ],
      },
    },
    {
      files: [
        "**/someWhere/**/*.js",
        "**/someWhere/**/*.ts"
      ],
      rules: {
        "no-restricted-imports": [
          "error",
          {
            patterns: [
              {
                group: ["@lib/utils"],
                message: "Please avoid using @lib/utils in this folder to avoid circular dependencies. Use relative paths instead.",
              }
            ],
          }
        ]
      },
    },
  ],
};

如果您想以编程方式生成这些覆盖配置,可以读取您的 tsconfig 并获取路径及其文件夹,然后为所有这些路径生成此覆盖配置,以便避免定义的所有路径别名避免这些循环依赖。

如果这有帮助,请标记此答案并点赞 🙏

英文:

This is how I would do it:

  1. Create an ESLint configuration for the entire project, preferring the path aliases
  2. Create an override configuration for each path alias folder to prefer avoid the path aliases and encourage relative paths

if you use a javascript file for your .eslintrc, you can even do it programatically.

Manual Configuration (will need to add new paths by hand)

// https://json.schemastore.org/eslintrc
module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
  },
  env: {
    es6: true,
    node: true,
  },
  globals: {
    MyGlobal: true,
  },
  overrides: [
    {
      files: ["*.ts", "*.js"],
      parserOptions: {
        project: ["tsconfig.json"],
        createDefaultProgram: true,
      },
      rules: {
        "no-restricted-imports": [
          "error",
          {
            patterns: [
              {
                group: ["*someWhere"],
                message: "Please use our path alias `@lib/utils` instead.",
              }
            ],
          },
        ],
      },
    },
    {
      files: [
        "**/someWhere/**/*.js",
        "**/someWhere/**/*.ts"
      ],
      rules: {
        "no-restricted-imports": [
          "error",
          {
            patterns: [
              {
                group: ["@lib/utils"],
                message: "Please avoid using @lib/utils in this folder to avoid circular dependencies. Use relative paths instead.",
              }
            ],
          }
        ]
      },
    },
  ],
};

if you want to generate the overrides programmatically, you can read your tsconfig and get the paths and their folders there. then, generate this override for all those paths so it will make all the path aliases you define avoid those circular dependencies.

If this help, please mark this answer and upvote 🙏

huangapple
  • 本文由 发表于 2023年7月10日 23:49:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定