@@ -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
}
@@ -63,15 +63,22 @@ function groupCommaDelimitedLines(lines: string[]): string[][] {
63
63
64
64
// makes all lines the same length by appending spaces before comma
65
65
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 ) => {
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
+ return indentComma ( line , commaPosition ) ;
72
72
} ) ;
73
73
}
74
74
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
+
75
82
function formatBefore ( commaLines : string [ ] , indent : string ) : string [ ] {
76
83
return trimTrailingCommas ( commaLines ) . map ( ( line , i ) => {
77
84
if ( i === 0 ) {
@@ -91,5 +98,9 @@ function removeLastIndent(whitespace: string, indent: string): string {
91
98
}
92
99
93
100
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 ( / - - .* / , '' ) ) ;
95
106
}
0 commit comments