From 1f6f82b8d0da7aaab5fa726b5f64df58477dc641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Fri, 26 Feb 2021 18:39:17 +0100 Subject: [PATCH 1/2] Ignore blank import of embed --- rule/blank-imports.go | 26 ++++++++++++++++++++-- testdata/golint/blank-import-lib.go | 5 +++++ testdata/golint/blank-import-with-embed.go | 14 ++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 testdata/golint/blank-import-with-embed.go diff --git a/rule/blank-imports.go b/rule/blank-imports.go index 0a00e3707..b76f66bb8 100644 --- a/rule/blank-imports.go +++ b/rule/blank-imports.go @@ -2,6 +2,7 @@ package rule import ( "go/ast" + "strings" "github.com/mgechev/revive/lint" ) @@ -44,6 +45,8 @@ func (w lintBlankImports) Visit(_ ast.Node) ast.Visitor { return nil } + const embedImportPath = `"embed"` + // The first element of each contiguous group of blank imports should have // an explanatory comment of some kind. for i, imp := range w.fileAst.Imports { @@ -52,14 +55,21 @@ func (w lintBlankImports) Visit(_ ast.Node) ast.Visitor { if !isBlank(imp.Name) { continue // Ignore non-blank imports. } + if i > 0 { prev := w.fileAst.Imports[i-1] prevPos := w.file.ToPosition(prev.Pos()) - if isBlank(prev.Name) && prevPos.Line+1 == pos.Line { - continue // A subsequent blank in a group. + + isSubsequentBlancInAGroup := isBlank(prev.Name) && prevPos.Line+1 == pos.Line && prev.Path.Value != embedImportPath + if isSubsequentBlancInAGroup { + continue } } + if imp.Path.Value == embedImportPath && w.fileHasValidEmbedComment() { + continue + } + // This is the first blank import of a group. if imp.Doc == nil && imp.Comment == nil { w.onFailure(lint.Failure{ @@ -72,3 +82,15 @@ func (w lintBlankImports) Visit(_ ast.Node) ast.Visitor { } return nil } + +func (w lintBlankImports) fileHasValidEmbedComment() bool { + for _, commentGroup := range w.fileAst.Comments { + for _, comment := range commentGroup.List { + if strings.HasPrefix(comment.Text, "//go:embed ") { + return true + } + } + } + + return false +} diff --git a/testdata/golint/blank-import-lib.go b/testdata/golint/blank-import-lib.go index 7c5d36097..eb8c1ed2a 100644 --- a/testdata/golint/blank-import-lib.go +++ b/testdata/golint/blank-import-lib.go @@ -38,6 +38,11 @@ import ( _ "go/token" ) +import ( + _ "embed" + /* MATCH:42 /a blank import should be only in a main or test package, or have a comment justifying it/ */ +) + var ( _ fmt.Stringer // for "fmt" _ ast.Node // for "go/ast" diff --git a/testdata/golint/blank-import-with-embed.go b/testdata/golint/blank-import-with-embed.go new file mode 100644 index 000000000..3a52ab4ff --- /dev/null +++ b/testdata/golint/blank-import-with-embed.go @@ -0,0 +1,14 @@ +// Test that blank import of "embed" is allowed + +// Package foo ... +package foo + +import ( + _ "embed" + _ "fmt" + /* MATCH:8 /a blank import should be only in a main or test package, or have a comment justifying it/ */ +) + +//go:embed .gitignore +var _ string + From bbb4fffc08024bbac2f0dcbea40857e69384b7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Tue, 2 Mar 2021 10:47:04 +0100 Subject: [PATCH 2/2] Don't use ast.Walk in blank-imports --- rule/blank-imports.go | 63 +++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/rule/blank-imports.go b/rule/blank-imports.go index b76f66bb8..5e93be3ae 100644 --- a/rule/blank-imports.go +++ b/rule/blank-imports.go @@ -10,55 +10,38 @@ import ( // BlankImportsRule lints given else constructs. type BlankImportsRule struct{} -// Apply applies the rule to given file. -func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { - var failures []lint.Failure - - fileAst := file.AST - walker := lintBlankImports{ - file: file, - fileAst: fileAst, - onFailure: func(failure lint.Failure) { - failures = append(failures, failure) - }, - } - - ast.Walk(walker, fileAst) - - return failures -} - // Name returns the rule name. func (r *BlankImportsRule) Name() string { return "blank-imports" } -type lintBlankImports struct { - fileAst *ast.File - file *lint.File - onFailure func(lint.Failure) -} - -func (w lintBlankImports) Visit(_ ast.Node) ast.Visitor { - // In package main and in tests, we don't complain about blank imports. - if w.file.Pkg.IsMain() || w.file.IsTest() { +// Apply applies the rule to given file. +func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { + if file.Pkg.IsMain() || file.IsTest() { return nil } - const embedImportPath = `"embed"` + const ( + message = "a blank import should be only in a main or test package, or have a comment justifying it" + category = "imports" + + embedImportPath = `"embed"` + ) + + var failures []lint.Failure // The first element of each contiguous group of blank imports should have // an explanatory comment of some kind. - for i, imp := range w.fileAst.Imports { - pos := w.file.ToPosition(imp.Pos()) + for i, imp := range file.AST.Imports { + pos := file.ToPosition(imp.Pos()) if !isBlank(imp.Name) { continue // Ignore non-blank imports. } if i > 0 { - prev := w.fileAst.Imports[i-1] - prevPos := w.file.ToPosition(prev.Pos()) + prev := file.AST.Imports[i-1] + prevPos := file.ToPosition(prev.Pos()) isSubsequentBlancInAGroup := isBlank(prev.Name) && prevPos.Line+1 == pos.Line && prev.Path.Value != embedImportPath if isSubsequentBlancInAGroup { @@ -66,25 +49,21 @@ func (w lintBlankImports) Visit(_ ast.Node) ast.Visitor { } } - if imp.Path.Value == embedImportPath && w.fileHasValidEmbedComment() { + if imp.Path.Value == embedImportPath && r.fileHasValidEmbedComment(file.AST) { continue } // This is the first blank import of a group. if imp.Doc == nil && imp.Comment == nil { - w.onFailure(lint.Failure{ - Node: imp, - Failure: "a blank import should be only in a main or test package, or have a comment justifying it", - Confidence: 1, - Category: "imports", - }) + failures = append(failures, lint.Failure{Failure: message, Category: category, Node: imp, Confidence: 1}) } } - return nil + + return failures } -func (w lintBlankImports) fileHasValidEmbedComment() bool { - for _, commentGroup := range w.fileAst.Comments { +func (r *BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool { + for _, commentGroup := range fileAst.Comments { for _, comment := range commentGroup.List { if strings.HasPrefix(comment.Text, "//go:embed ") { return true