-
Notifications
You must be signed in to change notification settings - Fork 659
Group organized imports by distance #4725
Group organized imports by distance #4725
Conversation
✅ Deploy Preview for docs-rometools ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
I don't really agree with the philosophy proposed in the discussion, which is really old btw, a lot of things have changed. The reason why I don't agree with philosophy is because it doesn't take into consideration the possible side effects of external libraries. If I had to suggest a change, I propose the exact opposite, where local imports are moved at the very end. A real use case pops up in frontend applications, where CSS styles that belong to an external library must precede the local styles. |
The discussion may be old, but the prior art they reference (https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md and https://github.com/trivago/prettier-plugin-sort-imports) are still current and widely used as far as I know. I understand libraries may have side-effects, but that's the same reason the current
Maybe I'm misunderstanding, but that's what my proposal is also doing, no? It moves external imports to the top, and local imports to the bottom. |
FWIW, I've updated the tests, so hopefully they'll be all green now. I suspect @ematipico and I are actually mostly in agreement on what it should do, since this change would indeed cause "CSS styles that belong to an external" to be imported before local styles, so I haven't given up on it yet :) Another thing I would like to mention is that I think this PR might be a useful step towards a lint rule such as "noMixedImportGroups" (name TBD) that could be used to mandate newlines between import groups. This would basically support the |
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 is another fantastic contribution! Thank you!
Happy to merge once the conflicts is solved |
Done! |
Summary
This PR groups imports using a fixed order. This is similar to what is proposed in #42 although the exact groups is slightly different. I have tried to follow the suggestion in that discussion that the "philosophy in general is that code closer to the file is imported after code further away" and attempted to implement this in a way that does not require further config changes.
I have not yet updated any documentation, I'll be happy to do so if you think this approach is worthwhile to pursue.
Test Plan
I've added a test case for import groups.