keithamus / sort-package-json

Sort an Object or package.json based on the well-known package.json keys
MIT License
806 stars 88 forks source link

Convert to ESLint plugin/ Provide an ESLint plugin? #322

Open fisker opened 3 months ago

fisker commented 3 months ago

ESLint starting to support other languages. Should we consider?

Zamiell commented 1 month ago

The ESLint ecosystem has mostly agreed that formatting should be left to dedicated formatting tools like Prettier. Thus, I think package.json sorting is best done as a Prettier plugin, which is exactly what prettier-plugin-packagejson does. (It simply calls sort-package-json under the hood.)

kachkaev commented 1 month ago

I opened https://github.com/eslint/json/issues/37 but maybe this should be done outside @eslint/json plugin. Sorting keys via prettier-plugin-packagejson has been working well for me for a few years. The problem with this plugin is that it goes outside of the scope of Prettier:

What Prettier is not concerned about Prettier only prints code. It does not transform it. This is to limit the scope of Prettier. Let’s focus on the printing and do it really well! Here are a few examples of things that are out of scope for Prettier:

  • ...
  • Sorting/moving imports, object keys, class members, JSX keys, CSS properties or anything else. Apart from being a transform rather than just printing (as mentioned above), sorting is potentially unsafe because of side effects (for imports, as an example) and makes it difficult to verify the most important correctness goal.

Sorting package.json keys is similar to sorting imports. It’s not just formatting so it’s a concern of ESLint. The question is whether to sort as part of @eslint/json or as a separate sort-package-json plugin.

mrmckeb commented 1 month ago

We also use the Prettier plugin. I agree that this should not go into ESLint, as people have been moving away from using ESLint for formatting.