Skip to content

Commit 3c7f822

Browse files
authored
Improve formatting of block comments that aren't doc-comments (#559)
2 parents 6362e23 + d70c5dc commit 3c7f822

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/formatter/ExpressionFormatter.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormatOptions } from '../FormatOptions.js';
2-
import { equalizeWhitespace, isMultiline } from '../utils.js';
2+
import { equalizeWhitespace, isMultiline, last } from '../utils.js';
33

44
import Params from './Params.js';
55
import { isTabularStyle } from './config.js';
@@ -356,8 +356,20 @@ export default class ExpressionFormatter {
356356
return isMultiline(node.text) || isMultiline(node.precedingWhitespace || '');
357357
}
358358

359+
private isDocComment(comment: string): boolean {
360+
const lines = comment.split(/\n/);
361+
return (
362+
// first line starts with /* or /**
363+
/^\/\*\*?$/.test(lines[0]) &&
364+
// intermediate lines start with *
365+
lines.slice(1, lines.length - 1).every(line => /^\s*\*/.test(line)) &&
366+
// last line ends with */
367+
/^\s*\*\/$/.test(last(lines) as string)
368+
);
369+
}
370+
359371
// Breaks up block comment to multiple lines.
360-
// For example this comment (dots representing leading whitespace):
372+
// For example this doc-comment (dots representing leading whitespace):
361373
//
362374
// ..../**
363375
// .....* Some description here
@@ -371,14 +383,30 @@ export default class ExpressionFormatter {
371383
// '.* and here too',
372384
// '.*/' ]
373385
//
386+
// However, a normal comment (non-doc-comment) like this:
387+
//
388+
// ..../*
389+
// ....Some description here
390+
// ....*/
391+
//
392+
// gets broken to this array (no leading spaces):
393+
//
394+
// [ '/*',
395+
// 'Some description here',
396+
// '*/' ]
397+
//
374398
private splitBlockComment(comment: string): string[] {
375-
return comment.split(/\n/).map(line => {
376-
if (/^\s*\*/.test(line)) {
377-
return ' ' + line.replace(/^\s*/, '');
378-
} else {
379-
return line.replace(/^\s*/, '');
380-
}
381-
});
399+
if (this.isDocComment(comment)) {
400+
return comment.split(/\n/).map(line => {
401+
if (/^\s*\*/.test(line)) {
402+
return ' ' + line.replace(/^\s*/, '');
403+
} else {
404+
return line;
405+
}
406+
});
407+
} else {
408+
return comment.split(/\n/).map(line => line.replace(/^\s*/, ''));
409+
}
382410
}
383411

384412
private formatSubExpression(nodes: AstNode[]): Layout {

test/features/comments.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
211211
`);
212212
});
213213

214+
// Regression test for #558
215+
it('indents multiline block comment that is not a doc-comment', () => {
216+
const result = format(dedent`
217+
SELECT 1
218+
/*
219+
comment line
220+
*/
221+
`);
222+
expect(result).toBe(dedent`
223+
SELECT
224+
1
225+
/*
226+
comment line
227+
*/
228+
`);
229+
});
230+
214231
it('formats comments between qualified.names (after dot)', () => {
215232
const result = format(`
216233
SELECT foo. /* com1 */ bar, foo. /* com2 */ *;

0 commit comments

Comments
 (0)