Skip to content

Commit

Permalink
Use isGenerated from golang/lint
Browse files Browse the repository at this point in the history
  • Loading branch information
breml committed Mar 29, 2022
1 parent 490a775 commit 9f95252
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
36 changes: 36 additions & 0 deletions scanner/generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.

// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint.go

// Once https://github.com/golang/go/issues/28089 is implemented (proposal is accepted),
// this can be replaced with the official implementation.

package scanner

import (
"bufio"
"bytes"
)

var (
genHdr = []byte("// Code generated ")
genFtr = []byte(" DO NOT EDIT.")
)

// isGenerated reports whether the source file is generated code
// according the rules from https://golang.org/s/generatedcode.
//
func isGenerated(src []byte) bool {
sc := bufio.NewScanner(bytes.NewReader(src))
for sc.Scan() {
b := sc.Bytes()
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
return true
}
}
return false
}
38 changes: 38 additions & 0 deletions scanner/generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.

// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint_test.go

package scanner

import "testing"

func TestIsGenerated(t *testing.T) {
tests := []struct {
source string
generated bool
}{
{"// Code Generated by some tool. DO NOT EDIT.", false},
{"// Code generated by some tool. DO NOT EDIT.", true},
{"// Code generated by some tool. DO NOT EDIT", false},
{"// Code generated DO NOT EDIT.", true},
{"// Code generated DO NOT EDIT.", false},
{"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false},
{"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true},
{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true},
{"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false},
{"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false},
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true},
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true},
}

for i, test := range tests {
got := isGenerated([]byte(test.source))
if got != test.generated {
t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated)
}
}
}
6 changes: 2 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func (c *CodeMap) LoadProgram() error {
conf.Import(p.Path)
}
prog, err := conf.Load()
*/
if err != nil {
*/if err != nil {
return errors.Wrap(err, "Error loading config")
}
c.pkgs = pkgs
Expand Down Expand Up @@ -141,7 +140,7 @@ func (p *PackageMap) ScanPackage() error {
if err != nil {
return errors.WithStack(err)
}
if !doNotEditRe.Match(body) {
if !isGenerated(body) {
continue
}
for i := range bytes.Split(body, []byte("\n")) {
Expand Down Expand Up @@ -293,7 +292,6 @@ func (f *FileMap) boolOr(list []ast.Expr) ast.Expr {
}

func (f *FileMap) inspectIf(stmt *ast.IfStmt, falseExpr ...ast.Expr) error {

// main if block
s := brenda.NewSolver(f.fset, f.pkg.TypesInfo.Uses, f.pkg.TypesInfo.Defs, stmt.Cond, falseExpr...)
if err := s.SolveTrue(); err != nil {
Expand Down

0 comments on commit 9f95252

Please sign in to comment.