@@ -565,22 +565,28 @@ def fix_comma(cols: List[str]) -> List[str]:
565
565
if "," not in col [: col .index ("--" )]:
566
566
# use re.sub to replace (any whitespace)-- with , --
567
567
col = re .sub (r"\s*--" , ", --" , col )
568
- # check if string ends with comma
569
- elif "," not in col :
568
+ # check if string ends with comma (optionally with additional spaces)
569
+ elif not re . search ( r",\s*$" , col ) :
570
570
# replace all trailing spaces with ,
571
571
col = re .sub (r"\s+$" , "," , col )
572
572
fixed_cols .append (col )
573
573
# for the last col, we want to remove the comma
574
574
last_col = fixed_cols [- 1 ]
575
575
if "--" in last_col :
576
- # check if comma is before comment and remove if present
577
- last_col_split = last_col .split ("--" , 1 )
578
- if "," in last_col_split [0 ]:
579
- last_col = (
580
- "" .join (last_col_split [0 ].split ("," , 1 )) + "--" + last_col_split [1 ]
581
- )
582
- elif "," in last_col :
583
- last_col = "" .join (last_col .split ("," , 1 ))
576
+ # check if comma is after a word/closing brace, followed by spaces before -- and remove if present
577
+
578
+ pre_comment , after_comment = last_col .split ("--" , 1 )
579
+ # check if pre_comment ends with a comma with optional spaces
580
+ if re .search (r",\s*$" , pre_comment ):
581
+ pre_comment = re .sub (r",\s*$" , "" , pre_comment )
582
+ # remove any trailing spaces in pre_comment
583
+ pre_comment = pre_comment .rstrip ()
584
+ last_col = pre_comment + " --" + after_comment
585
+ # if last_col ends with a comma with optional spaces, remove it
586
+ elif re .search (r",\s*$" , last_col ):
587
+ last_col = re .sub (r",\s*$" , "" , last_col )
588
+ # remove any trailing spaces in last_col
589
+ last_col = last_col .rstrip ()
584
590
fixed_cols [- 1 ] = last_col
585
591
return fixed_cols
586
592
0 commit comments