Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ast-analysis/rules/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export const dataflow: DataflowRulesConfig = makeDataflowRules({
// ─── AST Node Types ───────────────────────────────────────────────────────

export const astTypes: Record<string, string> | null = {
call_expression: 'call',
new_expression: 'new',
throw_statement: 'throw',
await_expression: 'await',
Expand Down
135 changes: 102 additions & 33 deletions src/ast-analysis/visitors/ast-store-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function extractExpressionText(node: TreeSitterNode): string | null {
return truncate(node.text);
}

function extractCallName(node: TreeSitterNode): string {
for (const field of ['function', 'method', 'name']) {
const fn = node.childForFieldName(field);
if (fn) return fn.text;
}
return node.text?.split('(')[0] || '?';
}
Comment on lines +47 to +53
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Missing field fallbacks compared to native extract_call_name

The native Rust implementation (helpers.rs line 442) checks three field names in order — "function", "method", and "name" — to accommodate different Tree-sitter grammars:

for field in &["function", "method", "name"] {
    if let Some(fn_node) = node.child_by_field_name(field) {
        return node_text(&fn_node, source).to_string();
    }
}

The WASM extractCallName only checks "function". While the JavaScript/TypeScript Tree-sitter grammar always uses "function" for call_expression, if this helper is ever reused for another grammar (e.g., Python uses "function" but others use "method" or "name"), it would silently fall back to the text-split heuristic instead of the precise field lookup.

Suggested change
function extractCallName(node: TreeSitterNode): string {
const fn = node.childForFieldName('function');
if (fn) return fn.text;
return node.text?.split('(')[0] || '?';
}
function extractCallName(node: TreeSitterNode): string {
for (const field of ['function', 'method', 'name']) {
const fn = node.childForFieldName(field);
if (fn) return fn.text;
}
return node.text?.split('(')[0] || '?';
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a67cb64. extractCallName now checks all three fields in the same order as the native extract_call_name in helpers.rs:442:

function extractCallName(node: TreeSitterNode): string {
  for (const field of ['function', 'method', 'name']) {
    const fn = node.childForFieldName(field);
    if (fn) return fn.text;
  }
  return node.text?.split('(')[0] || '?';
}

This matches the native Rust implementation exactly and ensures grammar compatibility across languages.


function extractName(kind: string, node: TreeSitterNode): string | null {
if (kind === 'throw') {
for (let i = 0; i < node.childCount; i++) {
Expand Down Expand Up @@ -102,6 +110,93 @@ export function createAstStoreVisitor(
return nodeIdMap.get(`${parentDef.name}|${parentDef.kind}|${parentDef.line}`) || null;
}

/** Recursively walk a subtree collecting AST nodes — used for arguments-only traversal. */
function walkSubtree(node: TreeSitterNode | null): void {
if (!node) return;
if (matched.has(node.id)) return;

const kind = astTypeMap[node.type];
if (kind === 'call') {
// Capture this call and recurse only into its arguments
collectNode(node, kind);
walkCallArguments(node);
return;
}
if (kind) {
collectNode(node, kind);
if (kind !== 'string' && kind !== 'regex') return; // skipChildren for non-leaf kinds
}
for (let i = 0; i < node.childCount; i++) {
walkSubtree(node.child(i));
}
}

/**
* Recurse into only the arguments of a call node — mirrors the native engine's
* strategy that prevents double-counting nested calls in the function field
* (e.g. chained calls like `a().b()`).
*/
function walkCallArguments(callNode: TreeSitterNode): void {
// Try field-based lookup first, fall back to kind-based matching
const argsNode =
callNode.childForFieldName('arguments') ??
findChildByKind(callNode, ['arguments', 'argument_list', 'method_arguments']);
if (!argsNode) return;
for (let i = 0; i < argsNode.childCount; i++) {
walkSubtree(argsNode.child(i));
}
}

function findChildByKind(node: TreeSitterNode, kinds: string[]): TreeSitterNode | null {
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child && kinds.includes(child.type)) return child;
}
return null;
}

function collectNode(node: TreeSitterNode, kind: string): void {
if (matched.has(node.id)) return;

const line = node.startPosition.row + 1;
let name: string | null | undefined;
let text: string | null = null;

if (kind === 'call') {
name = extractCallName(node);
text = truncate(node.text);
} else if (kind === 'new') {
name = extractNewName(node);
text = truncate(node.text);
} else if (kind === 'throw') {
name = extractName('throw', node);
text = extractExpressionText(node);
} else if (kind === 'await') {
name = extractName('await', node);
text = extractExpressionText(node);
} else if (kind === 'string') {
const content = node.text?.replace(/^['"`]|['"`]$/g, '') || '';
if (content.length < 2) return;
name = truncate(content, 100);
text = truncate(node.text);
} else if (kind === 'regex') {
name = node.text || '?';
text = truncate(node.text);
}

rows.push({
file: relPath,
line,
kind,
name,
text,
receiver: null,
parentNodeId: resolveParentNodeId(line),
});

matched.add(node.id);
}

return {
name: 'ast-store',

Expand All @@ -111,40 +206,14 @@ export function createAstStoreVisitor(
const kind = astTypeMap[node.type];
if (!kind) return;

const line = node.startPosition.row + 1;
let name: string | null | undefined;
let text: string | null = null;

if (kind === 'new') {
name = extractNewName(node);
text = truncate(node.text);
} else if (kind === 'throw') {
name = extractName('throw', node);
text = extractExpressionText(node);
} else if (kind === 'await') {
name = extractName('await', node);
text = extractExpressionText(node);
} else if (kind === 'string') {
const content = node.text?.replace(/^['"`]|['"`]$/g, '') || '';
if (content.length < 2) return;
name = truncate(content, 100);
text = truncate(node.text);
} else if (kind === 'regex') {
name = node.text || '?';
text = truncate(node.text);
}
collectNode(node, kind);

rows.push({
file: relPath,
line,
kind,
name,
text,
receiver: null,
parentNodeId: resolveParentNodeId(line),
});

matched.add(node.id);
if (kind === 'call') {
// Mirror native: skip full subtree, recurse only into arguments.
// Prevents double-counting chained calls like service.getUser().getName().
walkCallArguments(node);
return { skipChildren: true };
}

if (kind !== 'string' && kind !== 'regex') {
return { skipChildren: true };
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/sample-project/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ class Calculator {
}
}

module.exports = { sumOfSquares, Calculator };
// Chained call — exercises call-in-function-field (a().b()) parity
function formatResults(items) {
return items.filter(Boolean).map(String);
}

module.exports = { sumOfSquares, Calculator, formatResults };
3 changes: 1 addition & 2 deletions tests/integration/build-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ describeOrSkip('Build parity: native vs WASM', () => {
expect(nativeGraph.roles).toEqual(wasmGraph.roles);
});

// Skip: WASM ast-store-visitor does not extract call-site AST nodes (#674)
it.skip('produces identical ast_nodes', () => {
it('produces identical ast_nodes', () => {
const wasmGraph = readGraph(path.join(wasmDir, '.codegraph', 'graph.db'));
const nativeGraph = readGraph(path.join(nativeDir, '.codegraph', 'graph.db'));
expect(nativeGraph.astNodes).toEqual(wasmGraph.astNodes);
Expand Down
Loading