Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame^] | 1 | package lib |
| 2 | |
| 3 | import ( |
| 4 | "go/ast" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | const ( |
| 9 | genPrefix = "// Code generated" |
| 10 | genSuffix = "DO NOT EDIT." |
| 11 | ) |
| 12 | |
| 13 | // IsGeneratedFile returns true if the file is generated according to |
| 14 | // https://golang.org/s/generatedcode and other heuristics. |
| 15 | func IsGeneratedFile(file *ast.File) bool { |
| 16 | for _, c := range file.Comments { |
| 17 | for _, t := range c.List { |
| 18 | if strings.HasPrefix(t.Text, genPrefix) && strings.HasSuffix(t.Text, genSuffix) { |
| 19 | return true |
| 20 | } |
| 21 | // Generated testmain.go stubs from rules_go - for some reason, they don't |
| 22 | // contain the expected markers. |
| 23 | if strings.Contains(t.Text, "This package must be initialized before packages being tested.") { |
| 24 | return true |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | return false |
| 29 | } |