-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/v2check: refactor for better testing
- Loading branch information
Showing
6 changed files
with
164 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2023 Datadog, Inc. | ||
|
||
package v2check | ||
|
||
import ( | ||
"context" | ||
"go/ast" | ||
"go/types" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/types/typeutil" | ||
) | ||
|
||
type Probe func(context.Context, ast.Node, *analysis.Pass) (context.Context, bool) | ||
|
||
// IsFuncCall returns true if the node is a function call. | ||
// The function call expression is stored in the context as "fn". | ||
func IsFuncCall(ctx context.Context, n ast.Node, pass *analysis.Pass) (context.Context, bool) { | ||
c, ok := n.(*ast.CallExpr) | ||
if !ok { | ||
return ctx, false | ||
} | ||
callee := typeutil.Callee(pass.TypesInfo, c) | ||
if callee == nil { | ||
return ctx, false | ||
} | ||
fn, ok := callee.(*types.Func) | ||
if !ok { | ||
return ctx, false | ||
} | ||
ctx = context.WithValue(ctx, "fn", fn) | ||
return ctx, true | ||
} | ||
|
||
// HasPackagePrefix returns true if the selector expression has a package prefix. | ||
func HasPackagePrefix(prefix string) Probe { | ||
return func(ctx context.Context, n ast.Node, pass *analysis.Pass) (context.Context, bool) { | ||
fn, ok := ctx.Value("fn").(*types.Func) | ||
if !ok { | ||
return ctx, false | ||
} | ||
return ctx, strings.HasPrefix(fn.Pkg().Path(), prefix) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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