Skip to content
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

Introduce importOrderBuiltinModulesToTop for Node Builtins #10

Merged
merged 4 commits into from
May 12, 2022

Conversation

IanVS
Copy link
Owner

@IanVS IanVS commented May 11, 2022

This is a cherry-pick of a PR from @nickhudkins to the upstream project: trivago/prettier-plugin-sort-imports#150, adjusted slightly to work within the rule of this project to not sort beyond side-effect import groups.

@blutorange what do you think of this? I'm reluctant to add another option, but if we changed this without an option it would be a breaking change. I don't have a strong opinion one way or the other, but personally I do like sorting builtins to the top, so it would be nice to have some way to do it.

# Conflicts:
#	src/utils/__tests__/get-sorted-nodes.spec.ts
#	src/utils/get-sorted-nodes.ts
@IanVS IanVS requested a review from blutorange May 11, 2022 12:55
@blutorange
Copy link
Collaborator

blutorange commented May 12, 2022

The commit itself looks good, it's only a very small change.

@IanVS Have you checked if this works for prefixed modules such as import {...} from "node:fs" or import * as fs from "node:fs/promises"? Perhaps adding a test case here might be a good idea.

Other than that, my very personal opinion is that a prettier plugin should be opinionated in the same way prettier is and have as few options as possible. But this plugin doesn't really follow that philosophy. I took a quick look at https://www.npmjs.com/package/@ianvs/prettier-plugin-sort-imports and I'd say there are enough downloads we can't just introduce a breaking change.

In the long term we should decide if we want to tend towards "many options for configurability" or "few options with an opinionated sort order" -- we could remove options in a major release. What are you thoughts about this? I also wonder what users are thinking in this regard.

But in short, if we add this without a new major release, it needs to be an option that defaults to false. And a major release imo only makes sense if we decided to set a new goal for this plugin and start removing other option.

@IanVS
Copy link
Owner Author

IanVS commented May 12, 2022

Thanks @blutorange. I've added some more tests as you suggested.

I agree with you about not making breaking changes outside of majors. It annoys me that prettier itself makes breaking changes in minors, and I don't want to follow that pattern.

I think one thing we should consider is that this plugin is changing the actual AST, rather than only formatting like prettier does. So, there's a chance we do need to allow a few more options than a purely-formatting tool would. But where it makes sense, I think we should avoid options. I'd like to make this behavior the default in the next major version, and maybe even remove the option entirely, unless we hear that it causes big problems for folks.

I think this is ready for another review, when you have time. Thanks again!

@blutorange
Copy link
Collaborator

I'd like to make this behavior the default in the next major version, and maybe even remove the option entirely, unless we hear that it causes big problems for folks.

Semver only works if people actually stick to it, so yes, sounds like a good idea to me

I also don't really like the importOrder option -- it requires me to manually add the dependency I use. At the least I'd prefer a sensible default behavior when no option is set that might perhaps group all imports with the same scope automatically. But that's outside the scope of this issue.

@IanVS IanVS changed the title Introduces importOrderBuiltinModulesToTop for Node Builtins Introduce importOrderBuiltinModulesToTop for Node Builtins May 12, 2022
@IanVS
Copy link
Owner Author

IanVS commented May 12, 2022

I'm going to merge this so I can rebase #12 on main. I think I got an approval above, but if you want any changes, @blutorange, let me know and I'll open another PR.

@IanVS IanVS merged commit 54b3cf5 into main May 12, 2022
@IanVS IanVS deleted the sort-builtins-to-top branch May 12, 2022 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants