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

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

Open
1 task done
kachkaev opened this issue Oct 6, 2024 · 1 comment
Open
1 task done

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

kachkaev opened this issue Oct 6, 2024 · 1 comment
Labels

Comments

@kachkaev
Copy link

kachkaev commented Oct 6, 2024

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

  • I am willing to submit a pull request to implement this rule.

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 keithamus/sort-package-json#322.

@kachkaev kachkaev changed the title New Rule: sorted-keys / key-order New Rule: package-json-sorted-keys / package-json-key-order Oct 6, 2024
@kachkaev
Copy link
Author

kachkaev commented Oct 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant