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

标签 eslint glob gitignore

根据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/

相关文章:

javascript - ESLint 规则通过创建同名的 const 来防止覆盖函数(尤其是在赋值期间)

javascript - eslint - 禁用错误通知

php - 使用 Glob() 时如何删除文件路径?

Bash rm 命令在循环内部不起作用

git - 保护文件免受 Git 攻击

javascript - 如何限制 git 跟踪/更新本地更改?

javascript - 如何修复或摆脱 ASP.NET Razor 应用程序中的 ESLint 定义要求?

javascript - 在预提交中运行 ESLint 不会因警告而停止

python - Python 的 glob 的限制?

git - .gitignore 文件夹内容