eslint-plugin-package-json icon indicating copy to clipboard operation
eslint-plugin-package-json copied to clipboard

🐛 Bug: `sort-collections`: `preinstall` and `postinstall` hooks aren't sorted as co-located to `install`

Open rakleed opened this issue 10 months ago • 14 comments

Bug Report Checklist

  • [x] I have tried restarting my IDE and the issue persists.
  • [x] I have pulled the latest main branch of the repository.
  • [x] I have searched for related issues and found none that matched my issue.

Expected

{
  "scripts": {
    "build": "vite build",
    "dev": "vite",
    "preinstall": "echo",
    "postinstall": "patch-package && echo && nolyfill install",
    "lint": "eslint .",
    "preview": "vite preview"
  },
}

Actual

{
  "scripts": {
    "build": "vite build",
    "dev": "vite",
    "lint": "eslint .",
    "postinstall": "patch-package && echo && nolyfill install",
    "preinstall": "echo",
    "preview": "vite preview"
  },
}

Additional Info

Although the prettier-plugin-packagejson plugin sorts as I expect, so they are currently incompatible for this case.

rakleed avatar Jan 19 '25 20:01 rakleed

Looks like a case related to https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/499 -> https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/pull/632. cc @sasial-dev from #632 - what do you think?

JoshuaKGoldberg avatar Jan 19 '25 20:01 JoshuaKGoldberg

That's interesting! I definitely think it could be a bug in my implimentation.

sasial-dev avatar Jan 19 '25 22:01 sasial-dev

After looking at it again - is it working as expected @rakleed? I notice that there's no install script in that package.json for it to colocate with? And so that's why it's down with the scripts with the first letter of p.

sasial-dev avatar Jan 19 '25 23:01 sasial-dev

And also - what happens when you add an install script to your package.json?

sasial-dev avatar Jan 19 '25 23:01 sasial-dev

I notice that there's no install script in that package.json for it to colocate with? And so that's why it's down with the scripts with the first letter of p.

But prettier-plugin-packagejson can handle such a case.

And also - what happens when you add an install script to your package.json?

In that case, it sorts as expected. But I have never seen anyone rewrite the install script.

rakleed avatar Jan 19 '25 23:01 rakleed

I get what you mean now! I totally didn't forget the install script was built in. The question we have now is do we consider it intended behaviour or do we create a list of builtin scripts that are considered colocated?

sasial-dev avatar Jan 19 '25 23:01 sasial-dev

I think the latter. At the very least, as a user, I would expect preinstall to come before postinstall. Now, whether they're sorted with the "i"s, I don't really have an opinion. So, if we're going to need to make a change anyway, perhaps aligning with prettier-plugin-packagejson should be a goal?

michaelfaith avatar Jan 20 '25 00:01 michaelfaith

perhaps aligning with prettier-plugin-packagejson should be a goal?

It is based on sort-package-json, but it seems that they updated defaultNpmScripts last time 6 years ago, so I am not sure if it's complete:

https://github.com/keithamus/sort-package-json/blame/819cd97d787e81c7cb12a618bd07dc100c4bc9f8/index.js#L199-L212

rakleed avatar Jan 20 '25 10:01 rakleed

@JoshuaKGoldberg what do you think?

rakleed avatar Jan 27 '25 19:01 rakleed

perhaps aligning with prettier-plugin-packagejson should be a goal?

It is based on sort-package-json, but it seems that they updated defaultNpmScripts last time 6 years ago, so I am not sure if it's complete:

https://github.com/keithamus/sort-package-json/blame/main/index.js#L209-L222

They do have 'install' in that list though, so we should be able to rely on it as a built-in. I think any missing built-ins from the last 6 years would be a separate issue from this one.

I'm 👍 on a feature request to align with prettier-plugin-packagejson. Sounds like that's the consensus here?

JoshuaKGoldberg avatar Jan 28 '25 19:01 JoshuaKGoldberg

@JoshuaKGoldberg @michaelfaith should the status of the issue be changed to https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/labels/status%3A%20accepting%20prs?

rakleed avatar Feb 09 '25 19:02 rakleed

Thanks for the nudge. Yeah, i think we've determined there's work to do here. 👍

michaelfaith avatar Feb 09 '25 19:02 michaelfaith

@all-contributors please add @rakleed for bug.

🤖 Beep boop! This comment was added automatically by all-contributors-auto-action. Not all contributions can be detected from Git & GitHub alone. Please comment any missing contribution types this bot missed. ...and of course, thank you for contributing! 💙

JoshuaKGoldberg avatar Feb 10 '25 02:02 JoshuaKGoldberg

@JoshuaKGoldberg

I've put up a pull request to add @rakleed! :tada:

allcontributors[bot] avatar Feb 10 '25 02:02 allcontributors[bot]

:tada: This is included in version v0.52.1 :tada:

The release is available on:

Cheers! 📦🚀

github-actions[bot] avatar Aug 07 '25 22:08 github-actions[bot]