import-js / import-js/eslint-plugin-import

Enhancement: [import/no-restricted-paths] accept target exceptions

Open
#3,050 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
5.9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

In order to support more complex use-cases, it would be really nice to be able to exclude parts of the target path.

Suggested config:

zones: [
  {
    target: './common',
    exceptTarget: './common/tests',
    from: './features',
    message: 'Avoid importing from features.'
  }
]

It could be implemented pretty simple by extending the matchingZones filter:

diff --git a/src/rules/no-restricted-paths.js b/src/rules/no-restricted-paths.js
index 75952dd0..22e06a52 100644
--- a/src/rules/no-restricted-paths.js
+++ b/src/rules/no-restricted-paths.js
@@ -53,0 +54,11 @@ module.exports = {
+                exceptTarget: {
+                  anyOf: [
+                    { type: 'string' },
+                    {
+                      type: 'array',
+                      items: { type: 'string' },
+                      uniqueItems: true,
+                      minLength: 1,
+                    },
+                  ],
+                },
@@ -92 +103,4 @@ module.exports = {
-        .some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)),
+        .some((targetPath) => isMatchingTargetPath(currentFilename, targetPath))
+        && ![].concat(zone.exceptTarget || [])
+        .map((except) => path.resolve(basePath, except))
+        .some((exceptPath) => isMatchingTargetPath(currentFilename, exceptPath)),

This would also resolve #2800

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/rules/no-restricted-paths.js, especially the zone schema and matchingZones filter shown in the issue. Check how target paths are matched, then verify that string and array exceptTarget values exclude matching target paths while ordinary targets remain restricted; add or update the relevant rule coverage if the repository provides it.

Written by the indexing model from the issue text.

Assessment

Tech stack
eslint, javascript
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.