-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Open
matheusmorett2
wants to merge
21
commits into
nodejs:main
Choose a base branch
from
matheusmorett2:process-assert-to-assert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a96083c
fix(util/remove-binding): handle scenario where have alias in require
matheusmorett2 48c4a1d
feat(process-assert-to-assert): introduce
matheusmorett2 91b0098
fix: remove test only
matheusmorett2 ccec01a
fix: apply comments suggestions
matheusmorett2 817993b
Merge branch 'main' into pr/200
AugustinMauroy a9fb7f1
doc: update
AugustinMauroy 679f077
update
AugustinMauroy 85589a2
update logic for import type
AugustinMauroy 6b636de
Apply suggestions from code review
AugustinMauroy f1527c2
Merge branch 'main' into pr/200
AugustinMauroy c6996a1
Update package-lock.json
AugustinMauroy fa09574
Merge branch 'main' into pr/200
AugustinMauroy 7b347db
fix
AugustinMauroy 19c7e11
rename
AugustinMauroy da74a79
add import type detection
AugustinMauroy d48be7b
Merge branch 'main' into pr/200
AugustinMauroy aa1c3b6
Merge branch 'main' into pr/200
AugustinMauroy b37052a
Update package-lock.json
AugustinMauroy 1fed5f2
simplify logic
AugustinMauroy 6430621
update detection logic
AugustinMauroy 50ee910
dynamic import
AugustinMauroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # `process.assert` DEP0100 | ||
|
|
||
| This recipe transforms the usage of `process.assert` to use `assert` module. | ||
|
|
||
| See [DEP0100](https://github.com/nodejs/userland-migrations/issues/197). | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Example | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| process.assert(condition, "Assertion failed"); | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| import assert from "node:assert"; | ||
| assert(condition, "Assertion failed"); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/process-assert-to-assert" | ||
| version: 1.0.0 | ||
| description: Handle DEP0100 via transforming `process.assert` to `assert`. | ||
| author: matheusmorett2 | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "@nodejs/process-assert-to-assert", | ||
| "version": "1.0.0", | ||
| "description": "Handle DEP0100 via transforming `process.assert` to `assert`.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l typescript --ignore-whitespace ./src/workflow.ts ./", | ||
| "test:u": "npx codemod jssg test -l typescript -u ./src/workflow.ts ./" | ||
matheusmorett2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/process-assert-to-assert", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "matheusmorett2", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/process-assert-to-assert/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.3" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import type { Edit, Range, Rule, SgNode, SgRoot } from "@codemod.com/jssg-types/main"; | ||
| import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; | ||
| import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; | ||
| import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; | ||
| import { removeBinding } from "@nodejs/codemod-utils/ast-grep/remove-binding"; | ||
| import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines"; | ||
|
|
||
| /** | ||
| * Transforms deprecated `process.assert` usage to the standard `assert` module. | ||
| * | ||
| * Transformations: | ||
| * 1. Replaces all `process.assert` references with `assert` | ||
| * 2. Adds the necessary import/require statement if not already present: | ||
| * - For ESM or files without require calls: adds `import assert from "node:assert"` | ||
| * - For CommonJS (.cjs files or files using require): adds `const assert = require("node:assert")` | ||
| * | ||
| * Examples: | ||
| * | ||
| * Before: | ||
| * ```js | ||
| * process.assert(value); | ||
| * process.assert.strictEqual(a, b); | ||
| * ``` | ||
| * | ||
| * After: | ||
| * ```js | ||
| * import assert from "node:assert"; | ||
| * assert(value); | ||
| * assert.strictEqual(a, b); | ||
| * ``` | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| export default function transform(root: SgRoot): string | null { | ||
| const rootNode = root.root(); | ||
| const edits: Edit[] = []; | ||
| const linesToRemove: Range[] = []; | ||
| const replaceRules: Array< | ||
| { | ||
| importNode?: SgNode | ||
| binding?: string | ||
| rule: Rule | ||
| } | ||
| > = [{ | ||
| rule: { | ||
| kind: 'member_expression', | ||
| pattern: "process.assert", | ||
| } | ||
| }]; | ||
|
|
||
| const requireProcess = getNodeRequireCalls(root, "process"); | ||
| const importProcess = getNodeImportStatements(root, "process"); | ||
|
|
||
| const allProcessImports = [...requireProcess, ...importProcess] | ||
|
|
||
| for (const processImport of allProcessImports) { | ||
| const binding = resolveBindingPath(processImport, "$.assert"); | ||
| replaceRules.push( | ||
| { | ||
| importNode: processImport, | ||
| binding, | ||
| rule: { | ||
| kind: "identifier", | ||
| regex: binding, | ||
| inside: { | ||
| kind: 'call_expression', | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| for (const replaceRule of replaceRules) { | ||
| const nodes = rootNode.findAll({ | ||
| rule: replaceRule.rule | ||
| }) | ||
|
|
||
| for (const node of nodes) { | ||
| if (replaceRule.importNode) { | ||
| const removeBind = removeBinding(replaceRule.importNode, replaceRule.binding) | ||
|
|
||
| if (removeBind.edit) { | ||
| edits.push(removeBind.edit); | ||
| } | ||
|
|
||
| if (removeBind.lineToRemove) { | ||
| linesToRemove.push(removeBind.lineToRemove) | ||
| } | ||
| } | ||
|
|
||
| edits.push(node.replace("assert")) | ||
| } | ||
| } | ||
|
|
||
| let sourceCode = rootNode.commitEdits(edits); | ||
| sourceCode = removeLines(sourceCode, linesToRemove); | ||
|
|
||
| const alreadyRequiringAssert = getNodeRequireCalls(root, "assert"); | ||
| const alreadyImportingAssert = getNodeImportStatements(root, "assert"); | ||
|
|
||
| if (!alreadyRequiringAssert.length && !alreadyImportingAssert.length) { | ||
| const usingRequire = rootNode.find({ | ||
| rule: { | ||
| kind: 'call_expression', | ||
| has: { | ||
| kind: 'identifier', | ||
| field: 'function', | ||
| regex: 'require' | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const isCommonJs = root.filename().includes('.cjs') | ||
|
|
||
| if (Boolean(usingRequire) || isCommonJs) { | ||
| return `const assert = require("node:assert");\n${sourceCode}` | ||
matheusmorett2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return `import assert from "node:assert";\n${sourceCode}` | ||
matheusmorett2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return sourceCode | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
recipes/process-assert-to-assert/tests/expected/inside-a-function.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import assert from "node:assert"; | ||
| function validateInput(input) { | ||
| assert(typeof input === "string", "Input must be string"); | ||
| return input.trim(); | ||
| } |
3 changes: 3 additions & 0 deletions
3
recipes/process-assert-to-assert/tests/expected/mixed-assert-usages.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import assert from "node:assert"; | ||
| assert(config.port, "Port must be configured"); | ||
| assert(config.port, "Port must be configured"); |
2 changes: 2 additions & 0 deletions
2
recipes/process-assert-to-assert/tests/expected/simple-usage.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import assert from "node:assert"; | ||
| assert(condition, "Assertion failed"); |
2 changes: 2 additions & 0 deletions
2
recipes/process-assert-to-assert/tests/expected/using-common-js.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| const assert = require("node:assert"); | ||
| assert(config.port, "Port must be configured"); |
3 changes: 3 additions & 0 deletions
3
recipes/process-assert-to-assert/tests/expected/using-process-import.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import assert from "node:assert"; | ||
| import { env } from "process"; | ||
| assert(condition, "Assertion valid"); |
3 changes: 3 additions & 0 deletions
3
recipes/process-assert-to-assert/tests/expected/using-process-require.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const assert = require("node:assert"); | ||
| const { env } = require("process"); | ||
| assert(condition, "Assertion valid"); |
3 changes: 3 additions & 0 deletions
3
recipes/process-assert-to-assert/tests/expected/using-require.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const assert = require("node:assert"); | ||
| const util = require("node:util"); | ||
| assert(config.port, "Port must be configured"); |
4 changes: 4 additions & 0 deletions
4
recipes/process-assert-to-assert/tests/input/inside-a-function.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| function validateInput(input) { | ||
| process.assert(typeof input === "string", "Input must be string"); | ||
| return input.trim(); | ||
| } |
4 changes: 4 additions & 0 deletions
4
recipes/process-assert-to-assert/tests/input/mixed-assert-usages.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import assert from "node:assert"; | ||
|
|
||
| process.assert(config.port, "Port must be configured"); | ||
| assert(config.port, "Port must be configured"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.assert(condition, "Assertion failed"); |
1 change: 1 addition & 0 deletions
1
recipes/process-assert-to-assert/tests/input/using-common-js.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.assert(config.port, "Port must be configured"); |
2 changes: 2 additions & 0 deletions
2
recipes/process-assert-to-assert/tests/input/using-process-import.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import { assert as nodeAssert, env } from "process"; | ||
| nodeAssert(condition, "Assertion valid"); |
2 changes: 2 additions & 0 deletions
2
recipes/process-assert-to-assert/tests/input/using-process-require.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| const { assert: nodeAssert, env } = require("process"); | ||
| nodeAssert(condition, "Assertion valid"); |
2 changes: 2 additions & 0 deletions
2
recipes/process-assert-to-assert/tests/input/using-require.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| const util = require("node:util"); | ||
| process.assert(config.port, "Port must be configured"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json | ||
|
|
||
| version: "1" | ||
|
|
||
| nodes: | ||
| - id: apply-transforms | ||
| name: Apply AST Transformations | ||
| type: automatic | ||
| steps: | ||
| - name: Handle DEP0100 via transforming `process.assert` to `assert`. | ||
| js-ast-grep: | ||
| js_file: src/workflow.ts | ||
| base_path: . | ||
| include: | ||
| - "**/*.cjs" | ||
| - "**/*.js" | ||
| - "**/*.jsx" | ||
| - "**/*.mjs" | ||
| - "**/*.cts" | ||
| - "**/*.mts" | ||
| - "**/*.ts" | ||
| - "**/*.tsx" | ||
| exclude: | ||
| - "**/node_modules/**" | ||
| language: typescript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 you look at commit history npm was doing some crazy thing after a merge commit. So idk how to fix that