Skip to content

Commit

Permalink
Merge pull request #255 from microsoft/awful-ptarmigan
Browse files Browse the repository at this point in the history
JSON sort: sort symbols first, then lower case, then upper case
  • Loading branch information
aiday-mar authored Dec 11, 2024
2 parents 3da5591 + af43912 commit 3d95996
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
44 changes: 32 additions & 12 deletions src/test/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1449,11 +1449,11 @@ suite('Sort JSON', () => {

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

Expand All @@ -1463,21 +1463,41 @@ suite('Sort JSON', () => {
test('sorting an already sorted JSON object with mixed case keys', () => {
const content = [
'{',
' "Test": "Test",',
' "tEst": "tEst",',
' "teSt": "teSt",',
' "test": "test",',
' "tesT": "tesT",',
' "test": "test"',
' "teSt": "teSt",',
' "tEst": "tEst",',
' "Test": "Test"',
'}'
].join('\n');

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

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

test('sorting symbols before letters', () => {
const content = [
'{',
' "Test": "Test",',
' "test": "test",',
' "[test]: "test',
'}'
].join('\n');

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

Expand Down
10 changes: 3 additions & 7 deletions src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,8 @@ 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 sortProperties(properties: PropertyTree[]): void {
properties.sort((a, b) => a.propertyName.localeCompare(b.propertyName));
}

function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningLineNumber: number) {
Expand All @@ -397,7 +393,7 @@ function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningL
const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber!;
beginningLineNumber = beginningLineNumber + diff;

sortPropertiesCaseSensitive(propertyTree.childrenProperties);
sortProperties(propertyTree.childrenProperties);

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

Expand Down

0 comments on commit 3d95996

Please sign in to comment.