-
-
Notifications
You must be signed in to change notification settings - Fork 20
feat(process-assert-to-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?
Changes from 3 commits
a96083c
48c4a1d
91b0098
ccec01a
File filter
Filter by extension
Conversations
Jump to
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.
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). | ||
|
||
## Example | ||
|
||
**Before:** | ||
|
||
```js | ||
process.assert(condition, "Assertion failed"); | ||
``` | ||
|
||
**After:** | ||
|
||
```js | ||
import assert from "node:assert"; | ||
assert(condition, "Assertion failed"); | ||
``` |
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 |
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": "*" | ||
} | ||
} |
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); | ||||||||||||||||||||||||||||||||||||||||||||||
* ``` | ||||||||||||||||||||||||||||||||||||||||||||||
|
* 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); | |
* ``` |
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.
actually my code wasn't working for assert.strictEqual
but is should

https://nodejs.org/api/assert.html
so I kept the assert.strictEqual
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.
Also added the bold
on before and after words
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.
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.
matheusmorett2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
matheusmorett2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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(); | ||
} |
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"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import assert from "node:assert"; | ||
assert(condition, "Assertion failed"); |
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"); |
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"); |
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"); |
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"); |
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(); | ||
} |
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"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
process.assert(condition, "Assertion failed"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
process.assert(config.port, "Port must be configured"); |
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"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const { assert: nodeAssert, env } = require("process"); | ||
nodeAssert(condition, "Assertion valid"); |
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"); |
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 |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -184,7 +184,14 @@ function handleNamedRequireBindings( | |||||||||||||||||||
|
||||||||||||||||||||
const declarations = node.findAll({ | ||||||||||||||||||||
rule: { | ||||||||||||||||||||
kind: "shorthand_property_identifier_pattern", | ||||||||||||||||||||
any: [ | ||||||||||||||||||||
{ | ||||||||||||||||||||
kind: "pair_pattern", | ||||||||||||||||||||
}, | ||||||||||||||||||||
{ | ||||||||||||||||||||
kind: "shorthand_property_identifier_pattern", | ||||||||||||||||||||
}, | ||||||||||||||||||||
Comment on lines
+188
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I found that simplest to read idk what you think about that |
||||||||||||||||||||
] | ||||||||||||||||||||
}, | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -195,7 +202,18 @@ function handleNamedRequireBindings( | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (declarations.length > 1) { | ||||||||||||||||||||
const restDeclarations = declarations.map((d) => d.text()).filter((d) => d !== binding); | ||||||||||||||||||||
const restDeclarations = declarations.map((d) => { | ||||||||||||||||||||
if (d.kind() === 'pair_pattern') { | ||||||||||||||||||||
const alias = d.find({ | ||||||||||||||||||||
rule: { | ||||||||||||||||||||
kind: 'identifier', | ||||||||||||||||||||
} | ||||||||||||||||||||
}) | ||||||||||||||||||||
|
||||||||||||||||||||
return alias.text() | ||||||||||||||||||||
} | ||||||||||||||||||||
return d.text() | ||||||||||||||||||||
}).filter((d) => d !== binding); | ||||||||||||||||||||
|
||||||||||||||||||||
return { | ||||||||||||||||||||
edit: objectPattern.replace(`{ ${restDeclarations.join(", ")} }`), | ||||||||||||||||||||
|
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 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.
Ps: verify the link I am on mobile for this review