Skip to content

feat: add validjson linter #3530

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
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
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ linters:
- unparam
- unused
- usestdlibvars
- validjson
- varcheck
- varnamelen
- wastedassign
Expand Down Expand Up @@ -2200,6 +2201,7 @@ linters:
- unparam
- unused
- usestdlibvars
- validjson
- varcheck
- varnamelen
- wastedassign
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/maratori/testableexamples v1.0.0
github.com/maratori/testpackage v1.1.0
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
github.com/matthewloring/validjson v0.2.0
github.com/mattn/go-colorable v0.1.13
github.com/mbilski/exhaustivestruct v1.2.0
github.com/mgechev/revive v1.2.5
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/validjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/matthewloring/validjson"
"golang.org/x/tools/go/analysis"

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

func NewValidJSON() *goanalysis.Linter {
a := validjson.Analyzer

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

linter.NewConfig(golinters.NewValidJSON()).
WithSince("v1.51.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/matthewloring/validjson"),

linter.NewConfig(golinters.NewVarcheck(varcheckCfg)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
Expand Down
42 changes: 42 additions & 0 deletions test/testdata/validjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//golangcitest:args -Evalidjson
package testdata

type Key struct {
A, B string
}

type StrAlias string

type ChanAlias chan<- int

type Textable [2]byte

func (Textable) MarshalText() ([]byte, error) {
return nil, nil
}

func (*Textable) UnmarshalText(_ []byte) error {
return nil
}

type ValidJsonTest struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should change the name?

Suggested change
type ValidJsonTest struct {
type ValidJSONTest struct {

A int `json:"A"`
B int `json:"B,omitempty"`
C int `json:",omitempty"`
D int `json:"-"`
E chan<- int `json:"E"` // want "struct field has json tag but non-serializable type chan<- int"
F chan<- int `json:"-"`
G chan<- int `json:"-,"` // want "struct field has json tag but non-serializable type chan<- int"
H ChanAlias `json:"H"` // want "struct field has json tag but non-serializable type command-line-arguments.ChanAlias"
I map[int]int `json:"I"`
J map[int64]int `json:"J"`
K map[Key]int `json:"K"` // want "struct field has json tag but non-serializable type"
L map[struct{ A, B string }]int `json:"L"` // want "struct field has json tag but non-serializable type"
M map[string]int `json:"M"`
N map[StrAlias]int `json:"N"`
O func() `json:"O"` // want "struct field has json tag but non-serializable type func()"
P complex64 `json:"P"` // want "struct field has json tag but non-serializable type complex64"
Q complex128 `json:"Q"` // want "struct field has json tag but non-serializable type complex128"
R StrAlias `json:"R"`
S map[Textable]int `json:"S"`
}