Skip to content

Conversation

@wzekin
Copy link

@wzekin wzekin commented Jan 26, 2026

Collect direct calls and anonymous functions in Go parser and store them in the Extra field of Function, Dependency, Type, and Var.

What type of PR is this?

feat

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • [] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

feat:增加 Extra Field 去存储额外的 AST 元信息

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

  • Dependency update: Add an Extra field to the Dependency to record whether a func or method is actually invoked via CallExpr. This information is only added to Function-type FunctionCalls and MethodCalls, and to Vars-type Dependencies.
type Dependency struct {
  ...
  Extra map[string]any
}

Dependency.Extra['FunctionIsCall'] = true
  • Function update: Add an Extra field to Function, and add a field AnonymousFunctions inside it to record the anonymous functions defined in this Function.
type Function struct {
  ...
  
  Extra map[string]any // 额外数据
}

Function.Extra['AnonymousFunctions'] = []FileLine{/* The exact location of the anonymous function. */}
  • Var update: Add an Extra field to Var, and include a AnonymousFunctions field within it to record the anonymous Function defined in the initialization Expr of this Var.
type Var struct {
    ...
    
    Extra map[string]any // 额外数据
}

Var.Extra['AnonymousFunctions'] = []FileLine{/* The exact location of the anonymous function. */}
  • Type update: Add Extra to Type (it's added for symmetry).
type Type struct {
    ...
    
    Extra map[string]any // 额外数据
}

Compatibility Analysis

Dependency

  • Source Code
func customizedRegister(r *server.Hertz) {
        r.GET("/ping", handler.Ping)
        r.POST("/api/v1/hkong/doc/compress_force", middleware.ReportCall, middleware.CheckAcessToken(), doc.CompressRepo)
        r.POST("/api/v1/abcoder/mcp/get_files_detail", append(api.RagMWS(4000, 4000), rag.GetFileAST)...)
}
  • Before Modification
{
    "ModPath": "code.byted.org/middleware/hkong_api",
    "PkgPath": "code.byted.org/middleware/hkong_api/biz/middleware",
    "Name": "ReportCall",
    "File": "router.go",
    "Line": 17,
    "StartOffset": 576,
    "EndOffset": 586
},
{
    "ModPath": "code.byted.org/middleware/hkong_api",
    "PkgPath": "code.byted.org/middleware/hkong_api/biz/middleware",
    "Name": "CheckAcessToken",
    "File": "router.go",
    "Line": 17,
    "StartOffset": 599,
    "EndOffset": 614,
}
  • After Modification
  • Currently, Extra only appears in:
    1. FunctionCall or MethodCalls of a Function
    2. Dependency of a Var
{
   "ModPath": "code.byted.org/middleware/hkong_api",
   "PkgPath": "code.byted.org/middleware/hkong_api/biz/middleware",
   "Name": "ReportCall",
   "File": "router.go",
   "Line": 17,
   "StartOffset": 576,
   "EndOffset": 586 // ``ReportCall`` is not directly called, so no ``Extra``
},
{
   "ModPath": "code.byted.org/middleware/hkong_api",
   "PkgPath": "code.byted.org/middleware/hkong_api/biz/middleware",
   "Name": "CheckAcessToken",
   "File": "router.go",
   "Line": 17,
   "StartOffset": 599,
   "EndOffset": 614,
   "Extra": {
       "FunctionIsCall": true // ``CheckAcessToken`` is directly called, so ``FunctionIsCall": true exists
   }
}

Function or Var
- Source Code

func GetProjectDocList(ctx context.Context, c *app.RequestContext) {
...
        go func() {
                err = db.UpdatePageData(ctx, string(c.Path()))
                if err != nil {
                        logs.CtxError(ctx, "db.UpdatePageData failed,err=%v,page=%v", err, string(c.Path()))
                }
        }()
...
}
  • Before Transformation

No relevant data

  • After Transformation

Added data related to AnonymousFunctions

"GetProjectDocList": {
    ...
    "Extra": {
        "AnonymousFunctions": [
            {
                "File": "biz/handler/doc/get_project_doc_list.go",
                "Line": 34,
                "StartOffset": 698,
                "EndOffset": 870
            }
        ]
    }
}

Other Language Related

  • Other Language Parsers: Since there is no relevant support for other languages, all Extra fields here will have no data (the field does not exist)
  • JSON Parsing in Other Languages
    • For languages that require binding to a struct during JSON unmarshalling, the Extra field will be directly ignored
    • For languages that bind directly to a map during JSON unmarshalling, the Extra field will exist in the map, but it will not be affected unless this field is specifically specified

zh(optional):

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

Collect direct calls and anonymous functions in Go parser and store
them in the Extra field of Function, Dependency, Type, and Var.
}
if len(collects.anonymousFunctions) > 0 {
v.Extra = map[string]any{}
v.Extra["AnonymousFunctions"] = collects.anonymousFunctions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在初始化的时候把 Extra初始化? 不然以后其他人使用 Extra 的时候都得记得判空了

if len(collects.directCalls) > 0 {
for i, dep := range v.Dependencies {
if collects.directCalls[dep.FileLine] {
v.Dependencies[i].SetExtra("FunctionIsCall", true)
Copy link
Member

@Duslia Duslia Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定义个常量吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants