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 3795772
Show file tree
Hide file tree
Showing 3 changed files with 75 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)
}
}
}
5 changes: 1 addition & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go/token"
"go/types"
"os"
"regexp"

"github.com/dave/astrid"
"github.com/dave/brenda"
Expand Down Expand Up @@ -119,8 +118,6 @@ func (c *CodeMap) ScanPackages() error {
return nil
}

var doNotEditRe = regexp.MustCompile(`(?m)^// Code generated .* DO NOT EDIT\.$`)

// ScanPackage scans a single package
func (p *PackageMap) ScanPackage() error {
for _, f := range p.pkg.Syntax {
Expand All @@ -141,7 +138,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

0 comments on commit 3795772

Please sign in to comment.