Skip to content

Conversation

brunocroh
Copy link
Member

Add new updateBinding utility function and make removeBinding a wrapper for it

Summary

This PR create the new updateBinding function. That can be used to update bindings in the file

##Examples

Updating a named require/import

updateBinding(node, 'types', {newBinding: 'newTypes'}

Before:

const { types, diff } = require('node:util');
import { types, diff } = from 'node:util';

After:

const { newTypes, diff } = require('node:util');
import { newTypes, diff } = from 'node:util';

Removing a named require/import

updateBinding(node, 'types', {newBinding: undefined}

Before:

const { types, diff } = require('node:util');
import { types, diff } = from 'node:util';

After:

const { diff } = require('node:util');
import { diff } = from 'node:util';

Updating a named import with alias

updateBinding(node, 'renamedTypes', {newBinding: 'newTypes'}

Before:

import { types as renamedTypes, diff } = from 'node:util';

After:

import { newTypes as renamedTypes, diff } = from 'node:util';

Changes Made

  • Created updateBinding function in /utils/src/ast-grep/update-binding.ts
  • Refactored existing remove-binding function to use the new updateBinding internally

Copy link
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ! thanks for that

@brunocroh brunocroh force-pushed the feat/update-binding-utility branch from 2b2978d to 164d78c Compare August 22, 2025 18:05
@AugustinMauroy
Copy link
Member

wait these function can support dynamic import

@brunocroh
Copy link
Member Author

brunocroh commented Aug 31, 2025

wait these function can support dynamic import

First, we need to merge #189. Once that is merged, we should update resolveBindingPath and adjust it if needed.

This utility function is called with the result of getNodeImportCalls and resolveBindingPath, e.g.:

const importStatement = getNodeImportCalls(node, "util")
const binding = resolveBindingPath(importStatement, "$.diff")
const edits = updateBinding(importStatement, binding, {newBinding: "newDiff"})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants