Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/linters/excessivefuncparams/excessivefuncparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"

"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
)

// DefaultMaxParams is the default maximum number of parameters allowed in a function declaration.
Expand Down Expand Up @@ -36,6 +38,7 @@ func run(pass *analysis.Pass) (any, error) {
if err != nil {
return nil, err
}
noLintLinesByFile := nolint.BuildLineIndex(pass, "excessivefuncparams")

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
Expand All @@ -49,6 +52,10 @@ func run(pass *analysis.Pass) (any, error) {
if fn.Type == nil || fn.Type.Params == nil {
return
}
position := pass.Fset.PositionFor(fn.Name.Pos(), false)
if filecheck.IsTestFile(position.Filename) {
return
}
Comment on lines +55 to +58

params := 0
for _, field := range fn.Type.Params.List {
Expand All @@ -60,6 +67,9 @@ func run(pass *analysis.Pass) (any, error) {
}

if params > maxParams {
if nolint.HasDirective(position, noLintLinesByFile) {
return
}
pass.ReportRangef(
fn.Name,
"%s has %d parameters (limit: %d); consider using an options struct",
Expand Down
3 changes: 3 additions & 0 deletions pkg/linters/excessivefuncparams/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ func fewParams(a, b, c int) {}
// tooManyParams has more than 8 parameters and should be flagged.
func tooManyParams(a, b, c, d, e, f, g, h, i int) { // want `tooManyParams has 9 parameters \(limit: 8\); consider using an options struct`
}

//nolint:excessivefuncparams
func suppressedTooManyParams(a, b, c, d, e, f, g, h, i int) {}
10 changes: 10 additions & 0 deletions pkg/linters/excessivefuncparams/testdata/src/a/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package a

import "testing"

func tooManyParamsTestHelper(
t *testing.T,
a, b, c, d, e, f, g, h int,
) {
_ = t
}
8 changes: 7 additions & 1 deletion pkg/linters/largefunc/largefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
)

// DefaultMaxLines is the default maximum number of lines allowed in a function body.
Expand Down Expand Up @@ -37,6 +38,7 @@ func run(pass *analysis.Pass) (any, error) {
if err != nil {
return nil, err
}
noLintLinesByFile := nolint.BuildLineIndex(pass, "largefunc")

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
Expand All @@ -63,7 +65,8 @@ func run(pass *analysis.Pass) (any, error) {
return
}

if filecheck.IsTestFile(pass.Fset.Position(body.Pos()).Filename) {
position := pass.Fset.PositionFor(reportNode.Pos(), false)
if filecheck.IsTestFile(position.Filename) {
return
}
Comment on lines +68 to 71

Expand All @@ -73,6 +76,9 @@ func run(pass *analysis.Pass) (any, error) {
lines := end.Line - start.Line - 1

if lines > maxLines {
if nolint.HasDirective(position, noLintLinesByFile) {
return
}
pass.ReportRangef(
reportNode,
"%s is %d lines long (limit: %d); consider breaking it up",
Expand Down
65 changes: 65 additions & 0 deletions pkg/linters/largefunc/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,68 @@ func largeFunc() { // want `largeFunc is 61 lines long \(limit: 60\); consider b
_ = 60
_ = 61
}

//nolint:largefunc
func suppressedLargeFunc() {
_ = 1
_ = 2
_ = 3
_ = 4
_ = 5
_ = 6
_ = 7
_ = 8
_ = 9
_ = 10
_ = 11
_ = 12
_ = 13
_ = 14
_ = 15
_ = 16
_ = 17
_ = 18
_ = 19
_ = 20
_ = 21
_ = 22
_ = 23
_ = 24
_ = 25
_ = 26
_ = 27
_ = 28
_ = 29
_ = 30
_ = 31
_ = 32
_ = 33
_ = 34
_ = 35
_ = 36
_ = 37
_ = 38
_ = 39
_ = 40
_ = 41
_ = 42
_ = 43
_ = 44
_ = 45
_ = 46
_ = 47
_ = 48
_ = 49
_ = 50
_ = 51
_ = 52
_ = 53
_ = 54
_ = 55
_ = 56
_ = 57
_ = 58
_ = 59
_ = 60
_ = 61
}