diff --git a/src/index.test.ts b/src/index.test.ts index de5852767..65d74a610 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -151,13 +151,14 @@ test('should throw if custom sort has invalid category sort values', async (t) = for (const validJson of validJsonExamples) { test(`should return '${validJson}' unchanged`, async (t) => { - const output = await format(validJson, { + const validJsonWithNewline = `${validJson}\n`; + const output = await format(validJsonWithNewline, { filepath: 'foo.json', parser: 'json', plugins: [SortJsonPlugin], }); - t.is(output, validJson); + t.is(output, validJsonWithNewline); }); } diff --git a/src/index.test.ts.md b/src/index.test.ts.md index abec415e6..7ae848998 100644 --- a/src/index.test.ts.md +++ b/src/index.test.ts.md @@ -20,7 +20,8 @@ Generated by [AVA](https://avajs.dev). "a": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted JSON object @@ -38,7 +39,8 @@ Generated by [AVA](https://avajs.dev). "a": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object recursively @@ -55,7 +57,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted JSON object recursively @@ -72,7 +75,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object within an array recursively @@ -94,7 +98,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ 2␊ - ]` + ]␊ + ` ## should sort an unsorted JSON object within an array recursively @@ -116,7 +121,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ 2␊ - ]` + ]␊ + ` ## should validate a deeply nested sorted JSON object recursively @@ -142,7 +148,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted deeply nested JSON object recursively @@ -168,7 +175,8 @@ Generated by [AVA](https://avajs.dev). "z": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object with unconventional keys @@ -204,7 +212,8 @@ Generated by [AVA](https://avajs.dev). "{": null,␊ "{}": null,␊ "￿": null␊ - }` + }␊ + ` ## should sort a JSON object with unconventional keys @@ -240,7 +249,8 @@ Generated by [AVA](https://avajs.dev). "{": null,␊ "{}": null,␊ "￿": null␊ - }` + }␊ + ` ## should sort JSON objects recursively within a nested array @@ -268,7 +278,8 @@ Generated by [AVA](https://avajs.dev). }␊ ],␊ "z": null␊ - }` + }␊ + ` ## should validate JSON objects recursively within a nested array @@ -296,7 +307,8 @@ Generated by [AVA](https://avajs.dev). }␊ ],␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object with a simple custom sort @@ -314,7 +326,8 @@ Generated by [AVA](https://avajs.dev). "a": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted JSON object with a simple custom sort @@ -332,7 +345,8 @@ Generated by [AVA](https://avajs.dev). "a": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object with a numeric custom sort @@ -356,7 +370,8 @@ Generated by [AVA](https://avajs.dev). },␊ "first": "first",␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted JSON object with a numeric custom sort @@ -380,7 +395,8 @@ Generated by [AVA](https://avajs.dev). },␊ "first": "first",␊ "z": null␊ - }` + }␊ + ` ## should validate a sorted JSON object with a complex custom sort @@ -427,7 +443,8 @@ Generated by [AVA](https://avajs.dev). "a": null␊ },␊ "z": null␊ - }` + }␊ + ` ## should sort an unsorted JSON object with a complex custom sort @@ -472,4 +489,5 @@ Generated by [AVA](https://avajs.dev). "050": null␊ },␊ "z": null␊ - }` + }␊ + ` diff --git a/src/index.test.ts.snap b/src/index.test.ts.snap index 21d4f6b99..1ec976235 100644 Binary files a/src/index.test.ts.snap and b/src/index.test.ts.snap differ diff --git a/src/index.ts b/src/index.ts index 62ebc92b9..d7000f035 100644 --- a/src/index.ts +++ b/src/index.ts @@ -195,7 +195,7 @@ export const parsers = { (ast.type === 'ArrayExpression' && jsonRecursiveSort) ) ) { - return ast; + return jsonRootAst; } let sortCompareFunction: (a: string, b: string) => number = lexicalSort; @@ -272,7 +272,12 @@ export const parsers = { return aIndex - bIndex; }; } - return sortAst(ast, jsonRecursiveSort, sortCompareFunction); + const sortedAst = sortAst(ast, jsonRecursiveSort, sortCompareFunction); + + return { + ...jsonRootAst, + node: sortedAst, + }; }, }, } as Record;