Skip to content

Commit e140c73

Browse files
authored
Merge pull request #223 from denisw/fix/sort-nested-property
Fix sorting error in case of nested trailing comma
2 parents bbba2d5 + 87b872e commit e140c73

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/test/sort.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ suite('Sort JSON', () => {
5353
'"hobbies" : ["volleyball","drawing","hiking"],',
5454
'"friends" : {',
5555
'"Marc" : {"hobbies" : ["kayaking", "mountaineering"],',
56-
'"age" : 35},',
56+
'"age" : 35,},',
5757
'"Leila" : {"hobbies" : ["watching movies",',
5858
'"reading books"], "age" : 32}}}'
5959
].join('\n');

src/utils/sort.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,17 @@ function findJsoncPropertyTree(formattedDocument: TextDocument) {
222222
endLineNumber = scanner.getTokenStartLine();
223223
currentContainerStack.pop();
224224

225-
// If we are not inside of an empty object and current property end line number has not yet been defined, define it
226-
if (lastNonTriviaNonCommentToken !== SyntaxKind.OpenBraceToken
227-
&& currentProperty!.endLineNumber === undefined) {
228-
229-
currentProperty!.endLineNumber = endLineNumber - 1;
230-
// The current property is also the last property
231-
currentProperty!.lastProperty = true;
232-
// The last property of an object is associated with the line and index of where to add the comma, in case after sorting, it is no longer the last property
233-
currentProperty!.lineWhereToAddComma = lineOfLastNonTriviaNonCommentToken;
234-
currentProperty!.indexWhereToAddComa = endIndexOfLastNonTriviaNonCommentToken;
225+
// If we are not inside of an empty object
226+
if (lastNonTriviaNonCommentToken !== SyntaxKind.OpenBraceToken) {
227+
// If current property end line number has not yet been defined, define it
228+
if (currentProperty!.endLineNumber === undefined) {
229+
currentProperty!.endLineNumber = endLineNumber - 1;
230+
// The current property is also the last property
231+
currentProperty!.lastProperty = true;
232+
// The last property of an object is associated with the line and index of where to add the comma, in case after sorting, it is no longer the last property
233+
currentProperty!.lineWhereToAddComma = lineOfLastNonTriviaNonCommentToken;
234+
currentProperty!.indexWhereToAddComa = endIndexOfLastNonTriviaNonCommentToken;
235+
}
235236
lastProperty = currentProperty;
236237
currentProperty = currentProperty ? currentProperty.parent : undefined;
237238
currentTree = currentProperty;

0 commit comments

Comments
 (0)