Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions recipes/util-extend-to-object-assign/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `util._extend` DEP0060

This recipe transforms the usage of deprecated `util._extend()` to use `Object.assign()`.

See [DEP0060](https://nodejs.org/api/deprecations.html#DEP0060).

## Example

```diff
- const util = require("node:util");
- const target = { a: 1 };
- const source = { b: 2 };
- const result = util._extend(target, source);
+ const target = { a: 1 };
+ const source = { b: 2 };
+ const result = Object.assign(target, source);
```
21 changes: 21 additions & 0 deletions recipes/util-extend-to-object-assign/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
schema_version: "1.0"
name: "@nodejs/util-extend-to-object-assign"
version: 1.0.0
description: "Handle DEP0060 by replacing `util._extend()` with `Object.assign()`."
author: GitHub Copilot
license: MIT
workflow: workflow.yaml
category: migration

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration

registry:
access: public
visibility: public
24 changes: 24 additions & 0 deletions recipes/util-extend-to-object-assign/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@nodejs/util-extend-to-object-assign",
"version": "1.0.0",
"description": "Handle DEP0060 by replacing `util._extend()` with `Object.assign()`.",
"type": "module",
"scripts": {
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/util-extend-to-object-assign",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "GitHub Copilot",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/util-extend-to-object-assign/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.0.9"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
133 changes: 133 additions & 0 deletions recipes/util-extend-to-object-assign/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {
getNodeImportStatements,
getNodeImportCalls,
getDefaultImportIdentifier,
} from '@nodejs/codemod-utils/ast-grep/import-statement';
import {
getNodeRequireCalls,
getRequireNamespaceIdentifier,
} from '@nodejs/codemod-utils/ast-grep/require-call';
import { removeBinding } from '@nodejs/codemod-utils/ast-grep/remove-binding';
import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path';
import { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines';
import type { SgRoot, Edit, Range } from '@codemod.com/jssg-types/main';
import type JS from '@codemod.com/jssg-types/langs/javascript';

const method = '_extend';

/**
* Transform function that converts deprecated util._extend() calls
* to Object.assign().
*
* Handles:
* 1. util._extend(target, source) → Object.assign(target, source)
* 2. const { _extend } = require('util'); _extend(target, source) → Object.assign(target, source)
* 3. import { _extend } from 'node:util'; _extend(target, source) → Object.assign(target, source)
* 4. Aliased imports: const { _extend: extend } = require('util'); extend(target, source) → Object.assign(target, source)
*
* Also cleans up unused imports and requires.
*/
export default function transform(root: SgRoot<JS>): string | null {
const rootNode = root.root();
const edits: Edit[] = [];
const linesToRemove: Range[] = [];
const editRanges: Range[] = [];

const importOrRequireNodes = [
...getNodeRequireCalls(root, 'util'),
...getNodeImportStatements(root, 'util'),
...getNodeImportCalls(root, 'util'),
];

// If no util imports/requires, nothing to do
if (!importOrRequireNodes.length) return null;

// 1. Resolve local bindings for util._extend and replace invocations
const localRefs = new Set<string>();

for (const node of importOrRequireNodes) {
const resolved = resolveBindingPath(node, `$.${method}`);
if (resolved) localRefs.add(resolved);

// Workaround for mixed imports (e.g. import util, { _extend } from 'util')
if (node.kind() === 'import_statement') {
const namedSpecifiers = node.findAll({
rule: {
kind: 'import_specifier',
},
});

for (const specifier of namedSpecifiers) {
const nameNode = specifier.field('name');
const aliasNode = specifier.field('alias');

if (nameNode && nameNode.text() === method) {
const localName = aliasNode ? aliasNode.text() : nameNode.text();
localRefs.add(localName);
}
}
}
}

for (const ref of localRefs) {
const calls = rootNode.findAll({
rule: {
kind: 'call_expression',
pattern: `${ref}($$$ARGS)`,
},
});

for (const call of calls) {
const args = call.find({
rule: { kind: 'arguments' },
});

edits.push(call.replace(`Object.assign${args.text()}`));
editRanges.push(call.range());
}
}

// if no edits were made, don't try to clean up imports
if (!edits.length) return null;

// 2. Cleanup imports
for (const node of importOrRequireNodes) {
let nsBinding = '';

const reqNs = getRequireNamespaceIdentifier(node);
const defaultImport = getDefaultImportIdentifier(node);
const namespaceImport = node.find({
rule: {
kind: 'identifier',
inside: {
kind: 'namespace_import',
},
},
});

if (reqNs) {
nsBinding = reqNs.text();
} else if (defaultImport) {
nsBinding = defaultImport.text();
} else if (namespaceImport) {
nsBinding = namespaceImport.text();
}

// Check if namespace binding is still used
if (nsBinding) {
const change = removeBinding(node, nsBinding, {
usageCheck: { ignoredRanges: editRanges },
root: rootNode,
});
if (change?.lineToRemove) linesToRemove.push(change.lineToRemove);
}

const change = removeBinding(node, method);
if (change?.edit) edits.push(change.edit);
if (change?.lineToRemove) linesToRemove.push(change.lineToRemove);
}

const sourceCode = rootNode.commitEdits(edits);

return removeLines(sourceCode, linesToRemove);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({}, {});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({}, {});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({}, {});
18 changes: 18 additions & 0 deletions recipes/util-extend-to-object-assign/tests/expected/edge_cases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as util1 from 'util';
Object.assign({}, {});
util1.types.isDate(new Date());

import util2 from 'node:util';
Object.assign({}, {});
console.log(util2.inspect({}));

const util3 = require('util');
Object.assign({}, {});
registerPlugin(util3);

import util4 from 'util';
Object.assign({}, {});
util4.promisify(() => { });

Object.assign({}, {});

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({}, {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const util = require('util');
Object.assign({}, {});
console.log(util.format('%s', 'hello'));
3 changes: 3 additions & 0 deletions recipes/util-extend-to-object-assign/tests/expected/mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { format } = require('util');
Object.assign({}, {});
console.log(format('%s', 'hello'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = { x: 1 };
const b = { y: 2 };
Object.assign(a, b);
2 changes: 2 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { _extend: extend } = require('util');
extend({}, {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { _extend } = require('util');
_extend({}, {});
2 changes: 2 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { _extend } = await import('node:util');
_extend({}, {});
19 changes: 19 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/edge_cases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as util1 from 'util';
util1._extend({}, {});
util1.types.isDate(new Date());

import util2 from 'node:util';
util2._extend({}, {});
console.log(util2.inspect({}));

const util3 = require('util');
util3._extend({}, {});
registerPlugin(util3);

import util4, { _extend } from 'util';
_extend({}, {});
util4.promisify(() => { });

import { _extend as extend } from 'util';
extend({}, {});

2 changes: 2 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import util from 'node:util';
util._extend({}, {});
3 changes: 3 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/mixed-ns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const util = require('util');
util._extend({}, {});
console.log(util.format('%s', 'hello'));
3 changes: 3 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { _extend, format } = require('util');
_extend({}, {});
console.log(format('%s', 'hello'));
4 changes: 4 additions & 0 deletions recipes/util-extend-to-object-assign/tests/input/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const util = require('util');
const a = { x: 1 };
const b = { y: 2 };
util._extend(a, b);
25 changes: 25 additions & 0 deletions recipes/util-extend-to-object-assign/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "1"

nodes:
- id: apply-transforms
name: Apply AST Transformations
type: automatic
runtime:
type: direct
steps:
- name: "Replace deprecated util._extend() with Object.assign() (DEP0060)"
js-ast-grep:
js_file: src/workflow.ts
base_path: .
include:
- "**/*.js"
- "**/*.jsx"
- "**/*.mjs"
- "**/*.cjs"
- "**/*.cts"
- "**/*.mts"
- "**/*.ts"
- "**/*.tsx"
exclude:
- "**/node_modules/**"
language: typescript
Loading
Loading