本文介绍了ESLint:如何使用"no-restricted-imports"限制某些路径?但是允许子路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Material-UI,并且正在从中切换所有导入

I am using Material-UI and I am switching all my imports from

import { Button } from "@material-ui/core";

import Button from "@material-ui/core/Button";

我想添加一个皮棉规则,以禁止从"@ material-ui/core"直接导入,但允许任何子文件夹,例如"@ material-ui/core/Button".

I wanted to add a lint rule to not allow direct imports from "@material-ui/core", but allow for any subfolders, eg "@material-ui/core/Button".

我发现无限制进口"应该解决的规则,但我只能限制"@ material-ui/core"下的任何内容,因此"@ material-ui/core/*"也受到限制.

I found a "no-restricted-imports" rule which is supposed to tackle it, but I can only restrict anything under "@material-ui/core", so "@material-ui/core/*" gets restricted as well.

我尝试了几种设置规则的方法,但是对于我的用例,所有这些方法都失败了.即:

I tried several ways to set up the rules, but all of them are failing for my use case. Namely:

"no-restricted-imports": ["error", "@material-ui/core"]
///////
"no-restricted-imports": ["error", {
    "patterns": ["@material-ui/core"]
}]

但是两者都不行.我是否缺少某些东西,或者只是无法实现?

But neither would work. Am I missing something or is it just not possible to achieve?

推荐答案

您可以对某些文件路径使用替代

You can use overrides for some filepaths

"no-restricted-imports": ["error", "@material-ui/core"],
"overrides": [{
  "files": [...],
  "rules: {
    "no-restricted-imports": ["error", {
      "patterns": ["@material-ui/core"]
    }]
  },
}],

这篇关于ESLint:如何使用"no-restricted-imports"限制某些路径?但是允许子路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:01