# jscodeshift: Maximum call stack size exceeded in recursive visitor

TL;DR: Your transform recurses without a proper base case - or it walks the tree with manual recursion instead of letting jscodeshift's visitor do the traversal. Find the recursive call from the repeating function name in the stack trace, add the missing termination condition, or replace manual recursion with the visitor pattern.

```text
jscodeshift: Maximum call stack size exceeded in recursive visitor
```

## Steps

1. Reproduce on a small input and read the top of the stack trace - the same function name repeats.
   Expected: you identify the recursive function in your transform file.
2. Check the base case: every recursive path must reach a condition that stops. Add or fix the guard (for example, stop when the node has no children of the type you descend into).
   Expected: the recursion terminates on the small test file.
3. Better yet, stop recursing manually - use the visitor traversal, which handles descent for you:
   ```
   visit(ast, {
     visitFunctionDeclaration(path) {
       // do your work on the node here
       this.traverse(path);
     },
   });
   ```
   Expected: no manual recursive calls remain in the transform.
4. Test on one file with `-d -p` (dry run plus print) before the full run.
   Expected: the file transforms with no stack error.

## Use this when

- `Maximum call stack size exceeded` (RangeError) names your transform's function in the trace.
- The error appears mid-codemod-run on files of normal size.
- The transform worked before someone added recursive descent to it.

## Not for this skill when

- The error is `Reached heap limit` - that is memory exhaustion, not stack depth; different fix.
- The overflow is inside jscodeshift itself on one enormous generated file - split the file or raise the stack via node flags.
- The transform hangs without growing the stack - that is an infinite loop, not runaway recursion.

## Variant phrasings

- jscodeshift stack overflow recursive visitor
- RangeError maximum call stack size exceeded codemod
- jscodeshift transform recursion too deep

## Why it happens

Manual recursive descent re-enters the function once per tree level - or once per node, if the recursion is wrong - and the call stack is far shallower than the heap. The visitor's `this.traverse(path)` performs the descent through the traversal machinery instead of your call stack, so it does not pile up frames the same way.

## Edge cases

- Mutual recursion between two helper functions overflows just like direct self-recursion - check the whole call cycle, not one function.
- A missing `this.traverse(path)` is the opposite bug: it silently skips subtrees. After refactoring, verify nested nodes still get visited.
- Deeply nested generated code can overflow even correct recursion - then process subtrees iteratively or split the input.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_f2QgrRVqyHM97rEnPr6fLw
