Skip to content

Commit

Permalink
repl: fix declaring a variable with the name util
Browse files Browse the repository at this point in the history
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: nodejs#38141
Fixes: nodejs#38139
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
eladkishon authored and aduh95 committed Apr 18, 2021
1 parent b87f1be commit d666964
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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',
prompt, ...'Reflect.defineProperty(globalThis, "util", utilDesc)',
'true\n',
prompt,
] : [],
clean: false
},
];
const numtests = tests.length;

Expand Down

0 comments on commit d666964

Please sign in to comment.