Skip to content

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Dec 5, 2025

Note: This is a reference implementation. closes #16525

Summary

Integrates the sort-package-json Rust crate into oxfmt to automatically sort package.json files according to npm conventions before passing them to Prettier for final formatting.

Changes

  • Added sort-package-json v0.0.1 dependency
  • Created package_json_sorter module with sorting logic
  • Modified format_by_external_formatter() to detect and sort package.json files
  • Added unit tests for sorting functionality

Behavior

package.json files are sorted - Fields reordered according to npm conventions (name, version, description, scripts, dependencies, etc.)

Sorting happens before Prettier - The sorted JSON is then formatted by Prettier for consistent spacing

package-lock.json is NOT sorted - Only files named exactly "package.json" are affected

Other JSON files unaffected - Regular .json files continue to work normally

Error handling - Invalid JSON returns clear error messages

Example

Before:

{
  "version": "1.0.0",
  "dependencies": {
    "lodash": "^4.17.21"
  },
  "name": "my-app",
  "scripts": {
    "test": "echo test"
  }
}

After:

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "test": "echo test"
  },
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

Test plan

  • Unit tests pass (cargo test -p oxfmt)
  • Code formatted (just fmt)
  • Clippy passes (cargo clippy -p oxfmt)
  • Manual testing confirms sorting works correctly

🤖 Generated with Claude Code

@github-actions github-actions bot added A-cli Area - CLI A-formatter Area - Formatter C-enhancement Category - New feature or request labels Dec 5, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 5, 2025

CodSpeed Performance Report

Merging #16541 will not alter performance

Comparing feat/oxfmt-sort-package-json (9a0fac2) with main (d6d2bcd)

Summary

✅ 42 untouched
⏩ 3 skipped1

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@leaysgur
Copy link
Member

leaysgur commented Dec 9, 2025

Will continue on #16593

@leaysgur leaysgur closed this Dec 9, 2025
@leaysgur leaysgur deleted the feat/oxfmt-sort-package-json branch December 9, 2025 05:27
graphite-app bot pushed a commit that referenced this pull request Dec 10, 2025
…16593)

Fixes #16525, closes #16541

- Use `sort-package-json: 0.0.2`
- Enable by default, can be disabled by `experimentalSortPackageJson: false`
Copilot AI pushed a commit that referenced this pull request Dec 10, 2025
…16593)

Fixes #16525, closes #16541

- Use `sort-package-json: 0.0.2`
- Enable by default, can be disabled by `experimentalSortPackageJson: false`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-formatter Area - Formatter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatter: sort package json

3 participants