Skip to content

Commit

Permalink
pkg/analyzer/globals,pkg/analyzer/imports: fix report's related infor…
Browse files Browse the repository at this point in the history
…mation being off by one
  • Loading branch information
leonklingele committed Jan 20, 2022
1 parent b00ac40 commit 26d9367
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 14 additions & 8 deletions pkg/analyzer/globals/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Global struct {

func Filepass(
p *analysis.Pass, f *ast.File,
token token.Token, requireSingle, requireGrouping bool,
tkn token.Token, requireSingle, requireGrouping bool,
) error {
var globals []*Global
for _, decl := range f.Decls {
Expand All @@ -24,7 +24,7 @@ func Filepass(
continue
}

if genDecl.Tok == token {
if genDecl.Tok == tkn {
globals = append(globals, &Global{
Decl: genDecl,
IsGroup: genDecl.Lparen != 0,
Expand All @@ -39,17 +39,23 @@ func Filepass(
}

if requireSingle && numGlobals > 1 {
msg := fmt.Sprintf("should only use a single global '%s' declaration, %d found", token.String(), numGlobals)
firstdup := globals[1]
msg := fmt.Sprintf("should only use a single global '%s' declaration, %d found", tkn.String(), numGlobals)
dups := globals[1:]
firstdup := dups[0]
decl := firstdup.Decl

p.Report(analysis.Diagnostic{ //nolint:exhaustivestruct // we do not need all fields
report := analysis.Diagnostic{ //nolint:exhaustivestruct // we do not need all fields
Pos: decl.Pos(),
End: decl.End(),
Message: msg,
Related: toRelated(globals[1:]),
// TODO(leon): Suggest fix
})
}

if len(dups) > 1 {
report.Related = toRelated(dups[1:])
}

p.Report(report)
}

if requireGrouping {
Expand All @@ -61,7 +67,7 @@ func Filepass(
}

if numUngrouped := len(ungrouped); numUngrouped != 0 {
msg := fmt.Sprintf("should only use grouped global '%s' declarations", token.String())
msg := fmt.Sprintf("should only use grouped global '%s' declarations", tkn.String())
firstmatch := ungrouped[0]
decl := firstmatch.Decl

Expand Down
14 changes: 10 additions & 4 deletions pkg/analyzer/imports/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ func Filepass(c *Config, p *analysis.Pass, f *ast.File) error {

if c.RequireSingleImport && numImports > 1 {
msg := fmt.Sprintf("should only use a single 'import' declaration, %d found", numImports)
firstdup := imports[1]
dups := imports[1:]
firstdup := dups[0]
decl := firstdup.Decl

p.Report(analysis.Diagnostic{ //nolint:exhaustivestruct // we do not need all fields
report := analysis.Diagnostic{ //nolint:exhaustivestruct // we do not need all fields
Pos: decl.Pos(),
End: decl.End(),
Message: msg,
Related: toRelated(imports[1:]),
// TODO(leon): Suggest fix
})
}

if len(dups) > 1 {
report.Related = toRelated(dups[1:])
}

p.Report(report)
}

if c.RequireGrouping {
Expand Down

0 comments on commit 26d9367

Please sign in to comment.