no-restricted-imports: allow some specific imports in some specific directories #17047
-
Hello, I have a question and idea about no-restricted-imports rule. Preconditions:In my projects I often use the following approach: for every UI element that is provided by external library (e.g. @mui/material/Button) I create own wrapper, that re-exports library classes/types etc. that I use in my project. Somethnig like this: // src/components/Button/index.ts
export {default, ButtonProps} from '@mui/material/Button'; And reuse this wrapper in all places in the project insead of direct imports from external libraries: // In all places of the project instead of this:
// import Button from '@mui/material/Button';
// I do this:
import Button from 'src/components/Button'; This way, if later I would like to customize this component, or fix a bug or something else - I could do it by creating own Button.tsx with necessary customizations and reexport this customized version, so no changes in the outside code required. For example, to add default variant for the button I could do like this: // src/components/Button/Button.tsx
import MUIButton from '@mui/material/Button';
export default function Button(props) {
return <MUIButton {...props} variant={props.variant || 'contained'} />
} and reexport this customized version instead of native one: // src/components/Button/index.ts
export {default} from './Button.tsx';
export {ButtonProps} from '@mui/material/Button'; And to enforce all developers to use this button wrapper everywhere instead of direct imports from external library I use // ...
'no-restricted-imports': ['error', {
paths: [
{ name: '@mui/material/Button', message: 'Import from @mui/material/Button is deprecated. Use "src/components/Button" instead.' }
]
}]; The problemWith the configuration above eslint gives error if I import '@mui/material/Button' inside my own button wrapper folder (src/components/Button), which is right behavior from the point of view of eslint, but it is not convenient for me, as in all places inside button wrapper folder I have to put eslint-disable comments. I could disabe no-restricted-imports rule inside src/components/Button folder using "overrides" section of eslint like this: overrides: {
"files": ["src/components/Button/*"],
"rules": {"no-restricted-imports": "off"}
} but that will disable all no-restricted-import paths (and I have several there, like separate path for every UI component of the MUI library), which is not what I need - I need to allow import of "@mui/material/Button" but keep all the rest imports specified in no-restricted-imports rule prevented. Question/ProposalSo my question is - do you know any other way how I could easily achieve desired behavior using curren eslint API? 'no-restricted-imports': ['error', {
paths: [
{
name: '@mui/material/Button',
message: 'Import from @mui/material/Button is deprecated. Use "src/components/Button" instead.' },
exceptIn: ['src/components/Button/*'] // this will still allow import from @mui/material/Button inside src/components/Button fodler
]
}]; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Instead of disabling the rule, you could use |
Beta Was this translation helpful? Give feedback.
Instead of disabling the rule, you could use
overrides
to set a configuration that doesn't disallow"@mui/material/Button"
.