Skip to content

Commit

Permalink
Fix accidental removal of trailing newline (#170)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Gudahtt authored Aug 27, 2023
1 parent e934268 commit 3c2b319
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
54 changes: 36 additions & 18 deletions src/index.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Generated by [AVA](https://avajs.dev).
"a": null␊
},␊
"z": null␊
}`
}␊
`

## should sort an unsorted JSON object

Expand All @@ -38,7 +39,8 @@ Generated by [AVA](https://avajs.dev).
"a": null␊
},␊
"z": null␊
}`
}␊
`

## should validate a sorted JSON object recursively

Expand All @@ -55,7 +57,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
"z": null␊
}`
}␊
`

## should sort an unsorted JSON object recursively

Expand All @@ -72,7 +75,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
"z": null␊
}`
}␊
`

## should validate a sorted JSON object within an array recursively

Expand All @@ -94,7 +98,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
2␊
]`
]␊
`

## should sort an unsorted JSON object within an array recursively

Expand All @@ -116,7 +121,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
2␊
]`
]␊
`

## should validate a deeply nested sorted JSON object recursively

Expand All @@ -142,7 +148,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
"z": null␊
}`
}␊
`

## should sort an unsorted deeply nested JSON object recursively

Expand All @@ -168,7 +175,8 @@ Generated by [AVA](https://avajs.dev).
"z": null␊
},␊
"z": null␊
}`
}␊
`

## should validate a sorted JSON object with unconventional keys

Expand Down Expand Up @@ -204,7 +212,8 @@ Generated by [AVA](https://avajs.dev).
"{": null,␊
"{}": null,␊
"￿": null␊
}`
}␊
`

## should sort a JSON object with unconventional keys

Expand Down Expand Up @@ -240,7 +249,8 @@ Generated by [AVA](https://avajs.dev).
"{": null,␊
"{}": null,␊
"￿": null␊
}`
}␊
`

## should sort JSON objects recursively within a nested array

Expand Down Expand Up @@ -268,7 +278,8 @@ Generated by [AVA](https://avajs.dev).
}␊
],␊
"z": null␊
}`
}␊
`

## should validate JSON objects recursively within a nested array

Expand Down Expand Up @@ -296,7 +307,8 @@ Generated by [AVA](https://avajs.dev).
}␊
],␊
"z": null␊
}`
}␊
`

## should validate a sorted JSON object with a simple custom sort

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -472,4 +489,5 @@ Generated by [AVA](https://avajs.dev).
"050": null␊
},␊
"z": null␊
}`
}␊
`
Binary file modified src/index.test.ts.snap
Binary file not shown.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const parsers = {
(ast.type === 'ArrayExpression' && jsonRecursiveSort)
)
) {
return ast;
return jsonRootAst;
}

let sortCompareFunction: (a: string, b: string) => number = lexicalSort;
Expand Down Expand Up @@ -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<string, Parser>;
Expand Down

0 comments on commit 3c2b319

Please sign in to comment.