eslint / json

JSON language plugin for ESLint
Apache License 2.0
46 stars 4 forks source link

New Rule: package-json-sorted-keys / package-json-key-order #37

Closed kachkaev closed 1 month ago

kachkaev commented 1 month ago

Rule details

The rule would normalize package.json keys by following a convention in sort-package-json (or similar).

What type of rule is this?

Suggests an alternate way of doing something

Example code

{
  "dependencies": {
    "sort-package-json": "1.0.0",
    "sort-object-keys": "1.0.0"
  },
  "version": "1.0.0",
  "name": "my-awesome-project"
}

{
  "name": "my-awesome-project",
  "version": "1.0.0",
  "dependencies": {
    "sort-object-keys": "1.0.0",
    "sort-package-json": "1.0.0"
  }
}

Participation

Additional comments

I am using sort-package-json via prettier-plugin-packagejson and it works well. Automatic key sorting is especially useful in monorepos with lots of package.json files and contributors. The problem with prettier-plugin-packagejson is that it goes outside 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.

Now that ESLint supports JSON parsing, it’d be great to achieve the value of sort-package-json via a rule. Conceptually it is similar to sorting imports in JS files, so I believe that it fits the scope. WDYT?


UPD Maybe this should be done elsewhere because the rule would be package.json specific. There is a discussion in https://github.com/keithamus/sort-package-json/issues/322.

kachkaev commented 1 month ago

Prior art: https://github.com/kellyselden/eslint-plugin-json-filesjson-files/sort-package-json (by @kellyselden)

fasttime commented 1 month ago

Thanks for the suggestion @kachkaev, this sounds like an interesting rule. My feeling is that such a rule would not fit well in the standard ESLint JSON plugin, because its usage is limited to Node.js. For reference, other built-in Node.js-specific rules like no-process-exit were deprecated in ESLint v7 and replaced by equivalent rules in eslint-plugin-n. To my knowledge, eslint-plugin-n only supports JavaScript rules, although that could change in the future. So my recommendation is to leave this rule for a userland ESLint plugin for the time being as you suggested in the other discussion.

Any other thought about this suggestion @eslint/eslint-tsc? cc @eslint/eslint-community.

bmeck commented 1 month ago

Just leaving this here but not really having much to add. This would need to be a bit careful as well since sorting can affect some keys like imports and exports etc.

nzakas commented 1 month ago

This plugin is focused on generic JSON linting. As such, package.json-specific rules are out of scope.

However, as with ESLint rules in general, you can always create your own plugin with any rule that you want.