-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: validate LinterExclusions configuration
- Loading branch information
Showing
4 changed files
with
107 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type BaseRule struct { | ||
Linters []string | ||
Path string | ||
PathExcept string `mapstructure:"path-except"` | ||
Text string | ||
Source string | ||
|
||
// For compatibility with exclude-use-default/include. | ||
InternalReference string `mapstructure:"-"` | ||
} | ||
|
||
func (b *BaseRule) Validate(minConditionsCount int) error { | ||
if err := validateOptionalRegex(b.Path); err != nil { | ||
return fmt.Errorf("invalid path regex: %w", err) | ||
} | ||
|
||
if err := validateOptionalRegex(b.PathExcept); err != nil { | ||
return fmt.Errorf("invalid path-except regex: %w", err) | ||
} | ||
|
||
if err := validateOptionalRegex(b.Text); err != nil { | ||
return fmt.Errorf("invalid text regex: %w", err) | ||
} | ||
|
||
if err := validateOptionalRegex(b.Source); err != nil { | ||
return fmt.Errorf("invalid source regex: %w", err) | ||
} | ||
|
||
if b.Path != "" && b.PathExcept != "" { | ||
return errors.New("path and path-except should not be set at the same time") | ||
} | ||
|
||
nonBlank := 0 | ||
if len(b.Linters) > 0 { | ||
nonBlank++ | ||
} | ||
|
||
// Filtering by path counts as one condition, regardless how it is done (one or both). | ||
// Otherwise, a rule with Path and PathExcept set would pass validation | ||
// whereas before the introduction of path-except that wouldn't have been precise enough. | ||
if b.Path != "" || b.PathExcept != "" { | ||
nonBlank++ | ||
} | ||
|
||
if b.Text != "" { | ||
nonBlank++ | ||
} | ||
|
||
if b.Source != "" { | ||
nonBlank++ | ||
} | ||
|
||
if nonBlank < minConditionsCount { | ||
return fmt.Errorf("at least %d of (text, source, path[-except], linters) should be set", minConditionsCount) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateOptionalRegex(value string) error { | ||
if value == "" { | ||
return nil | ||
} | ||
|
||
_, err := regexp.Compile(value) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,40 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
DefaultExclusionComments = "comments" | ||
DefaultExclusionStdErrorHandling = "stdErrorHandling" | ||
DefaultExclusionCommonFalsePositives = "commonFalsePositives" | ||
DefaultExclusionLegacy = "legacy" | ||
) | ||
|
||
const excludeRuleMinConditionsCount = 2 | ||
|
||
type LinterExclusions struct { | ||
Generated string `mapstructure:"generated"` | ||
WarnUnused bool `mapstructure:"warn-unused"` | ||
Default []string `mapstructure:"default"` | ||
Rules []ExcludeRule `mapstructure:"rules"` | ||
Paths []string `mapstructure:"paths"` | ||
} | ||
|
||
func (e *LinterExclusions) Validate() error { | ||
for i, rule := range e.Rules { | ||
if err := rule.Validate(); err != nil { | ||
return fmt.Errorf("error in exclude rule #%d: %w", i, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type ExcludeRule struct { | ||
BaseRule `mapstructure:",squash"` | ||
} | ||
|
||
func (e *ExcludeRule) Validate() error { | ||
return e.BaseRule.Validate(excludeRuleMinConditionsCount) | ||
} |