Skip to content
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

repl: runtime deprecate instantiating without new #54869

Merged
merged 6 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3748,14 +3748,17 @@ It is recommended to use the `new` qualifier instead. This applies to all Zlib c

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/54869
description: Runtime deprecation.
- version:
- v22.9.0
- v20.18.0
pr-url: https://github.com/nodejs/node/pull/54842
description: Documentation-only deprecation.
-->

Type: Documentation-only
Type: Runtime

Instantiating classes without the `new` qualifier exported by the `node:repl` module is deprecated.
It is recommended to use the `new` qualifier instead. This applies to all REPL classes, including
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const {
} = internalBinding('util');
const { isNativeError, isPromise } = internalBinding('types');
const { getOptionValue } = require('internal/options');
const assert = require('internal/assert');
const { encodings } = internalBinding('string_decoder');

const noCrypto = !process.versions.openssl;
Expand Down Expand Up @@ -179,6 +180,14 @@ function deprecate(fn, msg, code, useEmitSync) {
return deprecated;
}

function deprecateInstantiation(target, code, ...args) {
assert(typeof code === 'string');

getDeprecationWarningEmitter(code, `Instantiating ${target.name} without the 'new' keyword has been deprecated.`, target)();

return ReflectConstruct(target, args);
}

function decorateErrorStack(err) {
if (!(isError(err) && err.stack) || err[decorated_private_symbol])
return;
Expand Down Expand Up @@ -873,6 +882,7 @@ module.exports = {
defineLazyProperties,
defineReplaceableLazyAttribute,
deprecate,
deprecateInstantiation,
emitExperimentalWarning,
encodingsMap,
exposeInterface,
Expand Down
8 changes: 2 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const {
decorateErrorStack,
isError,
deprecate,
deprecateInstantiation,
SideEffectFreeRegExpPrototypeSymbolReplace,
SideEffectFreeRegExpPrototypeSymbolSplit,
} = require('internal/util');
Expand Down Expand Up @@ -262,12 +263,7 @@ function REPLServer(prompt,
ignoreUndefined,
replMode) {
if (!(this instanceof REPLServer)) {
return new REPLServer(prompt,
stream,
eval_,
useGlobal,
ignoreUndefined,
replMode);
return deprecateInstantiation(REPLServer, 'DEP0185', prompt, stream, eval_, useGlobal, ignoreUndefined, replMode);
}

let options;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-preview-without-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function runAndWait(cmds, repl) {
return promise;
}

const repl = REPLServer({
const repl = new REPLServer({
prompt: PROMPT,
stream: new REPLStream(),
ignoreUndefined: true,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function runAndWait(cmds, repl) {
}

async function tests(options) {
const repl = REPLServer({
const repl = new REPLServer({
prompt: PROMPT,
stream: new REPLStream(),
ignoreUndefined: true,
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,17 @@ function event(ee, expected) {
}));
});
}

{
const server = repl.REPLServer();
common.expectWarning({
DeprecationWarning: {
DEP0185: 'Instantiating REPLServer without the \'new\' keyword has been deprecated.',
// For the 'url.format' test-case.
DEP0169:
'`url.parse()` behavior is not standardized and prone to errors that have security implications. ' +
'Use the WHATWG URL API instead. CVEs are not issued for `url.parse()` vulnerabilities.',
}
});
server.emit('line', '.exit');
}
Loading