Skip to content

Commit e522cc8

Browse files
committed
format: make comment directive regex more maintainable
Split over multiple lines, order entries alphabetically, avoid unnecessary capturing groups for tiny performance gain. Moves NOSONAR to inside the "correct" set of parenthesis, with no functional effect though.
1 parent 72ae882 commit e522cc8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

format/format.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,26 @@ func (f *fumpter) lineEnd(line int) token.Pos {
313313
//
314314
// //go: | standard Go directives, like go:noinline
315315
// //some-words: | similar to the syntax above, like lint:ignore or go-sumtype:decl
316-
// //line | inserted line information for cmd/compile
317316
// //export | to mark cgo funcs for exporting
318317
// //extern | C function declarations for gccgo
319-
// //sys(nb)? | syscall function wrapper prototypes
320-
// //nolint | nolint directive for golangci
318+
// //line | inserted line information for cmd/compile
321319
// //noinspection | noinspection directive for GoLand and friends
320+
// //nolint | nolint directive for golangci
322321
// //NOSONAR | NOSONAR directive for SonarQube
322+
// //sys(nb)? | syscall function wrapper prototypes
323323
//
324324
// Note that the "some-words:" matching expects a letter afterward, such as
325325
// "go:generate", to prevent matching false positives like "https://site".
326-
var rxCommentDirective = regexp.MustCompile(`^([a-z-]+:[a-z]+|line\b|export\b|extern\b|sys(nb)?\b|no(lint|inspection)\b)|NOSONAR\b`)
326+
var rxCommentDirective = regexp.MustCompile(
327+
`^(?:` +
328+
`[a-z-]+:[a-z]+` +
329+
`|export` +
330+
`|extern` +
331+
`|line` +
332+
`|no(?:inspection|lint)` +
333+
`|NOSONAR` +
334+
`|sys(?:nb)?` +
335+
`)\b`)
327336

328337
func (f *fumpter) applyPre(c *astutil.Cursor) {
329338
f.splitLongLine(c)

0 commit comments

Comments
 (0)