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 <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
  • 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.