-
Notifications
You must be signed in to change notification settings - Fork 133
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
Do not reorder side effect imports #110
Comments
Probably also solves #95, if I understand that issue correctly. |
* The relative order of side effect and non side effect imports is now preseverd by default. See the readme and trivago#110 for more info. * I renamed the `getSortedNodes` method to `getSortedNodesByImportOrder`; and added a new implementation for `getSortedNodes` that first splits the imports at each side effect node, then sorts each chunk via the original `getSortedNodesByImportOrder`. I also moved the logic for adjusting comments and inserting new lines to the new `getSortedNodes` method. * With the exception of one test case (`imports-with-comments-and-third-party` has a side effect import `import "./commands"`), all existing tests still pass without any modifications. This leads me to believe you haven't considered these kind of import statements yet and how they should be treated. So this PR could be thought of as defining the behavior for that scenario. * I also added a few additional tests for the new behavior with side effect imports.
…ld keep their relative order) trivago#110
@blutorange Same for me. We would like to use this plugin, but the ordering must be "perfect", as the users do not expect their code to break unexpectedly just because of saving a file for example. |
…ct-import-order Do not reorder side effect nodes #110
…ide-effect-import-order Revert "Do not reorder side effect nodes #110"
Same for us, I just crashed all of our unit tests because for our jest setup we need a specific import order: nrwl/nx#6361 (comment). Also, I didn't find any workaround, except adding those files to the .prettierignore. |
@ayusharma This bug is really a showstopper, sometimes for a few files you can use @NiklasPor workaround mentioned above, but it does not scale when you run a huge codebase with many devs that should be aware, making the code really fragile. |
These issues make me wonder if anybody who's using this plugin actually has any side effect imports in files that are not in the .prettierignore. If nobody does, then #111 wouldn't even affect existing users. |
@blutorange #111 is great solution. Thanks! Now I hope to get Trivago's team support to get this bug solved. |
Actually, our workaround looks now a bit different. I just overwrote the sorting order for those specific files. Still not a great solution: // prettierrc.js
overrides: [
{
files: 'test-setup.ts',
options: {
importOrder: ['jest.*', '<THIRD_PARTY_MODULES>'],
},
},
] |
Has this issue being solved? |
The current state of the issue, as far as I know, is that it's not a bug and also working as intended. There was the suggestion to add an option (disabled by default) that enables #111. If anybody wants to take that PR and add said option, feel free to, but personally I'm not interested in a solution that is "broken" by default. Perhaps somebody else has an idea how to solve this in a way that can be enabled by default? |
@blutorange is it time to fork? |
@blutorange I agree that you PR would not bother anyone, and I don't understand the maintainers point, but maybe we can proceed step by step. |
Hmm, yeah, forking this project just for changing one default option is not worth the effort. If anything I was thinking about a different sorting plugin with a different philosophy (closer to prettier, opinionated, not many settings, more emphasis on correctness). But for now that's just a thought. So yes, since this plugin already quite a few options and keeping the number of options low does not seem to be a major goal (?), one more options could be the way to go. I might add that option to the PR when I have time for it, or sometime else does it in the mean time. Edit: If we make this an option, we can go a little step further and call it |
I think that #111 is an important bugfix, and that this plugin is not suitable for production use without it. So, I've published a fork which is currently the same as this package, but with #111 included (without any kind of option necessary). It can be found at https://www.npmjs.com/package/@ianvs/prettier-plugin-sort-imports for anyone else who might find it useful. |
Just had some annoying CSS issues because the import of the library's CSS was moved to after our custom style overrides. |
We moved too for the exact same reason. |
Any forward motion towards unifying these efforts? Can #111 not be merged or reworked? Or @IanVS's fork be merged into this one? It sux to have a whole fork for what is in essence a bug, as this plugin is unreliable without the fix. @blutorange it seems you are most involved with this, so may I ask what is the blocker here? |
@Morriz The blocker is that the maintainers of this project consider this bug fix as "it fails the basic idea of the plugin" |
@Morriz FWIW, my fork now has a few features that this project does not have, and gets a fair number of npm downloads. I use it in a few of my own projects, and continue to maintain it. In case that eases your mind about using a fork. |
Apologies if this has been mentioned already, I did not find any issue for, though.
Is your feature request related to a problem?
A side effect import is an import that only imports a module for its side effects, without importing any values (i.e. where
ImportDeclaration.specifiers
is an empty array). These are currently sorted just like any other imports. Since those imports are explicitly used for side-effects, changing the order will usually result in a different behavior.Now side effects are something that should be avoided if possible, and library code should have side effects. However, there are some valid use cases, such as loading mocks for tests, mock-fs or loading global libraries in an index file of a wep app, etc. For example:
The above would be re-ordered, which breaks the test.
Describe the solution you'd like
Side effect imports should not be reordered. eslint-plugin-import does not reorder them either, for the same reason.
Since it is hard to know which side effect imports affect which other imports, I would suggest the following solution: treat a side effect import as a boundary that splits the imports into two groups, the imports before the the side effect import and the imports after the side effect import. Then only sort the imports before and after the side effect import as you would normally. This is I think the easiest to implement and also the safest. If users want to have side effect at the top, they can just move the side effect imports to the top, and the formatter will leave them there.
I made this a feature, but I would almost consider it a bug. By default, side effect imports should not be reordered. An option could be added for sorting these imports too, but I'm not sure if that's really necessary. It also goes against prettier's philosophy of limiting the options as much as possible.
So for example:
In the example above, keep the side effect imports as they are. Sort the imports in group 1 and group 2 using whatever options are set. Do not move any import from group 1 to group 2 or vice-versa.
Describe alternatives you've considered
Another alternative I can think of is to simply keep the relative ordering of the side effect imports, but allow moving non side effect imports anywhere. Or always just put side effect imports at the top. Both of this possibilities have the disadvantage they the may be unsafe: putting an import from before a side effect import after it may break it because it does not expect that side effect; putting an import from after a side-effect before it may break it because it relies on the side effect. It's also unclear how imports with and without side effects should be ordered relatively to each other.
The text was updated successfully, but these errors were encountered: