import-js / import-js/eslint-plugin-import
Suggesting an `exceptTarget` param for `import/no-restricted-paths`
- Dominant language
- JavaScript
- Stars
- 5.9k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Description
Hi there 👋
I'm having a bit of trouble with the `import/no-restricted-paths` rule for a fine tuned setup. Given the following file structure, I'd like to lint our usage of [subpath imports](https://nodejs.org/api/packages.html#subpath-imports) (with an ESLint warning) for the `src/foo` folder and subfolders when importing them from outside those places (from `src/bar` for instance), but _not_ when importing from inside (from `src/foo/fooSubDirA` for instance).
```
src
├── foo # Subpath import setup with `#foo`
├── fooSubDirA
└── fooSubDirB
└── bar
└── barSubDir
```
This should trigger an ESLint warning:
```ts
// src/bar/barSubDir/file.ts
import { something } from '../../foo/fooSubDirA/file'; // ❌ Unexpected path "../../foo/fooSubDirA/file" imported in restricted zone. Please use path mapping with the '#foo' alias instead.
```
This should work without any warning:
```ts
// src/foo/fooSubDirB/file.ts
import { something } from '../fooSubDirA/file'; // ✅
```
My understanding of the `except` parameter is that it's a `from` exception, not a `target` one. I'd like to have a similar parameter, like `targetException` or `exceptTarget` to remove some target files from having to use a given subpath import. For my use-case, `target` would be `./src`, and `exceptTarget` would be `./src/foo`.
Do you think it's already possible with the currently available settings? If so, could you show me how I would do it? I'm aware I can use globs for targets, but I couldn't figure out how to say "everything, at any depth in my whole project, except this specific folder's content at any depth" with a glob. If that's currently not possible, would you be open to such an addition to the rule? I find it way easier to setup exceptions with "positive" matches rather than trying to setup negative ones (especially with globs, which are quite limited with exception patterns).
I'm well aware of the position of some maintainers here regarding subpath imports/path mapping in general, but I believe such a parameter could benefit more use-cases than just mine. The `exceptTarget` param I'm suggesting would cover much more things that this, it's in no way associated to subpath imports or any other path mapping feature.
Contributor guide
Research direction
Start with the `import/no-restricted-paths` rule and trace how its existing `target` and `except` settings are interpreted. Done means determining whether current settings can express the requested exception and, if not, defining and implementing `exceptTarget` behavior with coverage for imports inside and outside the excluded target.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100