Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add funcorder linter #5630

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
033e7be
adding gofuncor linter
manuelarte Mar 22, 2025
d01e5ac
Merge branch 'golangci:main' into gofuncor
manuelarte Mar 25, 2025
b89e1bd
newlinter: Adding gofuncor linter
manuelarte Mar 25, 2025
59190f5
newlinter: updating .golangci.next.reference.yml
manuelarte Mar 26, 2025
74ca9fe
newlinter: updating to next minor version
manuelarte Mar 26, 2025
9b578c6
Merge branch 'main' into gofuncor
manuelarte Mar 26, 2025
1bf0125
newlinter: fixing the last checks
manuelarte Mar 26, 2025
47e23e5
newlinter: applying reviewer's comments
manuelarte Mar 26, 2025
03984f5
Merge branch 'main' into gofuncor
manuelarte Mar 26, 2025
455d8ee
newlinter: fixing lint
manuelarte Mar 26, 2025
5d1fb67
newlinter: adding gofuncor integration test
manuelarte Mar 26, 2025
944a3f6
newlinter: adding gofuncor integration test
manuelarte Mar 26, 2025
778ccb6
Merge branch 'main' into gofuncor
manuelarte Mar 26, 2025
79d5f8f
newlinter: applying comments
manuelarte Mar 26, 2025
84a4968
newlinter: using new release v0.1.1
manuelarte Mar 26, 2025
4c6bfa3
newlinter: renaming gofuncor to funcorder
manuelarte Mar 26, 2025
304d72b
Merge branch 'golangci:main' into gofuncor
manuelarte Mar 28, 2025
59de295
newlinter: adding funcorder settings
manuelarte Mar 28, 2025
26c7b00
newlinter: using settings exported constants
manuelarte Mar 28, 2025
616d824
newlinter: fmt
manuelarte Mar 28, 2025
a427d22
newlinter: fixing settings check
manuelarte Mar 28, 2025
b08537c
Merge branch 'main' into gofuncor
manuelarte Mar 29, 2025
6603c25
adding final '.' at the end of the comments
manuelarte Mar 29, 2025
7e61de8
Merge branch 'main' into gofuncor
manuelarte Mar 30, 2025
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
8 changes: 8 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ linters:
- fatcontext
- forbidigo
- forcetypeassert
- funcorder
- funlen
- ginkgolinter
- gocheckcompilerdirectives
Expand Down Expand Up @@ -150,6 +151,7 @@ linters:
- fatcontext
- forbidigo
- forcetypeassert
- funcorder
- funlen
- ginkgolinter
- gocheckcompilerdirectives
Expand Down Expand Up @@ -529,6 +531,12 @@ linters:
# Default: false
analyze-types: true

funcorder:
# Enable/disable feature to check constructors are placed after struct declaration
constructor: true
# Enable/disable feature to check whether the exported struct's methods are placed before the non-exported
struct-method: true

funlen:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/ldez/usetesting v0.4.2
github.com/leonklingele/grouper v1.1.2
github.com/macabu/inamedparam v0.2.0
github.com/manuelarte/funcorder v0.2.0
github.com/maratori/testableexamples v1.0.0
github.com/maratori/testpackage v1.1.1
github.com/matoous/godox v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type LintersSettings struct {
Exhaustruct ExhaustructSettings `mapstructure:"exhaustruct"`
Fatcontext FatcontextSettings `mapstructure:"fatcontext"`
Forbidigo ForbidigoSettings `mapstructure:"forbidigo"`
FuncOrder FuncOrderSettings `mapstructure:"funcorder"`
Funlen FunlenSettings `mapstructure:"funlen"`
GinkgoLinter GinkgoLinterSettings `mapstructure:"ginkgolinter"`
Gocognit GocognitSettings `mapstructure:"gocognit"`
Expand Down Expand Up @@ -420,6 +421,11 @@ type ForbidigoPattern struct {
Msg string `yaml:"msg,omitempty" mapstructure:"msg,omitempty"`
}

type FuncOrderSettings struct {
Constructor *bool `mapstructure:"constructor,omitempty"`
StructMethod *bool `mapstructure:"struct-method,omitempty"`
}

type FunlenSettings struct {
Lines int `mapstructure:"lines"`
Statements int `mapstructure:"statements"`
Expand Down
39 changes: 39 additions & 0 deletions pkg/golinters/funcorder/funcorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package funcorder

import (
"github.com/manuelarte/funcorder/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/v2/pkg/config"

"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
)

func New(settings *config.FuncOrderSettings) *goanalysis.Linter {
a := analyzer.NewAnalyzer()

cfg := map[string]map[string]any{}

if settings != nil {
constructor := true
if settings.Constructor != nil {
constructor = *settings.Constructor
}
structMethod := true
if settings.StructMethod != nil {
structMethod = *settings.StructMethod
}

cfg[a.Name] = map[string]any{
"constructor": constructor,
"struct-method": structMethod,
}
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
11 changes: 11 additions & 0 deletions pkg/golinters/funcorder/funcorder_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package funcorder

import (
"testing"

"github.com/golangci/golangci-lint/v2/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
40 changes: 40 additions & 0 deletions pkg/golinters/funcorder/testdata/funcorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//golangcitest:args -Efuncorder
package testdata

import "time"

type MyStruct struct {
Name string
}

func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "SetName"`
return len(m.Name)
}

func (m MyStruct) GetName() string {
return m.Name
}

func (m *MyStruct) SetName(name string) {
m.Name = name
}

type MyStruct2 struct {
Name string
}

func (m MyStruct2) GetName() string {
return m.Name
}

func (m *MyStruct2) SetName(name string) {
m.Name = name
}

func NewMyStruct2() *MyStruct2 { // want `constructor "NewMyStruct2" for struct "MyStruct2" should be placed before struct method "GetName"`
return &MyStruct2{Name: "John"}
}

func NewTime() time.Time {
return time.Now()
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/fatcontext"
"github.com/golangci/golangci-lint/v2/pkg/golinters/forbidigo"
"github.com/golangci/golangci-lint/v2/pkg/golinters/forcetypeassert"
"github.com/golangci/golangci-lint/v2/pkg/golinters/funcorder"
"github.com/golangci/golangci-lint/v2/pkg/golinters/funlen"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gci"
"github.com/golangci/golangci-lint/v2/pkg/golinters/ginkgolinter"
Expand Down Expand Up @@ -254,6 +255,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),

linter.NewConfig(funcorder.New(&cfg.Linters.Settings.FuncOrder)).
WithSince("v2.1.0").
WithURL("https://github.com/manuelarte/funcorder"),

linter.NewConfig(fatcontext.New(&cfg.Linters.Settings.Fatcontext)).
WithSince("v1.58.0").
WithLoadForGoAnalysis().
Expand Down