Skip to content

Conversation

@matheusmorett2
Copy link

Closes #197

Add codemod recipe process-assert-to-assert to handle deprecation DEP0100.

Before

const { assert: nodeAssert, env } = require("process");
nodeAssert(condition, "Assertion valid");

After

const assert = require("node:assert");
const { env } = require("process");
assert(condition, "Assertion valid");

Fix: remove-binding.ts

handle scenario where have alias in require

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.

I didn't see any test for that kind cases

import process from "node:process"

But good first pr

Copy link
Member

@brunocroh brunocroh left a comment

Choose a reason for hiding this comment

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

Awesome first contribution 🎉, Thank you @matheusmorett2 !

Just small fixes needed

Comment on lines 19 to 30
* Before:
* ```js
* process.assert(value);
* process.assert.strictEqual(a, b);
* ```
*
* After:
* ```js
* import assert from "node:assert";
* assert(value);
* assert.strictEqual(a, b);
* ```
Copy link
Member

Choose a reason for hiding this comment

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

Remove process.assert.strictEqual reference, I think it was added by mistake. This scenario does not exist. Add bold formatting to the before/after text.

Suggested change
* Before:
* ```js
* process.assert(value);
* process.assert.strictEqual(a, b);
* ```
*
* After:
* ```js
* import assert from "node:assert";
* assert(value);
* assert.strictEqual(a, b);
* ```
* **Before**:
* ```js
* process.assert(value);
* ```
*
* **After**:
* ```js
* import assert from "node:assert";
* assert(value);
* ```

Copy link
Author

Choose a reason for hiding this comment

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

actually my code wasn't working for assert.strictEqual but is should

Screenshot 2025-09-06 at 5 15 23 PM

https://nodejs.org/api/assert.html

so I kept the assert.strictEqual

Copy link
Author

Choose a reason for hiding this comment

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

Also added the bold on before and after words

Copy link
Member

Choose a reason for hiding this comment

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

My point is that the TypeScript docs say that the code in the before block, process.assert.strictEqual(a, b), will be updated to use assert.strictEqual.

However, process.assert.strictEqual does not exist, at least I couldn’t find any reference to it.

Copy link
Member

@brunocroh brunocroh left a comment

Choose a reason for hiding this comment

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

Please fix the conflict in the remove-binding file

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.

For require call not in cjs context the transformed code may be wrong.

For example this before

import { createRequire} from 'node:module';
const require = createRequire(import.meta.dirname)

process.assert(true);

Broken after with the current implementation

const assert = require('node:assert');
import { createRequire} from 'node:module';
const require = createRequire(import.meta.dirname)

assert(true);

In this case I expect that require is undefined.

Solutions:

  1. Try to found a logic that handle that.
  2. Add it at inline comment so we know for the future


for (const processImport of allImports) {
const binding = resolveBindingPath(processImport, "$.assert");
replaceRules.push({
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
replaceRules.push({
replaceRules.push({


const processImportsToRemove = new Set<SgNode>();

function processImports(moduleName: "process" | "node:process") {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function processImports(moduleName: "process" | "node:process") {
function processImports(moduleName: string) {

Utilities used inside this function have regex to catch both usage of node:process or process. So you can simplify the logic of this function.

Comment on lines +74 to +98
if (binding) {
replaceRules.push({
importNode: processImport,
binding,
rule: {
kind: "member_expression",
has: {
kind: "identifier",
regex: `^${binding}$`,
field: "object"
}
},
replaceWith: "assert"
});
}

const processUsages = rootNode.findAll({
rule: {
kind: 'member_expression',
has: {
kind: 'identifier',
regex: '^process$'
}
}
});
Copy link
Member

Choose a reason for hiding this comment

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

@brunocroh correct me if I am wrong but here dinging also include process so code after the condition is useless ?

Copy link
Member

Choose a reason for hiding this comment

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

I think so

Comment on lines +188 to +193
{
kind: "pair_pattern",
},
{
kind: "shorthand_property_identifier_pattern",
},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{
kind: "pair_pattern",
},
{
kind: "shorthand_property_identifier_pattern",
},
{
kind: "pair_pattern" },
{ kind: "shorthand_property_identifier_pattern" },

I found that simplest to read idk what you think about that


This recipe transforms the usage of `process.assert` to use `assert` module.

See [DEP0100](https://github.com/nodejs/userland-migrations/issues/197).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
See [DEP0100](https://github.com/nodejs/userland-migrations/issues/197).
See [DEP0100](https://nodejs.org/api/deprecations.html#DEP100).

Copy link
Member

Choose a reason for hiding this comment

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

Ps: verify the link I am on mobile for this review

@JakobJingleheimer JakobJingleheimer added the awaiting author Reviewer has requested something from the author label Sep 23, 2025
@AugustinMauroy
Copy link
Member

hey @matheusmorett2 any news on this pr ?

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

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

I think this could use some refactoring. There's a lot of nesting, among other things.

Can you break it up, and look at the other mods for inspiration?

Comment on lines +39 to +44
const replaceRules: Array<{
importNode?: SgNode;
binding?: string;
rule: Rule;
replaceWith?: string;
}> = [{
Copy link
Member

Choose a reason for hiding this comment

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

Can we define this type outside as ReplaceRule?

}

return sourceCode;
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

const isCommonJs = root.filename().includes('.cjs');

if (Boolean(usingRequire) || isCommonJs) {
return `const assert = require("node:assert");${EOL}${sourceCode}`;
Copy link
Member

Choose a reason for hiding this comment

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

What if the assert variable is already defined?

}
});

const isCommonJs = root.filename().includes('.cjs');
Copy link
Member

Choose a reason for hiding this comment

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

What if the filename is .cjs.mjs?

@JakobJingleheimer JakobJingleheimer added the dep:v23 Migration handles deprecation introduced in node v23 label Oct 16, 2025
@AugustinMauroy
Copy link
Member

bump @matheusmorett2

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

Labels

awaiting author Reviewer has requested something from the author dep:v23 Migration handles deprecation introduced in node v23

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: handle DEP0100 by replacing process.assert() with the assert module

5 participants