These utilities work with JavaScript, TypeScript, JSX, and TSX ASTs. They handle both ESM (
import) and CJS (require()) formats automatically.getImport
Locate an import of a given module and return its alias/identifier node.
Parameters
SgNode<T, 'program'>
required
The root program node to search within.
GetImportOptions
required
What to look for:
{ type: "default", from: string }— Find the default import from a module.{ type: "named", name: string, from: string }— Find a specific named import from a module.
GetImportResult<T> | null — An object containing:
Returns
null if the import is not found.
Supported Import Shapes
Example
getAllImports
Locate imports of a given module and return their alias/identifier nodes.
Parameters
SgNode<T, 'program'>
required
The root program node to search within.
GetImportOptions
required
What to look for:
{ type: "default", from: string }— Find all default imports from a module.{ type: "named", name: string, from: string }— Find all instances of a specific named import from a module.
ResolvedImport<T>[] — An array containing:
Returns an empty array if no imports are found.
Example
addImport
Add an import to the program. Smart behavior:
- Skips if the import already exists
- Merges named specifiers into an existing import statement from the same source
- Creates a new import statement otherwise
- Inserts after the last existing import (or at file start if no imports exist)
SgNode<T, 'program'>
required
The root program node.
AddImportOptions
required
One of:
{ type: "default", name: string, from: string, moduleType?: "esm" | "cjs" }— Add a default import.{ type: "namespace", name: string, from: string }— Add a namespace import (import * as name).{ type: "named", specifiers: Array<{ name: string, alias?: string }>, from: string, moduleType?: "esm" | "cjs" }— Add named imports.
Edit | null — An edit to apply via rootNode.commitEdits(), or null if the import already exists.
Example
removeImport
Remove an import from the program. Smart behavior:
- Default/namespace: Removes the entire import statement.
- Named (multiple specifiers): Removes only the specified specifiers, keeping the rest.
- Named (last specifier): Removes the entire import statement.
- Handles both ESM and CJS formats.
SgNode<T, 'program'>
required
The root program node.
RemoveImportOptions
required
One of:
{ type: "default", from: string, removeSideEffectForms?: boolean }— Remove the default import from a module. SetremoveSideEffectFormsto also remove a side-effect-onlyimport "module"orrequire("module")when no binding import exists.{ type: "namespace", from: string }— Remove the namespace import from a module.{ type: "named", specifiers: string[], from: string }— Remove specific named imports.
Edit | null — An edit to apply via rootNode.commitEdits(), or null if the import was not found.
Example
updateImport
Replace named specifiers of an existing import or require in place. The whole clause is one edit, so the rewrite does not overlap other specifier edits.
import { foo } from "mod"with{ name: "foo", to: "bar" }becomesimport { bar } from "mod".- An existing local alias is kept:
import { foo as f }becomesimport { bar as f }. Passaliasto override that local name. - Works for ESM named imports, destructured
require(), and destructured dynamicimport(). - If
tois already imported in the same clause, the old specifier is dropped instead of duplicated.
SgNode<T, 'program'>
required
The root program node.
UpdateImportOptions
required
Named imports only:
{ type: "named", from: string, specifiers: Array<{ name: string, to: string, alias?: string }> }— Renamenametotoin the import fromfrom.
Edit | null — An edit to apply via rootNode.commitEdits(), or null if none of the named specifiers is found or specifiers is empty.
Example
stringToExactRegexString
Escape a string and return a regex that matches only that exact text. The result is wrapped in ^...$, with regex metacharacters escaped.
Common Patterns
Verify Import Before Transforming
Always verify that a symbol is imported from the expected package before transforming its usage:Replace One Import With Another
Rename a named specifier in the same module withupdateImport:
removeImport and addImport:
Match Existing Module Style
getImport automatically detects the module type. Use moduleType when adding imports to match the existing style: