Skip to content

Commit 7757b26

Browse files
committed
Merge branch 'commaPositionCommentsSupport' into master
2 parents 29bd767 + 11d82cd commit 7757b26

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/formatter/formatCommaPositions.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function formatCommaPositions(
3232
* [
3333
* 'SELECT',
3434
* ' foo,',
35-
* ' bar,',
35+
* ' bar, --comment',
3636
* ' baz',
3737
* 'FROM'
3838
* ]
@@ -41,7 +41,7 @@ export default function formatCommaPositions(
4141
*
4242
* [
4343
* ['SELECT'],
44-
* [' foo,', ' bar,', ' baz'],
44+
* [' foo,', ' bar, --comment', ' baz'],
4545
* ['FROM']
4646
* ]
4747
*/
@@ -52,7 +52,7 @@ function groupCommaDelimitedLines(lines: string[]): string[][] {
5252
// when line ends with comma,
5353
// gather together all following lines that also end with comma,
5454
// plus one (which doesn't end with comma)
55-
while (lines[i].match(/.*,$/)) {
55+
while (lines[i].match(/.*,(\s*(--.*)?$)/)) {
5656
i++;
5757
group.push(lines[i]);
5858
}
@@ -63,15 +63,22 @@ function groupCommaDelimitedLines(lines: string[]): string[][] {
6363

6464
// makes all lines the same length by appending spaces before comma
6565
function formatTabular(commaLines: string[]): string[] {
66-
const maxLineLength = maxLength(commaLines);
67-
return trimTrailingCommas(commaLines).map((line, i) => {
66+
const commaPosition = maxLength(trimTrailingComments(commaLines)) - 1;
67+
return commaLines.map((line, i) => {
6868
if (i === commaLines.length - 1) {
6969
return line; // do not add comma for last item
7070
}
71-
return line + ' '.repeat(maxLineLength - line.length - 1) + ',';
71+
return indentComma(line, commaPosition);
7272
});
7373
}
7474

75+
function indentComma(line: string, commaPosition: number) {
76+
const [, code, comment] = line.match(/^(.*?),(\s*--.*)?$/) || [];
77+
78+
const spaces = ' '.repeat(commaPosition - code.length);
79+
return `${code}${spaces},${comment ?? ''}`;
80+
}
81+
7582
function formatBefore(commaLines: string[], indent: string): string[] {
7683
return trimTrailingCommas(commaLines).map((line, i) => {
7784
if (i === 0) {
@@ -91,5 +98,9 @@ function removeLastIndent(whitespace: string, indent: string): string {
9198
}
9299

93100
function trimTrailingCommas(lines: string[]): string[] {
94-
return lines.map(line => line.replace(/,$/, ''));
101+
return lines.map(line => line.replace(/,(\s*(--.*)?$)/, '$1'));
102+
}
103+
104+
function trimTrailingComments(lines: string[]): string[] {
105+
return lines.map(line => line.replace(/--.*/, ''));
95106
}

test/options/commaPosition.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ export default function supportsCommaPosition(format: FormatFn) {
4848
);
4949
});
5050

51+
it('handles comments after commas', () => {
52+
const result = format(
53+
`SELECT alpha, --comment1
54+
MAX(beta), --comment2
55+
delta AS d, epsilon --comment3`,
56+
{ commaPosition: 'before' }
57+
);
58+
expect(result).toBe(
59+
dedent(`
60+
SELECT
61+
alpha --comment1
62+
, MAX(beta) --comment2
63+
, delta AS d
64+
, epsilon --comment3
65+
`)
66+
);
67+
});
68+
5169
it('works with larger indent', () => {
5270
const result = format(
5371
'SELECT alpha, MAX(beta), delta AS d, epsilon FROM gamma GROUP BY alpha, delta, epsilon',
@@ -106,6 +124,25 @@ export default function supportsCommaPosition(format: FormatFn) {
106124
);
107125
});
108126

127+
it('handles comments after commas', () => {
128+
const result = format(
129+
`SELECT alpha, --comment1
130+
beta,--comment2
131+
delta, epsilon, iota --comment3`,
132+
{ commaPosition: 'tabular' }
133+
);
134+
expect(result).toBe(
135+
dedent(`
136+
SELECT
137+
alpha , --comment1
138+
beta , --comment2
139+
delta ,
140+
epsilon,
141+
iota --comment3
142+
`)
143+
);
144+
});
145+
109146
it('is not effected by indent size', () => {
110147
const result = format(
111148
'SELECT alpha, MAX(beta), delta AS d, epsilon FROM gamma GROUP BY alpha, delta, epsilon',

0 commit comments

Comments
 (0)