Skip to content

fix(sort): stop custom sort data loss, sort indented selections and fix reverse/array sorting - #324

Merged
pascalre merged 6 commits into
masterfrom
feat/sort-correctness
Nov 19, 2026
Merged

pascalre merged 6 commits into
masterfrom
feat/sort-correctness

Conversation

@pascalre

Copy link
Copy Markdown
Owner

What changed

  • Custom sort keeps scalar and falsy values (373dfdf)
    dumpValue() is split from dumpYaml(): dumpValue() dumps any value, and dumpYaml() keeps the empty-document special case (now also for documents js-yaml loads as null/undefined, i.e. only comments). Custom sort checks for the key itself instead of its value.
  • Indented selections can be sorted (84d5012, closes Sorting an arbitrary section (with leading spaces) #53)
    A selection's indentation is removed before sorting and added back afterwards, so it is sorted like a top-level document. New helper removeLeadingSpacesOnEachLine() in lib.ts.
  • sortOrderReverse applies to the default sort (58815f7)
    New JsYamlAdapter.getKeyComparator() chooses js-yaml's sortKeys option and passes a reversed code point comparison when the setting is on.
  • sortArrays with a consistent comparison (3985a74)
    sortArraysRecursively() in sort-util.ts replaces sortArrays()/compareObjects() in yaml-util.ts.
  • Circular dependency removed (9987e13)
    splitYaml(), removeTrailingCharacters() and validateTextRange() moved into the new dependency-free module yaml-text.ts.
  • Smaller package (cb80ad6)
    @types/js-yaml and @vscode/test-electron moved to devDependencies.

Why

  • Data loss in custom sort: dumpYaml() returned "" for every value without keys. Custom sort dumps each keyword's value separately, so apiVersion: 3 became apiVersion: and metadata: {} became metadata: . In addition, if (doc[key]) skipped keywords with the value 0, false, null or "", so they were not moved into the custom order.
  • Sorting an arbitrary section (with leading spaces) #53: The comment processor trims the text, so the first line of an indented selection lost its indentation and loading failed with "bad indentation of a mapping entry". Users had to unindent the selection by hand before sorting.
  • sortOrderReverse was only evaluated in SortUtil.customSort(), i.e. for a recursive custom sort. The default sort was always ascending, although the README describes the setting without that restriction.
  • sortArrays compared items with >/<. Between a number and a string both are false, so Array.prototype.sort left arrays like [c, 10, b, 2] in an arbitrary order. The function also contained a statement that sorted a temporary copy and had no effect.
  • Circular dependency: js-yaml-adapter ↔ yaml-util, reported by dependency-cruiser on every run.
  • Package size: vsce shipped the test runner and type definitions with their transitive dependencies (607 instead of 253 node_modules files).

Behavior changes

  • sortOrderReverse: true now also reverses the default sort and the keys after the custom keywords. The custom keywords themselves keep their configured order.
  • With sortArrays: true, arrays are ordered as numbers (numerically), then booleans, then strings. Mappings, nested lists and null keep their relative order at the end.
  • A document containing only comments can now be sorted instead of failing.

Testing

  • Every fix comes with regression tests that failed before the fix (yaml-util.test.ts, document-transformer.test.ts, sort-util.test.ts, lib.test.ts). For sortArraysRecursively() the new tests target the new function, so the old behavior was reproduced separately.
  • Locally verified: tsc, eslint, dependency-cruiser (no violations), 145 tests via mocha, vsce ls packages without errors.
  • Not verified locally: the full Electron suite (npm test). Please rely on CI.

Not included

  • @types/vscode stays in dependencies: as a devDependency vsce rejects packaging because ^1.98.0 is newer than engines.vscode ^1.49.0. That needs a decision about the minimum VS Code version.
  • locale is still ignored by the default sort; fixing it would change the order for existing users.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M

pascalre and others added 6 commits November 19, 2026 18:15
dumpYaml() returned an empty string for every value without keys, which
was meant for empty documents but also hit numbers, booleans and empty
mappings. Custom sort dumps each keyword's value on its own, so
`apiVersion: 3` became `apiVersion: ` and `metadata: {}` became
`metadata: `. In addition, `if (doc[key])` skipped keywords whose value
is falsy, so they were not moved into the custom order.

Split dumpValue(), which dumps any value, from dumpYaml(), which keeps
the empty document special case and now also covers documents that
js-yaml loads as null or undefined (only comments). Check for the key
itself instead of its value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
A selection was sorted with its original indentation. The comment
processor trims the text, so the first line lost its indentation while
the others kept it, and loading failed with "bad indentation of a
mapping entry". Sorting an indented selection, e.g. a part of a list
or the children of a nested key, was therefore impossible (closes #53).

Remove the selection's indentation before sorting and indent the
result again afterwards, so the selection is sorted like a top-level
document and comments keep their position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
sortOrderReverse was only evaluated by SortUtil.customSort(), which is
used for a recursive custom sort. The default sort and the keys after
the custom keywords were passed to js-yaml as `sortKeys: true` and
therefore always sorted ascending.

Extract the choice of js-yaml's sortKeys option into
getKeyComparator() and pass a reversed code point comparison when
sortOrderReverse is set. The ascending order stays unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
sortArrays() compared items with `>` and `<`. Between a number and a
string both are false, so the comparison was inconsistent and
Array.prototype.sort left arrays like `[c, 10, b, 2]` in an arbitrary
order. Mappings were compared as "[object Object]". The function also
contained a statement that sorted a temporary copy and had no effect.

Replace it with sortArraysRecursively() in sort-util: numbers are
compared numerically, then booleans, then strings; mappings, nested
lists and null keep their relative order at the end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
js-yaml-adapter imported splitYaml() and removeTrailingCharacters()
from yaml-util, which in turn imports js-yaml-adapter. dependency-
cruiser reported this circular dependency on every run.

Move the plain string functions splitYaml(), removeTrailingCharacters()
and validateTextRange() into the new module yaml-text, which has no
dependencies, and import them from there. dependency-cruiser now
reports no violations.

No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
@types/js-yaml and @vscode/test-electron were listed as dependencies,
so vsce packed them and their transitive dependencies into the VSIX.
Both are only needed to compile and to run the tests. Moving them to
devDependencies reduces the packaged node_modules files from 607 to
253.

@types/vscode stays a dependency for now: as a devDependency vsce
rejects packaging because ^1.98.0 is newer than engines.vscode ^1.49.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
@pascalre
pascalre merged commit dcee4ce into master Nov 19, 2026
5 of 8 checks passed
@pascalre
pascalre deleted the feat/sort-correctness branch November 19, 2026 18:25
@sonarqubecloud

Copy link
Copy Markdown

@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 minor

Alerts:
⚠ 2 issues (≤ 0 issues of at least minor severity)

Results:
2 new issues

Category Results
CodeStyle 2 minor

View in Codacy

🟢 Metrics 22 complexity · 0 duplication

Metric Results
Complexity 22
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sorting an arbitrary section (with leading spaces)

1 participant