-
-
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
Proposal: disallow "index" in import paths #394
Comments
This should work in concert with resolution, so that when |
I think that should be an option. Some folks might want to prevent |
I think it makes sense. ( There is a similar co-existence issue with package.json defining I think it would make sense to ignore the edge cases for the first implementation. There is always |
I think it would be very dangerous to ignore the case of |
@ljharb I feel like that case would be better solved by a "no-ambiguous-imports" rule. Such a rule would report an import with >1 possible file given all of the extensions, path folders, magic indexes, package mains, etc. This no-index rule, as stated, is basically stylistic. And I agree, it would work best in tandem with another rule that protects against possible ambiguous resolved module paths. |
It's not just stylistic - in tandem with a separate rule, it'd be fine - but as I implied in #394 (comment), "stylistic" goes out the window when complying with the style suggestion would silently change the way your program runs. Even if my project allows ambiguous imports, it might still prefer to omit "index" when possible, thus I'd have that other rule disabled - if this rule warned on "foo/index" when "foo" exists, then nothing but a ♯ eye would be able to prevent me accidentally importing the wrong thing, as even the resolver rule would happily accept it. Put another way, the airbnb config couldn't possibly ever enable this rule as requested, unless it never, under any circumstances recommended a change that would result in a different file being imported. |
Fair enough. Would be easy enough to resolve both of "whatever/index" and "whatever" and do something different (nothing? different message?) if the two paths don't match. This rule would be easily auto-fix-able too, I suspect. But yeah, you certainly wouldn't want to auto fix to a different module. |
I think that it absolutely should be easily autofixable, and you wouldn't want to autofix to a different module, which is my exact objection :-) If that's handled, then I think this rule would be an amazing addition, along with a separate |
This is largely addressed by |
No useless path segments works for my use case. Disallowing import file names other than index feels like a very specific use case -- is it widespread enough to warrant inclusion in this plugin? No offense meant, I just haven't worked on any projects that followed that rule without exception. |
To clarify; Airbnb follows this rule without exception, but |
We are currently looking for a way to find imports with an useless New rule |
I thought no-useless-path-segments already covers this - is there a use case for forbidding index without also forbidding useless path segments? If it doesn’t cover it, it should; if there’s no use case, this can then be closed. |
It looks like it doesn't do this yet. I currently don't see a reasonable use case where one but not the other one is required. The only thing we should pay attention to is not to rewrite ambiguous imports. I'll work on a PR to add this to |
I have some issues with the setup on macOS. If I clone the repo, run Edit: Looks like the same tests are also failing in the pipeline of the PR. |
cc @benmosher for failing tests on master |
Importing the directory will resolve to index, so including index in your path is unnecessary. I think this would make a good rule for this project, what do you think?
Bad:
Good:
The text was updated successfully, but these errors were encountered: