diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 66699d365084..109527f3063c 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -97,6 +97,7 @@ linters: - nosprintfhostport - paralleltest - perfsprint + - pkgname - prealloc - predeclared - promlinter @@ -207,6 +208,7 @@ linters: - nosprintfhostport - paralleltest - perfsprint + - pkgname - prealloc - predeclared - promlinter @@ -2092,6 +2094,11 @@ linters: # Default: true hex-format: false + pkgname: + # Include import alias in checks. + # Default: false + import-alias: true + prealloc: # IMPORTANT: we don't recommend using this linter before doing performance profiling. # For most programs usage of prealloc will be a premature optimization. diff --git a/go.mod b/go.mod index ef62abf3ad54..ac8de88d845e 100644 --- a/go.mod +++ b/go.mod @@ -121,6 +121,7 @@ require ( github.com/ultraware/whitespace v0.2.0 github.com/uudashr/gocognit v1.2.0 github.com/uudashr/iface v1.4.0 + github.com/uudashr/pkgname v1.0.0 github.com/valyala/quicktemplate v1.8.0 github.com/xen0n/gosmopolitan v1.3.0 github.com/yagipy/maintidx v1.0.0 diff --git a/go.sum b/go.sum index b92fdfee4b85..7b60e3a773d8 100644 --- a/go.sum +++ b/go.sum @@ -594,6 +594,8 @@ github.com/uudashr/gocognit v1.2.0 h1:3BU9aMr1xbhPlvJLSydKwdLN3tEUUrzPSSM8S4hDYR github.com/uudashr/gocognit v1.2.0/go.mod h1:k/DdKPI6XBZO1q7HgoV2juESI2/Ofj9AcHPZhBBdrTU= github.com/uudashr/iface v1.4.0 h1:ImZ+1oEJPXvjap7nK0md7gA9RRH7PMp4vliaLkJ2+cg= github.com/uudashr/iface v1.4.0/go.mod h1:i/H4cfRMPe0izticV8Yz0g6/zcsh5xXlvthrdh1kqcY= +github.com/uudashr/pkgname v1.0.0 h1:SupGD3U97Jr/TDGhXBOmNV7o4R/f/U0L3d2LLPfuqGQ= +github.com/uudashr/pkgname v1.0.0/go.mod h1:n74Zn6wf+7TkLnErNsB9Yeq18zhn84rtBzFR0xRbJoQ= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/quicktemplate v1.8.0 h1:zU0tjbIqTRgKQzFY1L42zq0qR3eh4WoQQdIdqCysW5k= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 31e7fc2ffa7a..097792ffcc01 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1512,6 +1512,17 @@ } } }, + "pkgnameSettings": { + "type": "object", + "additionalProperties": false, + "properties": { + "import-alias": { + "description": "Include import alias in the checks.", + "type": "boolean", + "default": false + } + } + }, "goconstSettings": { "type": "object", "additionalProperties": false, @@ -4523,6 +4534,9 @@ "perfsprint": { "$ref": "#/definitions/settings/definitions/perfsprintSettings" }, + "pkgname": { + "$ref": "#/definitions/settings/definitions/pkgnameSettings" + }, "prealloc": { "$ref": "#/definitions/settings/definitions/preallocSettings" }, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 7b3b6a12954a..7145bd5e9837 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -259,6 +259,7 @@ type LintersSettings struct { NoNamedReturns NoNamedReturnsSettings `mapstructure:"nonamedreturns"` ParallelTest ParallelTestSettings `mapstructure:"paralleltest"` PerfSprint PerfSprintSettings `mapstructure:"perfsprint"` + Pkgname PkgnameSettings `mapstructure:"pkgname"` Prealloc PreallocSettings `mapstructure:"prealloc"` Predeclared PredeclaredSettings `mapstructure:"predeclared"` Promlinter PromlinterSettings `mapstructure:"promlinter"` @@ -738,6 +739,10 @@ type PerfSprintSettings struct { HexFormat bool `mapstructure:"hex-format"` } +type PkgnameSettings struct { + ImportAlias bool `mapstructure:"import-alias"` +} + type PreallocSettings struct { Simple bool `mapstructure:"simple"` RangeLoops bool `mapstructure:"range-loops"` diff --git a/pkg/golinters/pkgname/pkgname.go b/pkg/golinters/pkgname/pkgname.go new file mode 100644 index 000000000000..9765bd8dbcc9 --- /dev/null +++ b/pkg/golinters/pkgname/pkgname.go @@ -0,0 +1,22 @@ +package pkgname + +import ( + "github.com/golangci/golangci-lint/v2/pkg/config" + "github.com/golangci/golangci-lint/v2/pkg/goanalysis" + "github.com/uudashr/pkgname" +) + +func New(settings *config.PkgnameSettings) *goanalysis.Linter { + var cfg map[string]any + + if settings != nil { + cfg = map[string]any{ + "include-import-alias": settings.ImportAlias, + } + } + + return goanalysis. + NewLinterFromAnalyzer(pkgname.Analyzer). + WithConfig(cfg). + WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/golinters/pkgname/pkgname_integration_test.go b/pkg/golinters/pkgname/pkgname_integration_test.go new file mode 100644 index 000000000000..40dcb88ca6b0 --- /dev/null +++ b/pkg/golinters/pkgname/pkgname_integration_test.go @@ -0,0 +1,11 @@ +package pkgname + +import ( + "testing" + + "github.com/golangci/golangci-lint/v2/test/testshared/integration" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} diff --git a/pkg/golinters/pkgname/testdata/importalias.go b/pkg/golinters/pkgname/testdata/importalias.go new file mode 100644 index 000000000000..034b518b3cf4 --- /dev/null +++ b/pkg/golinters/pkgname/testdata/importalias.go @@ -0,0 +1,15 @@ +//golangcitest:args -Epkgname +//golangcitest:config_path importalias.yml +package hello + +import ( + go_format "fmt" // want "found import 'fmt' with alias 'go_format', should not use under_score in package alias name" + "log" + structLog "log/slog" // want "found import 'log/slog' with alias 'structLog', should not use mixedCaps in package alias name" +) + +func Hello() { + go_format.Println("Hello, World!") + structLog.Info("Hello, World!") + log.Println("Hello, World!") +} diff --git a/pkg/golinters/pkgname/testdata/importalias.yml b/pkg/golinters/pkgname/testdata/importalias.yml new file mode 100644 index 000000000000..b2a7a064a7f7 --- /dev/null +++ b/pkg/golinters/pkgname/testdata/importalias.yml @@ -0,0 +1,6 @@ +version: "2" + +linters: + settings: + pkgname: + import-alias: true diff --git a/pkg/golinters/pkgname/testdata/mixedcaps.go b/pkg/golinters/pkgname/testdata/mixedcaps.go new file mode 100644 index 000000000000..0d3167ad91f5 --- /dev/null +++ b/pkg/golinters/pkgname/testdata/mixedcaps.go @@ -0,0 +1,3 @@ +//golangcitest:args -Epkgname + +package helloWorld // want "found package 'helloWorld', should not use mixedCaps in package name" diff --git a/pkg/golinters/pkgname/testdata/underscore.go b/pkg/golinters/pkgname/testdata/underscore.go new file mode 100644 index 000000000000..3ee2f679de91 --- /dev/null +++ b/pkg/golinters/pkgname/testdata/underscore.go @@ -0,0 +1,2 @@ +//golangcitest:args -Epkgname +package hello_world // want "found package 'hello_world', should not use under_score in package name" diff --git a/pkg/golinters/pkgname/testdata/underscore_cgo.go b/pkg/golinters/pkgname/testdata/underscore_cgo.go new file mode 100644 index 000000000000..0c59da539740 --- /dev/null +++ b/pkg/golinters/pkgname/testdata/underscore_cgo.go @@ -0,0 +1,22 @@ +//golangcitest:args -Epkgname +package hello_world // want "found package 'hello_world', should not use under_score in package name" + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import ( + "unsafe" +) + +func _() { + cs := C.CString("Hello from stdio\n") + C.myprint(cs) + C.free(unsafe.Pointer(cs)) +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 734aab63cd1c..36be71244024 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -85,6 +85,7 @@ import ( "github.com/golangci/golangci-lint/v2/pkg/golinters/nosprintfhostport" "github.com/golangci/golangci-lint/v2/pkg/golinters/paralleltest" "github.com/golangci/golangci-lint/v2/pkg/golinters/perfsprint" + "github.com/golangci/golangci-lint/v2/pkg/golinters/pkgname" "github.com/golangci/golangci-lint/v2/pkg/golinters/prealloc" "github.com/golangci/golangci-lint/v2/pkg/golinters/predeclared" "github.com/golangci/golangci-lint/v2/pkg/golinters/promlinter" @@ -537,6 +538,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithAutoFix(). WithURL("https://github.com/catenacyber/perfsprint"), + linter.NewConfig(pkgname.New(&cfg.Linters.Settings.Pkgname)). + WithSince("v2.2.0"). + WithLoadForGoAnalysis(). + WithURL("https://github.com/uudashr/pkgname"), + linter.NewConfig(prealloc.New(&cfg.Linters.Settings.Prealloc)). WithSince("v1.19.0"). WithURL("https://github.com/alexkohler/prealloc"),