Skip to content

Commit 0f3c770

Browse files
authored
Merge PR #491: Fix indentation of first comment in file
2 parents 74192d1 + 3986b29 commit 0f3c770

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/formatter/ExpressionFormatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,11 @@ export default class ExpressionFormatter {
306306
private formatLineComment(node: LineCommentNode) {
307307
if (isMultiline(node.precedingWhitespace || '')) {
308308
this.layout.add(WS.NEWLINE, WS.INDENT, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
309-
} else {
309+
} else if (this.layout.getLayoutItems().length > 0) {
310310
this.layout.add(WS.NO_NEWLINE, WS.SPACE, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
311+
} else {
312+
// comment is the first item in code - no need to add preceding spaces
313+
this.layout.add(node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
311314
}
312315
}
313316

test/features/comments.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
116116
`);
117117
});
118118

119+
// Regression test for #481
120+
it('formats first line comment in a file', () => {
121+
expect(format('-- comment1\n-- comment2\n')).toBe(dedent`
122+
-- comment1
123+
-- comment2
124+
`);
125+
});
126+
it('formats first block comment in a file', () => {
127+
expect(format('/*comment1*/\n/*comment2*/\n')).toBe(dedent`
128+
/*comment1*/
129+
/*comment2*/
130+
`);
131+
});
132+
119133
it('preserves single-line comments at the end of lines', () => {
120134
expect(
121135
format(`

0 commit comments

Comments
 (0)