Skip to content

Commit f1cb842

Browse files
avivkellermcollina
authored andcommitted
repl: runtime deprecate instantiating without new
PR-URL: #54869 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> (cherry picked from commit 0368f2f)
1 parent 3e417f5 commit f1cb842

6 files changed

Lines changed: 30 additions & 9 deletions

File tree

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3744,12 +3744,15 @@ It is recommended to use the `new` qualifier instead. This applies to all Zlib c
37443744

37453745
<!-- YAML
37463746
changes:
3747+
- version: v22.22.3
3748+
pr-url: https://github.com/nodejs/node/pull/54869
3749+
description: Runtime deprecation.
37473750
- version: v22.9.0
37483751
pr-url: https://github.com/nodejs/node/pull/54842
37493752
description: Documentation-only deprecation.
37503753
-->
37513754

3752-
Type: Documentation-only
3755+
Type: Runtime
37533756

37543757
Instantiating classes without the `new` qualifier exported by the `node:repl` module is deprecated.
37553758
It is recommended to use the `new` qualifier instead. This applies to all REPL classes, including

lib/internal/util.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ function deprecate(fn, msg, code, useEmitSync, modifyPrototype = true) {
215215
return deprecated;
216216
}
217217

218+
function deprecateInstantiation(target, code, ...args) {
219+
assert(typeof code === 'string');
220+
221+
getDeprecationWarningEmitter(code, `Instantiating ${target.name} without the 'new' keyword has been deprecated.`, target)();
222+
223+
return ReflectConstruct(target, args);
224+
}
225+
218226
function decorateErrorStack(err) {
219227
if (!(isError(err) && err.stack) || err[decorated_private_symbol])
220228
return;

lib/repl.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const {
111111
decorateErrorStack,
112112
isError,
113113
deprecate,
114+
deprecateInstantiation,
114115
SideEffectFreeRegExpPrototypeSymbolReplace,
115116
SideEffectFreeRegExpPrototypeSymbolSplit,
116117
} = require('internal/util');
@@ -264,12 +265,7 @@ function REPLServer(prompt,
264265
ignoreUndefined,
265266
replMode) {
266267
if (!(this instanceof REPLServer)) {
267-
return new REPLServer(prompt,
268-
stream,
269-
eval_,
270-
useGlobal,
271-
ignoreUndefined,
272-
replMode);
268+
return deprecateInstantiation(REPLServer, 'DEP0185', prompt, stream, eval_, useGlobal, ignoreUndefined, replMode);
273269
}
274270

275271
let options;

test/parallel/test-repl-preview-without-inspector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function runAndWait(cmds, repl) {
6565
return promise;
6666
}
6767

68-
const repl = REPLServer({
68+
const repl = new REPLServer({
6969
prompt: PROMPT,
7070
stream: new REPLStream(),
7171
ignoreUndefined: true,

test/parallel/test-repl-preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function runAndWait(cmds, repl) {
5757
}
5858

5959
async function tests(options) {
60-
const repl = REPLServer({
60+
const repl = new REPLServer({
6161
prompt: PROMPT,
6262
stream: new REPLStream(),
6363
ignoreUndefined: true,

test/parallel/test-repl.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,17 @@ function event(ee, expected) {
10501050
}));
10511051
});
10521052
}
1053+
1054+
{
1055+
const server = repl.REPLServer();
1056+
common.expectWarning({
1057+
DeprecationWarning: {
1058+
DEP0185: 'Instantiating REPLServer without the \'new\' keyword has been deprecated.',
1059+
// For the 'url.format' test-case.
1060+
DEP0169:
1061+
'`url.parse()` behavior is not standardized and prone to errors that have security implications. ' +
1062+
'Use the WHATWG URL API instead. CVEs are not issued for `url.parse()` vulnerabilities.',
1063+
}
1064+
});
1065+
server.emit('line', '.exit');
1066+
}

0 commit comments

Comments
 (0)