2
2
package checkers
3
3
4
4
import (
5
- "fmt"
6
- "go/ast"
7
- "go/build"
8
- "go/token"
9
5
"os"
10
6
11
- "github.com/go-critic/go-critic/checkers/rulesdata"
12
7
"github.com/go-critic/go-critic/framework/linter"
13
- "github.com/quasilyte/go-ruleguard/ruleguard"
14
8
)
15
9
16
10
var collection = & linter.CheckerCollection {
@@ -23,93 +17,3 @@ var debug = func() func() bool {
23
17
return v
24
18
}
25
19
}()
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
- }
0 commit comments