Skip to content

Commit

Permalink
readline: refactor to use validateNumber
Browse files Browse the repository at this point in the history
`validateNumber` throws more proper error code and
error name.

PR-URL: #45801
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
deokjinkim authored and panva committed Jan 18, 2023
1 parent 671ffd7 commit dc06df3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
10 changes: 2 additions & 8 deletions lib/internal/readline/interface.js
Expand Up @@ -19,7 +19,6 @@ const {
MathMax,
MathMaxApply,
NumberIsFinite,
NumberIsNaN,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
StringPrototypeCodePointAt,
Expand All @@ -42,6 +41,7 @@ const {
const {
validateAbortSignal,
validateArray,
validateNumber,
validateString,
validateUint32,
} = require('internal/validators');
Expand Down Expand Up @@ -196,13 +196,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
historySize = kHistorySize;
}

if (
typeof historySize !== 'number' ||
NumberIsNaN(historySize) ||
historySize < 0
) {
throw new ERR_INVALID_ARG_VALUE.RangeError('historySize', historySize);
}
validateNumber(historySize, 'historySize', 0);

// Backwards compat; check the isTTY prop of the output stream
// when `terminal` was not specified
Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-readline-interface.js
Expand Up @@ -126,15 +126,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
});

// Constructor throws if historySize is not a positive number
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
[-1, NaN].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
code: 'ERR_OUT_OF_RANGE',
});
});

// Constructor throws if type of historySize is not a number
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
});
});

Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-readline-promises-interface.js
Expand Up @@ -103,15 +103,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
});

// Constructor throws if historySize is not a positive number
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
[-1, NaN].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
code: 'ERR_OUT_OF_RANGE',
});
});

// Constructor throws if type of historySize is not a number
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
assert.throws(() => {
readline.createInterface({
input,
historySize,
});
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
});
});

Expand Down

0 comments on commit dc06df3

Please sign in to comment.