Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
velvolue opened this issue Sep 4, 2024 · 5 comments
Open

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

velvolue opened this issue Sep 4, 2024 · 5 comments

Comments

@velvolue
Copy link

velvolue commented Sep 4, 2024

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

@ljharb
Copy link
Member

ljharb commented Sep 4, 2024

Can't you already handle this by using eslint overrides?

@velvolue
Copy link
Author

velvolue commented Sep 5, 2024

Can't you already handle this by using eslint overrides?

Some use-cases can be solved with overrides, but sadly not all. If you have multiple zones for the same target with different exceptions, you can't represent that with overrides as they would cancel each other out.

@velvolue
Copy link
Author

velvolue commented Sep 5, 2024

For our use-case specifically, we managed to find a way using overrides in the end (though not as clean as I hoped), so this issue is not a blocker for us. I still think this would be a nice feature though 🙂

@ljharb
Copy link
Member

ljharb commented Sep 6, 2024

also can't target be combined with except already?

@velvolue
Copy link
Author

velvolue commented Sep 9, 2024

also can't target be combined with except already?

No, except applies only to from as the documentation states:

except may be defined for a zone, allowing exception paths that would otherwise violate the related from. Note that it does not alter the behaviour of target in any way.

It would be nice if target and from had similar configuration. Either through execpt/exceptTarget as suggested here, or by allowing negated paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants