@jssg/utils provides reusable utility functions for JSSG codemods. These helpers handle common import manipulation tasks that are tedious and error-prone when done manually with AST queries.
Installation
Add@jssg/utils to your codemod project:
package.json
Import
These utilities work with JavaScript, TypeScript, JSX, and TSX ASTs. They handle both ESM (
import) and CJS (require()) formats automatically.Java Utilities
@jssg/utils also includes Java helpers for recurring safe-codemod patterns:
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.
Returns
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.
Returns
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)
Parameters
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.
Returns
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.
Parameters
SgNode<T, 'program'>
required
The root program node.
RemoveImportOptions
required
One of:
{ type: "default", from: string }— Remove the default import from a module.{ type: "namespace", from: string }— Remove the namespace import from a module.{ type: "named", specifiers: string[], from: string }— Remove specific named imports.
Returns
Edit | null — An edit to apply via rootNode.commitEdits(), or null if the import was not found.
Example
stringToExactRegexString
Escape a string for use as an exact-match regex pattern. Wraps in ^...$ and escapes special characters.
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
Match Existing Module Style
getImport automatically detects the module type. Use moduleType when adding imports to match the existing style: