Skip to content

Commit

Permalink
Merge pull request #12 from mx-psi/mx-psi/exclude-autogenerated
Browse files Browse the repository at this point in the history
Skip generated files
  • Loading branch information
jcchavezs authored Oct 10, 2021
2 parents b7c25c2 + 22b9346 commit 752edbc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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.
// Taken from https://github.com/golang/tools/blob/c5188f24a/refactor/rename/spec.go#L574-L576
generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)
)

// isGeneratedFile reports whether ast.File is a generated file.
// Taken from https://github.com/golang/tools/blob/c5188f24a/refactor/rename/spec.go#L578-L593
func isGeneratedFile(pf *ast.File, tokenFile *token.File) bool {
// 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 isGeneratedFile(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.

0 comments on commit 752edbc

Please sign in to comment.