From 081d5e88eeebdd0a0abd35263777c33b21769345 Mon Sep 17 00:00:00 2001 From: Alejandro Espa Date: Wed, 19 Nov 2025 14:05:00 +0100 Subject: [PATCH 1/3] feat(net-setsimultaneousaccepts-migration): create recipe Closes #173 --- package-lock.json | 19 ++++- package.json | 3 +- .../README.md | 17 ++++ .../codemod.yaml | 21 +++++ .../package.json | 24 ++++++ .../src/workflow.ts | 82 +++++++++++++++++++ .../tests/expected/file-1.js | 3 + .../tests/expected/file-2.js | 7 ++ .../tests/expected/file-3.js | 5 ++ .../tests/expected/file-4.js | 6 ++ .../tests/expected/file-5.js | 3 + .../tests/expected/file-6.js | 9 ++ .../tests/input/file-1.js | 4 + .../tests/input/file-2.js | 8 ++ .../tests/input/file-3.js | 6 ++ .../tests/input/file-4.js | 7 ++ .../tests/input/file-5.js | 4 + .../tests/input/file-6.js | 11 +++ .../workflow.yaml | 25 ++++++ 19 files changed, 258 insertions(+), 6 deletions(-) create mode 100644 recipes/net-setSimultaneousAccepts-migration/README.md create mode 100644 recipes/net-setSimultaneousAccepts-migration/codemod.yaml create mode 100644 recipes/net-setSimultaneousAccepts-migration/package.json create mode 100644 recipes/net-setSimultaneousAccepts-migration/src/workflow.ts create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/workflow.yaml diff --git a/package-lock.json b/package-lock.json index ca71bc67..9a8e7260 100644 --- a/package-lock.json +++ b/package-lock.json @@ -402,7 +402,6 @@ "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.28.5.tgz", "integrity": "sha512-e7jT4DxYvIDLk1ZHmU/m/mB19rex9sv0c2ftBtjSBv+kVM/902eh0fINUzD7UwLLNR+jU585GxUJ8/EBfAM5fw==", "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.28.5", @@ -1516,6 +1515,10 @@ "resolved": "recipes/import-assertions-to-attributes", "link": true }, + "node_modules/@nodejs/net-setsimultaneousaccepts-migration": { + "resolved": "recipes/net-setSimultaneousAccepts-migration", + "link": true + }, "node_modules/@nodejs/node-url-to-whatwg-url": { "resolved": "recipes/node-url-to-whatwg-url", "link": true @@ -1574,7 +1577,6 @@ "resolved": "https://registry.npmjs.org/@octokit/core/-/core-5.2.2.tgz", "integrity": "sha512-/g2d4sW9nUDJOMz3mabVQvOGhVa4e/BN/Um7yca9Bb2XTzPPnfTWHWQg+IsEYO7M3Vx+EXvaM/I2pJWIMun1bg==", "license": "MIT", - "peer": true, "dependencies": { "@octokit/auth-token": "^4.0.0", "@octokit/graphql": "^7.1.0", @@ -1772,7 +1774,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.1.tgz", "integrity": "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ==", "license": "MIT", - "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -2091,7 +2092,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.8.19", "caniuse-lite": "^1.0.30001751", @@ -4369,6 +4369,17 @@ "@codemod.com/jssg-types": "^1.0.9" } }, + "recipes/net-setSimultaneousAccepts-migration": { + "name": "@nodejs/net-setsimultaneousaccepts-migration", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@nodejs/codemod-utils": "*" + }, + "devDependencies": { + "@codemod.com/jssg-types": "^1.0.9" + } + }, "recipes/node-url-to-whatwg-url": { "name": "@nodejs/node-url-to-whatwg-url", "version": "1.0.0", diff --git a/package.json b/package.json index 1fbe95b8..cf0ad4af 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,7 @@ "automation", "codemod", "migrations", - "node.js" - ], + "node.js" ], "license": "MIT", "bugs": { "url": "https://github.com/nodejs/userland-migrations/issues" diff --git a/recipes/net-setSimultaneousAccepts-migration/README.md b/recipes/net-setSimultaneousAccepts-migration/README.md new file mode 100644 index 00000000..c57d7342 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/README.md @@ -0,0 +1,17 @@ +# `net._setSimultaneousAccepts()` DEP0121 + +This recipe provides a guide for removing `net._setSimultaneousAccepts()`. + +See [DEP0121](https://nodejs.org/api/deprecations.html#DEP0121). + +## Examples + +**Before:** + +```js +``` + +**After:** + +```js +``` diff --git a/recipes/net-setSimultaneousAccepts-migration/codemod.yaml b/recipes/net-setSimultaneousAccepts-migration/codemod.yaml new file mode 100644 index 00000000..01dfa500 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/codemod.yaml @@ -0,0 +1,21 @@ +schema_version: "1.0" +name: "@nodejs/net-setSimultaneousAccepts-migration" +version: 1.0.0 +description: "Handle DEDEP0121: Remove net._setSimultaneousAccepts()" +author: Alejandro Espa +license: MIT +workflow: workflow.yaml +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + +registry: + access: public + visibility: public diff --git a/recipes/net-setSimultaneousAccepts-migration/package.json b/recipes/net-setSimultaneousAccepts-migration/package.json new file mode 100644 index 00000000..6b9d8ce9 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/package.json @@ -0,0 +1,24 @@ +{ + "name": "@nodejs/net-setsimultaneousaccepts-migration", + "version": "1.0.0", + "description": "Handle DEP0121: Remove net._setSimultaneousAccepts().", + "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/net-setsimultaneousaccepts-migration", + "bugs": "https://github.com/nodejs/userland-migrations/issues" + }, + "author": "Alejandro Espa", + "license": "MIT", + "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/net-setsimultaneousaccepts-migration/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.0.9" + }, + "dependencies": { + "@nodejs/codemod-utils": "*" + } +} diff --git a/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts new file mode 100644 index 00000000..4dd0a429 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts @@ -0,0 +1,82 @@ +import { + getNodeImportStatements, + getNodeImportCalls, +} 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 { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines'; +import type { SgRoot, Edit, Range } from '@codemod.com/jssg-types/main'; +import type Js from '@codemod.com/jssg-types/langs/javascript'; + +/** + * Transform function that removes deprecated net._setSimultaneousAccepts() calls + * + * Handles: + * 1. net._setSimultaneousAccepts(true) → removed + * 2. net._setSimultaneousAccepts(false) → removed + * 3. Works with both CommonJS (require) and ESM (import) syntax + * 4. Handles aliased imports/requires + * + * Note: This was an internal API (DEP0121) that reached End-of-Life in Node.js v24.0.0 + */ +export default function transform(root: SgRoot): string | null { + const rootNode = root.root(); + const edits: Edit[] = []; + const linesToRemove: Range[] = []; + + // Collect all 'node:net' module imports/requires + const allStatements = [ + ...getNodeImportStatements(root, 'node:net'), + ...getNodeImportCalls(root, 'node:net'), + ...getNodeRequireCalls(root, 'node:net'), + ]; + + // If no net module statements found, skip transformation + if (allStatements.length === 0) return null; + + for (const statementNode of allStatements) { + // Resolve the binding path for the net module + const bindingPath = resolveBindingPath(statementNode, '$'); + + if (!bindingPath) continue; + + // Find all calls to _setSimultaneousAccepts on the resolved binding + const callExpressions = rootNode.findAll({ + rule: { + pattern: `${bindingPath}._setSimultaneousAccepts($$$ARGS)`, + }, + }); + + for (const callNode of callExpressions) { + // Find the parent statement (expression_statement) to remove the entire line + let parentStatement = callNode.parent(); + + // Traverse up to find the expression_statement + while (parentStatement && parentStatement.kind() !== 'expression_statement') { + parentStatement = parentStatement.parent(); + } + + if (parentStatement) { + // Get the range of the entire statement including semicolon + const range = parentStatement.range(); + linesToRemove.push(range); + } else { + // Fallback: just remove the call itself if we can't find the statement + edits.push(callNode.replace('')); + } + } + } + + // If no changes were made, return null + if (edits.length === 0 && linesToRemove.length === 0) return null; + + // Apply edits first if any + const sourceCode = edits.length > 0 + ? rootNode.commitEdits(edits) + : rootNode.text(); + + // Then remove the lines + return linesToRemove.length > 0 + ? removeLines(sourceCode, linesToRemove) + : sourceCode; +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js new file mode 100644 index 00000000..0b77a3cf --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js @@ -0,0 +1,3 @@ +const net = require("node:net"); + +const server = net.createServer(); \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js new file mode 100644 index 00000000..f621a46d --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js @@ -0,0 +1,7 @@ +const net = require("node:net"); + +function createServer() { + return net.createServer((socket) => { + // handle connection + }); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js new file mode 100644 index 00000000..c73a4ffd --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js @@ -0,0 +1,5 @@ +const net = require("node:net"); + +module.exports = { + createServer: () => net.createServer() +}; \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js new file mode 100644 index 00000000..c3e6e265 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js @@ -0,0 +1,6 @@ +const net = require("node:net"); + +function initializeApp() { + const server = net.createServer(); + server.listen(3000); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js new file mode 100644 index 00000000..e1a56511 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js @@ -0,0 +1,3 @@ +import net from "node:net"; + +const server = net.createServer(); \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js new file mode 100644 index 00000000..a0b0ba54 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js @@ -0,0 +1,9 @@ +const net = require("node:net"); + +const config = { + port: 8080 +}; + +function setupServer(config) { + return net.createServer().listen(config.port); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js new file mode 100644 index 00000000..df8dd3d8 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js @@ -0,0 +1,4 @@ +const net = require("node:net"); + +net._setSimultaneousAccepts(false); +const server = net.createServer(); \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js new file mode 100644 index 00000000..482cf37d --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js @@ -0,0 +1,8 @@ +const net = require("node:net"); + +function createServer() { + net._setSimultaneousAccepts(true); + return net.createServer((socket) => { + // handle connection + }); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js new file mode 100644 index 00000000..502d4df6 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js @@ -0,0 +1,6 @@ +const net = require("node:net"); + +net._setSimultaneousAccepts(false); +module.exports = { + createServer: () => net.createServer() +}; \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js new file mode 100644 index 00000000..d695bdfb --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js @@ -0,0 +1,7 @@ +const net = require("node:net"); + +function initializeApp() { + net._setSimultaneousAccepts(true); + const server = net.createServer(); + server.listen(3000); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js new file mode 100644 index 00000000..8848956d --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js @@ -0,0 +1,4 @@ +import net from "node:net"; + +net._setSimultaneousAccepts(false); +const server = net.createServer(); \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js new file mode 100644 index 00000000..88cb0e4a --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js @@ -0,0 +1,11 @@ +const net = require("node:net"); + +const config = { + simultaneousAccepts: false, + port: 8080 +}; + +function setupServer(config) { + net._setSimultaneousAccepts(config.simultaneousAccepts); + return net.createServer().listen(config.port); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/workflow.yaml b/recipes/net-setSimultaneousAccepts-migration/workflow.yaml new file mode 100644 index 00000000..e41bcbe7 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/workflow.yaml @@ -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 DEDEP0121 Remove net._setSimultaneousAccepts(). + js-ast-grep: + js_file: src/workflow.ts + base_path: . + include: + - "**/*.js" + - "**/*.jsx" + - "**/*.mjs" + - "**/*.cjs" + - "**/*.cts" + - "**/*.mts" + - "**/*.ts" + - "**/*.tsx" + exclude: + - "**/node_modules/**" + language: typescript From 9084315218e489f2054c8175ceb52e046855b114 Mon Sep 17 00:00:00 2001 From: Alejandro Espa Date: Thu, 20 Nov 2025 14:49:58 +0100 Subject: [PATCH 2/3] Remove variable passed to method --- package.json | 3 +- .../README.md | 68 +++- .../src/workflow.ts | 295 ++++++++++++++++-- .../tests/expected/file-10.js | 5 + .../tests/expected/file-7.js | 6 + .../tests/expected/file-8.js | 7 + .../tests/expected/file-9.js | 5 + .../tests/input/file-10.js | 6 + .../tests/input/file-7.js | 8 + .../tests/input/file-8.js | 8 + .../tests/input/file-9.js | 7 + 11 files changed, 379 insertions(+), 39 deletions(-) create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js diff --git a/package.json b/package.json index cf0ad4af..d7b31336 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "automation", "codemod", "migrations", - "node.js" ], + "node.js" + ], "license": "MIT", "bugs": { "url": "https://github.com/nodejs/userland-migrations/issues" diff --git a/recipes/net-setSimultaneousAccepts-migration/README.md b/recipes/net-setSimultaneousAccepts-migration/README.md index c57d7342..a4da6a4f 100644 --- a/recipes/net-setSimultaneousAccepts-migration/README.md +++ b/recipes/net-setSimultaneousAccepts-migration/README.md @@ -6,12 +6,72 @@ See [DEP0121](https://nodejs.org/api/deprecations.html#DEP0121). ## Examples -**Before:** +### Remove internal API call -```js +```diff +const net = require("node:net"); + +-net._setSimultaneousAccepts(false); + const server = net.createServer(); ``` -**After:** +### Remove from server initialization -```js +```diff +const net = require("node:net"); + + function createServer() { +- net._setSimultaneousAccepts(true); + return net.createServer((socket) => { + // handle connection + }); + } ``` + +### Remove from module setup + +```diff +const net = require("node:net"); + +-net._setSimultaneousAccepts(false); + module.exports = { + createServer: () => net.createServer() + }; +``` + +### Remove from application startup + +```diff +const net = require("node:net"); + + function initializeApp() { +- net._setSimultaneousAccepts(true); + const server = net.createServer(); + server.listen(3000); + } +``` + +### ESM import cleanup + +```diff +import net from "node:net"; + +-net._setSimultaneousAccepts(false); + const server = net.createServer(); +``` + +### Remove from configuration + +```diff +const net = require("node:net"); + + const config = { +- simultaneousAccepts: false, + port: 8080 + }; + + function setupServer(config) { +- net._setSimultaneousAccepts(config.simultaneousAccepts); + return net.createServer().listen(config.port); + } +``` \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts index 4dd0a429..7c915029 100644 --- a/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts +++ b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts @@ -5,7 +5,7 @@ import { import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; import { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines'; -import type { SgRoot, Edit, Range } from '@codemod.com/jssg-types/main'; +import type { SgRoot, Edit, Range, SgNode } from '@codemod.com/jssg-types/main'; import type Js from '@codemod.com/jssg-types/langs/javascript'; /** @@ -16,6 +16,7 @@ import type Js from '@codemod.com/jssg-types/langs/javascript'; * 2. net._setSimultaneousAccepts(false) → removed * 3. Works with both CommonJS (require) and ESM (import) syntax * 4. Handles aliased imports/requires + * 5. Removes unused variables/properties passed to the function * * Note: This was an internal API (DEP0121) that reached End-of-Life in Node.js v24.0.0 */ @@ -24,58 +25,284 @@ export default function transform(root: SgRoot): string | null { const edits: Edit[] = []; const linesToRemove: Range[] = []; - // Collect all 'node:net' module imports/requires - const allStatements = [ + const netImportStatements = getAllNetImportStatements(root); + if (netImportStatements.length === 0) return null; + + for (const statement of netImportStatements) { + processNetImportStatement(rootNode, statement, linesToRemove, edits); + } + + if (edits.length === 0 && linesToRemove.length === 0) return null; + + return applyTransformations(rootNode, edits, linesToRemove); +} + +/** + * Collects all import/require statements for 'node:net' + */ +function getAllNetImportStatements(root: SgRoot): SgNode[] { + return [ ...getNodeImportStatements(root, 'node:net'), ...getNodeImportCalls(root, 'node:net'), ...getNodeRequireCalls(root, 'node:net'), ]; +} - // If no net module statements found, skip transformation - if (allStatements.length === 0) return null; +/** + * Processes a single net import statement and finds all _setSimultaneousAccepts calls + */ +function processNetImportStatement( + rootNode: SgNode, + statementNode: SgNode, + linesToRemove: Range[], + edits: Edit[] +): void { + const bindingPath = resolveBindingPath(statementNode, '$'); + if (!bindingPath) return; + + const callExpressions = findSetSimultaneousAcceptsCalls(rootNode, bindingPath); + + for (const callNode of callExpressions) { + const argNode = callNode.getMatch('ARG'); + + if (argNode) { + handleCallArgument(rootNode, argNode, linesToRemove); + } + + removeCallExpression(callNode, linesToRemove, edits); + } +} - for (const statementNode of allStatements) { - // Resolve the binding path for the net module - const bindingPath = resolveBindingPath(statementNode, '$'); +/** + * Finds all _setSimultaneousAccepts() call expressions + */ +function findSetSimultaneousAcceptsCalls( + rootNode: SgNode, + bindingPath: string +): SgNode[] { + return rootNode.findAll({ + rule: { + pattern: `${bindingPath}._setSimultaneousAccepts($ARG)`, + }, + }); +} + +/** + * Handles the argument passed to _setSimultaneousAccepts() + * If it's a member expression or identifier that's only used here, marks it for removal + */ +function handleCallArgument( + rootNode: SgNode, + argNode: SgNode, + linesToRemove: Range[] +): void { + const argKind = argNode.kind(); + + if (argKind === 'member_expression') { + handleMemberExpressionArgument(rootNode, argNode, linesToRemove); + } else if (argKind === 'identifier') { + handleIdentifierArgument(rootNode, argNode, linesToRemove); + } +} + +/** + * Handles member expression arguments (e.g., config.flag) + * Removes the property from the object if it's only used in this call + */ +function handleMemberExpressionArgument( + rootNode: SgNode, + argNode: SgNode, + linesToRemove: Range[] +): void { + const objectNode = argNode.child(0); + const propertyNode = argNode.child(2); + + if (!objectNode || !propertyNode) return; + + const objectName = objectNode.text(); + const propertyName = propertyNode.text(); + + const propertyRefs = rootNode.findAll({ + rule: { pattern: `${objectName}.${propertyName}` }, + }); + + // Only remove if this is the only reference + if (propertyRefs.length === 1) { + removePropertyFromObjectDeclaration(rootNode, objectName, propertyName, linesToRemove); + } +} + +/** + * Removes a property from an object literal declaration + */ +function removePropertyFromObjectDeclaration( + rootNode: SgNode, + objectName: string, + propertyName: string, + linesToRemove: Range[] +): void { + const objDeclarations = rootNode.findAll({ + rule: { + any: [ + { pattern: `const ${objectName} = $_` }, + { pattern: `let ${objectName} = $_` }, + { pattern: `var ${objectName} = $_` }, + ], + }, + }); + + for (const objDecl of objDeclarations) { + const objectLiterals = objDecl.findAll({ rule: { kind: 'object' } }); - if (!bindingPath) continue; - - // Find all calls to _setSimultaneousAccepts on the resolved binding - const callExpressions = rootNode.findAll({ - rule: { - pattern: `${bindingPath}._setSimultaneousAccepts($$$ARGS)`, - }, - }); - - for (const callNode of callExpressions) { - // Find the parent statement (expression_statement) to remove the entire line - let parentStatement = callNode.parent(); + for (const obj of objectLiterals) { + const pairs = obj.findAll({ rule: { kind: 'pair' } }); - // Traverse up to find the expression_statement - while (parentStatement && parentStatement.kind() !== 'expression_statement') { - parentStatement = parentStatement.parent(); + for (const pair of pairs) { + const key = pair.child(0); + if (key?.text() === propertyName) { + const rangeWithComma = expandRangeToIncludeTrailingComma( + pair.range(), + rootNode.text() + ); + linesToRemove.push(rangeWithComma); + } } + } + } +} - if (parentStatement) { - // Get the range of the entire statement including semicolon - const range = parentStatement.range(); - linesToRemove.push(range); - } else { - // Fallback: just remove the call itself if we can't find the statement - edits.push(callNode.replace('')); +/** + * Expands a range to include a trailing comma if present + */ +function expandRangeToIncludeTrailingComma(range: Range, sourceText: string): Range { + const endPos = range.end.index; + + if (endPos < sourceText.length && sourceText[endPos] === ',') { + return { + start: range.start, + end: { + ...range.end, + index: endPos + 1, + column: range.end.column + 1 } + }; + } + + return range; +} + +/** + * Handles identifier arguments (e.g., a variable name) + * Removes the variable declaration if it's only used in this call + */ +function handleIdentifierArgument( + rootNode: SgNode, + argNode: SgNode, + linesToRemove: Range[] +): void { + const varName = argNode.text().trim(); + + const allIdentifiers = rootNode.findAll({ + rule: { + pattern: varName, + kind: 'identifier', + }, + }); + + // Only remove if there are exactly 2 references (declaration + usage) + if (allIdentifiers.length === 2) { + removeVariableDeclaration(rootNode, varName, linesToRemove); + } +} + +/** + * Removes a variable declaration statement + */ +function removeVariableDeclaration( + rootNode: SgNode, + varName: string, + linesToRemove: Range[] +): void { + const varDeclarationStatements = rootNode.findAll({ + rule: { + any: [ + { pattern: `let ${varName} = $$$_` }, + { pattern: `const ${varName} = $$$_` }, + { pattern: `var ${varName} = $$$_` }, + ], + }, + }); + + for (const declNode of varDeclarationStatements) { + const topLevelStatement = findTopLevelStatement(declNode); + if (topLevelStatement) { + linesToRemove.push(topLevelStatement.range()); } } +} - // If no changes were made, return null - if (edits.length === 0 && linesToRemove.length === 0) return null; +/** + * Finds the top-level statement (direct child of program) for a given node + */ +function findTopLevelStatement(node: SgNode): SgNode | null { + let current: SgNode | null = node; + + while (current) { + const parent = current.parent(); + if (!parent) break; + + if (parent.kind() === 'program') { + return current; + } + + current = parent; + } + + return null; +} - // Apply edits first if any +/** + * Removes the call expression itself + */ +function removeCallExpression( + callNode: SgNode, + linesToRemove: Range[], + edits: Edit[] +): void { + const expressionStatement = findParentExpressionStatement(callNode); + + if (expressionStatement) { + linesToRemove.push(expressionStatement.range()); + } else { + edits.push(callNode.replace('')); + } +} + +/** + * Finds the parent expression_statement node + */ +function findParentExpressionStatement(node: SgNode): SgNode | null { + let current: SgNode | null = node.parent(); + + while (current && current.kind() !== 'expression_statement') { + current = current.parent(); + } + + return current; +} + +/** + * Applies all edits and line removals to the source code + */ +function applyTransformations( + rootNode: SgNode, + edits: Edit[], + linesToRemove: Range[] +): string { const sourceCode = edits.length > 0 ? rootNode.commitEdits(edits) : rootNode.text(); - // Then remove the lines return linesToRemove.length > 0 ? removeLines(sourceCode, linesToRemove) : sourceCode; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js new file mode 100644 index 00000000..db42cad9 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js @@ -0,0 +1,5 @@ +const net = await import("node:net"); + +module.exports = { + createServer: () => net.createServer() +}; \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js new file mode 100644 index 00000000..0250e7da --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js @@ -0,0 +1,6 @@ +const net = require("node:net"); + + +function setupServer(config) { + return net.createServer().listen(8080); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js new file mode 100644 index 00000000..d04068a3 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js @@ -0,0 +1,7 @@ +const net = require("node:net"); + +const simultaneousAccepts = false; + +function setupServer(config) { + return net.createServer().listen(simultaneousAccepts); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js new file mode 100644 index 00000000..729ffe43 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js @@ -0,0 +1,5 @@ +const net = require("node:net"); + +function setupServer(config) { + return net.createServer().listen(8080); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js new file mode 100644 index 00000000..1a01b8c9 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js @@ -0,0 +1,6 @@ +const net = await import("node:net"); + +net._setSimultaneousAccepts(false); +module.exports = { + createServer: () => net.createServer() +}; \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js new file mode 100644 index 00000000..a9c0e89a --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js @@ -0,0 +1,8 @@ +const net = require("node:net"); + +let simultaneousAccepts = false; + +function setupServer(config) { + net._setSimultaneousAccepts(simultaneousAccepts); + return net.createServer().listen(8080); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js new file mode 100644 index 00000000..c4394e25 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js @@ -0,0 +1,8 @@ +const net = require("node:net"); + +const simultaneousAccepts = false; + +function setupServer(config) { + net._setSimultaneousAccepts(simultaneousAccepts); + return net.createServer().listen(simultaneousAccepts); +} \ No newline at end of file diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js new file mode 100644 index 00000000..eeca8d13 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js @@ -0,0 +1,7 @@ +const net = require("node:net"); +const simultaneousAccepts = require("some-module"); + +function setupServer(config) { + net._setSimultaneousAccepts(simultaneousAccepts); + return net.createServer().listen(8080); +} \ No newline at end of file From 0b17324adfefed4ba045d4a5d6f05d0408a77c7c Mon Sep 17 00:00:00 2001 From: Alejandro Espa Date: Fri, 19 Dec 2025 21:26:22 +0100 Subject: [PATCH 3/3] feat(net-seetsimultaneousaccepts-migration): fix comments --- package.json | 2 +- .../README.md | 68 ++++---- .../codemod.yaml | 2 +- .../src/workflow.ts | 146 +++++++++--------- .../tests/expected/file-1.js | 2 +- .../tests/expected/file-10.js | 2 +- .../tests/expected/file-11.js | 18 +++ .../tests/expected/file-12.js | 16 ++ .../tests/expected/file-13.js | 8 + .../tests/expected/file-14.js | 21 +++ .../tests/expected/file-15.js | 15 ++ .../tests/expected/file-2.js | 2 +- .../tests/expected/file-3.js | 2 +- .../tests/expected/file-4.js | 2 +- .../tests/expected/file-5.js | 2 +- .../tests/expected/file-6.js | 2 +- .../tests/expected/file-7.js | 2 +- .../tests/expected/file-8.js | 2 +- .../tests/expected/file-9.js | 2 +- .../tests/input/file-1.js | 2 +- .../tests/input/file-10.js | 2 +- .../tests/input/file-11.js | 19 +++ .../tests/input/file-12.js | 17 ++ .../tests/input/file-13.js | 9 ++ .../tests/input/file-14.js | 22 +++ .../tests/input/file-15.js | 16 ++ .../tests/input/file-2.js | 2 +- .../tests/input/file-3.js | 2 +- .../tests/input/file-4.js | 2 +- .../tests/input/file-5.js | 2 +- .../tests/input/file-6.js | 2 +- .../tests/input/file-7.js | 2 +- .../tests/input/file-8.js | 2 +- .../tests/input/file-9.js | 2 +- 34 files changed, 288 insertions(+), 131 deletions(-) create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-11.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-12.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-13.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-14.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/expected/file-15.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-11.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-12.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-13.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-14.js create mode 100644 recipes/net-setSimultaneousAccepts-migration/tests/input/file-15.js diff --git a/package.json b/package.json index d7b31336..191fe0ba 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "automation", "codemod", "migrations", - "node.js" + "node.js" ], "license": "MIT", "bugs": { diff --git a/recipes/net-setSimultaneousAccepts-migration/README.md b/recipes/net-setSimultaneousAccepts-migration/README.md index a4da6a4f..a04c94e3 100644 --- a/recipes/net-setSimultaneousAccepts-migration/README.md +++ b/recipes/net-setSimultaneousAccepts-migration/README.md @@ -9,69 +9,69 @@ See [DEP0121](https://nodejs.org/api/deprecations.html#DEP0121). ### Remove internal API call ```diff -const net = require("node:net"); - --net._setSimultaneousAccepts(false); - const server = net.createServer(); + const net = require("node:net"); + +- net._setSimultaneousAccepts(false); + const server = net.createServer(); ``` ### Remove from server initialization ```diff -const net = require("node:net"); - - function createServer() { -- net._setSimultaneousAccepts(true); - return net.createServer((socket) => { - // handle connection - }); - } + const net = require("node:net"); + + function createServer() { +- net._setSimultaneousAccepts(true); + return net.createServer((socket) => { + // handle connection + }); + } ``` ### Remove from module setup ```diff -const net = require("node:net"); - --net._setSimultaneousAccepts(false); - module.exports = { - createServer: () => net.createServer() - }; + const net = require("node:net"); + +- net._setSimultaneousAccepts(false); + module.exports = { + createServer: () => net.createServer() + }; ``` ### Remove from application startup ```diff -const net = require("node:net"); - - function initializeApp() { -- net._setSimultaneousAccepts(true); - const server = net.createServer(); - server.listen(3000); - } + const net = require("node:net"); + + function initializeApp() { +- net._setSimultaneousAccepts(true); + const server = net.createServer(); + server.listen(3000); + } ``` ### ESM import cleanup ```diff -import net from "node:net"; - --net._setSimultaneousAccepts(false); - const server = net.createServer(); + import net from "node:net"; + +- net._setSimultaneousAccepts(false); + const server = net.createServer(); ``` ### Remove from configuration ```diff -const net = require("node:net"); - - const config = { + const net = require("node:net"); + + const config = { - simultaneousAccepts: false, port: 8080 }; - + function setupServer(config) { - net._setSimultaneousAccepts(config.simultaneousAccepts); return net.createServer().listen(config.port); } -``` \ No newline at end of file +``` diff --git a/recipes/net-setSimultaneousAccepts-migration/codemod.yaml b/recipes/net-setSimultaneousAccepts-migration/codemod.yaml index 01dfa500..34efd690 100644 --- a/recipes/net-setSimultaneousAccepts-migration/codemod.yaml +++ b/recipes/net-setSimultaneousAccepts-migration/codemod.yaml @@ -1,5 +1,5 @@ schema_version: "1.0" -name: "@nodejs/net-setSimultaneousAccepts-migration" +name: "@nodejs/net-setSimultaneousAccepts-deprecation" version: 1.0.0 description: "Handle DEDEP0121: Remove net._setSimultaneousAccepts()" author: Alejandro Espa diff --git a/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts index 7c915029..8c683bc2 100644 --- a/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts +++ b/recipes/net-setSimultaneousAccepts-migration/src/workflow.ts @@ -26,13 +26,16 @@ export default function transform(root: SgRoot): string | null { const linesToRemove: Range[] = []; const netImportStatements = getAllNetImportStatements(root); - if (netImportStatements.length === 0) return null; + + // If no import found we don't process the file + if (!netImportStatements.length) return null; for (const statement of netImportStatements) { processNetImportStatement(rootNode, statement, linesToRemove, edits); } - if (edits.length === 0 && linesToRemove.length === 0) return null; + //No changes, nothing to do + if (!edits.length && !linesToRemove.length) return null; return applyTransformations(rootNode, edits, linesToRemove); } @@ -42,9 +45,9 @@ export default function transform(root: SgRoot): string | null { */ function getAllNetImportStatements(root: SgRoot): SgNode[] { return [ - ...getNodeImportStatements(root, 'node:net'), - ...getNodeImportCalls(root, 'node:net'), - ...getNodeRequireCalls(root, 'node:net'), + ...getNodeImportStatements(root, 'net'), + ...getNodeImportCalls(root, 'net'), + ...getNodeRequireCalls(root, 'net'), ]; } @@ -58,13 +61,18 @@ function processNetImportStatement( edits: Edit[] ): void { const bindingPath = resolveBindingPath(statementNode, '$'); + if (!bindingPath) return; - const callExpressions = findSetSimultaneousAcceptsCalls(rootNode, bindingPath); + const callExpressions = rootNode.findAll({ + rule: { + pattern: `${bindingPath}._setSimultaneousAccepts($ARG)`, + }, + }); for (const callNode of callExpressions) { const argNode = callNode.getMatch('ARG'); - + if (argNode) { handleCallArgument(rootNode, argNode, linesToRemove); } @@ -73,20 +81,6 @@ function processNetImportStatement( } } -/** - * Finds all _setSimultaneousAccepts() call expressions - */ -function findSetSimultaneousAcceptsCalls( - rootNode: SgNode, - bindingPath: string -): SgNode[] { - return rootNode.findAll({ - rule: { - pattern: `${bindingPath}._setSimultaneousAccepts($ARG)`, - }, - }); -} - /** * Handles the argument passed to _setSimultaneousAccepts() * If it's a member expression or identifier that's only used here, marks it for removal @@ -98,10 +92,9 @@ function handleCallArgument( ): void { const argKind = argNode.kind(); - if (argKind === 'member_expression') { - handleMemberExpressionArgument(rootNode, argNode, linesToRemove); - } else if (argKind === 'identifier') { - handleIdentifierArgument(rootNode, argNode, linesToRemove); + switch(argKind) { + case 'member_expression': handleMemberExpressionArgument(rootNode, argNode, linesToRemove); break; + case 'identifier': handleIdentifierArgument(rootNode, argNode, linesToRemove); break; } } @@ -116,17 +109,17 @@ function handleMemberExpressionArgument( ): void { const objectNode = argNode.child(0); const propertyNode = argNode.child(2); - + if (!objectNode || !propertyNode) return; const objectName = objectNode.text(); const propertyName = propertyNode.text(); - + const propertyRefs = rootNode.findAll({ rule: { pattern: `${objectName}.${propertyName}` }, }); - - // Only remove if this is the only reference + + // Remove when this is the only reference if (propertyRefs.length === 1) { removePropertyFromObjectDeclaration(rootNode, objectName, propertyName, linesToRemove); } @@ -141,33 +134,35 @@ function removePropertyFromObjectDeclaration( propertyName: string, linesToRemove: Range[] ): void { - const objDeclarations = rootNode.findAll({ - rule: { - any: [ - { pattern: `const ${objectName} = $_` }, - { pattern: `let ${objectName} = $_` }, - { pattern: `var ${objectName} = $_` }, - ], - }, - }); - - for (const objDecl of objDeclarations) { - const objectLiterals = objDecl.findAll({ rule: { kind: 'object' } }); - - for (const obj of objectLiterals) { - const pairs = obj.findAll({ rule: { kind: 'pair' } }); - - for (const pair of pairs) { - const key = pair.child(0); - if (key?.text() === propertyName) { - const rangeWithComma = expandRangeToIncludeTrailingComma( - pair.range(), - rootNode.text() - ); - linesToRemove.push(rangeWithComma); - } - } - } + const pairs = rootNode + .findAll({ + rule: { + kind: 'variable_declarator', + has: { + kind: 'identifier', + field: 'name', + pattern: objectName, + }, + }, + }) + .flatMap((decl) => + decl.findAll({ + rule: { + kind: 'pair', + has: { + field: 'key', + regex: propertyName, + }, + }, + }), + ); + + for (const pair of pairs) { + const rangeWithComma = expandRangeToIncludeTrailingComma( + pair.range(), + rootNode.text() + ); + linesToRemove.push(rangeWithComma); } } @@ -176,7 +171,7 @@ function removePropertyFromObjectDeclaration( */ function expandRangeToIncludeTrailingComma(range: Range, sourceText: string): Range { const endPos = range.end.index; - + if (endPos < sourceText.length && sourceText[endPos] === ',') { return { start: range.start, @@ -187,7 +182,7 @@ function expandRangeToIncludeTrailingComma(range: Range, sourceText: string): Ra } }; } - + return range; } @@ -201,14 +196,14 @@ function handleIdentifierArgument( linesToRemove: Range[] ): void { const varName = argNode.text().trim(); - + const allIdentifiers = rootNode.findAll({ rule: { pattern: varName, kind: 'identifier', }, }); - + // Only remove if there are exactly 2 references (declaration + usage) if (allIdentifiers.length === 2) { removeVariableDeclaration(rootNode, varName, linesToRemove); @@ -225,14 +220,15 @@ function removeVariableDeclaration( ): void { const varDeclarationStatements = rootNode.findAll({ rule: { - any: [ - { pattern: `let ${varName} = $$$_` }, - { pattern: `const ${varName} = $$$_` }, - { pattern: `var ${varName} = $$$_` }, - ], + kind: 'variable_declarator', + has: { + kind: 'identifier', + field: 'name', + pattern: varName, + }, }, }); - + for (const declNode of varDeclarationStatements) { const topLevelStatement = findTopLevelStatement(declNode); if (topLevelStatement) { @@ -246,18 +242,18 @@ function removeVariableDeclaration( */ function findTopLevelStatement(node: SgNode): SgNode | null { let current: SgNode | null = node; - + while (current) { const parent = current.parent(); if (!parent) break; - + if (parent.kind() === 'program') { return current; } - + current = parent; } - + return null; } @@ -283,11 +279,11 @@ function removeCallExpression( */ function findParentExpressionStatement(node: SgNode): SgNode | null { let current: SgNode | null = node.parent(); - + while (current && current.kind() !== 'expression_statement') { current = current.parent(); } - + return current; } @@ -299,11 +295,11 @@ function applyTransformations( edits: Edit[], linesToRemove: Range[] ): string { - const sourceCode = edits.length > 0 - ? rootNode.commitEdits(edits) + const sourceCode = edits.length > 0 + ? rootNode.commitEdits(edits) : rootNode.text(); return linesToRemove.length > 0 ? removeLines(sourceCode, linesToRemove) : sourceCode; -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js index 0b77a3cf..3e1b402d 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-1.js @@ -1,3 +1,3 @@ const net = require("node:net"); -const server = net.createServer(); \ No newline at end of file +const server = net.createServer(); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js index db42cad9..6cc1eaaa 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-10.js @@ -2,4 +2,4 @@ const net = await import("node:net"); module.exports = { createServer: () => net.createServer() -}; \ No newline at end of file +}; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-11.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-11.js new file mode 100644 index 00000000..6d2f9654 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-11.js @@ -0,0 +1,18 @@ +const net = require("node:net"); + + +const server = net.createServer((socket) => { + socket.on('data', (data) => { + console.log(data); + }); + socket.write('Hello World!'); + socket.end(); +}); + +server.listen(3000, () => { + console.log('Server listening on port 3000'); +}); + +server.on('connection', (socket) => { + console.log('New connection'); +}); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-12.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-12.js new file mode 100644 index 00000000..a41e9c3a --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-12.js @@ -0,0 +1,16 @@ +const net = require("node:net"); + +const client = net.createConnection({ port: 3000 }, () => { + console.log('connected to server!'); + client.write('world!\r\n'); +}); + + +client.on('data', (data) => { + console.log(data.toString()); + client.end(); +}); + +client.on('end', () => { + console.log('disconnected from server'); +}); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-13.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-13.js new file mode 100644 index 00000000..b6ed10b7 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-13.js @@ -0,0 +1,8 @@ +import net, { isIP, isIPv4, isIPv6 } from "node:net"; + + +const ipv4 = isIPv4('127.0.0.1'); +const ipv6 = isIPv6('::1'); +const ip = isIP('192.168.1.1'); + +console.log({ ipv4, ipv6, ip }); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-14.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-14.js new file mode 100644 index 00000000..fecd44b8 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-14.js @@ -0,0 +1,21 @@ +const net = require("node:net"); + +class ServerManager { + constructor() { + this.server = net.createServer(); + } + + start(port) { + this.server.listen(port); + } + + getAddress() { + return this.server.address(); + } + + stop() { + this.server.close(); + } +} + +module.exports = ServerManager; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-15.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-15.js new file mode 100644 index 00000000..9b87b39d --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-15.js @@ -0,0 +1,15 @@ +const net = require("node:net"); + +const server = net.createServer(); + +server.on('error', (err) => { + console.error(err); +}); + + +server.maxConnections = 10; +server.listen(8080, '0.0.0.0'); + +const socket = new net.Socket(); +socket.connect(8080, 'localhost'); +socket.setTimeout(3000); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js index f621a46d..c90799b7 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-2.js @@ -4,4 +4,4 @@ function createServer() { return net.createServer((socket) => { // handle connection }); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js index c73a4ffd..08793080 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-3.js @@ -2,4 +2,4 @@ const net = require("node:net"); module.exports = { createServer: () => net.createServer() -}; \ No newline at end of file +}; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js index c3e6e265..e6398fa2 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-4.js @@ -3,4 +3,4 @@ const net = require("node:net"); function initializeApp() { const server = net.createServer(); server.listen(3000); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js index e1a56511..5cd16aa9 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-5.js @@ -1,3 +1,3 @@ import net from "node:net"; -const server = net.createServer(); \ No newline at end of file +const server = net.createServer(); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js index a0b0ba54..3c15f15a 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-6.js @@ -6,4 +6,4 @@ const config = { function setupServer(config) { return net.createServer().listen(config.port); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js index 0250e7da..646fffb7 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-7.js @@ -3,4 +3,4 @@ const net = require("node:net"); function setupServer(config) { return net.createServer().listen(8080); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js index d04068a3..bccd83cb 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-8.js @@ -4,4 +4,4 @@ const simultaneousAccepts = false; function setupServer(config) { return net.createServer().listen(simultaneousAccepts); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js index 729ffe43..233209fa 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/expected/file-9.js @@ -2,4 +2,4 @@ const net = require("node:net"); function setupServer(config) { return net.createServer().listen(8080); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js index df8dd3d8..89bcc3b8 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-1.js @@ -1,4 +1,4 @@ const net = require("node:net"); net._setSimultaneousAccepts(false); -const server = net.createServer(); \ No newline at end of file +const server = net.createServer(); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js index 1a01b8c9..b86d73d1 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-10.js @@ -3,4 +3,4 @@ const net = await import("node:net"); net._setSimultaneousAccepts(false); module.exports = { createServer: () => net.createServer() -}; \ No newline at end of file +}; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-11.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-11.js new file mode 100644 index 00000000..353fd789 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-11.js @@ -0,0 +1,19 @@ +const net = require("node:net"); + +net._setSimultaneousAccepts(true); + +const server = net.createServer((socket) => { + socket.on('data', (data) => { + console.log(data); + }); + socket.write('Hello World!'); + socket.end(); +}); + +server.listen(3000, () => { + console.log('Server listening on port 3000'); +}); + +server.on('connection', (socket) => { + console.log('New connection'); +}); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-12.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-12.js new file mode 100644 index 00000000..a689234d --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-12.js @@ -0,0 +1,17 @@ +const net = require("node:net"); + +const client = net.createConnection({ port: 3000 }, () => { + console.log('connected to server!'); + client.write('world!\r\n'); +}); + +net._setSimultaneousAccepts(false); + +client.on('data', (data) => { + console.log(data.toString()); + client.end(); +}); + +client.on('end', () => { + console.log('disconnected from server'); +}); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-13.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-13.js new file mode 100644 index 00000000..dda9da53 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-13.js @@ -0,0 +1,9 @@ +import net, { isIP, isIPv4, isIPv6 } from "node:net"; + +net._setSimultaneousAccepts(true); + +const ipv4 = isIPv4('127.0.0.1'); +const ipv6 = isIPv6('::1'); +const ip = isIP('192.168.1.1'); + +console.log({ ipv4, ipv6, ip }); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-14.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-14.js new file mode 100644 index 00000000..9a50d5c1 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-14.js @@ -0,0 +1,22 @@ +const net = require("node:net"); + +class ServerManager { + constructor() { + this.server = net.createServer(); + net._setSimultaneousAccepts(true); + } + + start(port) { + this.server.listen(port); + } + + getAddress() { + return this.server.address(); + } + + stop() { + this.server.close(); + } +} + +module.exports = ServerManager; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-15.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-15.js new file mode 100644 index 00000000..31ba3103 --- /dev/null +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-15.js @@ -0,0 +1,16 @@ +const net = require("node:net"); + +const server = net.createServer(); + +server.on('error', (err) => { + console.error(err); +}); + +net._setSimultaneousAccepts(false); + +server.maxConnections = 10; +server.listen(8080, '0.0.0.0'); + +const socket = new net.Socket(); +socket.connect(8080, 'localhost'); +socket.setTimeout(3000); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js index 482cf37d..4868322d 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-2.js @@ -5,4 +5,4 @@ function createServer() { return net.createServer((socket) => { // handle connection }); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js index 502d4df6..78b47dd1 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-3.js @@ -3,4 +3,4 @@ const net = require("node:net"); net._setSimultaneousAccepts(false); module.exports = { createServer: () => net.createServer() -}; \ No newline at end of file +}; diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js index d695bdfb..d4154e6c 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-4.js @@ -4,4 +4,4 @@ function initializeApp() { net._setSimultaneousAccepts(true); const server = net.createServer(); server.listen(3000); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js index 8848956d..cf037959 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-5.js @@ -1,4 +1,4 @@ import net from "node:net"; net._setSimultaneousAccepts(false); -const server = net.createServer(); \ No newline at end of file +const server = net.createServer(); diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js index 88cb0e4a..24505c41 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-6.js @@ -8,4 +8,4 @@ const config = { function setupServer(config) { net._setSimultaneousAccepts(config.simultaneousAccepts); return net.createServer().listen(config.port); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js index a9c0e89a..5237bedc 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-7.js @@ -5,4 +5,4 @@ let simultaneousAccepts = false; function setupServer(config) { net._setSimultaneousAccepts(simultaneousAccepts); return net.createServer().listen(8080); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js index c4394e25..e14c6086 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-8.js @@ -5,4 +5,4 @@ const simultaneousAccepts = false; function setupServer(config) { net._setSimultaneousAccepts(simultaneousAccepts); return net.createServer().listen(simultaneousAccepts); -} \ No newline at end of file +} diff --git a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js index eeca8d13..c10d4c99 100644 --- a/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js +++ b/recipes/net-setSimultaneousAccepts-migration/tests/input/file-9.js @@ -4,4 +4,4 @@ const simultaneousAccepts = require("some-module"); function setupServer(config) { net._setSimultaneousAccepts(simultaneousAccepts); return net.createServer().listen(8080); -} \ No newline at end of file +}