Skip to content

Commit

Permalink
checkers/internal/astwalk: adapted from a lintpack (#934)
Browse files Browse the repository at this point in the history
Since we might want to reduce dependencies from
a lintpack project that is going to be replaced
with go/analysis, it's beneficial to move useful
things we want to keep inside go-critic repository.

astwalk only depends on the lintpack.FileWalker
interface that can be easily moved or replaced later.

Refs #854

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
  • Loading branch information
quasilyte committed May 27, 2020
1 parent 9b86fe6 commit 5c8144e
Show file tree
Hide file tree
Showing 90 changed files with 786 additions and 76 deletions.
2 changes: 1 addition & 1 deletion checkers/appendAssign_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/astp"
"golang.org/x/tools/go/ast/astutil"
Expand Down
2 changes: 1 addition & 1 deletion checkers/appendCombine_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/token"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astequal"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/argOrder_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astp"
Expand Down
2 changes: 1 addition & 1 deletion checkers/assignOp_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/token"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/typep"
Expand Down
2 changes: 1 addition & 1 deletion checkers/badCall_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/badCond_checker.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astequal"
Expand Down
2 changes: 1 addition & 1 deletion checkers/badRegexp_checker.go
Expand Up @@ -9,7 +9,7 @@ import (
"unicode/utf8"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/quasilyte/regex/syntax"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/boolExprSimplify_checker.go
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astequal"
Expand Down
2 changes: 1 addition & 1 deletion checkers/builtinShadow_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/captLocal_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/caseOrder_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/codegenComment_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/commentFormatting_checker.go
Expand Up @@ -8,7 +8,7 @@ import (
"unicode/utf8"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/commentedOutCode_checker.go
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/strparse"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/commentedOutImport_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/defaultCaseOrder_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/deferUnlambda_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/deprecatedComment_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/docStub_checker.go
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/dupArg_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astequal"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/dupBranchBody_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astequal"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/dupCase_checker.go
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/dupSubExpr_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/typep"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/elseif_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astp"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/emptyFallthrough_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/token"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/emptyStringTest_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/token"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/typep"
Expand Down
2 changes: 1 addition & 1 deletion checkers/equalFold_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/token"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astequal"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/evalOrder_checker.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/typep"
Expand Down
2 changes: 1 addition & 1 deletion checkers/exitAfterDefer_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astfmt"
"github.com/go-toolsmith/astp"
"golang.org/x/tools/go/ast/astutil"
Expand Down
2 changes: 1 addition & 1 deletion checkers/filepathJoin_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/flagDeref_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/flagName_checker.go
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/hexLiteral_checker.go
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
)

Expand Down
2 changes: 1 addition & 1 deletion checkers/hugeParam_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/ifElseChain_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/importShadow_checker.go
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion checkers/indexAlloc_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/typep"
)
Expand Down
2 changes: 1 addition & 1 deletion checkers/initClause_checker.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-toolsmith/astp"
)

Expand Down
41 changes: 41 additions & 0 deletions checkers/internal/astwalk/comment_walker.go
@@ -0,0 +1,41 @@
package astwalk

import (
"go/ast"
"strings"
)

type commentWalker struct {
visitor CommentVisitor
}

func (w *commentWalker) WalkFile(f *ast.File) {
if !w.visitor.EnterFile(f) {
return
}

for _, cg := range f.Comments {
visitCommentGroups(cg, w.visitor.VisitComment)
}
}

func visitCommentGroups(cg *ast.CommentGroup, visit func(*ast.CommentGroup)) {
var group []*ast.Comment
visitGroup := func(list []*ast.Comment) {
if len(list) == 0 {
return
}
cg := &ast.CommentGroup{List: list}
visit(cg)
}
for _, comment := range cg.List {
if strings.HasPrefix(comment.Text, "/*") {
visitGroup(group)
group = group[:0]
visitGroup([]*ast.Comment{comment})
} else {
group = append(group, comment)
}
}
visitGroup(group)
}

0 comments on commit 5c8144e

Please sign in to comment.