Skip to content
Merged
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
46 changes: 37 additions & 9 deletions src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormatOptions } from '../FormatOptions.js';
import { equalizeWhitespace, isMultiline } from '../utils.js';
import { equalizeWhitespace, isMultiline, last } from '../utils.js';

import Params from './Params.js';
import { isTabularStyle } from './config.js';
Expand Down Expand Up @@ -356,8 +356,20 @@ export default class ExpressionFormatter {
return isMultiline(node.text) || isMultiline(node.precedingWhitespace || '');
}

private isDocComment(comment: string): boolean {
const lines = comment.split(/\n/);
return (
// first line starts with /* or /**
/^\/\*\*?$/.test(lines[0]) &&
// intermediate lines start with *
lines.slice(1, lines.length - 1).every(line => /^\s*\*/.test(line)) &&
// last line ends with */
/^\s*\*\/$/.test(last(lines) as string)
);
}

// Breaks up block comment to multiple lines.
// For example this comment (dots representing leading whitespace):
// For example this doc-comment (dots representing leading whitespace):
//
// ..../**
// .....* Some description here
Expand All @@ -371,14 +383,30 @@ export default class ExpressionFormatter {
// '.* and here too',
// '.*/' ]
//
// However, a normal comment (non-doc-comment) like this:
//
// ..../*
// ....Some description here
// ....*/
//
// gets broken to this array (no leading spaces):
//
// [ '/*',
// 'Some description here',
// '*/' ]
//
private splitBlockComment(comment: string): string[] {
return comment.split(/\n/).map(line => {
if (/^\s*\*/.test(line)) {
return ' ' + line.replace(/^\s*/, '');
} else {
return line.replace(/^\s*/, '');
}
});
if (this.isDocComment(comment)) {
return comment.split(/\n/).map(line => {
if (/^\s*\*/.test(line)) {
return ' ' + line.replace(/^\s*/, '');
} else {
return line;
}
});
} else {
return comment.split(/\n/).map(line => line.replace(/^\s*/, ''));
}
}

private formatSubExpression(nodes: AstNode[]): Layout {
Expand Down
17 changes: 17 additions & 0 deletions test/features/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
`);
});

// Regression test for #558
it('indents multiline block comment that is not a doc-comment', () => {
const result = format(dedent`
SELECT 1
/*
comment line
*/
`);
expect(result).toBe(dedent`
SELECT
1
/*
comment line
*/
`);
});

it('formats comments between qualified.names (after dot)', () => {
const result = format(`
SELECT foo. /* com1 */ bar, foo. /* com2 */ *;
Expand Down