From d80fa9e54333152097434a97b67df3b6c9a35cd7 Mon Sep 17 00:00:00 2001 From: carlos-alm <127798846+carlos-alm@users.noreply.github.com> Date: Wed, 25 Mar 2026 23:03:02 -0600 Subject: [PATCH] refactor: extract MAX_WALK_DEPTH constant to extractors/helpers.ts Impact: 9 functions changed, 123 affected --- src/extractors/csharp.ts | 4 ++-- src/extractors/go.ts | 4 ++-- src/extractors/helpers.ts | 6 ++++++ src/extractors/javascript.ts | 4 ++-- src/extractors/php.ts | 4 ++-- src/extractors/python.ts | 4 ++-- src/extractors/rust.ts | 4 ++-- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/extractors/csharp.ts b/src/extractors/csharp.ts index 8308abbb..3a79bb28 100644 --- a/src/extractors/csharp.ts +++ b/src/extractors/csharp.ts @@ -6,7 +6,7 @@ import type { TreeSitterNode, TreeSitterTree, } from '../types.js'; -import { extractModifierVisibility, findChild, nodeEndLine } from './helpers.js'; +import { extractModifierVisibility, findChild, MAX_WALK_DEPTH, nodeEndLine } from './helpers.js'; /** * Extract symbols from C# files. @@ -333,7 +333,7 @@ function extractCSharpTypeMapDepth( ctx: ExtractorOutput, depth: number, ): void { - if (depth >= 200) return; + if (depth >= MAX_WALK_DEPTH) return; // local_declaration_statement → variable_declaration → type + variable_declarator(s) if (node.type === 'variable_declaration') { diff --git a/src/extractors/go.ts b/src/extractors/go.ts index b31d50c3..3e832b37 100644 --- a/src/extractors/go.ts +++ b/src/extractors/go.ts @@ -6,7 +6,7 @@ import type { TreeSitterTree, TypeMapEntry, } from '../types.js'; -import { findChild, goVisibility, nodeEndLine } from './helpers.js'; +import { findChild, goVisibility, MAX_WALK_DEPTH, nodeEndLine } from './helpers.js'; /** * Extract symbols from Go files. @@ -233,7 +233,7 @@ function setIfHigher( } function extractGoTypeMapDepth(node: TreeSitterNode, ctx: ExtractorOutput, depth: number): void { - if (depth >= 200) return; + if (depth >= MAX_WALK_DEPTH) return; // var x MyType = ... or var x, y MyType → var_declaration > var_spec (confidence 0.9) if (node.type === 'var_spec') { diff --git a/src/extractors/helpers.ts b/src/extractors/helpers.ts index be710d18..56b05543 100644 --- a/src/extractors/helpers.ts +++ b/src/extractors/helpers.ts @@ -1,5 +1,11 @@ import type { TreeSitterNode } from '../types.js'; +/** + * Maximum recursion depth for tree-sitter AST walkers. + * Shared across all language extractors to prevent stack overflow on deeply nested ASTs. + */ +export const MAX_WALK_DEPTH = 200; + export function nodeEndLine(node: TreeSitterNode): number { return node.endPosition.row + 1; } diff --git a/src/extractors/javascript.ts b/src/extractors/javascript.ts index a05a58d6..e6fa4fe1 100644 --- a/src/extractors/javascript.ts +++ b/src/extractors/javascript.ts @@ -12,7 +12,7 @@ import type { TreeSitterTree, TypeMapEntry, } from '../types.js'; -import { findChild, nodeEndLine } from './helpers.js'; +import { findChild, MAX_WALK_DEPTH, nodeEndLine } from './helpers.js'; /** Built-in globals that start with uppercase but are not user-defined types. */ const BUILTIN_GLOBALS: Set = new Set([ @@ -929,7 +929,7 @@ function extractTypeMapWalk(rootNode: TreeSitterNode, typeMap: Map= 200) return; + if (depth >= MAX_WALK_DEPTH) return; const t = node.type; if (t === 'variable_declarator') { const nameN = node.childForFieldName('name'); diff --git a/src/extractors/php.ts b/src/extractors/php.ts index 1219665b..653971ee 100644 --- a/src/extractors/php.ts +++ b/src/extractors/php.ts @@ -5,7 +5,7 @@ import type { TreeSitterNode, TreeSitterTree, } from '../types.js'; -import { extractModifierVisibility, findChild, nodeEndLine } from './helpers.js'; +import { extractModifierVisibility, findChild, MAX_WALK_DEPTH, nodeEndLine } from './helpers.js'; function extractPhpParameters(fnNode: TreeSitterNode): SubDeclaration[] { const params: SubDeclaration[] = []; @@ -340,7 +340,7 @@ function extractPhpTypeMap(node: TreeSitterNode, ctx: ExtractorOutput): void { } function extractPhpTypeMapDepth(node: TreeSitterNode, ctx: ExtractorOutput, depth: number): void { - if (depth >= 200) return; + if (depth >= MAX_WALK_DEPTH) return; // Function/method parameters with type hints if ( diff --git a/src/extractors/python.ts b/src/extractors/python.ts index 0443237d..b1d8804a 100644 --- a/src/extractors/python.ts +++ b/src/extractors/python.ts @@ -6,7 +6,7 @@ import type { TreeSitterTree, TypeMapEntry, } from '../types.js'; -import { findChild, nodeEndLine, pythonVisibility } from './helpers.js'; +import { findChild, MAX_WALK_DEPTH, nodeEndLine, pythonVisibility } from './helpers.js'; /** Built-in globals that start with uppercase but are not user-defined types. */ const BUILTIN_GLOBALS_PY: Set = new Set([ @@ -365,7 +365,7 @@ function extractPythonTypeMapDepth( ctx: ExtractorOutput, depth: number, ): void { - if (depth >= 200) return; + if (depth >= MAX_WALK_DEPTH) return; // typed_parameter: identifier : type (confidence 0.9) if (node.type === 'typed_parameter') { diff --git a/src/extractors/rust.ts b/src/extractors/rust.ts index b7e15764..e74f2e78 100644 --- a/src/extractors/rust.ts +++ b/src/extractors/rust.ts @@ -5,7 +5,7 @@ import type { TreeSitterNode, TreeSitterTree, } from '../types.js'; -import { findChild, nodeEndLine, rustVisibility } from './helpers.js'; +import { findChild, MAX_WALK_DEPTH, nodeEndLine, rustVisibility } from './helpers.js'; /** * Extract symbols from Rust files. @@ -274,7 +274,7 @@ function extractRustTypeMap(node: TreeSitterNode, ctx: ExtractorOutput): void { } function extractRustTypeMapDepth(node: TreeSitterNode, ctx: ExtractorOutput, depth: number): void { - if (depth >= 200) return; + if (depth >= MAX_WALK_DEPTH) return; // let x: MyType = ... if (node.type === 'let_declaration') {