Skip to content
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
repl: fix declaring a variable with the name util
The REPL no longer relies on `util` being a reference to the `util` core
module. It still relies on `globalThis` refering to the global object,
but no longer emits warnings when it's overwritten by the user.

PR-URL: #38141
Fixes: #38139
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
eladkishon authored and aduh95 committed Apr 18, 2021
commit d6669645c0d77ac5e86a930ac0de25440d060b2e
8 changes: 6 additions & 2 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
breakLength: Infinity
}, previewOptions);
session.post('Runtime.callFunctionOn', {
functionDeclaration: `(v) => util.inspect(v, ${inspectOptions})`,
functionDeclaration:
`(v) =>
Reflect
.getOwnPropertyDescriptor(globalThis, 'util')
.get().inspect(v, ${inspectOptions})`,
objectId: result.objectId,
arguments: [result]
}, (error, preview) => {
Expand Down Expand Up @@ -394,7 +398,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
}

const inputPreviewCallback = (error, inspected) => {
if (inspected === null) {
if (inspected == null) {
return;
}

Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-repl-history-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

process.throwDeprecation = true;
process.on('warning', common.mustNotCall());

const defaultHistoryPath = path.join(tmpdir.path, '.node_repl_history');

Expand Down Expand Up @@ -554,6 +555,42 @@ const tests = [
expected: [],
clean: false
},
{
env: { NODE_REPL_HISTORY: defaultHistoryPath },
test: ['const util = {}', ENTER,
'ut', RIGHT, ENTER],
expected: common.hasIntl && common.hasCrypto ? [
prompt, ...'const util = {}',
'undefined\n',
prompt, ...'ut', ' // il', '\n// {}',
'il', '\n// {}',
'{}\n',
prompt,
] : [],
clean: false
},
{
env: { NODE_REPL_HISTORY: defaultHistoryPath },
test: [
'const utilDesc = Reflect.getOwnPropertyDescriptor(globalThis, "util")',
ENTER,
'globalThis.util = {}', ENTER,
'ut', RIGHT, ENTER,
'Reflect.defineProperty(globalThis, "util", utilDesc)', ENTER],
expected: common.hasIntl && common.hasCrypto ? [
prompt, ...'const utilDesc = ' +
'Reflect.getOwnPropertyDescriptor(globalThis, "util")',
'undefined\n',
prompt, ...'globalThis.util = {}',
'{}\n',
prompt, ...'ut', ' // il', 'il',
'{}\n',
Comment thread
EladKeyshawn marked this conversation as resolved.
Outdated
prompt, ...'Reflect.defineProperty(globalThis, "util", utilDesc)',
'true\n',
prompt,
] : [],
clean: false
},
];
const numtests = tests.length;

Expand Down