【问题标题】:eslint no-restricted-imports - prevent an import from from path ending with patterneslint no-restricted-imports - 防止从以模式结尾的路径导入
【发布时间】:2021-03-07 18:50:44
【问题描述】:

根据eslint no-restricted-imports documentation

使用对象形式时,还可以指定一个数组 gitignore 风格的模式

"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 样式模式中指定“字符串结尾”?

【问题讨论】:

标签: eslint glob gitignore


【解决方案1】:

首先,确保您没有设置import/no-relative-parent-imports,否则任何../ 导入都会失败。

其次,如果这真的遵循.gitignore 规则,则您不能有文件夹规则(如**/..**/Components)。
因为,一旦您忽略了一个文件夹,该文件夹内部元素的任何其他规则都将被忽略。

试试:

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

【讨论】:

  • 手动为每个子文件夹添加异常是行不通的
  • @dwjohnston 我的意思是,尝试将其作为测试,以便确定它的行为与 .gitignore 相同。如果是这样,我将编辑我的答案。
猜你喜欢
  • 2023-03-12
  • 2021-09-08
  • 1970-01-01
  • 2021-08-27
  • 2020-10-27
  • 2022-06-24
  • 1970-01-01
  • 2021-09-05
  • 2021-09-20
相关资源
最近更新 更多