MCPcopy
hub / github.com/expr-lang/expr / Visit

Method Visit

patcher/with_context.go:20–68  ·  view source on GitHub ↗

Visit adds WithContext.Name argument to all functions calls with a context.Context argument.

(node *ast.Node)

Source from the content-addressed store, hash-verified

18
19// Visit adds WithContext.Name argument to all functions calls with a context.Context argument.
20func (w WithContext) Visit(node *ast.Node) {
21 switch call := (*node).(type) {
22 case *ast.CallNode:
23 fn := call.Callee.Type()
24 if fn == nil {
25 return
26 }
27 // If callee type is interface{} (unknown), look up the function type from
28 // the Functions table or Env. This handles cases where the checker returns early
29 // without visiting nested call arguments (e.g., Date2() in Now2().After(Date2()))
30 // because the outer call's type is unknown due to missing context arguments.
31 if fn.Kind() == reflect.Interface {
32 if ident, ok := call.Callee.(*ast.IdentifierNode); ok {
33 if w.Functions != nil {
34 if f, ok := w.Functions[ident.Value]; ok {
35 fn = f.Type()
36 }
37 }
38 if fn.Kind() == reflect.Interface && w.Env != nil {
39 if m, ok := w.Env.MethodByName(w.NtCache, ident.Value); ok {
40 fn = m.Type
41 }
42 }
43 }
44 }
45 if fn.Kind() != reflect.Func {
46 return
47 }
48 switch fn.NumIn() {
49 case 0:
50 return
51 case 1:
52 if fn.In(0).String() != "context.Context" {
53 return
54 }
55 default:
56 if fn.In(0).String() != "context.Context" &&
57 fn.In(1).String() != "context.Context" {
58 return
59 }
60 }
61 ast.Patch(node, &ast.CallNode{
62 Callee: call.Callee,
63 Arguments: append([]ast.Node{
64 &ast.IdentifierNode{Value: w.Name},
65 }, call.Arguments...),
66 })
67 }
68}

Callers

nothing calls this directly

Calls 6

PatchFunction · 0.92
MethodByNameMethod · 0.80
NumInMethod · 0.80
InMethod · 0.80
TypeMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected