-
-
Notifications
You must be signed in to change notification settings - Fork 18
feat(http-classes-with-new): add codemod for node:http
classes instantiation with new
keyword
#179
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
Merged
JakobJingleheimer
merged 12 commits into
nodejs:main
from
max-programming:http-classes-with-new
Sep 3, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3053654
feat(http-classes-with-new): add codemod for migrating deprecated nod…
max-programming f5056ae
chore(http-classes-with-new): format with biome
max-programming f955c9d
refactor(workflow): simplify class collection by initializing Set wit…
max-programming 2bc638d
refactor(http-classes-with-new): rename variable as it's a constant
max-programming 8c2ecc5
docs(http-classes-with-new): improve jsdoc
max-programming e2df92a
docs(http-classes-with-new): add JSDoc comment for CLASS_NAMES constant
max-programming dcaf572
refactor(http-classes-with-new): use better solution to handle multip…
max-programming de64673
fix(http-classes-with-new): syntax error fix
max-programming d42823b
Merge branch 'main' into http-classes-with-new
max-programming 9e54556
Merge branch 'main' into http-classes-with-new
max-programming 50585bc
Apply suggested changes
max-programming 957b2f0
Merge branch 'main' into http-classes-with-new
max-programming 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,33 @@ | ||
# `http.request` DEP0195 | ||
|
||
This recipe provides a guide for migrating from the deprecated `http.request` and its synchronous and promise-based counterparts to the new `http.request` method in Node.js. | ||
|
||
See [DEP0195](https://nodejs.org/api/deprecations.html#DEP0195). | ||
|
||
## Examples | ||
|
||
**Before:** | ||
|
||
```js | ||
// import { IncomingMessage, ClientRequest } from "node:http"; | ||
// const http = require("node:http"); | ||
|
||
const message = http.OutgoingMessage(); | ||
const response = http.ServerResponse(socket); | ||
|
||
const incoming = IncomingMessage(socket); | ||
const request = ClientRequest(options); | ||
``` | ||
|
||
**After:** | ||
|
||
```js | ||
// import { IncomingMessage, ClientRequest } from "node:http"; | ||
// const http = require("node:http"); | ||
|
||
const message = new http.OutgoingMessage(); | ||
const response = new http.ServerResponse(socket); | ||
|
||
const incoming = new IncomingMessage(socket); | ||
const request = new ClientRequest(options); | ||
``` |
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/http-classes-with-new" | ||
version: 1.0.0 | ||
description: "Handle DEDEP0195: Instantiating node:http classes without new" | ||
author: Usman S. | ||
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,24 @@ | ||
{ | ||
"name": "@nodejs/http-classes-with-new", | ||
"version": "1.0.0", | ||
"description": "Handle DEP0195: Instantiating node:http classes without new.", | ||
"type": "module", | ||
"scripts": { | ||
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nodejs/userland-migrations.git", | ||
"directory": "recipes/http-classes-with-new", | ||
"bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
}, | ||
"author": "Usman S.", | ||
"license": "MIT", | ||
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/http-classes-with-new/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,71 @@ | ||
import { getNodeImportStatements } from '@nodejs/codemod-utils/ast-grep/import-statement'; | ||
import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; | ||
import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; | ||
import type { SgRoot, Edit, SgNode } from '@codemod.com/jssg-types/main'; | ||
|
||
/** | ||
* Classes of the http module | ||
*/ | ||
const CLASS_NAMES = [ | ||
'Agent', | ||
'ClientRequest', | ||
'IncomingMessage', | ||
'OutgoingMessage', | ||
'Server', | ||
'ServerResponse', | ||
]; | ||
|
||
/** | ||
* Transform function that converts deprecated node:http classes to use the `new` keyword | ||
* | ||
* Handles: | ||
* 1. `http.Agent()` → `new http.Agent()` | ||
* 2. `http.ClientRequest()` → `new http.ClientRequest()` | ||
* 3. `http.IncomingMessage()` → `new http.IncomingMessage()` | ||
* 4. `http.OutgoingMessage()` → `new http.OutgoingMessage()` | ||
* 5. `http.Server()` → `new http.Server()` | ||
* 6. `http.ServerResponse() → `new http.ServerResponse()` | ||
*/ | ||
export default function transform(root: SgRoot): string | null { | ||
const rootNode = root.root(); | ||
const edits: Edit[] = []; | ||
|
||
const importNodes = getNodeImportStatements(root, 'http'); | ||
const requireNodes = getNodeRequireCalls(root, 'http'); | ||
const allStatementNodes = [...importNodes, ...requireNodes]; | ||
const classes = new Set<string>(getHttpClassBasePaths(allStatementNodes)); | ||
|
||
for (const cls of classes) { | ||
const classesWithoutNew = rootNode.findAll({ | ||
rule: { | ||
not: { follows: { pattern: 'new' } }, | ||
pattern: `${cls}($$$ARGS)`, | ||
}, | ||
}); | ||
|
||
for (const clsWithoutNew of classesWithoutNew) { | ||
edits.push(clsWithoutNew.replace(`new ${clsWithoutNew.text()}`)); | ||
} | ||
} | ||
|
||
if (edits.length === 0) return null; | ||
|
||
return rootNode.commitEdits(edits); | ||
} | ||
|
||
/** | ||
* Get the base path of the http classes | ||
* | ||
* @param statements - The import & require statements to search for the http classes | ||
* @returns The base path of the http classes | ||
*/ | ||
function* getHttpClassBasePaths(statements: SgNode[]) { | ||
for (const cls of CLASS_NAMES) { | ||
for (const stmt of statements) { | ||
const resolvedPath = resolveBindingPath(stmt, `$.${cls}`); | ||
if (resolvedPath) { | ||
yield resolvedPath; | ||
} | ||
} | ||
} | ||
} |
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,8 @@ | ||
const http = require("node:http"); | ||
|
||
const agent = new http.Agent(); | ||
const request = new http.ClientRequest(options); | ||
const incoming = new http.IncomingMessage(socket); | ||
const message = new http.OutgoingMessage(); | ||
const server = new http.Server(); | ||
const response = new http.ServerResponse(socket); |
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,15 @@ | ||
import { | ||
Agent, | ||
ClientRequest, | ||
IncomingMessage, | ||
OutgoingMessage, | ||
Server, | ||
ServerResponse, | ||
} from "node:http"; | ||
|
||
const agent = new Agent(); | ||
const request = new ClientRequest(options); | ||
const incoming = new IncomingMessage(socket); | ||
const message = new OutgoingMessage(); | ||
const server = new Server(); | ||
const response = new ServerResponse(socket); |
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,8 @@ | ||
const http = require("node:http"); | ||
|
||
const agent = http.Agent(); | ||
const request = http.ClientRequest(options); | ||
const incoming = http.IncomingMessage(socket); | ||
const message = http.OutgoingMessage(); | ||
const server = http.Server(); | ||
const response = http.ServerResponse(socket); |
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,15 @@ | ||
import { | ||
Agent, | ||
ClientRequest, | ||
IncomingMessage, | ||
OutgoingMessage, | ||
Server, | ||
ServerResponse, | ||
} from "node:http"; | ||
|
||
const agent = Agent(); | ||
const request = ClientRequest(options); | ||
const incoming = IncomingMessage(socket); | ||
const message = OutgoingMessage(); | ||
const server = Server(); | ||
const response = ServerResponse(socket); |
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,23 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowImportingTsExtensions": true, | ||
"allowJs": true, | ||
"alwaysStrict": true, | ||
"baseUrl": "./", | ||
"declaration": true, | ||
"declarationMap": true, | ||
"emitDeclarationOnly": true, | ||
"lib": ["ESNext", "DOM"], | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"noImplicitThis": true, | ||
"removeComments": true, | ||
"strict": true, | ||
"stripInternal": true, | ||
"target": "esnext" | ||
}, | ||
"include": ["./"], | ||
"exclude": [ | ||
"tests/**" | ||
] | ||
} |
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 DEDEP0195 Instantiating node:http classes without new. | ||
js-ast-grep: | ||
js_file: src/workflow.ts | ||
base_path: . | ||
include: | ||
- "**/*.js" | ||
- "**/*.jsx" | ||
- "**/*.mjs" | ||
- "**/*.cjs" | ||
- "**/*.cts" | ||
- "**/*.mts" | ||
- "**/*.ts" | ||
- "**/*.tsx" | ||
exclude: | ||
- "**/node_modules/**" | ||
language: typescript |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.