-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat: Add codemod for DEP0185 - Instantiating node:repl classes without new
#242
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 4 commits into
main
from
copilot/add-new-keyword-to-repl-instantiation
Oct 31, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
45ade64
Initial plan
Copilot ccb5f2c
Add repl-classes-with-new codemod for DEP0185
Copilot a3d51c6
Address review feedback: add Recoverable class, dynamic imports, impr…
Copilot 04b5b44
Update author to "GitHub Copilot" in codemod.yaml and package.json
Copilot 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
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,53 @@ | ||
| # `repl.REPLServer` DEP0185 | ||
|
|
||
| This recipe provides a guide for migrating from the deprecated instantiation of `node:repl` classes without `new` to proper class instantiation in Node.js. | ||
|
|
||
| See [DEP0185](https://nodejs.org/api/deprecations.html#DEP0185). | ||
|
|
||
| ## Examples | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| const repl = require("node:repl"); | ||
| const server = repl.REPLServer(); | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| const repl = require("node:repl"); | ||
| const server = new repl.REPLServer(); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| const { REPLServer } = require("node:repl"); | ||
| const server = REPLServer({ prompt: ">>> " }); | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| const { REPLServer } = require("node:repl"); | ||
| const server = new REPLServer({ prompt: ">>> " }); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| import { REPLServer } from "node:repl"; | ||
| const server = REPLServer(); | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| import { REPLServer } from "node:repl"; | ||
| const server = new REPLServer(); | ||
| ``` | ||
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/repl-classes-with-new" | ||
| version: 1.0.0 | ||
| description: "Handle DEP0185: Instantiating node:repl classes without new" | ||
| author: GitHub Copilot | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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/repl-classes-with-new", | ||
| "version": "1.0.0", | ||
| "description": "Handle DEP0185: Instantiating node:repl 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/repl-classes-with-new", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "GitHub Copilot", | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/repl-classes-with-new/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "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,64 @@ | ||
| 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'; | ||
| import type JS from "@codemod.com/jssg-types/langs/javascript"; | ||
|
|
||
| /** | ||
| * Classes of the repl module | ||
| */ | ||
| const CLASS_NAMES = [ | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'REPLServer', | ||
| ]; | ||
|
|
||
| /** | ||
| * Transform function that converts deprecated node:repl classes to use the `new` keyword | ||
| * | ||
| * Handles: | ||
| * 1. `repl.REPLServer()` → `new repl.REPLServer()` | ||
| * 2. Handles both CommonJS and ESM imports | ||
| * 3. Preserves constructor arguments and assignments | ||
| */ | ||
| export default function transform(root: SgRoot<JS>): string | null { | ||
| const rootNode = root.root(); | ||
| const edits: Edit[] = []; | ||
|
|
||
| const importNodes = getNodeImportStatements(root, 'repl'); | ||
| const requireNodes = getNodeRequireCalls(root, 'repl'); | ||
| const allStatementNodes = [...importNodes, ...requireNodes]; | ||
| const classes = new Set<string>(getReplClassBasePaths(allStatementNodes)); | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return rootNode.commitEdits(edits); | ||
| } | ||
|
|
||
| /** | ||
| * Get the base path of the repl classes | ||
| * | ||
| * @param statements - The import & require statements to search for the repl classes | ||
| * @returns The base path of the repl classes | ||
| */ | ||
| function* getReplClassBasePaths(statements: SgNode<JS>[]) { | ||
| 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,23 @@ | ||
| const repl = require("node:repl"); | ||
|
|
||
| // Example 1: Basic REPL server instantiation | ||
| const server = new repl.REPLServer(); | ||
|
|
||
| // Example 2: REPL server with options | ||
| const server2 = new repl.REPLServer({ | ||
| prompt: "custom> ", | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| // Example 5: Function parameter usage | ||
| function createREPL(options) { | ||
| return new repl.REPLServer(options); | ||
| } | ||
|
|
||
| // Example 6: Variable assignment with configuration | ||
| const customREPL = new repl.REPLServer({ | ||
| prompt: "node> ", | ||
| useColors: true, | ||
| useGlobal: false | ||
| }); |
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,13 @@ | ||
| // Example 3: Destructured import | ||
| const { REPLServer } = require("node:repl"); | ||
| const server = new REPLServer({ prompt: ">>> " }); | ||
|
|
||
| // Example 4: ESM import usage (simulated with require for testing) | ||
| // In real ESM: import { REPLServer } from "node:repl"; | ||
| const server2 = new REPLServer(); | ||
|
|
||
| // Another destructured case | ||
| const server3 = new REPLServer({ | ||
| prompt: "test> ", | ||
| useColors: false | ||
| }); |
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,17 @@ | ||
| import { REPLServer } from "node:repl"; | ||
|
|
||
| // ESM import with no arguments | ||
| const server = new REPLServer(); | ||
|
|
||
| // ESM import with options | ||
| const server2 = new REPLServer({ | ||
| prompt: ">>> ", | ||
| useColors: true | ||
| }); | ||
|
|
||
| // ESM import in function | ||
| function createCustomREPL() { | ||
| return new REPLServer({ | ||
| prompt: "custom> " | ||
| }); | ||
| } |
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 @@ | ||
| const repl = require("node:repl"); | ||
|
|
||
| // Example 1: Basic REPL server instantiation | ||
| const server = repl.REPLServer(); | ||
|
|
||
| // Example 2: REPL server with options | ||
| const server2 = repl.REPLServer({ | ||
| prompt: "custom> ", | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| // Example 5: Function parameter usage | ||
| function createREPL(options) { | ||
| return repl.REPLServer(options); | ||
| } | ||
|
|
||
| // Example 6: Variable assignment with configuration | ||
| const customREPL = repl.REPLServer({ | ||
| prompt: "node> ", | ||
| useColors: true, | ||
| useGlobal: false | ||
| }); |
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,13 @@ | ||
| // Example 3: Destructured import | ||
| const { REPLServer } = require("node:repl"); | ||
| const server = REPLServer({ prompt: ">>> " }); | ||
|
|
||
| // Example 4: ESM import usage (simulated with require for testing) | ||
| // In real ESM: import { REPLServer } from "node:repl"; | ||
| const server2 = REPLServer(); | ||
|
|
||
| // Another destructured case | ||
| const server3 = REPLServer({ | ||
| prompt: "test> ", | ||
| useColors: false | ||
| }); |
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,17 @@ | ||
| import { REPLServer } from "node:repl"; | ||
|
|
||
| // ESM import with no arguments | ||
| const server = REPLServer(); | ||
|
|
||
| // ESM import with options | ||
| const server2 = REPLServer({ | ||
| prompt: ">>> ", | ||
| useColors: true | ||
| }); | ||
|
|
||
| // ESM import in function | ||
| function createCustomREPL() { | ||
| return REPLServer({ | ||
| prompt: "custom> " | ||
| }); | ||
| } |
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 DEP0185 Instantiating node:repl 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 |
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.