From 50481a6f5b74f495ca727668c1b84a2af8ae6527 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Sun, 24 Dec 2023 21:46:24 +0200 Subject: [PATCH] linter to pkg --- linter/README.md | 17 ---------- linter/go.mod | 13 -------- linter/go.sum | 12 ------- linter/main.go | 45 -------------------------- {linter => pkg/goanalysis}/analyzer.go | 20 ++++++------ pkg/std/gen/gen.go | 2 +- 6 files changed, 10 insertions(+), 99 deletions(-) delete mode 100644 linter/README.md delete mode 100644 linter/go.mod delete mode 100644 linter/go.sum delete mode 100644 linter/main.go rename {linter => pkg/goanalysis}/analyzer.go (77%) diff --git a/linter/README.md b/linter/README.md deleted file mode 100644 index 4c0ad1e..0000000 --- a/linter/README.md +++ /dev/null @@ -1,17 +0,0 @@ -goimports reviser linter ---- - -### Build -Choose one of the binary(inside `./bin` dir) to your current OS & Arch types after the Make command: -```shell -make build-all-lint -``` - -### Run with `go vet` -```shell -go vet -vettool=bin/macos-amd64/goimportsreviserlint ./... -``` - -Output: - -!['linter output'](../images/linter-example.png) diff --git a/linter/go.mod b/linter/go.mod deleted file mode 100644 index a740730..0000000 --- a/linter/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module github.com/incu6us/goimports-reviser/linter - -go 1.18 - -require ( - github.com/incu6us/goimports-reviser/v2 v2.5.3 - golang.org/x/tools v0.1.12 -) - -require ( - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect -) diff --git a/linter/go.sum b/linter/go.sum deleted file mode 100644 index 179544a..0000000 --- a/linter/go.sum +++ /dev/null @@ -1,12 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/incu6us/goimports-reviser/v2 v2.5.3 h1:DzvFl1+qOIDukqN8vMM/10MQswFQywUdwXxsjuowxlc= -github.com/incu6us/goimports-reviser/v2 v2.5.3/go.mod h1:P18aXhQaED7izHIP9IPI9PqEs7Y7D9okq71Q8Y8yHN4= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/linter/main.go b/linter/main.go deleted file mode 100644 index 3420e9f..0000000 --- a/linter/main.go +++ /dev/null @@ -1,45 +0,0 @@ -//go:build linter -// +build linter - -package main - -import ( - "flag" - - "golang.org/x/tools/go/analysis/singlechecker" - - "github.com/incu6us/goimports-reviser/v2/reviser" -) - -func main() { - flagSet := flag.NewFlagSet("", flag.ContinueOnError) - - var localPackagePrefixes string - flagSet.StringVar(&localPackagePrefixes, "local", "", "Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.") - - var ( - shouldRemoveUnusedImports bool - shouldSetAlias bool - shouldFormat bool - ) - - flagSet.BoolVar(&shouldRemoveUnusedImports, "rm-unused", false, "Remove unused imports. Optional parameter.") - flagSet.BoolVar(&shouldSetAlias, "set-alias", false, "Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. "+ - "In this case import will be set as 'pg \"github.com/go-pg/pg/v9\"'. Optional parameter.") - flagSet.BoolVar(&shouldFormat, "format", false, "Option will perform additional formatting. Optional parameter.") - - var options reviser.Options - if shouldRemoveUnusedImports { - options = append(options, reviser.OptionRemoveUnusedImports) - } - - if shouldSetAlias { - options = append(options, reviser.OptionUseAliasForVersionSuffix) - } - - if shouldFormat { - options = append(options, reviser.OptionFormat) - } - - singlechecker.Main(NewAnalyzer(flagSet, localPackagePrefixes, options...)) -} diff --git a/linter/analyzer.go b/pkg/goanalysis/analyzer.go similarity index 77% rename from linter/analyzer.go rename to pkg/goanalysis/analyzer.go index ecd685b..257e4e0 100644 --- a/linter/analyzer.go +++ b/pkg/goanalysis/analyzer.go @@ -1,7 +1,4 @@ -//go:build linter -// +build linter - -package main +package goanalysis import ( "flag" @@ -9,23 +6,24 @@ import ( "go/parser" "go/token" - "github.com/incu6us/goimports-reviser/v2/pkg/module" - "github.com/incu6us/goimports-reviser/v2/reviser" "golang.org/x/tools/go/analysis" + + "github.com/incu6us/goimports-reviser/v3/pkg/module" + "github.com/incu6us/goimports-reviser/v3/reviser" ) const errMessage = "imports must be formatted" -func NewAnalyzer(flagSet *flag.FlagSet, localPkgPrefixes string, options ...reviser.Option) *analysis.Analyzer { +func NewAnalyzer(flagSet *flag.FlagSet, localPkgPrefixes string, options ...reviser.SourceFileOption) *analysis.Analyzer { return &analysis.Analyzer{ - Name: "goimportsreviserlint", - Doc: "Linter for imports sorting", + Name: "goimportsreviser", + Doc: "goimports-reviser linter", Run: run(localPkgPrefixes, options...), Flags: *flagSet, } } -func run(localPkgPrefixes string, options ...reviser.Option) func(pass *analysis.Pass) (interface{}, error) { +func run(localPkgPrefixes string, options ...reviser.SourceFileOption) func(pass *analysis.Pass) (interface{}, error) { return func(pass *analysis.Pass) (interface{}, error) { inspect := func(formattedFile *ast.File, hasChanged bool) func(node ast.Node) bool { return func(node ast.Node) bool { @@ -79,7 +77,7 @@ func run(localPkgPrefixes string, options ...reviser.Option) func(pass *analysis } } - formattedFileContent, hasChanged, err := reviser.Execute(projectName, filePath, localPkgPrefixes, options...) + formattedFileContent, hasChanged, err := reviser.NewSourceFile(projectName, filePath).Fix(options...) if err != nil { return nil, err } diff --git a/pkg/std/gen/gen.go b/pkg/std/gen/gen.go index 11719f3..1f3d0ac 100644 --- a/pkg/std/gen/gen.go +++ b/pkg/std/gen/gen.go @@ -13,7 +13,7 @@ import ( "golang.org/x/tools/go/packages" ) -//go:generate go run -tags gen github.com/incu6us/goimports-reviser/v2/pkg/std/gen +//go:generate go run -tags gen github.com/incu6us/goimports-reviser/v3/pkg/std/gen const ( fileName = "package_list.go"