forked from go-critic/go-critic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ignore_labeled_continue_in_select' of github.com:tony20…
…01/go-critic into ignore_labeled_continue_in_select * 'ignore_labeled_continue_in_select' of github.com:tony2001/go-critic: checkers: move out embedded rules from checkers.go (go-critic#1186) cmd/gocritic: add -cpuprofile and -memprofile flags (go-critic#1185) checker/whyNoLint: compile regex when it is needed (go-critic#1182) checkers: add enable/disable flags to ruleguard checker (go-critic#1181) checkers: add io.WriteString patterns to preferFprint (go-critic#1180) checkers: fix equalFold rules autofix templates (go-critic#1178) checkers: rename undefinedFormatting to dynamicFmtString (go-critic#1174) checkers: add dynamicFmtString checker (go-critic#1156) update ruleguard (go-critic#1173) checkers: add tests for go-critic#1166 (go-critic#1172) checkers): treat //noinspection as pragma in commentFormatting (go-critic#1166) checkers: add At() for httpNoBody checker (go-critic#1164) tools: move tools into own module (go-critic#1162)
- Loading branch information
Showing
23 changed files
with
998 additions
and
680 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package checkers | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/build" | ||
"go/token" | ||
"os" | ||
|
||
"github.com/go-critic/go-critic/checkers/rulesdata" | ||
"github.com/go-critic/go-critic/framework/linter" | ||
"github.com/quasilyte/go-ruleguard/ruleguard" | ||
) | ||
|
||
//go:generate go run ./rules/precompile.go -rules ./rules/rules.go -o ./rulesdata/rulesdata.go | ||
|
||
func init() { | ||
filename := "rules/rules.go" | ||
|
||
fset := token.NewFileSet() | ||
var groups []ruleguard.GoRuleGroup | ||
|
||
var buildContext *build.Context | ||
|
||
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != "" | ||
|
||
// First we create an Engine to parse all rules. | ||
// We need it to get the structured info about our rules | ||
// that will be used to generate checkers. | ||
// We introduce an extra scope in hope that rootEngine | ||
// will be garbage-collected after we don't need it. | ||
// LoadedGroups() returns a slice copy and that's all what we need. | ||
{ | ||
rootEngine := ruleguard.NewEngine() | ||
rootEngine.InferBuildContext() | ||
buildContext = rootEngine.BuildContext | ||
|
||
loadContext := &ruleguard.LoadContext{ | ||
Fset: fset, | ||
DebugImports: ruleguardDebug, | ||
DebugPrint: func(s string) { | ||
fmt.Println("debug:", s) | ||
}, | ||
} | ||
if err := rootEngine.LoadFromIR(loadContext, filename, rulesdata.PrecompiledRules); err != nil { | ||
panic(fmt.Sprintf("load embedded ruleguard rules: %v", err)) | ||
} | ||
groups = rootEngine.LoadedGroups() | ||
} | ||
|
||
// For every rules group we create a new checker and a separate engine. | ||
// That dedicated ruleguard engine will contain rules only from one group. | ||
for i := range groups { | ||
g := groups[i] | ||
info := &linter.CheckerInfo{ | ||
Name: g.Name, | ||
Summary: g.DocSummary, | ||
Before: g.DocBefore, | ||
After: g.DocAfter, | ||
Note: g.DocNote, | ||
Tags: g.DocTags, | ||
|
||
EmbeddedRuleguard: true, | ||
} | ||
collection.AddChecker(info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) { | ||
parseContext := &ruleguard.LoadContext{ | ||
Fset: fset, | ||
GroupFilter: func(name string) bool { | ||
return name == g.Name | ||
}, | ||
DebugImports: ruleguardDebug, | ||
DebugPrint: func(s string) { | ||
fmt.Println("debug:", s) | ||
}, | ||
} | ||
engine := ruleguard.NewEngine() | ||
engine.BuildContext = buildContext | ||
err := engine.LoadFromIR(parseContext, filename, rulesdata.PrecompiledRules) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := &embeddedRuleguardChecker{ | ||
ctx: ctx, | ||
engine: engine, | ||
} | ||
return c, nil | ||
}) | ||
} | ||
} | ||
|
||
type embeddedRuleguardChecker struct { | ||
ctx *linter.CheckerContext | ||
engine *ruleguard.Engine | ||
} | ||
|
||
func (c *embeddedRuleguardChecker) WalkFile(f *ast.File) { | ||
runRuleguardEngine(c.ctx, f, c.engine, &ruleguard.RunContext{ | ||
Pkg: c.ctx.Pkg, | ||
Types: c.ctx.TypesInfo, | ||
Sizes: c.ctx.SizesInfo, | ||
Fset: c.ctx.FileSet, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.