Skip to content

Commit 1c59cef

Browse files
addaleaxevanlucas
authored andcommitted
repl: make key of repl.write() optional always
In `.editor` mode, `repl.write()` would have crashed when the `key` argument was not present, because the overwritten `_ttyWrite` of REPLs doesn’t check for the absence of a second argument like `readline.write()` does. Since the docs indicate that the argument is optional, add a check paralleling the one in `readline.write()`. PR-URL: #9207 Reviewed-By: Prince John Wesley <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8211904 commit 1c59cef

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

‎lib/repl.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ function REPLServer(prompt,
601601
// Wrap readline tty to enable editor mode
602602
constttyWrite=self._ttyWrite.bind(self);
603603
self._ttyWrite=(d,key)=>{
604+
key=key||{};
604605
if(!self.editorMode||!self.terminal){
605606
ttyWrite(d,key);
606607
return;

‎test/parallel/test-repl-.editor.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ const repl = require('repl');
88
// \u001b[0J - Clear screen
99
// \u001b[3G - Moves the cursor to 3rd column
1010
constterminalCode='\u001b[1G\u001b[0J> \u001b[3G';
11+
constterminalCodeRegex=newRegExp(terminalCode.replace(/\[/g,'\\['),'g');
1112

12-
functionrun({input, output, event}){
13+
functionrun({input, output, event, checkTerminalCodes =true}){
1314
conststream=newcommon.ArrayStream();
1415
letfound='';
1516

1617
stream.write=(msg)=>found+=msg.replace('\r','');
1718

18-
constexpected=`${terminalCode}.editor\n`+
19-
'// Entering editor mode (^D to finish, ^C to cancel)\n'+
20-
`${input}${output}\n${terminalCode}`;
19+
letexpected=`${terminalCode}.editor\n`+
20+
'// Entering editor mode (^D to finish, ^C to cancel)\n'+
21+
`${input}${output}\n${terminalCode}`;
2122

2223
constreplServer=repl.start({
2324
prompt: '> ',
@@ -31,6 +32,12 @@ function run({input, output, event}){
3132
stream.emit('data',input);
3233
replServer.write('',event);
3334
replServer.close();
35+
36+
if(!checkTerminalCodes){
37+
found=found.replace(terminalCodeRegex,'').replace(/\n/g,'');
38+
expected=expected.replace(terminalCodeRegex,'').replace(/\n/g,'');
39+
}
40+
3441
assert.strictEqual(found,expected);
3542
}
3643

@@ -54,6 +61,12 @@ const tests = [
5461
input: ' var i = 1;\ni + 3',
5562
output: '\n4',
5663
event: {ctrl: true,name: 'd'}
64+
},
65+
{
66+
input: '',
67+
output: '',
68+
checkTerminalCodes: false,
69+
event: null,
5770
}
5871
];
5972

0 commit comments

Comments
(0)