From 5e98cacf77b19a9034730f288737606d0ab03ac0 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 21 May 2022 17:54:41 +0800 Subject: [PATCH] readline: use `kEmptyObject` PR-URL: https://github.com/nodejs/node/pull/43159 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel --- lib/internal/readline/interface.js | 3 ++- lib/readline.js | 15 +++++++++++---- lib/readline/promises.js | 6 +++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 47e5ca580ab317..457b67716c460f 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -48,6 +48,7 @@ const { validateString, validateUint32, } = require('internal/validators'); +const { kEmptyObject } = require('internal/util'); const { inspect, getStringWidth, @@ -1053,7 +1054,7 @@ class Interface extends InterfaceConstructor { // Handle a write from the tty [kTtyWrite](s, key) { const previousKey = this[kPreviousKey]; - key = key || {}; + key = key || kEmptyObject; this[kPreviousKey] = key; if (!key.meta || key.name !== 'y') { diff --git a/lib/readline.js b/lib/readline.js index 82afaa285d67ad..26e277c7abfd15 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -47,7 +47,10 @@ const { const { inspect, } = require('internal/util/inspect'); -const { promisify } = require('internal/util'); +const { + kEmptyObject, + promisify, +} = require('internal/util'); const { validateAbortSignal } = require('internal/validators'); /** @@ -128,7 +131,9 @@ const superQuestion = _Interface.prototype.question; */ Interface.prototype.question = function(query, options, cb) { cb = typeof options === 'function' ? options : cb; - options = typeof options === 'object' && options !== null ? options : {}; + if (options === null || typeof options !== 'object') { + options = kEmptyObject; + } if (options.signal) { validateAbortSignal(options.signal, 'options.signal'); @@ -154,7 +159,9 @@ Interface.prototype.question = function(query, options, cb) { } }; Interface.prototype.question[promisify.custom] = function question(query, options) { - options = typeof options === 'object' && options !== null ? options : {}; + if (options === null || typeof options !== 'object') { + options = kEmptyObject; + } if (options.signal && options.signal.aborted) { return PromiseReject( @@ -457,7 +464,7 @@ Interface.prototype._moveCursor = _Interface.prototype[kMoveCursor]; Interface.prototype._ttyWrite = _Interface.prototype[kTtyWrite]; function _ttyWriteDumb(s, key) { - key = key || {}; + key = key || kEmptyObject; if (key.name === 'escape') return; diff --git a/lib/readline/promises.js b/lib/readline/promises.js index 534558ec31ffdc..728a6253fd5e67 100644 --- a/lib/readline/promises.js +++ b/lib/readline/promises.js @@ -18,12 +18,16 @@ const { } = require('internal/errors'); const { validateAbortSignal } = require('internal/validators'); +const { + kEmptyObject, +} = require('internal/util'); + class Interface extends _Interface { // eslint-disable-next-line no-useless-constructor constructor(input, output, completer, terminal) { super(input, output, completer, terminal); } - question(query, options = {}) { + question(query, options = kEmptyObject) { return new Promise((resolve, reject) => { let cb = resolve;