根据eslint no-restricted-imports documentation
When using the object form, you can also specify an array of gitignore-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/