Skip to content

Commit 1958124

Browse files
checkers: move out embedded rules from checkers.go (#1186)
Issue: #1177
1 parent 457ee31 commit 1958124

File tree

2 files changed

+103
-96
lines changed

2 files changed

+103
-96
lines changed

checkers/checkers.go

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
package checkers
33

44
import (
5-
"fmt"
6-
"go/ast"
7-
"go/build"
8-
"go/token"
95
"os"
106

11-
"github.com/go-critic/go-critic/checkers/rulesdata"
127
"github.com/go-critic/go-critic/framework/linter"
13-
"github.com/quasilyte/go-ruleguard/ruleguard"
148
)
159

1610
var collection = &linter.CheckerCollection{
@@ -23,93 +17,3 @@ var debug = func() func() bool {
2317
return v
2418
}
2519
}()
26-
27-
//go:generate go run ./rules/precompile.go -rules ./rules/rules.go -o ./rulesdata/rulesdata.go
28-
29-
func init() {
30-
filename := "rules/rules.go"
31-
32-
fset := token.NewFileSet()
33-
var groups []ruleguard.GoRuleGroup
34-
35-
var buildContext *build.Context
36-
37-
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""
38-
39-
// First we create an Engine to parse all rules.
40-
// We need it to get the structured info about our rules
41-
// that will be used to generate checkers.
42-
// We introduce an extra scope in hope that rootEngine
43-
// will be garbage-collected after we don't need it.
44-
// LoadedGroups() returns a slice copy and that's all what we need.
45-
{
46-
rootEngine := ruleguard.NewEngine()
47-
rootEngine.InferBuildContext()
48-
buildContext = rootEngine.BuildContext
49-
50-
loadContext := &ruleguard.LoadContext{
51-
Fset: fset,
52-
DebugImports: ruleguardDebug,
53-
DebugPrint: func(s string) {
54-
fmt.Println("debug:", s)
55-
},
56-
}
57-
if err := rootEngine.LoadFromIR(loadContext, filename, rulesdata.PrecompiledRules); err != nil {
58-
panic(fmt.Sprintf("load embedded ruleguard rules: %v", err))
59-
}
60-
groups = rootEngine.LoadedGroups()
61-
}
62-
63-
// For every rules group we create a new checker and a separate engine.
64-
// That dedicated ruleguard engine will contain rules only from one group.
65-
for i := range groups {
66-
g := groups[i]
67-
info := &linter.CheckerInfo{
68-
Name: g.Name,
69-
Summary: g.DocSummary,
70-
Before: g.DocBefore,
71-
After: g.DocAfter,
72-
Note: g.DocNote,
73-
Tags: g.DocTags,
74-
75-
EmbeddedRuleguard: true,
76-
}
77-
collection.AddChecker(info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
78-
parseContext := &ruleguard.LoadContext{
79-
Fset: fset,
80-
GroupFilter: func(name string) bool {
81-
return name == g.Name
82-
},
83-
DebugImports: ruleguardDebug,
84-
DebugPrint: func(s string) {
85-
fmt.Println("debug:", s)
86-
},
87-
}
88-
engine := ruleguard.NewEngine()
89-
engine.BuildContext = buildContext
90-
err := engine.LoadFromIR(parseContext, filename, rulesdata.PrecompiledRules)
91-
if err != nil {
92-
return nil, err
93-
}
94-
c := &embeddedRuleguardChecker{
95-
ctx: ctx,
96-
engine: engine,
97-
}
98-
return c, nil
99-
})
100-
}
101-
}
102-
103-
type embeddedRuleguardChecker struct {
104-
ctx *linter.CheckerContext
105-
engine *ruleguard.Engine
106-
}
107-
108-
func (c *embeddedRuleguardChecker) WalkFile(f *ast.File) {
109-
runRuleguardEngine(c.ctx, f, c.engine, &ruleguard.RunContext{
110-
Pkg: c.ctx.Pkg,
111-
Types: c.ctx.TypesInfo,
112-
Sizes: c.ctx.SizesInfo,
113-
Fset: c.ctx.FileSet,
114-
})
115-
}

checkers/embedded_rules.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package checkers
2+
3+
import (
4+
"fmt"
5+
"go/ast"
6+
"go/build"
7+
"go/token"
8+
"os"
9+
10+
"github.com/go-critic/go-critic/checkers/rulesdata"
11+
"github.com/go-critic/go-critic/framework/linter"
12+
"github.com/quasilyte/go-ruleguard/ruleguard"
13+
)
14+
15+
//go:generate go run ./rules/precompile.go -rules ./rules/rules.go -o ./rulesdata/rulesdata.go
16+
17+
func init() {
18+
filename := "rules/rules.go"
19+
20+
fset := token.NewFileSet()
21+
var groups []ruleguard.GoRuleGroup
22+
23+
var buildContext *build.Context
24+
25+
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""
26+
27+
// First we create an Engine to parse all rules.
28+
// We need it to get the structured info about our rules
29+
// that will be used to generate checkers.
30+
// We introduce an extra scope in hope that rootEngine
31+
// will be garbage-collected after we don't need it.
32+
// LoadedGroups() returns a slice copy and that's all what we need.
33+
{
34+
rootEngine := ruleguard.NewEngine()
35+
rootEngine.InferBuildContext()
36+
buildContext = rootEngine.BuildContext
37+
38+
loadContext := &ruleguard.LoadContext{
39+
Fset: fset,
40+
DebugImports: ruleguardDebug,
41+
DebugPrint: func(s string) {
42+
fmt.Println("debug:", s)
43+
},
44+
}
45+
if err := rootEngine.LoadFromIR(loadContext, filename, rulesdata.PrecompiledRules); err != nil {
46+
panic(fmt.Sprintf("load embedded ruleguard rules: %v", err))
47+
}
48+
groups = rootEngine.LoadedGroups()
49+
}
50+
51+
// For every rules group we create a new checker and a separate engine.
52+
// That dedicated ruleguard engine will contain rules only from one group.
53+
for i := range groups {
54+
g := groups[i]
55+
info := &linter.CheckerInfo{
56+
Name: g.Name,
57+
Summary: g.DocSummary,
58+
Before: g.DocBefore,
59+
After: g.DocAfter,
60+
Note: g.DocNote,
61+
Tags: g.DocTags,
62+
63+
EmbeddedRuleguard: true,
64+
}
65+
collection.AddChecker(info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
66+
parseContext := &ruleguard.LoadContext{
67+
Fset: fset,
68+
GroupFilter: func(name string) bool {
69+
return name == g.Name
70+
},
71+
DebugImports: ruleguardDebug,
72+
DebugPrint: func(s string) {
73+
fmt.Println("debug:", s)
74+
},
75+
}
76+
engine := ruleguard.NewEngine()
77+
engine.BuildContext = buildContext
78+
err := engine.LoadFromIR(parseContext, filename, rulesdata.PrecompiledRules)
79+
if err != nil {
80+
return nil, err
81+
}
82+
c := &embeddedRuleguardChecker{
83+
ctx: ctx,
84+
engine: engine,
85+
}
86+
return c, nil
87+
})
88+
}
89+
}
90+
91+
type embeddedRuleguardChecker struct {
92+
ctx *linter.CheckerContext
93+
engine *ruleguard.Engine
94+
}
95+
96+
func (c *embeddedRuleguardChecker) WalkFile(f *ast.File) {
97+
runRuleguardEngine(c.ctx, f, c.engine, &ruleguard.RunContext{
98+
Pkg: c.ctx.Pkg,
99+
Types: c.ctx.TypesInfo,
100+
Sizes: c.ctx.SizesInfo,
101+
Fset: c.ctx.FileSet,
102+
})
103+
}

0 commit comments

Comments
 (0)