-
Notifications
You must be signed in to change notification settings - Fork 32
Better handle trailing commas with comments #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4c89382
35b3fd6
0a96ebb
a85ec57
9cb6680
9c19ba4
ab7d555
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,25 +7,37 @@ Trailing comma except for last | |
|
|
||
|
|
||
| Trailing comma and a comment | ||
| TODO: https://github.com/google/keep-sorted/issues/33 - Fix this | ||
| keep-sorted-test start | ||
| 1, | ||
| 2 | ||
| 3, # three | ||
| 2, | ||
| 3 # three | ||
| keep-sorted-test end | ||
|
|
||
| Trailing comma except for last, last has a comment | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the inconsistent spacing here. I played with an approach that made spacing consistently one space after the comma, but it was a bit more complex. I have this behavior in the PR since the implementation is simpler.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, yeah, that's weird. I'd somewhat prefer if we kept the spacing the same as what the user wrote originally. Oh I see, the Maybe we should replace a single space with a comma, but if the comment only has a single leading space, preserve that space? |
||
| TODO: https://github.com/google/keep-sorted/issues/33 - Fix this | ||
| keep-sorted-test start | ||
| 1, | ||
| 2 # two, | ||
| 1, <!-- one --> | ||
| 2, <!-- two --> | ||
| 3 | ||
| keep-sorted-test end | ||
|
|
||
| Trailing comma and a comment, last has a comment | ||
| TODO: https://github.com/google/keep-sorted/issues/33 - Fix this | ||
| keep-sorted-test start | ||
| 1, | ||
| 2 # two | ||
| 3, # three | ||
| 2, -- two | ||
| 3 -- three | ||
| keep-sorted-test end | ||
|
|
||
| Trailing commas with inline comment | ||
| keep-sorted-test start | ||
| "a", /* a */ | ||
| "b", | ||
| "c" /* c */ | ||
| keep-sorted-test end | ||
|
|
||
| Trailing commas with quoted comment characters | ||
| TODO: https://github.com/google/keep-sorted/issues/33 - Fix this | ||
| keep-sorted-test start | ||
| "a, #" | ||
| "b, //", | ||
| "c", | ||
| keep-sorted-test end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ package keepsorted | |
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "slices" | ||
| "strings" | ||
|
|
||
|
|
@@ -389,13 +390,23 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup) | |
| } | ||
| } | ||
|
|
||
| if n := len(dataGroups); n > 1 && allHaveSuffix(dataGroups[0:n-1], ",") && !dataGroups[n-1].hasSuffix(",") { | ||
| dataGroups[n-1].append(",") | ||
| knownCommentMarkersList := knownCommentMarkers() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What do you think about using |
||
| for i, marker := range knownCommentMarkersList { | ||
| // Some comment markers, such as "/*", include regex metacharacters. | ||
| knownCommentMarkersList[i] = regexp.QuoteMeta(marker) | ||
| } | ||
| knownCommentMarkersStr := strings.Join(knownCommentMarkersList, "|") | ||
|
|
||
| commaLineEnd := regexp.MustCompile(fmt.Sprintf(",(\\s*(%s).*)?$", knownCommentMarkersStr)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use grave ticks so you don't need to escape the backslash |
||
| lineEnd := regexp.MustCompile(fmt.Sprintf("(\\s*(%s).*)?$", knownCommentMarkersStr)) | ||
|
|
||
| if n := len(dataGroups); n > 1 && allMatchSuffix(dataGroups[0:n-1], commaLineEnd) && !dataGroups[n-1].matchesSuffix(commaLineEnd) { | ||
| dataGroups[n-1].replaceSuffix(lineEnd, ",$1") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you happen to know how nested capturing groups behave in go? Is Either way, we only seem to be using |
||
|
|
||
| return func(lgs []*lineGroup) { | ||
| for i := len(lgs) - 1; i >= 0; i-- { | ||
| if len(lgs[i].lines) > 0 { | ||
| lgs[i].trimSuffix(",") | ||
| lgs[i].replaceSuffix(commaLineEnd, "$1") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this why the spacing seems weird? Should we replace this with |
||
| return | ||
| } | ||
| } | ||
|
|
@@ -405,9 +416,9 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup) | |
| return func([]*lineGroup) {} | ||
| } | ||
|
|
||
| func allHaveSuffix(lgs []*lineGroup, s string) bool { | ||
| func allMatchSuffix(lgs []*lineGroup, suffix *regexp.Regexp) bool { | ||
| for _, lg := range lgs { | ||
| if !lg.hasSuffix(s) { | ||
| if !lg.matchesSuffix(suffix) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block=yeshas some logic that looks at quotes and tries to understand whether we're in a quoted section or not:keep-sorted/keepsorted/line_group.go
Line 354 in e2a9a7f
I wonder if we could leverage that here to exclude comment leaders that are actually part of a string constant
Per my other comment, that code should probably use
StickyPrefixesinstead of justcommentMarker, but that's again me just thinking out loud