Overview
These utilities handle the complexity of:- Determining how imported functions are accessed locally (namespace, destructured, aliased)
- Safely removing unused imports while preserving other bindings
- Updating or replacing import bindings
- Handling both ES modules and CommonJS
Binding Path Resolution
resolveBindingPath
Resolves how a global API path should be accessed based on the import pattern.SgNode<Js>
required
The AST node representing the import or require statement. Must be one of:
lexical_declarationvariable_declaratorimport_statementimport_clause
string
required
The expected dotted path to resolve (e.g., ”` represents the module namespace.
string | undefined - The local access path that should be used in code, or undefined if the binding is not found
The
$ in the path parameter represents the root module namespace and gets replaced with the actual local binding name.Advanced Examples
- Namespace Require
- Destructured Import
- Aliased Destructure
- Member Expression Require
Binding Removal
removeBinding
Removes a specific binding from imports/requires, or removes the entire statement if it’s the only binding.SgNode<Js>
required
The AST node representing the import or require statement
string
required
The name of the binding to remove (e.g., “isNativeError”)
object
{ edit?: Edit, lineToRemove?: Range } | undefined
- If multiple bindings exist: returns
{ edit }to modify the destructuring pattern - If single binding: returns
{ lineToRemove }with the line range to remove - If binding not found: returns
undefined
Usage Check Example
Binding Updates
updateBinding
Updates a specific binding from imports/requires. Can replace, add, or remove bindings.SgNode<Js>
required
The AST node representing the import or require statement
object
Configuration object for the update operation
string
The name of the binding to update or remove. Omit to add a new binding.
string | string[]
The new binding name(s) to replace the old one. Can be:
- A single string for one-to-one replacement
- An array of strings for one-to-many replacement
undefinedto remove the binding
object
Optional usage checking configuration
Range[]
Ranges to ignore when checking if a binding is used
SgNode<Js>
Custom root node for usage checking
{ edit?: Edit, lineToRemove?: Range } | undefined
Advanced Update Patterns
- One-to-Many
- Adding Multiple
- Rename Alias