Skip to content

Commit e8d402f

Browse files
committed
[patch] fix sorting with empty comment arrays in JSON
GitHub: fixes #75
1 parent 0228ad9 commit e8d402f

4 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎cspell.config.cjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
'farmerpaul',
1212
'meriyah',
1313
'robinfr',
14+
'softline',
1415
],
1516
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {assert} from '@augment-vir/assert';
2+
import {describe, it} from '@augment-vir/test';
3+
import {type Doc, doc} from 'prettier';
4+
import {insertLinesIntoArray} from './insert-new-lines.js';
5+
6+
describe(insertLinesIntoArray.name, () => {
7+
it('handles comment-only array doc emitted by Prettier GitHub Issue #75', () => {
8+
const commentOnlyArrayDoc: Doc = [
9+
'[',
10+
[
11+
doc.builders.indent([
12+
doc.builders.softline,
13+
[
14+
'// "source.fixAll.eslint",',
15+
[
16+
doc.builders.hardline,
17+
doc.builders.breakParent,
18+
],
19+
'// "source.fixAll.prettier",',
20+
],
21+
]),
22+
[
23+
doc.builders.hardline,
24+
doc.builders.breakParent,
25+
],
26+
],
27+
']',
28+
];
29+
30+
assert.strictEquals(
31+
insertLinesIntoArray({
32+
inputDoc: commentOnlyArrayDoc,
33+
manualWrap: false,
34+
lineCounts: [],
35+
wrapThreshold: Infinity,
36+
debug: false,
37+
}),
38+
commentOnlyArrayDoc,
39+
);
40+
});
41+
});

‎src/printer/insert-new-lines.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ const nestingSyntaxClose = ']})`';
1717

1818
const found = 'Found "[" but:';
1919

20-
function insertLinesIntoArray({
20+
function extractIndentDoc({
21+
maybeIndentDoc,
22+
}: Readonly<{
23+
maybeIndentDoc: Doc | undefined;
24+
}>): ReturnType<typeof doc.builders.indent> | undefined {
25+
if (isDocCommand(maybeIndentDoc) && maybeIndentDoc.type === 'indent') {
26+
return maybeIndentDoc;
27+
} else if (Array.isArray(maybeIndentDoc)) {
28+
const firstDoc = maybeIndentDoc[0];
29+
30+
if (isDocCommand(firstDoc) && firstDoc.type === 'indent') {
31+
return firstDoc;
32+
}
33+
}
34+
35+
return undefined;
36+
}
37+
38+
export function insertLinesIntoArray({
2139
inputDoc,
2240
manualWrap,
2341
lineCounts,
@@ -95,20 +113,25 @@ function insertLinesIntoArray({
95113
parentDoc[childIndex + 2] = maybeBreak.breakContents;
96114
}
97115
const indentIndex = childIndex + 1;
98-
const bracketSibling =
116+
const maybeBracketSibling =
99117
parentDoc[indentIndex] === ''
100118
? parentDoc[indentIndex + 1]
101119
: parentDoc[indentIndex];
120+
const bracketSibling = extractIndentDoc({
121+
maybeIndentDoc: maybeBracketSibling,
122+
});
102123
if (debug) {
103124
console.info({
104125
bracketSibling,
105126
});
106127
}
107-
if (bracketSibling === ']') {
128+
if (maybeBracketSibling === ']') {
108129
return false;
109-
} else if (!isDocCommand(bracketSibling) || bracketSibling.type !== 'indent') {
130+
} else if (!bracketSibling) {
110131
throw new Error(
111-
`${found} its sibling was not an indent Doc.: ${stringify(bracketSibling)}`,
132+
`${found} its sibling was not an indent Doc.: ${stringify(
133+
maybeBracketSibling,
134+
)}`,
112135
);
113136
}
114137
const indentContents = bracketSibling.contents;

‎src/test/json.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ const jsonTests: MultilineArrayTest[] = [
345345
*/
346346
`,
347347
},
348+
{
349+
it: 'handles array with only comments GitHub Issue #75',
350+
code: `
351+
{
352+
"editor.codeActionsOnSave": [
353+
// "hello",
354+
// "source.fixAll.eslint",
355+
// "source.fixAll.prettier",
356+
]
357+
}
358+
`,
359+
},
348360
{
349361
it: 'basic JSON array',
350362
code: `

0 commit comments

Comments
 (0)