From d6669645c0d77ac5e86a930ac0de25440d060b2e Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Thu, 8 Apr 2021 02:36:46 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/38141 Fixes: https://github.com/nodejs/node/issues/38139 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum --- lib/internal/repl/utils.js | 8 +++- test/parallel/test-repl-history-navigation.js | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index a1c7ef8a81a5a1..80c56144051e19 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -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) => { @@ -394,7 +398,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { } const inputPreviewCallback = (error, inspected) => { - if (inspected === null) { + if (inspected == null) { return; } diff --git a/test/parallel/test-repl-history-navigation.js b/test/parallel/test-repl-history-navigation.js index 044c788a16af3f..df4f0390a69c23 100644 --- a/test/parallel/test-repl-history-navigation.js +++ b/test/parallel/test-repl-history-navigation.js @@ -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'); @@ -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;