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

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

@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?

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

bump @matheusmorett2

@AugustinMauroy AugustinMauroy added awaiting reviewer Author has responded and needs action from the reviewer and removed awaiting author Reviewer has requested something from the author labels Nov 1, 2025
@brunocroh brunocroh self-requested a review November 7, 2025 15:25
@AugustinMauroy
Copy link
Member

Oh fuck ci is red for strange post script strange

@AugustinMauroy AugustinMauroy changed the title feat(process-assert-to-assert): introduce feat(process-assert-to-node-assert): introduce Nov 8, 2025
@AugustinMauroy AugustinMauroy requested a review from a team November 8, 2025 11:20
Copy link
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

Thanks for this! It's looking pretty good :)

Copy link
Member

Choose a reason for hiding this comment

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

This has some changes that I think should not be here.


## Additional Notes

This codemod use [`fs` capability](https://docs.codemod.com/jssg/security) to read the `package.json` file and determine if the project is using ES modules or CommonJS. Based on this information, it adds the appropriate import statement for the `assert` module.
Copy link
Member

Choose a reason for hiding this comment

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

It may not need to do that: node:module.findPackageJSON()

Comment on lines +60 to +62
const requireCalls = getNodeRequireCalls(root, 'process');
const importStatements = getNodeImportStatements(root, 'process');
const allImports = [...requireCalls, ...importStatements];
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 you also need getNodeImportCalls (from the same util as getNodeImportStatements) to handle dynamic imports.

Comment on lines +178 to +180
let sourceCode = rootNode.commitEdits(edits);

sourceCode = removeLines(sourceCode, linesToRemove);
Copy link
Member

Choose a reason for hiding this comment

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

It's kind of weird to set and then immediately overwrite the variable.

Suggested change
let sourceCode = rootNode.commitEdits(edits);
sourceCode = removeLines(sourceCode, linesToRemove);
const sourceCode = removeLines(
rootNode.commitEdits(edits),
linesToRemove,
);

Comment on lines +187 to +188
if (alreadyRequiringAssert.length || alreadyImportingAssert.length)
return sourceCode;
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
if (alreadyRequiringAssert.length || alreadyImportingAssert.length)
return sourceCode;
if (alreadyRequiringAssert.length || alreadyImportingAssert.length) return sourceCode;

Comment on lines +206 to +207
const isCjsFile = filename.endsWith('.cjs');
const isMjsFile = filename.endsWith('.mjs');
Copy link
Member

Choose a reason for hiding this comment

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

Since node supports typescript by default now, I think this should handle that too:

Suggested change
const isCjsFile = filename.endsWith('.cjs');
const isMjsFile = filename.endsWith('.mjs');
const isCjsFile = filename.endsWith('.cjs') || filename.endsWith('.cts');
const isMjsFile = filename.endsWith('.mjs') || filename.endsWith('.mts');

return `const assert = require("node:assert");${EOL}${sourceCode}`;
}

const packageJsonPath = join(process.cwd(), 'package.json');
Copy link
Member

Choose a reason for hiding this comment

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

There's a more robust builtin way to find the package.json: module.findPackageJSON()

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 since this check is last it should be okay: the migration has already determined that the file contents is ambiguous; this will forcibly disambiguate the file, but that's probably okay.

I think best to add a note around here though that the sequence of checks needs to stay:

  1. pre-existing import statement(s)
  2. pre-existing require() calls
  3. authoritative file extension
  4. pjson.type

@JakobJingleheimer JakobJingleheimer added awaiting author Reviewer has requested something from the author and removed awaiting reviewer Author has responded and needs action from the reviewer labels Dec 2, 2025
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