Skip to content

Commit 4fbc09d

Browse files
committed
fix: Pretty-printer: keep the comment after opening an object scope indented
1 parent aa6e8a5 commit 4fbc09d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/printer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@
126126
}
127127

128128
var token = skipWhitespace()
129-
if (token && token.type === 'comment') {
130-
if (foundLineBreak || needsLineBreak) {
129+
// If line break followed the previous token, leave the comment
130+
// to be handled by the next usual token processing.
131+
if (!foundLineBreak && token && token.type === 'comment') {
132+
if (needsLineBreak) {
131133
// If the previous non-whitespace token was ended by a line
132134
// break, retain it. Print the comment after the line break too.
133135
if (!addedLineBreak) {

test/print.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,30 @@ addTest('keep comment locations', function () {
115115
// // test`
116116
})
117117

118+
addTest('keep comment after opening an object scope indented', function () {
119+
// `{
120+
// // String parameter
121+
// "key": 'value',
122+
// }`
123+
var tokens = [
124+
{ type: 'symbol', raw: '{', value: '{' },
125+
{ type: 'whitespace', raw: '\n' },
126+
{ type: 'comment', raw: '// String parameter' },
127+
{ type: 'whitespace', raw: '\n' },
128+
{ type: 'literal', raw: '"key"', value: 'key' },
129+
{ type: 'symbol', raw: ':', value: ':' },
130+
{ type: 'whitespace', raw: ' ' },
131+
{ type: 'literal', raw: '\'value\'', value: 'value' },
132+
{ type: 'symbol', raw: ',', value: ',' },
133+
{ type: 'whitespace', raw: '\n' },
134+
{ type: 'symbol', raw: '}', value: '}' }
135+
]
136+
var output = print(tokens, { indent: ' ' })
137+
assert.equal(output, '{\n // String parameter\n "key": \'value\',\n \n}')
138+
// `{
139+
// // String parameter
140+
// "key": 'value',
141+
// }`
142+
})
143+
118144
if (require.main === module) { require('test').run(exports) }

0 commit comments

Comments
 (0)