mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-04 04:23:02 +00:00
i dont like commits
This commit is contained in:
@@ -239,7 +239,7 @@ func (d *SingleUseDetector) getFuncLOC(file string, startLine int) (int, error)
|
||||
fset := token.NewFileSet()
|
||||
node, err := parser.ParseFile(fset, file, nil, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("parse %s for function loc lookup: %w", file, err)
|
||||
}
|
||||
|
||||
loc := 0
|
||||
|
||||
@@ -43,6 +43,21 @@ func (d *LargeFileDetector) Detect(ctx context.Context, path string, config *qua
|
||||
for _, file := range files {
|
||||
loc, err := countLines(file)
|
||||
if err != nil {
|
||||
findings = append(findings, quality.Finding{
|
||||
ID: fmt.Sprintf("detector_read_error::large_file::%s", file),
|
||||
Type: "detector_error",
|
||||
Title: "Large file detector could not read file",
|
||||
Description: fmt.Sprintf("Failed to count lines in %s: %v", filepath.Base(file), err),
|
||||
File: file,
|
||||
Line: 1,
|
||||
Severity: quality.SeverityT2,
|
||||
Score: 0,
|
||||
Status: quality.StatusOpen,
|
||||
Metadata: map[string]string{
|
||||
"detector": "large_file",
|
||||
"error": err.Error(),
|
||||
},
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -99,18 +114,21 @@ func (d *GodStructDetector) Detect(ctx context.Context, path string, config *qua
|
||||
|
||||
var findings []quality.Finding
|
||||
for _, file := range files {
|
||||
fileFindings := d.analyzeFile(file)
|
||||
fileFindings, err := d.analyzeFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("analyze god struct in %q: %w", file, err)
|
||||
}
|
||||
findings = append(findings, fileFindings...)
|
||||
}
|
||||
|
||||
return findings, nil
|
||||
}
|
||||
|
||||
func (d *GodStructDetector) analyzeFile(path string) []quality.Finding {
|
||||
func (d *GodStructDetector) analyzeFile(path string) ([]quality.Finding, error) {
|
||||
fset := token.NewFileSet()
|
||||
node, err := parser.ParseFile(fset, path, nil, 0)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
|
||||
methodCounts := make(map[string]int)
|
||||
@@ -198,7 +216,7 @@ func (d *GodStructDetector) analyzeFile(path string) []quality.Finding {
|
||||
}
|
||||
}
|
||||
|
||||
return findings
|
||||
return findings, nil
|
||||
}
|
||||
|
||||
type DebugLogDetector struct {
|
||||
@@ -227,22 +245,25 @@ func (d *DebugLogDetector) Detect(ctx context.Context, path string, config *qual
|
||||
|
||||
var findings []quality.Finding
|
||||
for _, file := range files {
|
||||
fileFindings := d.analyzeFile(file)
|
||||
fileFindings, err := d.analyzeFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("analyze debug logs in %q: %w", file, err)
|
||||
}
|
||||
findings = append(findings, fileFindings...)
|
||||
}
|
||||
|
||||
return findings, nil
|
||||
}
|
||||
|
||||
func (d *DebugLogDetector) analyzeFile(path string) []quality.Finding {
|
||||
func (d *DebugLogDetector) analyzeFile(path string) ([]quality.Finding, error) {
|
||||
fset := token.NewFileSet()
|
||||
node, err := parser.ParseFile(fset, path, nil, 0)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
normPath := filepath.ToSlash(path)
|
||||
if strings.Contains(normPath, "internal/ui/") || strings.Contains(normPath, "examples/") {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
debugPatterns := []string{
|
||||
@@ -324,7 +345,7 @@ func (d *DebugLogDetector) analyzeFile(path string) []quality.Finding {
|
||||
return true
|
||||
})
|
||||
|
||||
return findings
|
||||
return findings, nil
|
||||
}
|
||||
|
||||
type GodFunctionDetector struct {
|
||||
|
||||
@@ -37,7 +37,7 @@ func (d *TestCoverageDetector) Detect(ctx context.Context, path string, config *
|
||||
|
||||
_, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("go toolchain is not available: %w", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(coverFile); os.IsNotExist(err) {
|
||||
@@ -48,13 +48,13 @@ func (d *TestCoverageDetector) Detect(ctx context.Context, path string, config *
|
||||
}
|
||||
|
||||
if _, err := os.Stat(coverFile); os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("coverage profile was not generated at %q", coverFile)
|
||||
}
|
||||
}
|
||||
|
||||
coverage, err := d.parseCoverageFile(coverFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("parse coverage profile %q: %w", coverFile, err)
|
||||
}
|
||||
|
||||
var findings []quality.Finding
|
||||
@@ -210,7 +210,7 @@ func (d *UntestedFuncDetector) Detect(ctx context.Context, path string, config *
|
||||
coverFile := filepath.Join(path, "coverage.out")
|
||||
data, err := os.ReadFile(coverFile)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("read coverage profile %q: %w", coverFile, err)
|
||||
}
|
||||
|
||||
uncoveredFuncs := make(map[string][]UncoveredFunc)
|
||||
|
||||
@@ -82,8 +82,12 @@ func (p *GoPlugin) AnalyzeFile(ctx context.Context, path string, config *quality
|
||||
analysis := &plugins.FileAnalysis{
|
||||
Path: path,
|
||||
Package: node.Name.Name,
|
||||
LOC: countLOC(path),
|
||||
}
|
||||
loc, err := countLOC(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count loc for %s: %w", path, err)
|
||||
}
|
||||
analysis.LOC = loc
|
||||
|
||||
analysis.Imports = p.extractImports(node, fset)
|
||||
analysis.Functions = p.extractFunctions(node, path, fset)
|
||||
@@ -349,16 +353,24 @@ func (p *GoPlugin) LoadTypesInfo(ctx context.Context, path string) (*types.Info,
|
||||
return pkgs[0].TypesInfo, pkgs[0].Fset, nil
|
||||
}
|
||||
|
||||
func countLOC(path string) int {
|
||||
func countLOC(path string) (int, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
return 0, fmt.Errorf("read file for loc %q: %w", path, err)
|
||||
}
|
||||
return strings.Count(string(data), "\n") + 1
|
||||
return strings.Count(string(data), "\n") + 1, nil
|
||||
}
|
||||
|
||||
var pluginRegistrationErr error
|
||||
|
||||
// RegistrationError returns a plugin registration error captured during init, if any.
|
||||
func RegistrationError() error {
|
||||
return pluginRegistrationErr
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := plugins.Register(New()); err != nil {
|
||||
panic(fmt.Sprintf("failed to register go plugin: %v", err))
|
||||
pluginRegistrationErr = fmt.Errorf("register go quality plugin: %w", err)
|
||||
_, _ = fmt.Fprintf(os.Stderr, "warning: %v\n", pluginRegistrationErr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user