Skip to content

fix(extraction): visit anonymous function bodies#581

Open
msnandhis wants to merge 1 commit into
colbymchenry:mainfrom
msnandhis:fix/528-visit-anonymous-function-bodies
Open

fix(extraction): visit anonymous function bodies#581
msnandhis wants to merge 1 commit into
colbymchenry:mainfrom
msnandhis:fix/528-visit-anonymous-function-bodies

Conversation

@msnandhis
Copy link
Copy Markdown
Contributor

Summary

Fixes #528.

  • Keep traversing anonymous JavaScript/TypeScript function bodies while still suppressing anonymous function nodes.
  • Add regression coverage for AMD/CommonJS anonymous callbacks so inner handlers and calls remain in the graph.
  • Add an Unreleased changelog fix note.

Root Cause

extractFunction() returned immediately for anonymous function_expression / arrow functions. That avoided noisy <anonymous> symbols, but it also skipped the callback body. For AMD/CommonJS loader callbacks, that meant named functions and calls inside the callback never reached the graph.

Testing

  • npx -y -p node@22 -p npm@10 npm exec -- vitest run __tests__/extraction.test.ts -t 'anonymous AMD'
  • npx -y -p node@22 -p npm@10 npm exec -- vitest run __tests__/extraction.test.ts
  • npx -y -p node@22 -p npm@10 npm run build
  • git diff --check

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 933ab1e09a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
?? getChildByField(node, this.extractor.bodyField);
if (body) {
this.visitFunctionBody(body, '');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Visit variable-assigned callbacks in anonymous module bodies

For anonymous AMD/CommonJS wrappers that define handlers as const handler = () => {} or var handler = function () {}, this branch still drops the handler node: visitFunctionBody only calls extractFunction for nested functions whose extractName is already non-anonymous, and extractName intentionally returns <anonymous> for arrows/function expressions before the parent variable_declarator inference in extractFunction can run. As a result, a common module-scope callback style remains absent from the graph even though function declarations in the new test pass.

Useful? React with 👍 / 👎.

@msnandhis msnandhis force-pushed the fix/528-visit-anonymous-function-bodies branch from 933ab1e to a380a6d Compare May 31, 2026 11:17
@msnandhis
Copy link
Copy Markdown
Contributor Author

Updated in a380a6d to address the automated review feedback: visitFunctionBody now delegates nested function nodes to extractFunction, so true anonymous callbacks still have their bodies traversed without creating nodes, while variable-assigned arrow/function-expression callbacks can use the existing parent variable_declarator name inference. I also expanded the AMD/CommonJS regression test to cover const arrow and var function-expression callbacks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous function_expression bodies not visited - AMD/CommonJS modules lose all inner symbols

1 participant