Skip to content

Commit

Permalink
handle configureErr in Apply func
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz committed Nov 17, 2024
1 parent f5e3a0e commit 5c98306
Show file tree
Hide file tree
Showing 34 changed files with 220 additions and 55 deletions.
8 changes: 7 additions & 1 deletion lint/package.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lint

import (
"fmt"
"go/ast"
"go/importer"
"go/token"
"go/types"
"os"
"sync"

goversion "github.com/hashicorp/go-version"
Expand Down Expand Up @@ -188,7 +190,11 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error
for _, file := range p.files {
wg.Add(1)
go (func(file *File) {
file.lint(rules, config, failures)
err := file.lint(rules, config, failures)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
wg.Done()
})(file)
}
Expand Down
10 changes: 7 additions & 3 deletions rule/add_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ type AddConstantRule struct {
allowList allowList
ignoreFunctions []*regexp.Regexp
strLitLimit int

configureOnce sync.Once
configureErr error
configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure

Expand Down
10 changes: 7 additions & 3 deletions rule/argument_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

// ArgumentsLimitRule lints given else constructs.
type ArgumentsLimitRule struct {
max int

max int
configureErr error
configureOnce sync.Once
}

Expand All @@ -35,7 +35,11 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
25 changes: 15 additions & 10 deletions rule/banned_characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type BannedCharsRule struct {
bannedCharList []string

configureErr error
configureOnce sync.Once
}

Expand All @@ -22,22 +23,26 @@ const bannedCharsRuleName = "banned-characters"
func (r *BannedCharsRule) configure(arguments lint.Arguments) error {
if len(arguments) > 0 {
check := checkNumberOfArguments(1, arguments, bannedCharsRuleName)
if check != nil {
return check
}
list, err := r.getBannedCharsList(arguments)
if err != nil {
return err
}

r.bannedCharList = list
if check != nil {
return check
}
list, err := r.getBannedCharsList(arguments)
if err != nil {
return err
}

r.bannedCharList = list
}
return nil
}

// Apply applied the rule to the given file.
func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
7 changes: 6 additions & 1 deletion rule/cognitive_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type CognitiveComplexityRule struct {
maxComplexity int

configureErr error
configureOnce sync.Once
}

Expand All @@ -37,7 +38,11 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure

Expand Down
7 changes: 6 additions & 1 deletion rule/comment_spacings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type CommentSpacingsRule struct {
allowList []string

configureErr error
configureOnce sync.Once
}

Expand All @@ -31,7 +32,11 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error {

// Apply the rule.
func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure

Expand Down
7 changes: 6 additions & 1 deletion rule/comments_density.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type CommentsDensityRule struct {
minimumCommentsDensity int64

configureErr error
configureOnce sync.Once
}

Expand All @@ -35,7 +36,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

commentsLines := countDocLines(file.AST.Comments)
statementsCount := countStatements(file.AST)
Expand Down
9 changes: 7 additions & 2 deletions rule/context_as_argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ import (
type ContextAsArgumentRule struct {
allowTypesLUT map[string]struct{}

configureErr error
configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(args) })
func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
walker := lintContextArguments{
Expand Down
7 changes: 6 additions & 1 deletion rule/cyclomatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type CyclomaticRule struct {
maxComplexity int

configureErr error
configureOnce sync.Once
}

Expand All @@ -37,7 +38,11 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
fileAst := file.AST
Expand Down
7 changes: 6 additions & 1 deletion rule/defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type DeferRule struct {
allow map[string]bool

configureErr error
configureOnce sync.Once
}

Expand All @@ -27,7 +28,11 @@ func (r *DeferRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
onFailure := func(failure lint.Failure) {
Expand Down
7 changes: 6 additions & 1 deletion rule/dot_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import (
type DotImportsRule struct {
allowedPackages allowPackages

configureErr error
configureOnce sync.Once
}

// Apply applies the rule to given file.
func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure

Expand Down
7 changes: 6 additions & 1 deletion rule/enforce_map_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) {
type EnforceMapStyleRule struct {
enforceMapStyle enforceMapStyleType

configureErr error
configureOnce sync.Once
}

Expand All @@ -67,7 +68,11 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

if r.enforceMapStyle == enforceMapStyleTypeAny {
// this linter is not configured
Expand Down
9 changes: 7 additions & 2 deletions rule/enforce_repeated_arg_type_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type EnforceRepeatedArgTypeStyleRule struct {
funcArgStyle enforceRepeatedArgTypeStyleType
funcRetValStyle enforceRepeatedArgTypeStyleType

configureErr error
configureOnce sync.Once
}

Expand Down Expand Up @@ -103,14 +104,18 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er

// Apply applies the rule to a given file.
func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny {
// This linter is not configured, return no failures.
return nil, nil
}

var failures []lint.Failure
var failures []lint.Failure

astFile := file.AST
ast.Inspect(astFile, func(n ast.Node) bool {
Expand Down
7 changes: 6 additions & 1 deletion rule/enforce_slice_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) {
type EnforceSliceStyleRule struct {
enforceSliceStyle enforceSliceStyleType

configureErr error
configureOnce sync.Once
}

Expand All @@ -70,7 +71,11 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

if r.enforceSliceStyle == enforceSliceStyleTypeAny {
// this linter is not configured
Expand Down
7 changes: 6 additions & 1 deletion rule/error_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type ErrorStringsRule struct {
errorFunctions map[string]map[string]struct{}

configureErr error
configureOnce sync.Once
}

Expand Down Expand Up @@ -57,7 +58,11 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error {
func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
var failures []lint.Failure

r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

fileAst := file.AST
walker := lintErrorStrings{
Expand Down
7 changes: 6 additions & 1 deletion rule/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ExportedRule struct {
stuttersMsg string
disabledChecks disabledChecks

configureErr error
configureOnce sync.Once
}

Expand Down Expand Up @@ -100,7 +101,11 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

var failures []lint.Failure
if file.IsTest() {
Expand Down
7 changes: 6 additions & 1 deletion rule/file_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type FileHeaderRule struct {
header string

configureErr error
configureOnce sync.Once
}

Expand All @@ -36,7 +37,11 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error {

// Apply applies the rule to given file.
func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) {
r.configureOnce.Do(func() { r.configure(arguments) })
r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) })

if r.configureErr != nil {
return nil, r.configureErr
}

if r.header == "" {
return nil, nil
Expand Down
Loading

0 comments on commit 5c98306

Please sign in to comment.