Repository navigation
fix(sort): stop custom sort data loss, sort indented selections and fix reverse/array sorting - #324
Merged
Merged
Conversation
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
|
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| CodeStyle | 2 minor |
🟢 Metrics 22 complexity · 0 duplication
Metric Results Complexity 22 Duplication 0
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.
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.



What changed
373dfdf)dumpValue()is split fromdumpYaml():dumpValue()dumps any value, anddumpYaml()keeps the empty-document special case (now also for documents js-yaml loads asnull/undefined, i.e. only comments). Custom sort checks for the key itself instead of its value.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()inlib.ts.sortOrderReverseapplies to the default sort (58815f7)New
JsYamlAdapter.getKeyComparator()chooses js-yaml'ssortKeysoption and passes a reversed code point comparison when the setting is on.sortArrayswith a consistent comparison (3985a74)sortArraysRecursively()insort-util.tsreplacessortArrays()/compareObjects()inyaml-util.ts.9987e13)splitYaml(),removeTrailingCharacters()andvalidateTextRange()moved into the new dependency-free moduleyaml-text.ts.cb80ad6)@types/js-yamland@vscode/test-electronmoved todevDependencies.Why
dumpYaml()returned""for every value without keys. Custom sort dumps each keyword's value separately, soapiVersion: 3becameapiVersion:andmetadata: {}becamemetadata:. In addition,if (doc[key])skipped keywords with the value0,false,nullor"", so they were not moved into the custom order.sortOrderReversewas only evaluated inSortUtil.customSort(), i.e. for a recursive custom sort. The default sort was always ascending, although the README describes the setting without that restriction.sortArrayscompared items with>/<. Between a number and a string both are false, soArray.prototype.sortleft 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.js-yaml-adapter↔yaml-util, reported by dependency-cruiser on every run.node_modulesfiles).Behavior changes
sortOrderReverse: truenow also reverses the default sort and the keys after the custom keywords. The custom keywords themselves keep their configured order.sortArrays: true, arrays are ordered as numbers (numerically), then booleans, then strings. Mappings, nested lists andnullkeep their relative order at the end.Testing
yaml-util.test.ts,document-transformer.test.ts,sort-util.test.ts,lib.test.ts). ForsortArraysRecursively()the new tests target the new function, so the old behavior was reproduced separately.tsc,eslint, dependency-cruiser (no violations), 145 tests via mocha,vsce lspackages without errors.npm test). Please rely on CI.Not included
@types/vscodestays independencies: as a devDependency vsce rejects packaging because^1.98.0is newer thanengines.vscode ^1.49.0. That needs a decision about the minimum VS Code version.localeis 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