Skip to content

Commit 1e21065

Browse files
authored
Merge PR #462: Better formatting of inline block-comments
2 parents 77821a2 + 392f537 commit 1e21065

File tree

9 files changed

+51
-60
lines changed

9 files changed

+51
-60
lines changed

src/formatter/ExpressionFormatter.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormatOptions } from 'src/FormatOptions';
2-
import { equalizeWhitespace } from 'src/utils';
2+
import { equalizeWhitespace, isMultiline } from 'src/utils';
33

44
import Params from 'src/formatter/Params';
55
import { isTabularStyle } from 'src/formatter/config';
@@ -309,18 +309,26 @@ export default class ExpressionFormatter {
309309
}
310310

311311
private formatLineComment(node: LineCommentNode) {
312-
if (/\n/.test(node.precedingWhitespace || '')) {
312+
if (isMultiline(node.precedingWhitespace || '')) {
313313
this.layout.add(WS.NEWLINE, WS.INDENT, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
314314
} else {
315315
this.layout.add(WS.NO_NEWLINE, WS.SPACE, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
316316
}
317317
}
318318

319319
private formatBlockComment(node: BlockCommentNode) {
320-
this.splitBlockComment(node.text).forEach(line => {
321-
this.layout.add(WS.NEWLINE, WS.INDENT, line);
322-
});
323-
this.layout.add(WS.NEWLINE, WS.INDENT);
320+
if (this.isMultilineBlockComment(node)) {
321+
this.splitBlockComment(node.text).forEach(line => {
322+
this.layout.add(WS.NEWLINE, WS.INDENT, line);
323+
});
324+
this.layout.add(WS.NEWLINE, WS.INDENT);
325+
} else {
326+
this.layout.add(node.text, WS.SPACE);
327+
}
328+
}
329+
330+
private isMultilineBlockComment(node: BlockCommentNode): boolean {
331+
return isMultiline(node.text) || isMultiline(node.precedingWhitespace || '');
324332
}
325333

326334
// Breaks up block comment to multiple lines.

src/parser/ast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export interface LineCommentNode extends BaseNode {
159159
export interface BlockCommentNode extends BaseNode {
160160
type: NodeType.block_comment;
161161
text: string;
162+
precedingWhitespace: string;
162163
}
163164

164165
export type CommentNode = LineCommentNode | BlockCommentNode;

src/parser/grammar.ne

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,9 @@ comment -> %LINE_COMMENT {%
308308
})
309309
%}
310310
comment -> %BLOCK_COMMENT {%
311-
([token]) => ({ type: NodeType.block_comment, text: token.text })
311+
([token]) => ({
312+
type: NodeType.block_comment,
313+
text: token.text,
314+
precedingWhitespace: token.precedingWhitespace,
315+
})
312316
%}

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const sum = (arr: number[]): number => {
2828
export const flatKeywordList = (obj: Record<string, string[]>): string[] =>
2929
dedupe(Object.values(obj).flat());
3030

31+
// True when string contains multiple lines
32+
export const isMultiline = (text: string): boolean => /\n/.test(text);
33+
3134
// Given a type and a field name, returns a type where this field is optional
3235
//
3336
// For example, these two type definitions are equivalent:

test/features/arrayAndMapAccessors.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ export default function supportsArrayAndMapAccessors(format: FormatFn) {
3535
const result = format(`SELECT arr /* comment */ [1];`);
3636
expect(result).toBe(dedent`
3737
SELECT
38-
arr
39-
/* comment */
40-
[1];
38+
arr/* comment */ [1];
4139
`);
4240
});
4341

4442
it('formats namespaced array accessor with comment in-between', () => {
4543
const result = format(`SELECT foo./* comment */arr[1];`);
4644
expect(result).toBe(dedent`
4745
SELECT
48-
foo.
49-
/* comment */
50-
arr[1];
46+
foo./* comment */ arr[1];
5147
`);
5248
});
5349
}

test/features/between.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ export default function supportsBetween(format: FormatFn) {
1313
it('formats BETWEEN with comments inside', () => {
1414
expect(format('WHERE foo BETWEEN /*C1*/ t.bar /*C2*/ AND /*C3*/ t.baz')).toBe(dedent`
1515
WHERE
16-
foo BETWEEN
17-
/*C1*/
18-
t.bar
19-
/*C2*/
20-
AND
21-
/*C3*/
22-
t.baz
16+
foo BETWEEN /*C1*/ t.bar /*C2*/ AND /*C3*/ t.baz
2317
`);
2418
});
2519
}

test/features/case.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,10 @@ export default function supportsCase(format: FormatFn) {
116116

117117
expect(result).toBe(dedent`
118118
SELECT
119-
CASE
120-
/*c1*/
121-
foo
122-
/*c2*/
123-
WHEN
124-
/*c3*/
125-
1
126-
/*c4*/
127-
THEN
128-
/*c5*/
129-
2
130-
/*c6*/
131-
ELSE
132-
/*c7*/
133-
3
134-
/*c8*/
135-
END;
119+
CASE /*c1*/ foo
120+
/*c2*/ WHEN /*c3*/ 1 /*c4*/ THEN /*c5*/ 2
121+
/*c6*/ ELSE /*c7*/ 3
122+
/*c8*/ END;
136123
`);
137124
});
138125

test/features/comments.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
4848
expect(format(sql)).toBe(sql);
4949
});
5050

51+
it('keeps block comment on separate line when it is separate in original SQL', () => {
52+
const sql = dedent`
53+
SELECT
54+
/* separate-line block comment */
55+
foo,
56+
bar /* inline block comment */
57+
FROM
58+
tbl;
59+
`;
60+
expect(format(sql)).toBe(sql);
61+
});
62+
5163
it('formats tricky line comments', () => {
5264
expect(format('SELECT a--comment, here\nFROM b--comment')).toBe(dedent`
5365
SELECT
@@ -167,9 +179,7 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
167179
`);
168180
expect(result).toBe(dedent`
169181
SELECT
170-
count
171-
/* comment */
172-
(*);
182+
count/* comment */ (*);
173183
`);
174184
});
175185

@@ -179,18 +189,10 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
179189
`);
180190
expect(result).toBe(dedent`
181191
SELECT
182-
foo
183-
/* com1 */
184-
.bar,
185-
count()
186-
/* com2 */
187-
.bar,
188-
foo.bar
189-
/* com3 */
190-
.baz,
191-
(1, 2)
192-
/* com4 */
193-
.foo;
192+
foo /* com1 */.bar,
193+
count() /* com2 */.bar,
194+
foo.bar /* com3 */.baz,
195+
(1, 2) /* com4 */.foo;
194196
`);
195197
});
196198

@@ -200,12 +202,8 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
200202
`);
201203
expect(result).toBe(dedent`
202204
SELECT
203-
foo.
204-
/* com1 */
205-
bar,
206-
foo.
207-
/* com2 */
208-
*;
205+
foo./* com1 */ bar,
206+
foo./* com2 */ *;
209207
`);
210208
});
211209

@@ -226,8 +224,7 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
226224
const result = format('SELECT alpha /* /* commment */ */ FROM beta');
227225
expect(result).toBe(dedent`
228226
SELECT
229-
alpha
230-
/* /* commment */ */
227+
alpha /* /* commment */ */
231228
FROM
232229
beta
233230
`);

test/unit/__snapshots__/Parser.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Array [
362362
"text": "my_array",
363363
"trailingComments": Array [
364364
Object {
365+
"precedingWhitespace": " ",
365366
"text": "/*haha*/",
366367
"type": "block_comment",
367368
},

0 commit comments

Comments
 (0)