Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
Expand All @@ -14,12 +15,33 @@ import (

var (
errMainPackage = errors.New("failed to add import to a main package")
errGenerated = errors.New("failed to add import to a generated file")
// Matches https://golang.org/s/generatedcode and cgo generated comment.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3 love the comments

// Taken from https://github.com/golang/tools/blob/c5188f24a/refactor/rename/spec.go#L574-L576
generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)
)

// generated reports whether ast.File is a generated file.
// Taken from https://github.com/golang/tools/blob/c5188f24a/refactor/rename/spec.go#L578-L593
func generated(pf *ast.File, tokenFile *token.File) bool {
Comment thread
mx-psi marked this conversation as resolved.
Outdated
// Iterate over the comments in the file
for _, commentGroup := range pf.Comments {
for _, comment := range commentGroup.List {
if matched := generatedRx.MatchString(comment.Text); matched {
// Check if comment is at the beginning of the line in source
if pos := tokenFile.Position(comment.Slash); pos.Column == 1 {
return true
}
}
}
}
return false
}

// addImportPath adds the vanity import path to a given go file.
func addImportPath(absFilepath string, module string) (bool, []byte, error) {
fset := token.NewFileSet()
pf, err := parser.ParseFile(fset, absFilepath, nil, 0)
pf, err := parser.ParseFile(fset, absFilepath, nil, parser.ParseComments)
if err != nil {
return false, nil, fmt.Errorf("failed to parse the file %q: %v", absFilepath, err)
}
Expand All @@ -28,6 +50,12 @@ func addImportPath(absFilepath string, module string) (bool, []byte, error) {
return false, nil, errMainPackage
}

// Skip generated files.
tokenFile := fset.File(pf.Pos())
if generated(pf, tokenFile) {
return false, nil, errGenerated
}

content, err := ioutil.ReadFile(absFilepath)
if err != nil {
return false, nil, fmt.Errorf("failed to parse the file %q: %v", absFilepath, err)
Expand Down
10 changes: 10 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func TestAddImportPathAddsVanityImport(t *testing.T) {
assert.Equal(t, "package leftpad // import \"mypackage\"", string(newContent[15:52]))
}

func TestAddImportAutogenerated(t *testing.T) {
cwd, _ := os.Getwd()
hasChanged, _, err := addImportPath(
cwd+"/testdata/codegen/generated.go",
"codegen")

assert.Equal(t, errGenerated, err)
assert.False(t, hasChanged)
}

func TestAddImportPathFixesTheVanityImport(t *testing.T) {
cwd, _ := os.Getwd()
hasChanged, newContent, err := addImportPath(
Expand Down
16 changes: 16 additions & 0 deletions testdata/codegen/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.