Skip to content

Commit 7f70642

Browse files
committed
repl: runtime deprecate instantiating without new
1 parent a202666 commit 7f70642

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

doc/api/deprecations.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3733,9 +3733,12 @@ changes:
37333733
- version: REPLACEME
37343734
pr-url: https://github.com/nodejs/node/pull/54842
37353735
description: Documentation-only deprecation.
3736+
- version: REPLACEME
3737+
pr-url: https://github.com/nodejs/node/pull/54869
3738+
description: Runtime deprecation.
37363739
-->
37373740

3738-
Type: Documentation-only
3741+
Type: Runtime
37393742

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

lib/internal/util.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function isError(e) {
100100
// each one once.
101101
const codesWarned = new SafeSet();
102102

103-
let validateString;
103+
const lazyValidateString = getLazy(() => require('internal/validators').validateString);
104104

105105
function getDeprecationWarningEmitter(
106106
code, msg, deprecated, useEmitSync,
@@ -148,12 +148,8 @@ function pendingDeprecate(fn, msg, code) {
148148
// Returns a modified function which warns once by default.
149149
// If --no-deprecation is set, then it is a no-op.
150150
function deprecate(fn, msg, code, useEmitSync) {
151-
// Lazy-load to avoid a circular dependency.
152-
if (validateString === undefined)
153-
({ validateString } = require('internal/validators'));
154-
155151
if (code !== undefined)
156-
validateString(code, 'code');
152+
lazyValidateString()(code, 'code');
157153

158154
const emitDeprecationWarning = getDeprecationWarningEmitter(
159155
code, msg, deprecated, useEmitSync,
@@ -182,6 +178,15 @@ function deprecate(fn, msg, code, useEmitSync) {
182178
return deprecated;
183179
}
184180

181+
function deprecateInstantation(target, code, ...args) {
182+
if (code !== undefined)
183+
lazyValidateString()(code, 'code');
184+
185+
getDeprecationWarningEmitter(code, `Instantiating ${target.name} without the 'new' keyword has been deprecated.`, target)();
186+
187+
return ReflectConstruct(target, args);
188+
}
189+
185190
function decorateErrorStack(err) {
186191
if (!(isError(err) && err.stack) || err[decorated_private_symbol])
187192
return;
@@ -902,6 +907,7 @@ module.exports = {
902907
defineLazyProperties,
903908
defineReplaceableLazyAttribute,
904909
deprecate,
910+
deprecateInstantation,
905911
emitExperimentalWarning,
906912
encodingsMap,
907913
exposeInterface,

lib/repl.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const {
111111
decorateErrorStack,
112112
isError,
113113
deprecate,
114+
deprecateInstantation,
114115
SideEffectFreeRegExpPrototypeSymbolReplace,
115116
SideEffectFreeRegExpPrototypeSymbolSplit,
116117
} = require('internal/util');
@@ -262,12 +263,7 @@ function REPLServer(prompt,
262263
ignoreUndefined,
263264
replMode) {
264265
if (!(this instanceof REPLServer)) {
265-
return new REPLServer(prompt,
266-
stream,
267-
eval_,
268-
useGlobal,
269-
ignoreUndefined,
270-
replMode);
266+
return deprecateInstantation(REPLServer, 'DEP0185', prompt, stream, eval_, useGlobal, ignoreUndefined, replMode);
271267
}
272268

273269
let options;
@@ -1849,6 +1845,8 @@ function defineDefaultCommands(repl) {
18491845
}
18501846

18511847
function Recoverable(err) {
1848+
if (!(this instanceof Recoverable))
1849+
return deprecateInstantation(Recoverable, 'DEP0185');
18521850
this.err = err;
18531851
}
18541852
ObjectSetPrototypeOf(Recoverable.prototype, SyntaxErrorPrototype);

test/parallel/test-repl.js

+10
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,13 @@ function event(ee, expected) {
10141014
}));
10151015
});
10161016
}
1017+
1018+
{
1019+
const server = repl.REPLServer();
1020+
common.expectWarning({
1021+
DeprecationWarning: {
1022+
DEP0185: 'Instantiating REPLServer without the \'new\' keyword has been deprecated.',
1023+
}
1024+
});
1025+
server.emit('line', '.exit');
1026+
}

0 commit comments

Comments
 (0)