Skip to content

Commit

Permalink
Only recognize well-known comment markers
Browse files Browse the repository at this point in the history
Restricting what comment markers keep-sorted accepts leads to less
surprising behavior, such as // go/keep-sorted treating the comment
marker as "// go/" when using the default id of just "keep-sorted".
  • Loading branch information
Colecf committed Oct 13, 2023
1 parent b048cb7 commit 6349d27
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ allows for sorting data such as Go structs and JSON objects.
#### Comments

Comments embedded within the sorted block are made to stick with their
successor. The comment lines must start with the same token as the
keep-sorted instruction itself (e.g. `#` in the case below).
successor. The comment lines must start with the same comment marker as the
keep-sorted instruction itself (e.g. `#` in the case below). keep-sorted
will recognize `//`, `/*`, `#`, `--`, `;`, and `<!--` as comment markers, for
any other kinds of comments, use `sticky_prefixes`.

This special handling can be disabled by specifying the parameter
`sticky_comments=no`:
Expand Down
89 changes: 89 additions & 0 deletions keepsorted/keep_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,95 @@ baz
foo
// keep-sorted-test end`,
},
{
name: "sticky comment with prefix to id",

in: `
// some prefix (normally this is go/) keep-sorted-test start
2
1
// comment on 3
3
// keep-sorted-test end
# some prefix (normally this is go/) keep-sorted-test start
2
1
# comment on 3
3
# keep-sorted-test end
/* some prefix (normally this is go/) keep-sorted-test start */
2
1
/* comment on 3 */
3
/* keep-sorted-test end */
-- some prefix (normally this is go/) keep-sorted-test start
2
1
-- comment on 3
3
-- keep-sorted-test end
; some prefix (normally this is go/) keep-sorted-test start
2
1
; comment on 3
3
; keep-sorted-test end
<!-- some prefix (normally this is go/) keep-sorted-test start -->
2
1
<!-- comment on 3 -->
3
<!-- keep-sorted-test end -->`,

want: `
// some prefix (normally this is go/) keep-sorted-test start
1
2
// comment on 3
3
// keep-sorted-test end
# some prefix (normally this is go/) keep-sorted-test start
1
2
# comment on 3
3
# keep-sorted-test end
/* some prefix (normally this is go/) keep-sorted-test start */
1
2
/* comment on 3 */
3
/* keep-sorted-test end */
-- some prefix (normally this is go/) keep-sorted-test start
1
2
-- comment on 3
3
-- keep-sorted-test end
; some prefix (normally this is go/) keep-sorted-test start
1
2
; comment on 3
3
; keep-sorted-test end
<!-- some prefix (normally this is go/) keep-sorted-test start -->
1
2
<!-- comment on 3 -->
3
<!-- keep-sorted-test end -->`,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, gotAlreadyFixed := New("keep-sorted-test").Fix(tc.in, nil)
Expand Down
17 changes: 15 additions & 2 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,21 @@ func parseValueWithDefault(f reflect.StructField, key, val string, defaultFn fun
}

func (f *Fixer) guessCommentMarker(startLine string) string {
sp := strings.SplitN(startLine, f.ID, 2)
return strings.TrimSpace(sp[0])
startLine = strings.TrimSpace(startLine)
if strings.HasPrefix(startLine, "//") {
return "//"
} else if strings.HasPrefix(startLine, "#") {
return "#"
} else if strings.HasPrefix(startLine, "/*") {
return "/*"
} else if strings.HasPrefix(startLine, "--") {
return "--"
} else if strings.HasPrefix(startLine, ";") {
return ";"
} else if strings.HasPrefix(startLine, "<!--") {
return "<!--"
}
return ""
}

// hasStickyPrefix determines if s has one of the StickyPrefixes.
Expand Down

0 comments on commit 6349d27

Please sign in to comment.