Skip to content

Commit

Permalink
feat(linters): add new linter canonicalheader.
Browse files Browse the repository at this point in the history
Canonicalheader checks http header via http.CanonicalHeaderKey().
  • Loading branch information
lasiar authored and Роман Чалый committed Dec 7, 2023
1 parent 84442f2 commit 5e8074f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,7 @@ linters:
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- cyclop
Expand Down Expand Up @@ -2443,6 +2444,7 @@ linters:
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- cyclop
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ require (
github.com/kulti/thelper v0.6.3
github.com/kunwardeep/paralleltest v1.0.8
github.com/kyoh86/exportloopref v0.1.11
github.com/lasiar/canonicalheader v1.0.4
github.com/ldez/gomoddirectives v0.2.3
github.com/ldez/tagliatelle v0.5.0
github.com/leonklingele/grouper v1.1.1
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.

19 changes: 19 additions & 0 deletions pkg/golinters/canonicalheader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/lasiar/canonicalheader"
"golang.org/x/tools/go/analysis"

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

func NewCanonicalheder() *goanalysis.Linter {
a := canonicalheader.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{canonicalheader.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetPerformance, linter.PresetBugs).
WithURL("https://github.com/timakin/bodyclose"),

linter.NewConfig(golinters.NewCanonicalheder()).
WithSince("v1.56.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/lasiar/canonicalHeader"),

linter.NewConfig(golinters.NewContainedCtx()).
WithSince("1.44.0").
WithLoadForGoAnalysis().
Expand Down
19 changes: 19 additions & 0 deletions test/testdata/canonicalheader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//golangcitest:args -Ecanonicalheader
package testdata

import "net/http"

func canonicalheader() {
v := http.Header{}

v.Get("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
v.Set("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
v.Add("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
v.Del("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
v.Values("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`

v.Set("Test-Header", "value")
v.Add("Test-Header", "value")
v.Del("Test-Header")
v.Values("Test-Header")
}

0 comments on commit 5e8074f

Please sign in to comment.