Skip to content
Permalink
Browse files

readline: set null as callback return in case there's no error

The cursor move functions accept a callback. It was possible that
`undefined` was returned in case there was no error instead of null.

PR-URL: #31006
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR committed Dec 18, 2019
1 parent 92dcf3e commit ffbf7903588ac6889762224a2d4a733013ed5ce4
Showing with 16 additions and 8 deletions.
  1. +4 −4 lib/readline.js
  2. +12 −4 test/parallel/test-readline-csi.js
@@ -1200,7 +1200,7 @@ function cursorTo(stream, x, y, callback) {

if (stream == null || (typeof x !== 'number' && typeof y !== 'number')) {
if (typeof callback === 'function')
process.nextTick(callback);
process.nextTick(callback, null);
return true;
}

@@ -1221,7 +1221,7 @@ function moveCursor(stream, dx, dy, callback) {

if (stream == null || !(dx || dy)) {
if (typeof callback === 'function')
process.nextTick(callback);
process.nextTick(callback, null);
return true;
}

@@ -1255,7 +1255,7 @@ function clearLine(stream, dir, callback) {

if (stream === null || stream === undefined) {
if (typeof callback === 'function')
process.nextTick(callback);
process.nextTick(callback, null);
return true;
}

@@ -1277,7 +1277,7 @@ function clearScreenDown(stream, callback) {

if (stream === null || stream === undefined) {
if (typeof callback === 'function')
process.nextTick(callback);
process.nextTick(callback, null);
return true;
}

@@ -39,7 +39,9 @@ assert.throws(() => {
}, /ERR_INVALID_CALLBACK/);

// Verify that clearScreenDown() does not throw on null or undefined stream.
assert.strictEqual(readline.clearScreenDown(null, common.mustCall()), true);
assert.strictEqual(readline.clearScreenDown(null, common.mustCall((err) => {
assert.strictEqual(err, null);
})), true);
assert.strictEqual(readline.clearScreenDown(undefined, common.mustCall()),
true);

@@ -67,7 +69,9 @@ assert.throws(() => {
// Verify that clearLine() does not throw on null or undefined stream.
assert.strictEqual(readline.clearLine(null, 0), true);
assert.strictEqual(readline.clearLine(undefined, 0), true);
assert.strictEqual(readline.clearLine(null, 0, common.mustCall()), true);
assert.strictEqual(readline.clearLine(null, 0, common.mustCall((err) => {
assert.strictEqual(err, null);
})), true);
assert.strictEqual(readline.clearLine(undefined, 0, common.mustCall()), true);

// Nothing is written when moveCursor 0, 0
@@ -101,15 +105,19 @@ assert.throws(() => {
// Verify that moveCursor() does not throw on null or undefined stream.
assert.strictEqual(readline.moveCursor(null, 1, 1), true);
assert.strictEqual(readline.moveCursor(undefined, 1, 1), true);
assert.strictEqual(readline.moveCursor(null, 1, 1, common.mustCall()), true);
assert.strictEqual(readline.moveCursor(null, 1, 1, common.mustCall((err) => {
assert.strictEqual(err, null);
})), true);
assert.strictEqual(readline.moveCursor(undefined, 1, 1, common.mustCall()),
true);

// Undefined or null as stream should not throw.
assert.strictEqual(readline.cursorTo(null), true);
assert.strictEqual(readline.cursorTo(), true);
assert.strictEqual(readline.cursorTo(null, 1, 1, common.mustCall()), true);
assert.strictEqual(readline.cursorTo(undefined, 1, 1, common.mustCall()), true);
assert.strictEqual(readline.cursorTo(undefined, 1, 1, common.mustCall((err) => {
assert.strictEqual(err, null);
})), true);

writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 'a'), true);

0 comments on commit ffbf790

Please sign in to comment.
You can’t perform that action at this time.