@@ -32,7 +32,7 @@ export default function formatCommaPositions(
32
32
* [
33
33
* 'SELECT',
34
34
* ' foo,',
35
- * ' bar,',
35
+ * ' bar, --comment ',
36
36
* ' baz',
37
37
* 'FROM'
38
38
* ]
@@ -41,7 +41,7 @@ export default function formatCommaPositions(
41
41
*
42
42
* [
43
43
* ['SELECT'],
44
- * [' foo,', ' bar,', ' baz'],
44
+ * [' foo,', ' bar, --comment ', ' baz'],
45
45
* ['FROM']
46
46
* ]
47
47
*/
@@ -52,7 +52,7 @@ function groupCommaDelimitedLines(lines: string[]): string[][] {
52
52
// when line ends with comma,
53
53
// gather together all following lines that also end with comma,
54
54
// plus one (which doesn't end with comma)
55
- while ( lines [ i ] . match ( / .* , $ / ) ) {
55
+ while ( lines [ i ] . match ( / .* , ( ( \s * - - . + ) | $ ) / ) ) {
56
56
i ++ ;
57
57
group . push ( lines [ i ] ) ;
58
58
}
@@ -68,7 +68,11 @@ function formatTabular(commaLines: string[]): string[] {
68
68
if ( i === commaLines . length - 1 ) {
69
69
return line ; // do not add comma for last item
70
70
}
71
- return line + ' ' . repeat ( maxLineLength - line . length - 1 ) + ',' ;
71
+ 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 ) } `;
72
76
} ) ;
73
77
}
74
78
@@ -91,5 +95,5 @@ function removeLastIndent(whitespace: string, indent: string): string {
91
95
}
92
96
93
97
function trimTrailingCommas ( lines : string [ ] ) : string [ ] {
94
- return lines . map ( line => line . replace ( / , $ / , '' ) ) ;
98
+ return lines . map ( line => line . replace ( / , ( ( \s * - - . + ) | $ ) / , '$1 ' ) ) ;
95
99
}
0 commit comments