Skip to content

Commit d24778d

Browse files
author
Stas Germanovskiy
committed
BUG-505 review fixes
1 parent a6d15ea commit d24778d

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/formatter/formatCommaPositions.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/.*,((\s*--.+)|$)/)) {
55+
while (lines[i].match(/.*,(\s*(--.*)?$)/)) {
5656
i++;
5757
group.push(lines[i]);
5858
}
@@ -63,16 +63,23 @@ 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);
66+
const commaLinesWithoutComments = commaLines.map(line => line.replace(/--.*/, ''));
67+
const maxLineLength = maxLength(commaLinesWithoutComments);
6768
return trimTrailingCommas(commaLines).map((line, i) => {
6869
if (i === commaLines.length - 1) {
6970
return line; // do not add comma for last item
7071
}
72+
// find comment match in string
7173
const commentMatch = /\s*--/.exec(line);
72-
const endOfLinePosition = commentMatch ? commentMatch.index : line.length;
73-
return `${line.slice(0, endOfLinePosition)}${' '.repeat(
74-
maxLineLength - line.length - 1
75-
)},${line.slice(endOfLinePosition + 1)}`;
74+
// if comment found, get its start index to slice string by it
75+
// if comment not found, get line.length so slice will return whole line
76+
const endOfContentWithoutCommentPosition = commentMatch ? commentMatch.index : line.length;
77+
const lineContentWithoutComment = line.slice(0, endOfContentWithoutCommentPosition).trimEnd();
78+
const spaces = ' '.repeat(maxLineLength - lineContentWithoutComment.length - 1);
79+
const comment = line.slice(endOfContentWithoutCommentPosition + 1);
80+
81+
// trim end will handle case without comment
82+
return `${lineContentWithoutComment}${spaces}, ${comment}`.trimEnd();
7683
});
7784
}
7885

@@ -95,5 +102,5 @@ function removeLastIndent(whitespace: string, indent: string): string {
95102
}
96103

97104
function trimTrailingCommas(lines: string[]): string[] {
98-
return lines.map(line => line.replace(/,((\s*--.+)|$)/, '$1'));
105+
return lines.map(line => line.replace(/,(\s*(--.*)?$)/, '$1'));
99106
}

test/options/commaPosition.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ export default function supportsCommaPosition(format: FormatFn) {
7373
);
7474
});
7575

76+
it('should work with comma position tabular and comment', () => {
77+
const result = format(
78+
`
79+
SELECT
80+
alpha ,
81+
beta,--comment
82+
delta ,
83+
epsilon ,
84+
iota
85+
`,
86+
{ commaPosition: 'tabular' }
87+
);
88+
expect(result).toBe(
89+
dedent(`
90+
SELECT
91+
alpha ,
92+
beta , --comment
93+
delta ,
94+
epsilon,
95+
iota
96+
`)
97+
);
98+
});
99+
76100
it('works with larger indent', () => {
77101
const result = format(
78102
'SELECT alpha, MAX(beta), delta AS d, epsilon FROM gamma GROUP BY alpha, delta, epsilon',

0 commit comments

Comments
 (0)