Skip to content

Commit

Permalink
Add ignore flag (#20)
Browse files Browse the repository at this point in the history
* ignore flag

* fix
  • Loading branch information
notJoon authored Jul 25, 2024
1 parent adb4ee3 commit e34a09d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/tlin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"github.com/gnoswap-labs/lint/formatter"
"github.com/gnoswap-labs/lint/internal"
Expand All @@ -27,6 +28,8 @@ func main() {
// [*] MaCabe's article recommends 10 or less, but up to 15 is acceptable (by Microsoft).
// [*] https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-cyclomatic-complexity?view=vs-2022
cyclomaticThreshold := flag.Int("threshold", 10, "Cyclomatic complexity threshold")
ignoreRules := flag.String("ignore", "", "Comma-separated list of lint rules to ignore")

flag.Parse()

args := flag.Args()
Expand All @@ -42,6 +45,13 @@ func main() {
os.Exit(1)
}

if *ignoreRules != "" {
rules := strings.Split(*ignoreRules, ",")
for _, rule := range rules {
engine.IgnoreRule(strings.TrimSpace(rule))
}
}

if *cyclomaticComplexity {
runCyclomaticComplexityAnalysis(args, *cyclomaticThreshold)
} else {
Expand Down
11 changes: 11 additions & 0 deletions internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Engine struct {
SymbolTable *SymbolTable
rules []LintRule
ignoredRules map[string]bool
}

// NewEngine creates a new lint engine.
Expand Down Expand Up @@ -57,6 +58,9 @@ func (e *Engine) Run(filename string) ([]tt.Issue, error) {

var allIssues []tt.Issue
for _, rule := range e.rules {
if e.ignoredRules[rule.Name()] {
continue
}
issues, err := rule.Check(tempFile)
if err != nil {
return nil, fmt.Errorf("error running lint rule: %w", err)
Expand All @@ -76,6 +80,13 @@ func (e *Engine) Run(filename string) ([]tt.Issue, error) {
return filtered, nil
}

func (e *Engine) IgnoreRule(rule string) {
if e.ignoredRules == nil {
e.ignoredRules = make(map[string]bool)
}
e.ignoredRules[rule] = true
}

func (e *Engine) prepareFile(filename string) (string, error) {
if strings.HasSuffix(filename, "gno") {
return createTempGoFile(filename)
Expand Down
1 change: 1 addition & 0 deletions internal/lints/loop_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func DetectLoopAllocation(filename string) ([]tt.Issue, error) {
case *ast.CallExpr:
if isAllocationFunction(innerNode) {
issues = append(issues, tt.Issue{
Rule: "loop-allocation",
Message: "Potential unnecessary allocation inside loop",
Start: fset.Position(innerNode.Pos()),
End: fset.Position(innerNode.End()),
Expand Down
39 changes: 39 additions & 0 deletions internal/rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
type LintRule interface {
// Check runs the lint rule on the given file and returns a slice of Issues.
Check(filename string) ([]tt.Issue, error)

// Name returns the name of the lint rule.
Name() string
}

type GolangciLintRule struct{}
Expand All @@ -21,42 +24,70 @@ func (r *GolangciLintRule) Check(filename string) ([]tt.Issue, error) {
return lints.RunGolangciLint(filename)
}

func (r *GolangciLintRule) Name() string {
return "golangci-lint"
}

type UnnecessaryElseRule struct{}

func (r *UnnecessaryElseRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectUnnecessaryElse(filename)
}

func (r *UnnecessaryElseRule) Name() string {
return "unnecessary-else"
}

type UnusedFunctionRule struct{}

func (r *UnusedFunctionRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectUnusedFunctions(filename)
}

func (r *UnusedFunctionRule) Name() string {
return "unused-function"
}

type SimplifySliceExprRule struct{}

func (r *SimplifySliceExprRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectUnnecessarySliceLength(filename)
}

func (r *SimplifySliceExprRule) Name() string {
return "simplify-slice-range"
}

type UnnecessaryConversionRule struct{}

func (r *UnnecessaryConversionRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectUnnecessaryConversions(filename)
}

func (r *UnnecessaryConversionRule) Name() string {
return "unnecessary-type-conversion"
}

type LoopAllocationRule struct{}

func (r *LoopAllocationRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectLoopAllocation(filename)
}

func (r *LoopAllocationRule) Name() string {
return "loop-allocation"
}

type DetectCycleRule struct{}

func (r *DetectCycleRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectCycle(filename)
}

func (r *DetectCycleRule) Name() string {
return "cycle-detection"
}

// -----------------------------------------------------------------------------

type CyclomaticComplexityRule struct {
Expand All @@ -67,6 +98,10 @@ func (r *CyclomaticComplexityRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectHighCyclomaticComplexity(filename, r.Threshold)
}

func (r *CyclomaticComplexityRule) Name() string {
return "high-cyclomatic-complexity"
}

// -----------------------------------------------------------------------------

// GnoSpecificRule checks for gno-specific package imports. (p, r and std)
Expand All @@ -75,3 +110,7 @@ type GnoSpecificRule struct{}
func (r *GnoSpecificRule) Check(filename string) ([]tt.Issue, error) {
return lints.DetectGnoPackageImports(filename)
}

func (r *GnoSpecificRule) Name() string {
return "unused-package"
}

0 comments on commit e34a09d

Please sign in to comment.