Skip to content

Add pkgname linter #5899

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ linters:
- nosprintfhostport
- paralleltest
- perfsprint
- pkgname
- prealloc
- predeclared
- promlinter
Expand Down Expand Up @@ -207,6 +208,7 @@ linters:
- nosprintfhostport
- paralleltest
- perfsprint
- pkgname
- prealloc
- predeclared
- promlinter
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

14 changes: 14 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -4523,6 +4534,9 @@
"perfsprint": {
"$ref": "#/definitions/settings/definitions/perfsprintSettings"
},
"pkgname": {
"$ref": "#/definitions/settings/definitions/pkgnameSettings"
},
"prealloc": {
"$ref": "#/definitions/settings/definitions/preallocSettings"
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
22 changes: 22 additions & 0 deletions pkg/golinters/pkgname/pkgname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pkgname

import (

Check failure on line 3 in pkg/golinters/pkgname/pkgname.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (goimports)

Check failure on line 3 in pkg/golinters/pkgname/pkgname.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (goimports)

Check failure on line 3 in pkg/golinters/pkgname/pkgname.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (goimports)
"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)
}
11 changes: 11 additions & 0 deletions pkg/golinters/pkgname/pkgname_integration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions pkg/golinters/pkgname/testdata/importalias.go
Original file line number Diff line number Diff line change
@@ -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!")
}
6 changes: 6 additions & 0 deletions pkg/golinters/pkgname/testdata/importalias.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

linters:
settings:
pkgname:
import-alias: true
3 changes: 3 additions & 0 deletions pkg/golinters/pkgname/testdata/mixedcaps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//golangcitest:args -Epkgname

package helloWorld // want "found package 'helloWorld', should not use mixedCaps in package name"
2 changes: 2 additions & 0 deletions pkg/golinters/pkgname/testdata/underscore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//golangcitest:args -Epkgname
package hello_world // want "found package 'hello_world', should not use under_score in package name"
22 changes: 22 additions & 0 deletions pkg/golinters/pkgname/testdata/underscore_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//golangcitest:args -Epkgname
package hello_world // want "found package 'hello_world', should not use under_score in package name"

/*
#include <stdio.h>
#include <stdlib.h>

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))
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"),
Expand Down
Loading