Skip to content

Commit 49eceac

Browse files
committed
feat: adding embeddedcheck linter
1 parent 2f6a2f4 commit 49eceac

File tree

9 files changed

+120
-0
lines changed

9 files changed

+120
-0
lines changed

.golangci.next.reference.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ linters:
3232
- dupl
3333
- dupword
3434
- durationcheck
35+
- embeddedcheck
3536
- err113
3637
- errcheck
3738
- errchkjson
@@ -140,6 +141,7 @@ linters:
140141
- dupl
141142
- dupword
142143
- durationcheck
144+
- embeddedcheck
143145
- err113
144146
- errcheck
145147
- errchkjson

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ require (
176176
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
177177
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
178178
github.com/magiconair/properties v1.8.6 // indirect
179+
github.com/manuelarte/embeddedcheck v0.1.0 // indirect
179180
github.com/mattn/go-isatty v0.0.20 // indirect
180181
github.com/mattn/go-runewidth v0.0.16 // indirect
181182
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package embeddedcheck
2+
3+
import (
4+
"github.com/manuelarte/embeddedcheck/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
8+
)
9+
10+
func New() *goanalysis.Linter {
11+
a := analyzer.NewAnalyzer()
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package embeddedcheck
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//golangcitest:args -Eembeddedcheck
2+
package simple
3+
4+
import "time"
5+
6+
type ValidStructWithSingleLineComments struct {
7+
// time.Time Single line comment
8+
time.Time
9+
10+
// version Single line comment
11+
version int
12+
}
13+
14+
type StructWithSingleLineComments struct {
15+
// time.Time Single line comment
16+
time.Time // want `there must be an empty line separating embedded fields from regular fields`
17+
// version Single line comment
18+
version int
19+
}
20+
21+
type StructWithMultiLineComments struct {
22+
// time.Time Single line comment
23+
time.Time // want `there must be an empty line separating embedded fields from regular fields`
24+
// version Single line comment
25+
// very long comment
26+
version int
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//golangcitest:args -Eembeddedcheck
2+
package simple
3+
4+
import (
5+
"context"
6+
"time"
7+
)
8+
9+
type ValidStruct struct {
10+
time.Time
11+
12+
version int
13+
}
14+
15+
type NoSpaceStruct struct {
16+
time.Time // want `there must be an empty line separating embedded fields from regular fields`
17+
version int
18+
}
19+
20+
type NotSortedStruct struct {
21+
version int
22+
23+
time.Time // want `embedded types should be listed before non embedded types`
24+
}
25+
26+
type MixedEmbeddedAndNotEmbedded struct {
27+
context.Context
28+
29+
name string
30+
31+
time.Time // want `embedded types should be listed before non embedded types`
32+
33+
age int
34+
}
35+
36+
type EmbeddedWithPointers struct {
37+
*time.Time // want `there must be an empty line separating embedded fields from regular fields`
38+
version int
39+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//golangcitest:args -Eembeddedcheck
2+
package simple
3+
4+
import "time"
5+
6+
func myFunction() {
7+
type myType struct {
8+
version int
9+
time.Time // want `embedded types should be listed before non embedded types`
10+
}
11+
}

pkg/lint/lintersdb/builder_linter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/golangci/golangci-lint/v2/pkg/golinters/dupl"
1919
"github.com/golangci/golangci-lint/v2/pkg/golinters/dupword"
2020
"github.com/golangci/golangci-lint/v2/pkg/golinters/durationcheck"
21+
"github.com/golangci/golangci-lint/v2/pkg/golinters/embeddedcheck"
2122
"github.com/golangci/golangci-lint/v2/pkg/golinters/err113"
2223
"github.com/golangci/golangci-lint/v2/pkg/golinters/errcheck"
2324
"github.com/golangci/golangci-lint/v2/pkg/golinters/errchkjson"
@@ -205,6 +206,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
205206
WithLoadForGoAnalysis().
206207
WithURL("https://github.com/charithe/durationcheck"),
207208

209+
linter.NewConfig(embeddedcheck.New()).
210+
WithSince("v2.2.0").
211+
WithLoadForGoAnalysis().
212+
WithURL("https://github.com/manuelarte/embeddedcheck"),
213+
208214
linter.NewConfig(errcheck.New(&cfg.Linters.Settings.Errcheck)).
209215
WithGroups(config.GroupStandard).
210216
WithSince("v1.0.0").

0 commit comments

Comments
 (0)