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

Fix microsoft/vscode#209655: fix case-sensitive JSON sorting error #238

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/test/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,4 +1435,52 @@ suite('Sort JSON', () => {

testSort(content, expected, formattingOptions);
});

test('sorting a JSON object with mixed case keys', () => {
const content = [
'{',
' "tEst": "tEst",',
' "tesT": "tesT",',
' "teSt": "teSt",',
' "Test": "Test",',
' "test": "test"',
'}'
].join('\n');

const expected = [
'{',
' "Test": "Test",',
' "tEst": "tEst",',
' "teSt": "teSt",',
' "tesT": "tesT",',
' "test": "test"',
'}'
].join('\n');

testSort(content, expected, formattingOptions);
});

test('sorting an already sorted JSON object with mixed case keys', () => {
const content = [
'{',
' "Test": "Test",',
' "tEst": "tEst",',
' "teSt": "teSt",',
' "tesT": "tesT",',
' "test": "test"',
'}'
].join('\n');

const expected = [
'{',
' "Test": "Test",',
' "tEst": "tEst",',
' "teSt": "teSt",',
' "tesT": "tesT",',
' "test": "test"',
'}'
].join('\n');

testSort(content, expected, formattingOptions);
});
});
10 changes: 10 additions & 0 deletions src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ function sortJsoncDocument(jsonDocument: TextDocument, propertyTree: PropertyTre
return sortedJsonDocument;
}

function sortPropertiesCaseSensitive(properties: PropertyTree[]): void {
properties.sort((a, b) => {
const aName = a.propertyName ?? '';
const bName = b.propertyName ?? '';
return aName < bName ? -1 : aName > bName ? 1 : 0;
});
}

function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningLineNumber: number) {
if (propertyTree.childrenProperties.length === 0) {
return;
Expand All @@ -390,6 +398,8 @@ function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningL
const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber!;
beginningLineNumber = beginningLineNumber + diff;

sortPropertiesCaseSensitive(propertyTree.childrenProperties);

queue.push(new SortingRange(beginningLineNumber, propertyTree.childrenProperties));

} else if (propertyTree.type === Container.Array) {
Expand Down