@@ -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 ( / .* , ( ( \s * - - .+ ) | $ ) / ) ) {
55
+ while ( lines [ i ] . match ( / .* , ( \s * ( - - .* ) ? $ ) / ) ) {
56
56
i ++ ;
57
57
group . push ( lines [ i ] ) ;
58
58
}
@@ -63,16 +63,23 @@ 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 ) ;
66
+ const commaLinesWithoutComments = commaLines . map ( line => line . replace ( / - - .* / , '' ) ) ;
67
+ const maxLineLength = maxLength ( commaLinesWithoutComments ) ;
67
68
return trimTrailingCommas ( commaLines ) . map ( ( line , i ) => {
68
69
if ( i === commaLines . length - 1 ) {
69
70
return line ; // do not add comma for last item
70
71
}
72
+ // find comment match in string
71
73
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 ( ) ;
76
83
} ) ;
77
84
}
78
85
@@ -95,5 +102,5 @@ function removeLastIndent(whitespace: string, indent: string): string {
95
102
}
96
103
97
104
function trimTrailingCommas ( lines : string [ ] ) : string [ ] {
98
- return lines . map ( line => line . replace ( / , ( ( \s * - - .+ ) | $ ) / , '$1' ) ) ;
105
+ return lines . map ( line => line . replace ( / , ( \s * ( - - .* ) ? $ ) / , '$1' ) ) ;
99
106
}
0 commit comments