Skip to content

Commit dc06df3

Browse files
deokjinkimpanva
authored andcommittedJan 18, 2023
readline: refactor to use validateNumber
`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>
1 parent 671ffd7 commit dc06df3

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed
 

‎lib/internal/readline/interface.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
MathMax,
2020
MathMaxApply,
2121
NumberIsFinite,
22-
NumberIsNaN,
2322
ObjectSetPrototypeOf,
2423
RegExpPrototypeExec,
2524
StringPrototypeCodePointAt,
@@ -42,6 +41,7 @@ const {
4241
const {
4342
validateAbortSignal,
4443
validateArray,
44+
validateNumber,
4545
validateString,
4646
validateUint32,
4747
} = require('internal/validators');
@@ -196,13 +196,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
196196
historySize = kHistorySize;
197197
}
198198

199-
if (
200-
typeof historySize !== 'number' ||
201-
NumberIsNaN(historySize) ||
202-
historySize < 0
203-
) {
204-
throw new ERR_INVALID_ARG_VALUE.RangeError('historySize', historySize);
205-
}
199+
validateNumber(historySize, 'historySize', 0);
206200

207201
// Backwards compat; check the isTTY prop of the output stream
208202
// when `terminal` was not specified

‎test/parallel/test-readline-interface.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
126126
});
127127

128128
// Constructor throws if historySize is not a positive number
129-
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
129+
[-1, NaN].forEach((historySize) => {
130130
assert.throws(() => {
131131
readline.createInterface({
132132
input,
133133
historySize,
134134
});
135135
}, {
136136
name: 'RangeError',
137-
code: 'ERR_INVALID_ARG_VALUE'
137+
code: 'ERR_OUT_OF_RANGE',
138+
});
139+
});
140+
141+
// Constructor throws if type of historySize is not a number
142+
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
143+
assert.throws(() => {
144+
readline.createInterface({
145+
input,
146+
historySize,
147+
});
148+
}, {
149+
name: 'TypeError',
150+
code: 'ERR_INVALID_ARG_TYPE',
138151
});
139152
});
140153

‎test/parallel/test-readline-promises-interface.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,28 @@ function assertCursorRowsAndCols(rli, rows, cols) {
103103
});
104104

105105
// Constructor throws if historySize is not a positive number
106-
['not a number', -1, NaN, {}, true, Symbol(), null].forEach((historySize) => {
106+
[-1, NaN].forEach((historySize) => {
107107
assert.throws(() => {
108108
readline.createInterface({
109109
input,
110110
historySize,
111111
});
112112
}, {
113113
name: 'RangeError',
114-
code: 'ERR_INVALID_ARG_VALUE'
114+
code: 'ERR_OUT_OF_RANGE',
115+
});
116+
});
117+
118+
// Constructor throws if type of historySize is not a number
119+
['not a number', {}, true, Symbol(), null].forEach((historySize) => {
120+
assert.throws(() => {
121+
readline.createInterface({
122+
input,
123+
historySize,
124+
});
125+
}, {
126+
name: 'TypeError',
127+
code: 'ERR_INVALID_ARG_TYPE',
115128
});
116129
});
117130

0 commit comments

Comments
 (0)
Please sign in to comment.