Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ const count_trailing_line_breaks = str => {
return count
}

const count_leading_line_breaks = str => {
let i = 0
let count = 0

while (i < str.length) {
while (i < str.length && is_inline_whitespace(str[i])) {
i ++
}

if (i === str.length || str[i] !== LF) {
return count
}

i ++
count ++
}

return count
}

// display_block `boolean` whether the
// WHOLE block of comments is always a block group
const process_comments = (host, symbol_tag, deeper_gap, display_block) => {
Expand Down Expand Up @@ -138,8 +158,20 @@ const join = (one, two, gap) =>
// Symbol.for('before') and Symbol.for('before:prop')
// might both exist if user mannually add comments to the object
// and make a mistake.
// SO, we are not to only trimRight but trim for both sides
? one + two.trim() + LF + gap
// SO, we are not to only trimRight but trim for both sides.
// The trailing `LF + gap` plus the trailing line breaks of `one`
// already supply some line breaks, so only emit the blank lines that
// `two` carries beyond those, otherwise `two.trim()` would drop them.
? one
+ repeat_line_breaks(
Math.max(
count_leading_line_breaks(two)
- count_trailing_line_breaks(one) - 1,
0
),
gap
)
+ two.trim() + LF + gap
: one.trimRight() + repeat_line_breaks(
Math.max(1, count_trailing_line_breaks(one)),
gap
Expand Down
30 changes: 30 additions & 0 deletions test/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ test('preserve blank lines after before comments', t => {
t.is(output, content)
})

test('preserve blank line after inline block comment following a comma', t => {
const content = `{
"a": 1, /* x */

"b": 2
}`

t.is(stringify(parse(content), null, 2), content)
})

test('preserve blank line after inline line comment following a comma', t => {
const content = `{
"a": 1, // x

"b": 2
}`

t.is(stringify(parse(content), null, 2), content)
})

test('preserve blank line after inline block comment in arrays', t => {
const content = `[
1, /* x */

2
]`

t.is(stringify(parse(content), null, 2), content)
})

test('render explicit BlankLine tokens between comments', t => {
const comments = [
{
Expand Down