readline: refactor to use validateNumber

`validateNumber` throws more proper error code and
error name.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Deokjin Kim 2022-12-09 20:36:43 +09:00 committed by Filip Skokan
parent 671ffd7825
commit dc06df31b6
3 changed files with 32 additions and 12 deletions

View File

@ -19,7 +19,6 @@ const {
MathMax, MathMax,
MathMaxApply, MathMaxApply,
NumberIsFinite, NumberIsFinite,
NumberIsNaN,
ObjectSetPrototypeOf, ObjectSetPrototypeOf,
RegExpPrototypeExec, RegExpPrototypeExec,
StringPrototypeCodePointAt, StringPrototypeCodePointAt,
@ -42,6 +41,7 @@ const {
const { const {
validateAbortSignal, validateAbortSignal,
validateArray, validateArray,
validateNumber,
validateString, validateString,
validateUint32, validateUint32,
} = require('internal/validators'); } = require('internal/validators');
@ -196,13 +196,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
historySize = kHistorySize; historySize = kHistorySize;
} }
if ( validateNumber(historySize, 'historySize', 0);
typeof historySize !== 'number' ||
NumberIsNaN(historySize) ||
historySize < 0
) {
throw new ERR_INVALID_ARG_VALUE.RangeError('historySize', historySize);
}
// Backwards compat; check the isTTY prop of the output stream // Backwards compat; check the isTTY prop of the output stream
// when `terminal` was not specified // when `terminal` was not specified

View File

@ -126,7 +126,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}); });
// Constructor throws if historySize is not a positive number // 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(() => { assert.throws(() => {
readline.createInterface({ readline.createInterface({
input, input,
@ -134,7 +134,20 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}); });
}, { }, {
name: 'RangeError', 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',
}); });
}); });

View File

@ -103,7 +103,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}); });
// Constructor throws if historySize is not a positive number // 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(() => { assert.throws(() => {
readline.createInterface({ readline.createInterface({
input, input,
@ -111,7 +111,20 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}); });
}, { }, {
name: 'RangeError', 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',
}); });
}); });