Repository navigation
feat(fmt): sort object keys with the sort-keys setting - #360
Open
simonvanlierde wants to merge 5 commits into
Open
simonvanlierde wants to merge 5 commits into
simonvanlierde wants to merge 5 commits into
Conversation
- Add `sort-keys` to `[format]` and `per-file-format`, off by default: it is the main thing the pretty-format-json hook does that `jsonvet fmt` did not. - Sort keys at every depth by code point after reading quotes and escapes, as Python's `sorted` does, with a stable sort so duplicate keys keep their order. - A comment above a key or at the end of its line moves with the key. The comments before the first key are kept apart, in `Tree::heads`, so they stay at the top; the comments after the last key and in an empty object were never attached to a key and stay too. Keeping them out of `Container` holds the fmt memory peak where it was. - The language server turns sorting off for range formatting, which cannot move members across the object; `--fix` never formats, so it never sorts. - A key that carries a suppressed problem can move, which the suppression check reads as a change of what is hidden, so such a file is left as it is. - The fuzz target runs its second option set with sorting on. Fixes #352 Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
…orting - A block comment before the first key on its own line, such as /* about b */ "b": 1, was taken into the object's head comments, so after sorting it sat in front of whichever key came first. Only the comments on the lines before the first key stay at the top now. Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
simonvanlierde
temporarily deployed
to
preview
December 2, 2026 01:40 — with
GitHub Actions
Inactive
|
Playground screenshots for 7dd83d4, desktop and phone in light and dark: download the zip (kept 14 days). |
- The Windows checkout gives docs/format.md CRLF line ends, so splitting on a fenced block found nothing and test (windows-latest) panicked with an index out of bounds. Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
simonvanlierde
temporarily deployed
to
preview
December 2, 2026 01:55 — with
GitHub Actions
Inactive
- The escaped-pair case only compared a pair with U+FFFF, so a wrong joined code point still sorted last and 10 key_order mutants survived. A pair between its raw neighbours, and escapes that form no pair, now catch them. - The early returns in Tree::sort_keys (one-member objects and arrays, objects already in order, an empty head list) change no output, so .cargo/mutants.toml excludes their mutants. Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
sort-keystojsonvet fmt, so it can replace the pretty-format-json hook without losing comments.[format]andper-file-formattakesort-keys, off by default. Keys sort at every depth by code point after quotes and escapes are read, with a stable sort, so duplicate keys keep their order.--fixnever sort. Whole-file formatting in the language server follows the setting.Fixes #352
Tested: the repo check passes (697 tests). The new tests fail with sorting off, with the header handling off, and with a sort that reverses duplicates. The format fuzz target ran for 10 minutes with sorting on: 1,761,285 runs and no crash.
Not checked: I did not add the setting to the playground, which passes format options to wasm one by one. A file where a suppression hides a problem at a key that sorting moves is still left as it is, because the suppression check counts tokens by position; I did not teach it to follow moved keys.