From 3c2b31995195eb891d5887ebc324059eba3b86ec Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Sun, 27 Aug 2023 16:09:06 -0230 Subject: [PATCH] Fix accidental removal of trailing newline (#170) This plugin began removing the trailing newline from files as of v3. This was caused by an accidental removal of the "root" AST node, which was introduced by Prettier v3. The parse has been updated to preserve this node, which in turn preserves the trailing newline. Fixes #168 --- src/index.test.ts | 5 ++-- src/index.test.ts.md | 54 +++++++++++++++++++++++++++-------------- src/index.test.ts.snap | Bin 911 -> 911 bytes src/index.ts | 9 +++++-- 4 files changed, 46 insertions(+), 22 deletions(-) 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 21d4f6b99adae45588f0236043c6f54bdbdfca3b..1ec9762352cf5597545f5f0e093e9cf6b0037b4b 100644 GIT binary patch literal 911 zcmV;A191F7RzVIslSa(6RJCUNGLER%74m%7J#t-hA z;z|sr4^jJ$t?CSEo_k-vul+pFeYhum z)Ag;r;KN749OjbIaxm~i*G31}b!^NC!YJ@5BQ|>ea(fH;<{M%$C-Cq-Ph`Qn6Pst@ zogX)9w`$^3zgfGz^rd#xK&ZLgTti;yx-9`T8DJThG9V*7+;_F{_ zt~DK65qls?tdIu0x;svD^A&LOX~Inzbl`I7I4nTNY5{j#A2uEpGRIqo4IEFfg~iR} z)AJk9^Q+e3t)pAIFTUbO)!?Hhp4?Otp(HJJaY5p8se|Oj^eGrM8oe2l8%2Co5qUO| z57U;k3E6kY$cyfK(ba3WZUG`+)z53fV$Wv-Les>S!Q+^)%}IT!a@5pTjVt6-I6|dS zq*Fuwo28m+L0Qk522jIFyvr=!*}DPP)$+Xq;xWhfu#5J{I8cc99fJH`MqC zi~t3@3?L7nK0s7P2!ue(yE3?^YYDib6F9aC&VV~GsEr^N*K2xmBvDa+mcWU42LQcg zwAwu|1W>mtqds;6A`gfkGG;Uc@go3^W%TFtH0==sOz_GWl`&#ttjWfZY!S!{{OklY z-3X#{kisDAz#uE5#~A+&NNHDJ_$*BY=gG)K0H zumKRbSra0VX@V*i$b14aEs$vg^b};;pbCKqWR~h-C}2v$l!zCz_Aj#B6mz%>9NxRy l4ll*jSK7w^;>nb;b4;3=6^XO`+$Aa-{{;Y3;t}8+0034xt-}BS literal 911 zcmV;A191F7RzVe!V%PLoz$u;ByvgYpaP*c7ortk|*S4-mh=jFU8T{74<-mR5BZY0lj1d+l@1Jlx}s z;dth5@ZlrlHg{NmA=vRk$09xI*cRoCP!f1PXBK(2vA#w;<1I6}9k_H)B=X?hu_dzT z=8qedTNU}K-mKi7`%*cq5i-9pzf9cFaq1FiDxfMbRKWVQyXUYq7KonfQ8y8m3DFDXYojtVw_pmfBrn?atl_gsi6L_L$3U&!rC8WrH9^yYHag=b?70K&?;{ z9~c1vTO5!Jr~}AX5rGi2yr+W2h8BbO8w|&m;PAJJL9LHiUavLcBe9B&moXfRZv$x5 z^?I`nLx5T>6?Lc+Fm*uwP%-yIh#vwtQqiB2lY~WdnBJu?N?#0pv8?(+aYdl6?x%;J z@h%V@fl~bO2>h{lZfkzwr)w^{%0f|i(&%(AF{r$xdAcujJWpUT?9Z!<^(5by`Fb+{ z%_v`dw=-7r#4)Ors8J1Nt?sRBK=^A*8k6M58Q@4VRL0(ejJOZHUH zW8G4UbCqJIX<>S@dw;qXbMHZm{kiOg 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;