bazel: migrate go:generate for staticcheck (#59215)

Closes https://github.com/sourcegraph/sourcegraph/issues/54833

## Test plan

`bazel run //dev/linters/staticcheck:write_generated_bazelfile` and `cd dev/linters/staticcheck && go generate` as well as adding staticchecks to be ignored
This commit is contained in:
Noah S-C 2024-01-02 13:02:50 +00:00 committed by GitHub
parent 3115247914
commit a524ca752e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1941 additions and 1910 deletions

View File

@ -24,5 +24,6 @@ write_source_files(
"//migrations/codeintel:write_squashed",
"//migrations/frontend:write_squashed",
"//schema:write_generated_schema",
"//dev/linters/staticcheck:write_generated_bazelfile",
],
)

File diff suppressed because it is too large Load Diff

View File

@ -23,37 +23,29 @@ var BazelBuildTemplate = `# GENERATED FILE - DO NOT EDIT
# This file was generated by running go generate on dev/linters/staticcheck
#
# If you want to ignore an analyzer add it to the ignore list in dev/linters/staticcheck/cmd/gen.go,
# and re-run go generate
# and re-run go generate.
#
# IMPORTANT: Starlark is spaces/tabs sensitive, make sure the below is SPACE indented.
load("@io_bazel_rules_go//go:def.bzl", "go_library")
{{ range .Analyzers}}
go_library(
name = "{{.Analyzer.Name}}",
srcs = ["staticcheck.go"],
importpath = "github.com/sourcegraph/sourcegraph/dev/linters/staticcheck/{{.Analyzer.Name}}",
visibility = ["//visibility:public"],
x_defs = {"AnalyzerName": "{{.Analyzer.Name}}"},
deps = [
"//dev/linters/nolint",
"@co_honnef_go_tools//analysis/lint",
"@co_honnef_go_tools//simple",
"@co_honnef_go_tools//staticcheck",
"@org_golang_x_tools//go/analysis",
],
)
{{ end}}
go_library(
name = "staticcheck",
srcs = ["staticcheck.go"],
importpath = "github.com/sourcegraph/sourcegraph/dev/linters/staticcheck",
visibility = ["//visibility:public"],
deps = [
"//dev/linters/nolint",
"@co_honnef_go_tools//simple",
"@co_honnef_go_tools//staticcheck",
"@org_golang_x_tools//go/analysis",
],
)
def staticcheck_targets():
{{ range .Analyzers }} go_library(
name = "{{.Analyzer.Name}}",
srcs = ["staticcheck.go"],
importpath = "github.com/sourcegraph/sourcegraph/dev/linters/staticcheck/{{.Analyzer.Name}}",
visibility = ["//visibility:public"],
x_defs = {"AnalyzerName": "{{.Analyzer.Name}}"},
deps = [
"//dev/linters/nolint",
"@co_honnef_go_tools//analysis/lint",
"@co_honnef_go_tools//simple",
"@co_honnef_go_tools//staticcheck",
"@org_golang_x_tools//go/analysis",
],
)
{{ end -}}
`
var BazelDefTemplate = `# DO NOT EDIT - this file was generated by running go generate on dev/linters/staticcheck
@ -62,9 +54,9 @@ var BazelDefTemplate = `# DO NOT EDIT - this file was generated by running go ge
# and re-run go generate
STATIC_CHECK_ANALYZERS = [
{{- range .Analyzers}}
{{- range .Analyzers }}
"//dev/linters/staticcheck:{{.Analyzer.Name}}",
{{- end}}
{{- end }}
]
`
@ -108,7 +100,7 @@ func writeTemplate(targetFile, templateDef string) error {
name := targetFile
tmpl := template.Must(template.New(name).Parse(templateDef))
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0o666)
if err != nil {
return err
}
@ -130,7 +122,7 @@ func writeTemplate(targetFile, templateDef string) error {
// 1: buildfile path - file where the analyzer targets should be generated to
// 2: analyzer definition path - file where a convienience analyzer array is generated that contains all the targets
func main() {
targetFile := "BUILD.bazel"
targetFile := "targets.bzl"
if len(os.Args) > 1 {
targetFile = os.Args[1]
}
@ -150,5 +142,4 @@ func main() {
fmt.Fprintln(os.Stderr, "failed to render Anazlyers definiton template")
panic(err)
}
}

View File

@ -1,4 +1,5 @@
//go:generate go run ./cmd/gen.go BUILD.bazel
//go:generate go run ./cmd/gen.go targets.bzl
//go:generate truncate -s -1 targets.bzl
package staticcheck
import (
@ -12,8 +13,10 @@ import (
// AllAnalyzers contains staticcheck and gosimple Analyzers
var AllAnalyzers = append(staticcheck.Analyzers, simple.Analyzers...)
var AnalyzerName = ""
var Analyzer *analysis.Analyzer = GetAnalyzerByName(AnalyzerName)
var (
AnalyzerName = ""
Analyzer *analysis.Analyzer = GetAnalyzerByName(AnalyzerName)
)
func GetAnalyzerByName(name string) *analysis.Analyzer {
for _, a := range AllAnalyzers {

File diff suppressed because it is too large Load Diff