英文:
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-import
和no-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
这是我会这样做的方式:
- 为整个项目创建一个 ESLint 配置,优先使用路径别名。
- 为每个路径别名文件夹创建一个覆盖配置,以避免使用路径别名并鼓励使用相对路径。
如果您在 .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:
- Create an ESLint configuration for the entire project, preferring the path aliases
- 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 🙏
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论