-
Notifications
You must be signed in to change notification settings - Fork 71
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
Case-insensitive sort of named imports #7
Comments
I actually prefer the uppercase ones (CONSTANTS, Classes, Types) coming first. |
I prefer functions, Classes, CONSTANTS, Types :) |
Could there be an option to toggle this? I could write a PR. |
In my opinion one of the benefits of this package is that it has no options. I'd like to keep it like that until an option is absolutely necessary. Convince me and we'll change this! |
That's fair! This would be an entirely optional thing, with the default being to continue working as things do now, so for the vast majority of users that are happy with the current setup, they wouldn't even need to know that an option exists. For users like me, that one boolean would mean the difference between having no option but to enforce rules manually and having an easy way to standardize it. So I guess my argument is that for most people, it would be as if there were no options, and only people who tried it and ran into that frustration would be likely to even discover it. |
+1 for such an option. WebStorm/PhpStorm has an opposing opinion here and sorts case-insensitive.
That's fine. I personally don't care how my imports are sorted, it's not a part of the code that I really read. As long as all my tools handle them automatically, quickly and consistently, any ordering is fine.
Fair point. And WebStorm/PhpStorm has an option to turn off the sorting completely. However, it's just extremely convenient to be able to hit Whether or not WebStorm/PhpStorm can be configured differently, the fact that a major IDE's default behavior opposes the only behavior of this plugin warrants adding an option, IMHO. It doesn't make it "absolutely necessary", but it might convince a few users not to drop the plugin. |
I don't care about which way to use, but agree that fewer options are better. Since in this case most established tooling seems to be case-insensitive (examples being TSLint and WebStorm) I think that would be the obvious choice here as well. Whatever your decision though, thanks for the great work :) |
Following TSLint's and WebStorm's lead sounds like a good idea! Three is the magic number, though. Can you find one more popular precedent? Also, how do TSLint and WebStorm work when it comes to sorting the import statements (sorting on |
IntelliJ/WebStorm sorts case insensitive on import { buttonPrimary, linkButton, Select, spinnerOverlay } from 'src/components/common'
import DataTable from 'src/components/DataTable'
import ExportDataModal from 'src/components/ExportDataModal'
import FloatingActionButton from 'src/components/FloatingActionButton'
import { icon, spinner } from 'src/components/icons'
import { IGVBrowser } from 'src/components/IGVBrowser'
import { IGVFileSelector } from 'src/components/IGVFileSelector'
import { DelayedSearchInput, TextInput } from 'src/components/input'
import Modal from 'src/components/Modal'
import { notify } from 'src/components/Notifications'
import { FlexTable, HeaderCell, SimpleTable, TextCell } from 'src/components/table |
Thanks! Status: I’m now researching using String.prototype.localeCompare/Intl.Collator for a more human sort. const collator = new Intl.Collator("en", {
sensitivity: "base",
numeric: true
});
function compare(a, b) {
return collator.compare(a, b) || (a < b ? -1 : a > b ? 1 : 0);
} |
Fixes #7. Imports are now sorted case insensitively, just like TSLint and many popular editors/IDEs. As a bonus, numbers in imports are now sorted by their numeric value for even more humane sorting. Fixed: `import {} from "a"` is no longer considered a side-effect import. Only imports completely lacking the `{...} from` part are. Fixed: ".foo" is no longer considered a relative import. Improved: The special casing of "." and ".." is now more consistent. Those two cases are now treated just like "./" and "../", and no other patterns are adjusted. Improved: Trailing spaces after imports are now preserved for better UX. Before, accidentally adding some trailing spaces would result in a "Please run autofix to sort these imports!" error, but the autofix wouldn’t actually sort anything but only remove some spaces, which was a bit weird.
Could you all try out v4.0.0-beta.1? |
tl;dr: It works as intended, all the caveats below are unrelated to sorting and harmless. I tested the new beta with PhpStorm, where I turned "Sort imported members" and "Sort imports by modules" on. For the first test, I essentially want PhpStorm not to destroy the eslint sorting:
All of the remaining diffs are formatting changes (for the better). For instance:
becomes
if it fits my configured line length. I understand that the For the second test, I essentially ran the inverse scenario. Essentially I want to use PhpStorm for formatting, and then
All of the remaining (non-comma) diffs are only about where blank lines are inserted -- PhpStorm does not handle that. In conclusion, the sort order is now 100% aligned on my project. PhpStorm's "organizing" of imports not only sorts but also ensures the line length fits. And |
I’ve tried the beta on a couple of projects myself now, and the diff churn isn’t too bad actually. And I do agree that case insensitive sorting does feel more natural now that I’ve tried it a bit for real. @kachkaev @zarsky-broad What do you think? |
Hi @lydell 👋 I just tried the new version in a rather big monorepo at work and the upgrade went quite smoothly. The diffs look adequate and the resulting order is oftentimes indeed a bit easier to read. Glad that you like case-insensitive sorting too! |
I see minor thrash in the following situations, which I think are beyond the scope of this plugin to address, and which I think are acceptable (I have a macro that causes intellij to invoke the eslint fix action on a file after I run reformat on it, so any thrash is resolved immediately anyway):
Perhaps interestingly, we use absolute imports for internal paths, rather than relative (i.e. tl;dr 👍👍👍 EDIT: the first point above caused issues with our implementation of Prism, but they have a babel plugin that they recommend using instead of the way we did it. |
Released in v4.0.0. Thank you everyone for the great discussion! |
I like the original defaults. 😅 |
@JounQin Hi! This plugin was never supposed to be a replacement for tslint. I created this before I used TypeScript. I’ve only used tslint in one project that someone else created. So “tslint supports that” arguments don’t bite on me 😄 I’m not interested in changing the sorting. My advice would be to:
|
👋 @lydell!
After switching from TSLint to ESLint and your plugin, I've noticed that named imports are sorted differently. Your approach is case-sensitive, while TSLint's
ordered-imports
is not by default.The former notation feels more natural to me. WDYT?
The text was updated successfully, but these errors were encountered: