Skip to content

Commit 73f30aa

Browse files
committed
test: cover remaining blank-line branches
1 parent 7032c69 commit 73f30aa

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

src/stringify.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ const process_comments = (host, symbol_tag, deeper_gap, display_block) => {
109109
})
110110

111111
if (!last_comment) {
112-
return blank_lines
113-
? repeat_line_breaks(blank_lines + 1, deeper_gap)
114-
: EMPTY
112+
return repeat_line_breaks(blank_lines + 1, deeper_gap)
115113
}
116114

117115
const default_line_breaks_after = display_block

test/remove-blank-lines.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,55 @@ test('removeBlankLines: should return early if the location does not exist', t =
8585

8686
t.deepEqual(obj, {foo: 1})
8787
})
88+
89+
test('removeBlankLines: should return early when location has no blank lines', t => {
90+
const obj = parse(`{
91+
// before foo
92+
"foo": 1
93+
}`)
94+
95+
removeBlankLines(obj, {where: 'before', key: 'foo'})
96+
97+
t.is(stringify(obj, null, 2), `{
98+
// before foo
99+
"foo": 1
100+
}`)
101+
})
102+
103+
test('removeBlankLines: should ignore non-array comment slots', t => {
104+
const obj = {foo: 1}
105+
106+
Object.defineProperty(obj, Symbol.for('before:foo'), {
107+
value: 'not-an-array',
108+
writable: true,
109+
configurable: true
110+
})
111+
112+
removeBlankLines(obj, {where: 'before', key: 'foo'})
113+
114+
t.is(obj[Symbol.for('before:foo')], 'not-an-array')
115+
})
116+
117+
test('removeBlankLines: should ignore non-comment symbols during recursive cleanup', t => {
118+
const obj = parse(`{
119+
// before foo
120+
121+
"foo": 1
122+
}`)
123+
const marker = Symbol('marker')
124+
125+
Object.defineProperty(obj, marker, {
126+
value: [{type: 'BlankLine', inline: false}],
127+
writable: true,
128+
configurable: true
129+
})
130+
131+
removeBlankLines(obj)
132+
133+
t.true(Object.hasOwn(obj, marker))
134+
t.deepEqual(obj[marker], [{type: 'BlankLine', inline: false}])
135+
t.is(stringify(obj, null, 2), `{
136+
// before foo
137+
"foo": 1
138+
}`)
139+
})

test/stringify.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,60 @@ test('render explicit BlankLine tokens mixed with inline comments', t => {
340340
}`)
341341
})
342342

343+
test('render BlankLine tokens before inline comments', t => {
344+
const comments = [
345+
{
346+
type: 'BlankLine',
347+
inline: false
348+
},
349+
{
350+
type: 'BlockComment',
351+
value: ' second ',
352+
inline: true
353+
}
354+
]
355+
356+
const obj = {
357+
a: 1,
358+
b: 2
359+
}
360+
361+
Object.defineProperty(obj, Symbol.for('after:a'), {
362+
value: comments,
363+
writable: true,
364+
configurable: true
365+
})
366+
367+
const output = stringify(obj, null, 2)
368+
369+
t.is(output, `{
370+
"a": 1,
371+
/* second */
372+
"b": 2
373+
}`)
374+
})
375+
376+
test('ignore blank-line-only before-all slots when stringifying root output', t => {
377+
const obj = {
378+
a: 1
379+
}
380+
381+
Object.defineProperty(obj, Symbol.for('before-all'), {
382+
value: [
383+
{
384+
type: 'BlankLine',
385+
inline: false
386+
}
387+
],
388+
writable: true,
389+
configurable: true
390+
})
391+
392+
t.is(stringify(obj, null, 2), `{
393+
"a": 1
394+
}`)
395+
})
396+
343397
test('escape control characters same as JSON.stringify', t => {
344398
for (let i = 0; i <= 0x1f; i ++) {
345399
const char = String.fromCharCode(i)

0 commit comments

Comments
 (0)