Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions recipes/repl-classes-with-new/README.md
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();
```
21 changes: 21 additions & 0 deletions recipes/repl-classes-with-new/codemod.yaml
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
24 changes: 24 additions & 0 deletions recipes/repl-classes-with-new/package.json
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": "*"
}
}
64 changes: 64 additions & 0 deletions recipes/repl-classes-with-new/src/workflow.ts
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 = [
'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));

for (const cls of classes) {
const classesWithoutNew = rootNode.findAll({
rule: {
not: { follows: { pattern: 'new' } },
pattern: `${cls}($$$ARGS)`,
},
});

for (const clsWithoutNew of classesWithoutNew) {
edits.push(clsWithoutNew.replace(`new ${clsWithoutNew.text()}`));
}
}

if (edits.length === 0) return null;

return rootNode.commitEdits(edits);
}

/**
* Get the base path of the 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;
}
}
}
}
23 changes: 23 additions & 0 deletions recipes/repl-classes-with-new/tests/expected/file-1.js
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
});
13 changes: 13 additions & 0 deletions recipes/repl-classes-with-new/tests/expected/file-2.js
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
});
17 changes: 17 additions & 0 deletions recipes/repl-classes-with-new/tests/expected/file-3.mjs
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> "
});
}
23 changes: 23 additions & 0 deletions recipes/repl-classes-with-new/tests/input/file-1.js
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
});
13 changes: 13 additions & 0 deletions recipes/repl-classes-with-new/tests/input/file-2.js
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
});
17 changes: 17 additions & 0 deletions recipes/repl-classes-with-new/tests/input/file-3.mjs
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> "
});
}
23 changes: 23 additions & 0 deletions recipes/repl-classes-with-new/tsconfig.json
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/**"
]
}
25 changes: 25 additions & 0 deletions recipes/repl-classes-with-new/workflow.yaml
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