From 7cf08f72dab89f30aa8f99725516d26232abe053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= <mvdan@mvdan.cc> Date: Fri, 12 Apr 2019 07:12:01 +0900 Subject: [PATCH] don't break //export and //foo:bar directives Fixes #7. --- internal/gofumpt.go | 26 ++++++++++++++++---------- testdata/scripts/comment-spaced.txt | 12 ++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/gofumpt.go b/internal/gofumpt.go index a3313e8..3c9379a 100644 --- a/internal/gofumpt.go +++ b/internal/gofumpt.go @@ -11,6 +11,7 @@ import ( "go/parser" "go/token" "reflect" + "regexp" "sort" "strings" "unicode" @@ -155,6 +156,14 @@ func (f *fumpter) printLength(node ast.Node) int { return int(count) } +// rxCommentDirective covers all common Go comment directives: +// +// //go: | standard Go directives, like go:noinline +// //someword: | similar to the syntax above, like lint:ignore +// //line | inserted line information for cmd/compile +// //export | to mark cgo funcs for exporting +var rxCommentDirective = regexp.MustCompile(`^([a-z]+:|line\b|export\b)`) + func (f *fumpter) visit(node ast.Node) { switch node := node.(type) { case *ast.File: @@ -192,16 +201,13 @@ func (f *fumpter) visit(node ast.Node) { // /*-style comment break } - switch { - case strings.HasPrefix(body, "go:"): - // standard go:foobar directive - case strings.HasPrefix(body, "line "): - // cmd/compile line directive - default: - r, _ := utf8.DecodeRuneInString(body) - if unicode.IsLetter(r) || unicode.IsNumber(r) { - node.Text = "// " + body - } + if rxCommentDirective.MatchString(body) { + // this comment is a directive + break + } + r, _ := utf8.DecodeRuneInString(body) + if unicode.IsLetter(r) || unicode.IsNumber(r) { + node.Text = "// " + body } case *ast.GenDecl: diff --git a/testdata/scripts/comment-spaced.txt b/testdata/scripts/comment-spaced.txt index 0c7dcef..6f506cd 100644 --- a/testdata/scripts/comment-spaced.txt +++ b/testdata/scripts/comment-spaced.txt @@ -10,6 +10,12 @@ package p //go:unknowndirective +//lint:disablefoo + +//not actually: a directive + +//export CgoFunc + //line 123 //foo is foo. @@ -36,6 +42,12 @@ package p //go:unknowndirective +//lint:disablefoo + +// not actually: a directive + +//export CgoFunc + //line 123 // foo is foo.