-
-
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 useless index in import paths #1290
No useless index in import paths #1290
Conversation
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.
Let's put this behind an option, to avoid it being a breaking change.
6c97e3b
to
1b1abc0
Compare
@ljharb I've updated the PR to use an option 'import/resolver': {
'node': {
'extensions': [
// some extensions
],
},
}, If this setting is not set, Let me know if that looks like a reasonable approach to you. I'm totally open for different ideas. The tests are still failing. I'm not sure what to do about them. To make sure my changes pass the tests, I've only used the test suite for |
1b1abc0
to
29ad42c
Compare
1 similar comment
60c9f11
to
26c60e4
Compare
|
||
```js | ||
// in my-project/app.js | ||
import "./helpers/index"; // should be "./helpers" (not auto-fixable because of helpers.js) |
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 should be autofixable to ./helpers/
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.
Ah. I didn't even know that there is a difference between ./helpers/
--> ./helpers/index.js
and ./helpers
--> ./helpers.js
.
Just to make sure we don't miss an option, these are all possible import paths:
./helpers.js --> ./helpers.js
./helpers --> ./helpers.js
./helpers/ --> ./helpers/index.js
./helpers/index.js --> ./helpers/index.js
./helpers/index --> ./helpers/index.js
./helpers/index/ --> ./helpers/index/index.js
If I'm not mistaken, this means that there is no possibility for ambiguous imports. Either it's an index.js
file in an directory (in this case, we have a trailing /
) or it's a file (in this case, we have no trailing /
). Am I missing something?
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.
Yes.
In the presence of both helpers.js
and helpers/index.js
:
./helpers.js → ./helpers.js
./helpers → ./helpers.js (ambiguous)
./helpers/ → ./helpers/index.js
./helpers/index.js → ./helpers/index.js
./helpers/index → ./helpers/index.js
/.helpers/index/ → ./helpers/index/index.js (error, nonexistent here)
When there is only helpers.js
:
./helpers.js → ./helpers.js
./helpers → ./helpers.js
./helpers/ → ./helpers/index.js (error, nonexistent here)
./helpers/index.js → ./helpers/index.js (error, nonexistent here)
./helpers/index → ./helpers/index.js (error, nonexistent here)
/.helpers/index/ → ./helpers/index/index.js (error, nonexistent here)
When there is only helpers/index.js
:
./helpers.js → ./helpers.js (error, nonexistent here)
./helpers → ./helpers/index.js
./helpers/ → ./helpers/index.js
./helpers/index.js → ./helpers/index.js
./helpers/index → ./helpers/index.js
/.helpers/index/ → ./helpers/index/index.js (error, nonexistent here)
Everything that exists is autofixable except the one ambiguous case.
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.
Thank you very much! That helped a lot!
I've extended the list by the expected result:
helpers.js
and helpers/index.js
exist
Import path (import '...' ) |
Resolves to | Shortest possible path? (Explanation) |
---|---|---|
./helpers.js |
./helpers.js |
✅ (changing the path to ./helpers would make it ambiguous) |
./helpers |
./helpers.js (ambiguous) |
✅ (path is ambiguous, but still decidedly resolves to the file, not the directory) |
./helpers/ |
./helpers/index.js |
✅ (changing the path to ./helpers would make it ambiguous) |
./helpers/index.js |
./helpers/index.js |
❌ (change to ./helpers/ possible) |
./helpers/index |
./helpers/index.js |
❌ (change to ./helpers/ possible) |
./helpers/index/ |
./helpers/index/index.js |
Only helpers.js
exists:
Import path (import '...' ) |
Resolves to | Shortest possible path? (Explanation) |
---|---|---|
./helpers.js |
./helpers.js |
✅ (no useless index part in this path - .js is not needed but could be detected by extensions rule) |
./helpers |
./helpers.js |
✅ |
./helpers/ |
./helpers.js |
|
./helpers/index.js |
./helpers/index.js |
|
./helpers/index |
./helpers/index.js |
|
./helpers/index/ |
./helpers/index/index.js |
Only helpers/index.js
exists:
Import path (import '...' ) |
Resolves to | Shortest possible path? (Explanation) |
---|---|---|
./helpers.js |
./helpers.js |
|
./helpers |
./helpers/index.js |
✅ |
./helpers/ |
./helpers/index.js |
❌ (change to ./helpers possible) |
./helpers/index.js |
./helpers/index.js |
❌ (change to ./helpers possible) |
./helpers/index |
./helpers/index.js |
❌ (change to ./helpers possible) |
./helpers/index/ |
./helpers/index/index.js |
I've added tests for all these cases and adapted the implementation to that. Would you prefer to see a dedicated warning for the ambiguous case or should this just be accepted as a valid import?
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've updated your table for two cases, otherwise looks good.
b2c17d0
to
a8242dd
Compare
I've changed the name of the new option from |
ed359c1
to
7fa9386
Compare
I've updated the PR to include tests for |
53a31b0
to
2d6c203
Compare
I'm struggling a little bit with the support for ESLint 2 and 3. I've fixed the issue that It looks like the other failing tests are mostly due to the TypeScript/alternate parsers support which isn't working in ESLint 2 and 3 apparently. I get messages like the following: errors:
[ TypeError: traverser.traverse is not a function
at Object.parseForESLint (/Users/timkraut/Development/eslint-plugin-import/node_modules/typescript-eslint-parser/parser.js:41:15)
at Object.exports.parse (/Users/timkraut/Development/eslint-plugin-import/node_modules/typescript-eslint-parser/parser.js:79:17)
at parse (/Users/timkraut/Development/eslint-plugin-import/utils/parse.js:38:17)
at Function.ExportMap.parse (/Users/timkraut/Development/eslint-plugin-import/src/ExportMap.js:339:15)
at Function.ExportMap.for (/Users/timkraut/Development/eslint-plugin-import/src/ExportMap.js:323:25)
at Function.ExportMap.get (/Users/timkraut/Development/eslint-plugin-import/src/ExportMap.js:286:10)
at Context.<anonymous> (/Users/timkraut/Development/eslint-plugin-import/tests/src/core/getExports.js:332:31)
at callFn (/Users/timkraut/Development/eslint-plugin-import/node_modules/mocha/lib/runnable.js:348:21)
at Hook.Runnable.run (/Users/timkraut/Development/eslint-plugin-import/node_modules/mocha/lib/runnable.js:340:7)
at next (/Users/timkraut/Development/eslint-plugin-import/node_modules/mocha/lib/runner.js:309:10)
at Immediate.<anonymous> (/Users/timkraut/Development/eslint-plugin-import/node_modules/mocha/lib/runner.js:339:5)
at processImmediate (timers.js:632:19) ], Do you have an idea how I might fix those?
|
@timkraut i've pulled out your chai change into a separate commit on master (it'll be pushed shortly) If older eslints don't support messageId, let's not use it yet. |
2d6c203
to
f0025ec
Compare
f0025ec
to
4808942
Compare
@ljharb I've removed the |
Is there anything else I can do to get this merged/released? |
@ljharb Could you release this, too? Currently the documentation states something which isn't possible with the release from january. |
Nope, only one person can release this package. |
As discussed in #394, this PR adds an option
noUselessIndex
to detect (and fix) paths containing unnecessaryindex
parts (with or without file extension):Given the following file structure:
Before:
After:
Implementation detail:
I've replaced the
sumBy
function withreduce
to get rid of Lodash (not necessary in this case IMO). A benchmark which compares these 2 alternatives shows no difference between them or a slight advantage forreduce
: https://www.measurethat.net/Benchmarks/ShowResult/46787Fixes #394.