Skip to content

Commit

Permalink
readline: use kEmptyObject
Browse files Browse the repository at this point in the history
PR-URL: #43159
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros authored and danielleadams committed Jun 13, 2022
1 parent a3310d1 commit 5e98cac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {
validateString,
validateUint32,
} = require('internal/validators');
const { kEmptyObject } = require('internal/util');
const {
inspect,
getStringWidth,
Expand Down Expand Up @@ -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') {
Expand Down
15 changes: 11 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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(
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion lib/readline/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 5e98cac

Please sign in to comment.