-
-
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
No relative parent imports rule #1093
No relative parent imports rule #1093
Conversation
As discussed IRL with @ljharb, this is a more extreme version of #966. This helps engineers write code that fits well with DAG based build tools like Bazel or Buck. The inspiration for this change comes from this essay by the Google Fuschia team and the OCAML module system. This is my first commit to the project so please let me know if I've missed anything or I haven't followed norms. |
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.
This looks great - can we add some tests for require
s, and also, can we try to handle import()
?
} | ||
|
||
return { | ||
ImportDeclaration(node) { |
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.
what about import()
calls?
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.
LGTM!
Is there a way to use this rule to restrict parent imports out of a subdir (say, projects/*
) - but allow parent imports up to that subdir? #966 doesn't seem to have a solution for this either.
My general approach for that would be either a.) create a projects folder relative to the file ( Also, holy moly you're the fastest PR reviewer ever. |
Actually, re-reading your question - I think I misunderstood. Could an example of what you're asking for be:
And you'd like to allow |
@chrislloyd yes, precisely. |
I think you could achieve that with overrides? "overrides": [
{
"files": [ "./project-foo/*.js" ],
"excludedFiles": "./project-foo/tests/*.test.js",
"rules": {
"import/no-relative-parent-imports": "error"
}
}
] |
Wouldn't that allow the test file to import from |
🤔 yep you're right. I think this rule could definitely take options to override some of the behavior but a.) I'd like to understand the use-cases a bit better. The way I intend to use this, exceptions won't be a problem. b.) They can be introduced in further PR's as they'd be non-breaking changes to the rule. |
I totally agree that such options can be added later, but I'd like to get an idea of what the schema might look like before merging the rule, to ensure future compatibility :-) |
Ok! Are you looking to perhaps exclude certain folder types and also use {
"rules": {
"import/no-relative-parent-imports": [2, { "exclude": "*/tests/*.js" }],
"import/no-restricted-paths": [2, { "zones": [ { "target": ".", "from": "./project-foo" } ] }]
}
} |
Hmm - it's more like i want to have: Maybe I don't understand the use case of never wanting to do a parent import from anywhere. Certainly tests always have to do a parent import, unless the test file always lives as a direct sibling of the implementation (which i think even the pattern of colocating tests doesn't provide) - but in general, having a dir with a "helpers" directory, and having all sibling folders of "helpers" able to import "../helpers/blah", is a common pattern. |
Ah great, yeah. I think in those cases I'd like to see a layout like:
That effectively shifts files further up and creates lots of big directories with lots of files in it. The rule is intended to be opinionated. I think what you're asking for could be achieved perhaps with more flexible configuration to
|
Yes, that’s possible. Maybe since the rule is intended to be opinionated, we should really flesh out the docs so the intention and reasoning is clear? |
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.
I'd like to see this use utils/moduleVisitor
instead of isStaticImport
, etc., and enhance utils/moduleVisitor
to visit dynamic imports (import()
).
See the no-unresolved
rule for an example. Enhancing moduleVisitor
to support dynamic import()
means every rule using it will test import()
as well!
Awesome, thanks for the reviews @ljharb & @benmosher. Addressed your feedback in followup commits. |
The CI failure above seems to be because of a flakey test environment (passing on all other environments, randomly timing out) - could ya'll restart the job? I don't think I have permissions in Travis without being a contributor to the repo. |
@benmosher review ping! |
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.
looks great! thanks!
Released in v2.13.0 |
Thanks for adding rule! But found a bug: |
@Alex-Sokolov please file a new issue if you’ve found a bug |
no-relative-parent-imports
Use this rule to prevent imports to folds in relative parent paths.
It's useful for large codebases codebases to enforce directed-acyclic-graph like folder structures.
Examples
Given the following folder structure:
And the .eslintrc file:
The following patterns are considered problems:
The following patterns are NOT considered problems: