gpt4 book ai didi

eslint no-restricted-imports - 防止从以模式结尾的路径导入

转载 作者:行者123 更新时间:2023-12-04 11:29:01 27 4
gpt4 key购买 nike

根据eslint no-restricted-imports documentation

When using the object form, you can also specify an array ofgitignore-style patterns:

"no-restricted-imports": ["error", {
"paths": ["import1", "import2"],
"patterns": ["import1/private/*", "import2/*", "!import2/good"] }]

(强调我的)
我想要做的是限制从父索引文件导入 - 因为这会导致循环依赖问题(我也在使用 import/no-cycle 规则,但也显式使用此规则是有意义的。)
也就是说,我想禁止进口,如:
import foo from "../.."; 
import bar from "../../..";
我还想禁止进口,如:
import a from "../Components"; 
但不喜欢
import  b from "../Components/Foo"; 
我曾尝试使用此规则:
'no-restricted-imports': [
'error',
{
patterns: [
'**/..',
'**/Components'
]
},
但这会导致导入错误:
import  b from "../Components/Foo"; 
有没有办法在 gitignore 样式模式中指定“字符串结尾”?

最佳答案

首先,确保您没有设置 import/no-relative-parent-imports ,或任何 ../导入会失败。
其次,如果这真的遵循 .gitignore规则,您不能为文件夹设置规则(例如 **/..**/Components )。
因为,一旦您忽略一个文件夹,该文件夹内元素的任何其他规则都将被忽略。
尝试:

'no-restricted-imports': [
'error',
{
patterns: [
'**/../*',
'!**/../Components',
'**/../Components/*',
'!**/../Components/Foo',
]
},

关于eslint no-restricted-imports - 防止从以模式结尾的路径导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64995811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com