From ea3d4139c13ad425d654add975cf34b657ed9469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kimi=20L=C3=B6ffel?= <89143896+MrSpoony@users.noreply.github.com> Date: Fri, 14 Apr 2023 19:06:33 +0200 Subject: [PATCH] don't add whitespace to //NOSONAR comments When using sonarqube, `//NOSONAR` can be used to disable SONAR parsing for this line on false-positives. See: https://docs.sonarcloud.io/appendices/frequently-asked-questions/ This only works if there is no space between the slashes and `NOSONAR`. Co-authored-by: Kimi Loeffel --- format/format.go | 3 ++- testdata/script/comment-spaced.txtar | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/format/format.go b/format/format.go index 7875617..1fe4b23 100644 --- a/format/format.go +++ b/format/format.go @@ -305,10 +305,11 @@ func (f *fumpter) lineEnd(line int) token.Pos { // //sys(nb)? | syscall function wrapper prototypes // //nolint | nolint directive for golangci // //noinspection | noinspection directive for GoLand and friends +// //NOSONAR | NOSONAR directive for SonarQube // // Note that the "some-words:" matching expects a letter afterward, such as // "go:generate", to prevent matching false positives like "https://site". -var rxCommentDirective = regexp.MustCompile(`^([a-z-]+:[a-z]+|line\b|export\b|extern\b|sys(nb)?\b|no(lint|inspection)\b)`) +var rxCommentDirective = regexp.MustCompile(`^([a-z-]+:[a-z]+|line\b|export\b|extern\b|sys(nb)?\b|no(lint|inspection)\b)|NOSONAR\b`) func (f *fumpter) applyPre(c *astutil.Cursor) { f.splitLongLine(c) diff --git a/testdata/script/comment-spaced.txtar b/testdata/script/comment-spaced.txtar index 9f330fc..aac33b1 100644 --- a/testdata/script/comment-spaced.txtar +++ b/testdata/script/comment-spaced.txtar @@ -24,6 +24,10 @@ package p //nolint:somelinter // explanation +//NOSONAR + +//NOSONAR // explanation + //noinspection ALL //noinspection foo,bar @@ -87,6 +91,10 @@ package p //nolint:somelinter // explanation +//NOSONAR + +//NOSONAR // explanation + //noinspection ALL //noinspection foo,bar