Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: refactor to use validateNumber #45801

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
RegExpPrototypeSymbolReplace,
Expand All @@ -45,6 +44,7 @@ const {
const {
validateAbortSignal,
validateArray,
validateNumber,
validateString,
validateUint32,
} = require('internal/validators');
Expand Down Expand Up @@ -199,13 +199,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