-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New rule: no-relative-packages
#966
New rule: no-relative-packages
#966
Conversation
Use this rule to prevent importing packages through relative paths. It's useful in Yarn/Lerna workspaces, were it's possible to import a sibling package using `../package` relative path, while direct `package` is the correct one. Co-authored-by: Rafal Lindemann <[email protected]> Co-authored-by: Tom Payne <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
0a8d571
to
4337f0f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I love this use case, I don't think this is the proper way to configure it. What if the foo
package had a "src" dir, and a file inside it wanted to require('../')
?
I think perhaps a better way to configure a rule like this is that the user would provide a list of paths (globs would be supported), and those paths would be considered boundaries - also called "jails".
So, for your example structure:
"rules": {
"import/package-boundaries": ["error", {
"boundaries": ["packages/*"]
}],
}
That would overlap nicely with lerna/yarn workspaces package settings, and could allow for any imports or requires within a boundary, but nothing could escape the boundary from within.
@ljharb I would say it's an idea for another rule. Here, it's not forbidden to use ../ from src, because both 'src' and 'src/..' live in the same package. My proposal detects only, when package boundaries are crossed with relative paths. What it does, it verifies to which packages both modules (importer and importee) belong. If the package is different - an error is raised. So as long as you stay within same package, you can have any relative paths. On additional note, with this setup it's possible to make a simple autofix. |
How does your rule - since it has no configuration - know what a package boundary is? One use case that some people have is nested package.jsons within the same project; would that conflict with this rule? |
Every With this directory structure:
Importing Both are unsafe because:
We're using this rule successfully on our yarn monorepo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, thanks for clarifying.
This should be useful for normal single-package repos also, since they could just as easily require out of their own directory.
4337f0f
to
2804af1
Compare
Looks nice, would love to use it. But PR looks abandoned. Any updates on that? |
It's not abandoned; it just needs review from other maintainers, after which I'll rebase and merge it. |
I don't really understand everything in this thread but I was wishing there was a rule to limit the depth of relative paths. like |
any update with this PR ? |
Anything we can do to get this merged? |
@benmosher could you help with this one? :D |
Can I do anything to get this merged? - I find this new rule really useful for monorepos |
2804af1
to
b934ed2
Compare
@panrafal I've rebased this, but there seems to be a test failing. |
5 similar comments
b934ed2
to
6f5c52c
Compare
no-relative-packages
Use this rule to prevent importing packages through relative paths.
It's useful in Yarn/Lerna workspaces, were it's possible to import a sibling
package using
../package
relative path, while directpackage
is the correct one.Examples
Given the following folder structure:
And the .eslintrc file:
The following patterns are considered problems:
The following patterns are NOT considered problems: