Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { resolveBindingPath } from '@nodejs/codemod-utils';

#### `removeBinding(node, binding)`

Removes a specific binding from destructured imports/requires, or removes the entire statement if it's the only binding.
Removes a specific binding from imports/requires, or removes the entire statement if it's the only binding.

```typescript
import { removeBinding } from '@nodejs/codemod-utils';
Expand All @@ -82,6 +82,26 @@ import { removeBinding } from '@nodejs/codemod-utils';

// Given: const { isNativeError } = require('node:util');
// removeBinding(node, 'isNativeError') → Returns line range to remove entire statement

// Given: const util = require('node:util');
// removeBinding(node, 'util') → Returns line range to remove entire statement
```

#### `updateBinding(node, { old, new })`

Updates a specific binding from imports/requires. It can be used to replace, add, or remove bindings.

```typescript
import { updateBinding } from '@nodejs/codemod-utils';

// Given: const { isNativeError } = require('node:util');
// updateBinding(node, {old: 'isNativeError', new: 'types'}) → Edit to: const { types } = require('node:util');

// Given: const { isNativeError } = require('node:util');
// updateBinding(node, {old: undefined, new: 'types'}) → Edit to: const { isNativeError, types } = require('node:util');

// Given: const { isNativeError, types } = require('node:util');
// updateBinding(node, {old: isNativeError, new: undefined}) → Works exactly as removeBinding util: const { types } = require('node:util');
```

### Code Manipulation
Expand Down Expand Up @@ -230,5 +250,3 @@ const { types: t } = require('util'); // → t.isNativeError
import { types } from 'node:util'; // → types.isNativeError
const util = require('node:util'); // → util.types.isNativeError
```

This unified approach ensures your codemods work correctly regardless of how developers import Node.js modules in their projects.
98 changes: 82 additions & 16 deletions utils/src/ast-grep/remove-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { describe, it } from "node:test";
import astGrep from "@ast-grep/napi";
import dedent from "dedent";
import { removeBinding } from "./remove-binding.ts";
import type Js from "@codemod.com/jssg-types/langs/javascript";
import type { SgNode } from "@codemod.com/jssg-types/main";

describe("remove-binding", () => {
it("should remove the entire require statement when the only imported binding is removed", () => {
Expand All @@ -11,7 +13,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const requireStatement = node.find({
rule: {
Expand All @@ -35,7 +37,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const requireStatement = node.find({
rule: {
Expand All @@ -55,7 +57,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -79,7 +81,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const requireStatement = node.find({
rule: {
Expand All @@ -101,7 +103,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const requireStatement = node.find({
rule: {
Expand All @@ -125,7 +127,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -149,7 +151,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -169,7 +171,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -193,7 +195,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -212,7 +214,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -236,7 +238,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -258,7 +260,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -277,7 +279,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -301,7 +303,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -323,7 +325,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -345,7 +347,7 @@ describe("remove-binding", () => {
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root();
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
Expand All @@ -360,4 +362,68 @@ describe("remove-binding", () => {
assert.strictEqual(change?.lineToRemove, undefined);
assert.strictEqual(sourceCode, `const { types: { isMap } } = require("util");`);
});

it("Should remove the line in member expression scenarios", () => {
const code = dedent`
const Buffer = require("buffer").Buffer;
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
kind: "lexical_declaration",
},
});

const change = removeBinding(importStatement!, "Buffer");

assert.notEqual(change, undefined);
assert.strictEqual(change?.edit, undefined);
assert.deepEqual(change?.lineToRemove, {
end: {
column: 40,
index: 40,
line: 0,
},
start: {
column: 0,
index: 0,
line: 0,
},
});
});

it("Should remove the line when the accessed property is different from the identifier", () => {
const code = dedent`
const Buffer = require("buffer").SlowBuffer
`;

const rootNode = astGrep.parse(astGrep.Lang.JavaScript, code);
const node = rootNode.root() as SgNode<Js>;

const importStatement = node.find({
rule: {
kind: "lexical_declaration",
},
});

const change = removeBinding(importStatement!, "Buffer");

assert.notEqual(change, undefined);
assert.strictEqual(change?.edit, undefined);
assert.deepEqual(change?.lineToRemove, {
end: {
column: 43,
index: 43,
line: 0,
},
start: {
column: 0,
index: 0,
line: 0,
},
});
});
});
Loading