Skip to content

Commit bdff4b9

Browse files
committed
Add pkgname linter
1 parent 6d9ab42 commit bdff4b9

File tree

13 files changed

+116
-0
lines changed

13 files changed

+116
-0
lines changed

.golangci.next.reference.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ linters:
9797
- nosprintfhostport
9898
- paralleltest
9999
- perfsprint
100+
- pkgname
100101
- prealloc
101102
- predeclared
102103
- promlinter
@@ -207,6 +208,7 @@ linters:
207208
- nosprintfhostport
208209
- paralleltest
209210
- perfsprint
211+
- pkgname
210212
- prealloc
211213
- predeclared
212214
- promlinter
@@ -2092,6 +2094,11 @@ linters:
20922094
# Default: true
20932095
hex-format: false
20942096

2097+
pkgname:
2098+
# Include import alias in checks.
2099+
# Default: false
2100+
import-alias: true
2101+
20952102
prealloc:
20962103
# IMPORTANT: we don't recommend using this linter before doing performance profiling.
20972104
# For most programs usage of prealloc will be a premature optimization.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ require (
121121
github.com/ultraware/whitespace v0.2.0
122122
github.com/uudashr/gocognit v1.2.0
123123
github.com/uudashr/iface v1.4.0
124+
github.com/uudashr/pkgname v1.0.0
124125
github.com/valyala/quicktemplate v1.8.0
125126
github.com/xen0n/gosmopolitan v1.3.0
126127
github.com/yagipy/maintidx v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,17 @@
15121512
}
15131513
}
15141514
},
1515+
"pkgnameSettings": {
1516+
"type": "object",
1517+
"additionalProperties": false,
1518+
"properties": {
1519+
"import-alias": {
1520+
"description": "Include import alias in the checks.",
1521+
"type": "boolean",
1522+
"default": false
1523+
}
1524+
}
1525+
},
15151526
"goconstSettings": {
15161527
"type": "object",
15171528
"additionalProperties": false,
@@ -4523,6 +4534,9 @@
45234534
"perfsprint": {
45244535
"$ref": "#/definitions/settings/definitions/perfsprintSettings"
45254536
},
4537+
"pkgname": {
4538+
"$ref": "#/definitions/settings/definitions/pkgnameSettings"
4539+
},
45264540
"prealloc": {
45274541
"$ref": "#/definitions/settings/definitions/preallocSettings"
45284542
},

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ type LintersSettings struct {
259259
NoNamedReturns NoNamedReturnsSettings `mapstructure:"nonamedreturns"`
260260
ParallelTest ParallelTestSettings `mapstructure:"paralleltest"`
261261
PerfSprint PerfSprintSettings `mapstructure:"perfsprint"`
262+
Pkgname PkgnameSettings `mapstructure:"pkgname"`
262263
Prealloc PreallocSettings `mapstructure:"prealloc"`
263264
Predeclared PredeclaredSettings `mapstructure:"predeclared"`
264265
Promlinter PromlinterSettings `mapstructure:"promlinter"`
@@ -738,6 +739,10 @@ type PerfSprintSettings struct {
738739
HexFormat bool `mapstructure:"hex-format"`
739740
}
740741

742+
type PkgnameSettings struct {
743+
ImportAlias bool `mapstructure:"import-alias"`
744+
}
745+
741746
type PreallocSettings struct {
742747
Simple bool `mapstructure:"simple"`
743748
RangeLoops bool `mapstructure:"range-loops"`

pkg/golinters/pkgname/pkgname.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pkgname
2+
3+
import (
4+
"github.com/golangci/golangci-lint/v2/pkg/config"
5+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
6+
"github.com/uudashr/pkgname"
7+
)
8+
9+
func New(settings *config.PkgnameSettings) *goanalysis.Linter {
10+
var cfg map[string]any
11+
12+
if settings != nil {
13+
cfg = map[string]any{
14+
"include-import-alias": settings.ImportAlias,
15+
}
16+
}
17+
18+
return goanalysis.
19+
NewLinterFromAnalyzer(pkgname.Analyzer).
20+
WithConfig(cfg).
21+
WithLoadMode(goanalysis.LoadModeTypesInfo)
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkgname
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Epkgname
2+
//golangcitest:config_path importalias.yml
3+
package hello
4+
5+
import (
6+
go_format "fmt" // want "found import 'fmt' with alias 'go_format', should not use under_score in package alias name"
7+
"log"
8+
structLog "log/slog" // want "found import 'log/slog' with alias 'structLog', should not use mixedCaps in package alias name"
9+
)
10+
11+
func Hello() {
12+
go_format.Println("Hello, World!")
13+
structLog.Info("Hello, World!")
14+
log.Println("Hello, World!")
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
pkgname:
6+
import-alias: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//golangcitest:args -Epkgname
2+
3+
package helloWorld // want "found package 'helloWorld', should not use mixedCaps in package name"

0 commit comments

Comments
 (0)