|
| 1 | +package singleflightcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "go/ast" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/samber/lo" |
| 11 | + "golang.org/x/tools/go/analysis" |
| 12 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 13 | + "golang.org/x/tools/go/ast/inspector" |
| 14 | +) |
| 15 | + |
| 16 | +func Analyzer() *analysis.Analyzer { |
| 17 | + flagSet := flag.NewFlagSet("singleflightcheck", flag.ExitOnError) |
| 18 | + skipPkg := flagSet.String("skip-pkg", "", "package(s) to skip for linting") |
| 19 | + skipFiles := flagSet.String("skip-files", "", "patterns of files to skip for linting") |
| 20 | + |
| 21 | + return &analysis.Analyzer{ |
| 22 | + Name: "singleflightcheck", |
| 23 | + Doc: "reports uses of golang.org/x/sync/singleflight.Group.Do in functions that have a context.Context parameter; use resenje.org/singleflight instead", |
| 24 | + Run: func(pass *analysis.Pass) (any, error) { |
| 25 | + // Check for a skipped package. |
| 26 | + if len(*skipPkg) > 0 { |
| 27 | + skipped := lo.Map(strings.Split(*skipPkg, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) }) |
| 28 | + for _, s := range skipped { |
| 29 | + if strings.Contains(pass.Pkg.Path(), s) { |
| 30 | + return nil, nil |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Check for a skipped file. |
| 36 | + skipFilePatterns := make([]string, 0) |
| 37 | + if len(*skipFiles) > 0 { |
| 38 | + skipFilePatterns = lo.Map(strings.Split(*skipFiles, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) }) |
| 39 | + } |
| 40 | + for _, pattern := range skipFilePatterns { |
| 41 | + _, err := regexp.Compile(pattern) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("invalid skip-files pattern `%s`: %w", pattern, err) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 48 | + |
| 49 | + nodeFilter := []ast.Node{ |
| 50 | + (*ast.File)(nil), |
| 51 | + (*ast.CallExpr)(nil), |
| 52 | + } |
| 53 | + |
| 54 | + inspect.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) bool { |
| 55 | + switch s := n.(type) { |
| 56 | + case *ast.File: |
| 57 | + for _, pattern := range skipFilePatterns { |
| 58 | + isMatch, _ := regexp.MatchString(pattern, pass.Fset.Position(s.Package).Filename) |
| 59 | + if isMatch { |
| 60 | + return false |
| 61 | + } |
| 62 | + } |
| 63 | + return true |
| 64 | + |
| 65 | + case *ast.CallExpr: |
| 66 | + // Check if this is a call to .Do on a selector expression. |
| 67 | + selector, ok := s.Fun.(*ast.SelectorExpr) |
| 68 | + if !ok || selector.Sel.Name != "Do" { |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + // Check that the receiver type is golang.org/x/sync/singleflight.Group. |
| 73 | + receiverType := pass.TypesInfo.TypeOf(selector.X) |
| 74 | + if receiverType == nil { |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + typeStr := receiverType.String() |
| 79 | + if !strings.Contains(typeStr, "golang.org/x/sync/singleflight") { |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + // Check if the enclosing function has a context.Context parameter. |
| 84 | + if !enclosingFuncHasContext(stack) { |
| 85 | + return true |
| 86 | + } |
| 87 | + |
| 88 | + pass.Reportf(n.Pos(), "In package %s: use resenje.org/singleflight instead of golang.org/x/sync/singleflight in functions with context.Context", pass.Pkg.Path()) |
| 89 | + return false |
| 90 | + |
| 91 | + default: |
| 92 | + return true |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return nil, nil |
| 97 | + }, |
| 98 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 99 | + Flags: *flagSet, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func enclosingFuncHasContext(stack []ast.Node) bool { |
| 104 | + for i := len(stack) - 1; i >= 0; i-- { |
| 105 | + var params *ast.FieldList |
| 106 | + switch f := stack[i].(type) { |
| 107 | + case *ast.FuncDecl: |
| 108 | + params = f.Type.Params |
| 109 | + case *ast.FuncLit: |
| 110 | + params = f.Type.Params |
| 111 | + default: |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + if params == nil { |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + for _, param := range params.List { |
| 120 | + if sel, ok := param.Type.(*ast.SelectorExpr); ok { |
| 121 | + if ident, ok := sel.X.(*ast.Ident); ok { |
| 122 | + if ident.Name == "context" && sel.Sel.Name == "Context" { |
| 123 | + return true |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return false |
| 129 | + } |
| 130 | + return false |
| 131 | +} |
0 commit comments