-
-
Notifications
You must be signed in to change notification settings - Fork 29
feat(process-assert-to-node-assert): introduce
#200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(process-assert-to-node-assert): introduce
#200
Conversation
AugustinMauroy
left a comment
There was a problem hiding this 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
brunocroh
left a comment
There was a problem hiding this 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
brunocroh
left a comment
There was a problem hiding this 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
AugustinMauroy
left a comment
There was a problem hiding this 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:
- Try to found a logic that handle that.
- Add it at inline comment so we know for the future
|
hey @matheusmorett2 any news on this pr ? |
avivkeller
left a comment
There was a problem hiding this 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?
|
bump @matheusmorett2 |
Co-authored-by: Bruno Rodrigues <[email protected]>
|
Oh fuck ci is red for strange post script strange |
process-assert-to-node-assert): introduce
JakobJingleheimer
left a comment
There was a problem hiding this 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 :)
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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()
| const requireCalls = getNodeRequireCalls(root, 'process'); | ||
| const importStatements = getNodeImportStatements(root, 'process'); | ||
| const allImports = [...requireCalls, ...importStatements]; |
There was a problem hiding this comment.
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.
| let sourceCode = rootNode.commitEdits(edits); | ||
|
|
||
| sourceCode = removeLines(sourceCode, linesToRemove); |
There was a problem hiding this comment.
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.
| let sourceCode = rootNode.commitEdits(edits); | |
| sourceCode = removeLines(sourceCode, linesToRemove); | |
| const sourceCode = removeLines( | |
| rootNode.commitEdits(edits), | |
| linesToRemove, | |
| ); |
| if (alreadyRequiringAssert.length || alreadyImportingAssert.length) | ||
| return sourceCode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (alreadyRequiringAssert.length || alreadyImportingAssert.length) | |
| return sourceCode; | |
| if (alreadyRequiringAssert.length || alreadyImportingAssert.length) return sourceCode; |
| const isCjsFile = filename.endsWith('.cjs'); | ||
| const isMjsFile = filename.endsWith('.mjs'); |
There was a problem hiding this comment.
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:
| 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'); |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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:
- pre-existing
importstatement(s) - pre-existing
require()calls - authoritative file extension
- pjson.type
Closes #197
Add codemod recipe
process-assert-to-assertto handle deprecation DEP0100.Before
After
Fix:
remove-binding.tshandle scenario where have alias in require