-
-
Notifications
You must be signed in to change notification settings - Fork 29
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 all 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
Some comments aren't visible on the classic Files Changed page.
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,39 @@ | ||
| # `repl` classes 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). | ||
|
|
||
| ## Example | ||
|
|
||
| **Before:** | ||
|
|
||
| ```js | ||
| const repl = require("node:repl"); | ||
| const { REPLServer, Recoverable } = require("node:repl"); | ||
| import { REPLServer } from "node:repl"; | ||
| const { REPLServer: REPL } = await import("node:repl"); | ||
|
|
||
| // Missing 'new' keyword | ||
| const server1 = repl.REPLServer(); | ||
| const server2 = REPLServer({ prompt: ">>> " }); | ||
| const server3 = repl.Recoverable(); | ||
| const error = Recoverable(new SyntaxError()); | ||
| const server4 = REPL({ prompt: ">>> " }); | ||
| ``` | ||
|
|
||
| **After:** | ||
|
|
||
| ```js | ||
| const repl = require("node:repl"); | ||
| const { REPLServer, Recoverable } = require("node:repl"); | ||
| import { REPLServer } from "node:repl"; | ||
| const { REPLServer: REPL } = await import("node:repl"); | ||
|
|
||
| // With 'new' keyword | ||
| const server1 = new repl.REPLServer(); | ||
| const server2 = new REPLServer({ prompt: ">>> " }); | ||
| const server3 = new repl.Recoverable(); | ||
| const error = new Recoverable(new SyntaxError()); | ||
| const server4 = new REPL({ prompt: ">>> " }); | ||
| ``` |
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 | ||
| 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", | ||
| "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,72 @@ | ||
| 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 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 = [ | ||
| 'REPLServer', | ||
| 'Recoverable', | ||
| ]; | ||
|
|
||
| /** | ||
| * Transform function that converts deprecated node:repl classes to use the `new` keyword | ||
| * | ||
| * Handles: | ||
| * 1. `repl.REPLServer()` → `new repl.REPLServer()` | ||
| * 2. `repl.Recoverable()` → `new repl.Recoverable()` | ||
| * 3. Handles both CommonJS, ESM imports, and dynamic imports | ||
| * 4. Preserves constructor arguments and assignments | ||
| */ | ||
| export default function transform(root: SgRoot<JS>): string | null { | ||
| const rootNode = root.root(); | ||
| const edits: Edit[] = []; | ||
|
|
||
| const allStatementNodes = [ | ||
| ...getNodeImportStatements(root, 'repl'), | ||
| ...getNodeRequireCalls(root, 'repl'), | ||
| ...getNodeImportCalls(root, 'repl'), | ||
| ]; | ||
|
|
||
| // if no imports are present it means that we don't need to process the file | ||
| if (!allStatementNodes.length) return null; | ||
|
|
||
| const classes = new Set<string>(getReplClassBasePaths(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) return null; | ||
|
|
||
| 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,26 @@ | ||
| 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 3: Recoverable class | ||
| const error = new repl.Recoverable(new SyntaxError()); | ||
|
|
||
| // Example 4: Function parameter usage | ||
| function createREPL(options) { | ||
| return new repl.REPLServer(options); | ||
| } | ||
|
|
||
| // Example 5: 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,15 @@ | ||
| // Destructured import | ||
| const { REPLServer, Recoverable } = require("node:repl"); | ||
| const server = new REPLServer({ prompt: ">>> " }); | ||
|
|
||
| // Recoverable without new | ||
| const error = new Recoverable(new SyntaxError()); | ||
|
|
||
| // Another destructured case | ||
| const server2 = new REPLServer(); | ||
|
|
||
| // With options | ||
| 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,14 @@ | ||
| // Dynamic import with await | ||
| const { REPLServer, Recoverable } = await import("node:repl"); | ||
|
|
||
| // REPLServer without new | ||
| const server = new REPLServer(); | ||
|
|
||
| // Recoverable without new | ||
| const error = new Recoverable(new SyntaxError()); | ||
|
|
||
| // With options | ||
| const server2 = new REPLServer({ | ||
| prompt: ">>> ", | ||
| useColors: true | ||
| }); |
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,26 @@ | ||
| 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 3: Recoverable class | ||
| const error = repl.Recoverable(new SyntaxError()); | ||
|
|
||
| // Example 4: Function parameter usage | ||
| function createREPL(options) { | ||
| return repl.REPLServer(options); | ||
| } | ||
|
|
||
| // Example 5: 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,15 @@ | ||
| // Destructured import | ||
| const { REPLServer, Recoverable } = require("node:repl"); | ||
| const server = REPLServer({ prompt: ">>> " }); | ||
|
|
||
| // Recoverable without new | ||
| const error = Recoverable(new SyntaxError()); | ||
|
|
||
| // Another destructured case | ||
| const server2 = REPLServer(); | ||
|
|
||
| // With options | ||
| 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,14 @@ | ||
| // Dynamic import with await | ||
| const { REPLServer, Recoverable } = await import("node:repl"); | ||
|
|
||
| // REPLServer without new | ||
| const server = REPLServer(); | ||
|
|
||
| // Recoverable without new | ||
| const error = Recoverable(new SyntaxError()); | ||
|
|
||
| // With options | ||
| const server2 = REPLServer({ | ||
| prompt: ">>> ", | ||
| useColors: true | ||
| }); |
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 |
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.