Eslint no-restricted-import仅限制从软件包根目录导入? [英] Eslint no-restricted-imports only restrict importing from a package root?
问题描述
例如我想限制导入 react-use
,但允许 react-use/lib/usePrevious
.我试过了:
E.g. I want to restrict importing react-use
, but allow react-use/lib/usePrevious
. I tried:
rules: {
'no-restricted-imports': [2, { patterns: [
'react-use',
] }],
},
还有
rules: {
'no-restricted-imports': [2, { patterns: [
'react-use',
'!react-use/*',
] }],
},
但是,在两种情况下,我都会遇到以下错误:
However, in both case, I get an error for:
import usePrevious from 'react-use/lib/usePrevious';
我只想要一个错误:
import something from 'react-use';
推荐答案
This is a limitation with the ignore
library that ESLint uses to check each pattern. It was built to replace existing file ignore methods with something more compact and barebones.
具体来说,问题似乎在于此忽略系统假定您的文件结构严格类似于目录,其中限制 react-use
并忽略!react-use/*
表示 react-use
是一个文件夹.但是,通过模块导入结构, react-use
可以引用文件(通过其 index.js
),同时包含诸如 react-use/lib之类的子文件夹.
使其既可以用作文件,也可以用作文件夹.
Specifically, the problem seems to be that this ignore system assumes your file structure is strictly directory-like, where restricting react-use
and ignoring !react-use/*
implies that react-use
is a folder. However, with a module import structure, react-use
can refer to a file (via its index.js
) and simultaneously contain subfolders like react-use/lib
making it usable as both a file and a folder.
因此,在限制子路径时将无法忽略模块的基础.
So it won't be possible to ignore the base of the module while restricting a subpath.
这篇关于Eslint no-restricted-import仅限制从软件包根目录导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!