Skip to content

Commit

Permalink
Commit changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Aug 29, 2024
1 parent f4deb83 commit c23bbe1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 48 deletions.
11 changes: 2 additions & 9 deletions tools/analyzers/closeafterusagecheck/closeafterusagecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"slices"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

type nodeAndStack struct {
node ast.Node
stack []ast.Node
Expand All @@ -39,7 +32,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skip) > 0 {
skipped := sliceMap(strings.Split(*skip, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skip, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand Down
11 changes: 2 additions & 9 deletions tools/analyzers/exprstatementcheck/exprstatementcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"slices"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

type disallowedExprStatementConfig struct {
fullTypePath string
errorMessage string
Expand Down Expand Up @@ -47,7 +40,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skip) > 0 {
skipped := sliceMap(strings.Split(*skip, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skip, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand Down
13 changes: 3 additions & 10 deletions tools/analyzers/lendowncastcheck/lendowncastcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"regexp"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

var disallowedDowncastTypes = map[string]bool{
"int8": true,
"int16": true,
Expand All @@ -44,7 +37,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skipPkg) > 0 {
skipped := sliceMap(strings.Split(*skipPkg, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand All @@ -55,7 +48,7 @@ func Analyzer() *analysis.Analyzer {
// Check for a skipped file.
skipFilePatterns := make([]string, 0)
if len(*skipFiles) > 0 {
skipFilePatterns = sliceMap(strings.Split(*skipFiles, ","), strings.TrimSpace)
skipFilePatterns = lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
}
for _, pattern := range skipFilePatterns {
_, err := regexp.Compile(pattern)
Expand Down
13 changes: 3 additions & 10 deletions tools/analyzers/nilvaluecheck/nilvaluecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"slices"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

func Analyzer() *analysis.Analyzer {
flagSet := flag.NewFlagSet("nilvaluecheck", flag.ExitOnError)
disallowedPaths := flagSet.String("disallowed-nil-return-type-paths", "", "full paths of the types for whom nil returns are disallowed")
Expand All @@ -31,7 +24,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skip) > 0 {
skipped := sliceMap(strings.Split(*skip, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skip, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand All @@ -46,7 +39,7 @@ func Analyzer() *analysis.Analyzer {
(*ast.DeclStmt)(nil),
}

typePaths := sliceMap(strings.Split(*disallowedPaths, ","), strings.TrimSpace)
typePaths := lo.Map(strings.Split(*disallowedPaths, ","), func(path string, _ int) string { return strings.TrimSpace(path) })
hasTypePath := func(path string) bool {
return slices.Contains(typePaths, path)
}
Expand Down
13 changes: 3 additions & 10 deletions tools/analyzers/paniccheck/paniccheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"regexp"
"strings"

"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}

func Analyzer() *analysis.Analyzer {
flagSet := flag.NewFlagSet("paniccheck", flag.ExitOnError)
skipPkg := flagSet.String("skip-pkg", "", "package(s) to skip for linting")
Expand All @@ -31,7 +24,7 @@ func Analyzer() *analysis.Analyzer {
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skipPkg) > 0 {
skipped := sliceMap(strings.Split(*skipPkg, ","), strings.TrimSpace)
skipped := lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
Expand All @@ -42,7 +35,7 @@ func Analyzer() *analysis.Analyzer {
// Check for a skipped file.
skipFilePatterns := make([]string, 0)
if len(*skipFiles) > 0 {
skipFilePatterns = sliceMap(strings.Split(*skipFiles, ","), strings.TrimSpace)
skipFilePatterns = lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) })
}
for _, pattern := range skipFilePatterns {
_, err := regexp.Compile(pattern)
Expand Down

0 comments on commit c23bbe1

Please sign in to comment.