Skip to main content
Utilities for resolving how global API paths should be accessed based on import patterns, and for safely removing or updating bindings.

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_declaration
  • variable_declarator
  • import_statement
  • import_clause
string
required
The expected dotted path to resolve (e.g., ”.types.isNativeError").The.types.isNativeError"). The `` represents the module namespace.
Returns: 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

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
Optional configuration object
object
If provided, the binding will only be removed if it is unused
Range[]
Array of ranges to ignore when checking for usage
SgNode<Js>
Custom root node for usage checking. Defaults to the file root.
Returns: { 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
  • undefined to 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
Returns: { edit?: Edit, lineToRemove?: Range } | undefined

Advanced Update Patterns

Complete Migration Example