Skip to content

Commit a36dc07

Browse files
feat: Add swaggo/swag formatter
1 parent 221803a commit a36dc07

File tree

15 files changed

+184
-5
lines changed

15 files changed

+184
-5
lines changed

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ require (
109109
github.com/ssgreg/nlreturn/v2 v2.2.1
110110
github.com/stbenjam/no-sprintf-host-port v0.2.0
111111
github.com/stretchr/testify v1.10.0
112+
github.com/swaggo/swag v1.16.4
112113
github.com/tdakkota/asciicheck v0.4.1
113114
github.com/tetafro/godot v1.5.0
114115
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67
@@ -138,7 +139,10 @@ require (
138139
)
139140

140141
require (
142+
github.com/KyleBanks/depth v1.2.1 // indirect
141143
github.com/Masterminds/semver/v3 v3.3.1 // indirect
144+
github.com/PuerkitoBio/purell v1.1.1 // indirect
145+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
142146
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
143147
github.com/beorn7/perks v1.0.1 // indirect
144148
github.com/ccojocar/zxcvbn-go v1.0.2 // indirect
@@ -156,6 +160,10 @@ require (
156160
github.com/fatih/structtag v1.2.0 // indirect
157161
github.com/fsnotify/fsnotify v1.5.4 // indirect
158162
github.com/go-ole/go-ole v1.2.6 // indirect
163+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
164+
github.com/go-openapi/jsonreference v0.19.6 // indirect
165+
github.com/go-openapi/spec v0.20.4 // indirect
166+
github.com/go-openapi/swag v0.19.15 // indirect
159167
github.com/go-toolsmith/astcast v1.1.0 // indirect
160168
github.com/go-toolsmith/astcopy v1.1.0 // indirect
161169
github.com/go-toolsmith/astequal v1.2.0 // indirect
@@ -173,9 +181,11 @@ require (
173181
github.com/hashicorp/hcl v1.0.0 // indirect
174182
github.com/hexops/gotextdiff v1.0.3 // indirect
175183
github.com/inconshreveable/mousetrap v1.1.0 // indirect
184+
github.com/josharian/intern v1.0.0 // indirect
176185
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
177186
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
178187
github.com/magiconair/properties v1.8.6 // indirect
188+
github.com/mailru/easyjson v0.7.6 // indirect
179189
github.com/mattn/go-isatty v0.0.20 // indirect
180190
github.com/mattn/go-runewidth v0.0.16 // indirect
181191
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
@@ -209,6 +219,7 @@ require (
209219
go.uber.org/zap v1.24.0 // indirect
210220
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
211221
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
222+
golang.org/x/net v0.39.0 // indirect
212223
golang.org/x/sync v0.13.0 // indirect
213224
golang.org/x/text v0.24.0 // indirect
214225
google.golang.org/protobuf v1.36.6 // indirect

go.sum

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@
836836
"gofmt",
837837
"gofumpt",
838838
"goimports",
839-
"golines"
839+
"golines",
840+
"swaggo"
840841
]
841842
},
842843
"settings": {

pkg/config/linters.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func (l *Linters) validateNoFormatters() error {
4949
}
5050

5151
func getAllFormatterNames() []string {
52-
return []string{"gci", "gofmt", "gofumpt", "goimports", "golines"}
52+
return []string{"gci", "gofmt", "gofumpt", "goimports", "golines", "swaggo"}
5353
}

pkg/goformatters/meta_formatter.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/golangci/golangci-lint/v2/pkg/goformatters/gofumpt"
1313
"github.com/golangci/golangci-lint/v2/pkg/goformatters/goimports"
1414
"github.com/golangci/golangci-lint/v2/pkg/goformatters/golines"
15+
"github.com/golangci/golangci-lint/v2/pkg/goformatters/swaggo"
1516
"github.com/golangci/golangci-lint/v2/pkg/logutils"
1617
)
1718

@@ -41,6 +42,10 @@ func NewMetaFormatter(log logutils.Log, cfg *config.Formatters, runCfg *config.R
4142
m.formatters = append(m.formatters, goimports.New(&cfg.Settings.GoImports))
4243
}
4344

45+
if slices.Contains(cfg.Enable, swaggo.Name) {
46+
m.formatters = append(m.formatters, swaggo.New())
47+
}
48+
4449
// gci is a last because the only goal of gci is to handle imports.
4550
if slices.Contains(cfg.Enable, gci.Name) {
4651
formatter, err := gci.New(&cfg.Settings.Gci)
@@ -86,5 +91,5 @@ func (m *MetaFormatter) Format(filename string, src []byte) []byte {
8691
}
8792

8893
func IsFormatter(name string) bool {
89-
return slices.Contains([]string{gofmt.Name, gofumpt.Name, goimports.Name, gci.Name, golines.Name}, name)
94+
return slices.Contains([]string{gofmt.Name, gofumpt.Name, goimports.Name, gci.Name, golines.Name, swaggo.Name}, name)
9095
}

pkg/goformatters/swaggo/swaggo.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package swaggo
2+
3+
import "github.com/swaggo/swag"
4+
5+
const Name = "swaggo"
6+
7+
type Formatter struct {
8+
formatter *swag.Formatter
9+
}
10+
11+
func New() *Formatter {
12+
return &Formatter{
13+
formatter: swag.NewFormatter(),
14+
}
15+
}
16+
17+
func (*Formatter) Name() string {
18+
return Name
19+
}
20+
21+
func (f *Formatter) Format(path string, src []byte) ([]byte, error) {
22+
return f.formatter.Format(path, src)
23+
}

pkg/golinters/swaggo/swaggo.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package swaggo
2+
3+
import (
4+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
5+
"github.com/golangci/golangci-lint/v2/pkg/goformatters"
6+
"github.com/golangci/golangci-lint/v2/pkg/goformatters/swaggo"
7+
"github.com/golangci/golangci-lint/v2/pkg/golinters/internal"
8+
"golang.org/x/tools/go/analysis"
9+
)
10+
11+
const linterName = "swaggo"
12+
13+
func New() *goanalysis.Linter {
14+
a := goformatters.NewAnalyzer(
15+
internal.LinterLogger.Child(linterName),
16+
"Checks if swaggo comments are formatted",
17+
swaggo.New(),
18+
)
19+
20+
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil).
21+
WithLoadMode(goanalysis.LoadModeSyntax)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package swaggo
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+
}
12+
13+
func TestFix(t *testing.T) {
14+
integration.RunFix(t)
15+
}
16+
17+
func TestFixPathPrefix(t *testing.T) {
18+
integration.RunFixPathPrefix(t)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//golangcitest:config_path testdata/swaggo.yml
2+
//golangcitest:expected_exitcode 0
3+
package api
4+
5+
import "net/http"
6+
7+
// @Summary Add a new pet to the store
8+
// @Description get string by ID
9+
// @ID get-string-by-int
10+
// @Accept json
11+
// @Produce json
12+
// @Param some_id path int true "Some ID" Format(int64)
13+
// @Param some_id body web.Pet true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//golangcitest:config_path testdata/swaggo.yml
2+
//golangcitest:expected_exitcode 0
3+
package api
4+
5+
import "net/http"
6+
7+
// @Summary Add a new pet to the store
8+
// @Description get string by ID
9+
// @ID get-string-by-int
10+
// @Accept json
11+
// @Produce json
12+
// @Param some_id path int true "Some ID" Format(int64)
13+
// @Param some_id body web.Pet true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//golangcitest:config_path testdata/swaggo.yml
2+
package api
3+
4+
import "net/http"
5+
6+
// want +1 "File is not properly formatted"
7+
// @Summary Add a new pet to the store
8+
// @Description get string by ID
9+
// @ID get-string-by-int
10+
// @Accept json
11+
// @Produce json
12+
// @Param some_id path int true "Some ID" Format(int64)
13+
// @Param some_id body web.Pet true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- swaggo

pkg/lint/lintersdb/builder_linter.go

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import (
9494
"github.com/golangci/golangci-lint/v2/pkg/golinters/spancheck"
9595
"github.com/golangci/golangci-lint/v2/pkg/golinters/sqlclosecheck"
9696
"github.com/golangci/golangci-lint/v2/pkg/golinters/staticcheck"
97+
"github.com/golangci/golangci-lint/v2/pkg/golinters/swaggo"
9798
"github.com/golangci/golangci-lint/v2/pkg/golinters/tagalign"
9899
"github.com/golangci/golangci-lint/v2/pkg/golinters/tagliatelle"
99100
"github.com/golangci/golangci-lint/v2/pkg/golinters/testableexamples"
@@ -581,6 +582,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
581582
WithAutoFix().
582583
WithURL("https://staticcheck.dev/"),
583584

585+
linter.NewConfig(swaggo.New()).
586+
WithSince("v2.2.0").
587+
WithAutoFix().
588+
WithURL("https://github.com/swaggo/swaggo"),
589+
584590
linter.NewConfig(tagalign.New(&cfg.Linters.Settings.TagAlign)).
585591
WithSince("v1.53.0").
586592
WithAutoFix().

0 commit comments

Comments
 (0)