Core utilities for working with AST nodes, including indentation detection, scope resolution, and code manipulation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
Use this file to discover all available pages before exploring further.
Indentation Utilities
detectIndentUnit
Detects the indentation unit used in source code (\t or spaces).
The source code to analyze
string - The indentation unit ('\t' for tabs or a string of spaces)
The function analyzes all indented lines and uses the greatest common divisor (GCD) to determine the space-based indentation unit.
getLineIndent
Returns the indentation prefix for the line containing the given character index.The source code
The character index within the source code
string - The indentation string (spaces or tabs) at the start of the line
Scope Resolution
getScope
Finds the enclosing scope for a node (e.g.,statement_block or program).
The AST node to find the scope for
Optional custom parent node type to stop at
SgNode<Js> | null - The scope node or null if not found
Shebang Utilities
getShebang
Returns the Node.js shebang line node when present (e.g.,#!/usr/bin/env node).
The root AST node to search
SgNode | null - The shebang node if found, otherwise null
Only returns shebangs that appear at the start of the file (line 0) and contain “node” or “node.exe”.
replaceNodeJsArgs (shebang)
Replaces Node.js arguments in shebang lines.The root AST node to search
A mapping of argument names to their replacement values. Use empty string to remove an argument.
Edit[] - Array of edit operations to apply
Code Manipulation
removeLines
Safely removes multiple line ranges from source code, handling overlaps and duplicates.The original source code as a single string
An array of ranges to remove. Each range has
start and end with line and column properties.string - The modified source code with the specified ranges removed
The function automatically handles duplicate and overlapping ranges by normalizing them before applying removals.
Import/Require Identifier Extraction
getDefaultImportIdentifier
Returns the identifier node for a defaultimport (e.g., import fs from 'fs').
The
import_statement or import_clause nodeSgNode<Js> | null - The identifier node for the default import, or null if none
getRequireNamespaceIdentifier
Returns the identifier node for namespace-stylerequire (e.g., const util = require('util')).
The
variable_declarator node containing the require callSgNode<Js> | null - The identifier node for the namespace binding, or null for destructured requires