github.com/Johnny2210/revive@v1.0.8-0.20210625134200-febf37ccd0f5/rule/dot-imports.go (about)

     1  package rule
     2  
     3  import (
     4  	"go/ast"
     5  
     6  	"github.com/mgechev/revive/lint"
     7  )
     8  
     9  // DotImportsRule lints given else constructs.
    10  type DotImportsRule struct{}
    11  
    12  // Apply applies the rule to given file.
    13  func (r *DotImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
    14  	var failures []lint.Failure
    15  
    16  	fileAst := file.AST
    17  	walker := lintImports{
    18  		file:    file,
    19  		fileAst: fileAst,
    20  		onFailure: func(failure lint.Failure) {
    21  			failures = append(failures, failure)
    22  		},
    23  	}
    24  
    25  	ast.Walk(walker, fileAst)
    26  
    27  	return failures
    28  }
    29  
    30  // Name returns the rule name.
    31  func (r *DotImportsRule) Name() string {
    32  	return "dot-imports"
    33  }
    34  
    35  type lintImports struct {
    36  	file      *lint.File
    37  	fileAst   *ast.File
    38  	onFailure func(lint.Failure)
    39  }
    40  
    41  func (w lintImports) Visit(_ ast.Node) ast.Visitor {
    42  	for i, is := range w.fileAst.Imports {
    43  		_ = i
    44  		if is.Name != nil && is.Name.Name == "." && !w.file.IsTest() {
    45  			w.onFailure(lint.Failure{
    46  				Confidence: 1,
    47  				Failure:    "should not use dot imports",
    48  				Node:       is,
    49  				Category:   "imports",
    50  			})
    51  		}
    52  	}
    53  	return nil
    54  }