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: https://github.com/nodejs/node/pull/9207
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen 2016-10-20 16:47:33 +02:00
parent d0d4ca5ece
commit 8b6fd4386a
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
2 changed files with 18 additions and 4 deletions

View File

@ -601,6 +601,7 @@ function REPLServer(prompt,
// Wrap readline tty to enable editor mode // Wrap readline tty to enable editor mode
const ttyWrite = self._ttyWrite.bind(self); const ttyWrite = self._ttyWrite.bind(self);
self._ttyWrite = (d, key) => { self._ttyWrite = (d, key) => {
key = key || {};
if (!self.editorMode || !self.terminal) { if (!self.editorMode || !self.terminal) {
ttyWrite(d, key); ttyWrite(d, key);
return; return;

View File

@ -8,16 +8,17 @@ const repl = require('repl');
// \u001b[0J - Clear screen // \u001b[0J - Clear screen
// \u001b[3G - Moves the cursor to 3rd column // \u001b[3G - Moves the cursor to 3rd column
const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G'; const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');
function run({input, output, event}) { function run({input, output, event, checkTerminalCodes = true}) {
const stream = new common.ArrayStream(); const stream = new common.ArrayStream();
let found = ''; let found = '';
stream.write = (msg) => found += msg.replace('\r', ''); stream.write = (msg) => found += msg.replace('\r', '');
const expected = `${terminalCode}.editor\n` + let expected = `${terminalCode}.editor\n` +
'// Entering editor mode (^D to finish, ^C to cancel)\n' + '// Entering editor mode (^D to finish, ^C to cancel)\n' +
`${input}${output}\n${terminalCode}`; `${input}${output}\n${terminalCode}`;
const replServer = repl.start({ const replServer = repl.start({
prompt: '> ', prompt: '> ',
@ -31,6 +32,12 @@ function run({input, output, event}) {
stream.emit('data', input); stream.emit('data', input);
replServer.write('', event); replServer.write('', event);
replServer.close(); replServer.close();
if (!checkTerminalCodes) {
found = found.replace(terminalCodeRegex, '').replace(/\n/g, '');
expected = expected.replace(terminalCodeRegex, '').replace(/\n/g, '');
}
assert.strictEqual(found, expected); assert.strictEqual(found, expected);
} }
@ -54,6 +61,12 @@ const tests = [
input: ' var i = 1;\ni + 3', input: ' var i = 1;\ni + 3',
output: '\n4', output: '\n4',
event: {ctrl: true, name: 'd'} event: {ctrl: true, name: 'd'}
},
{
input: '',
output: '',
checkTerminalCodes: false,
event: null,
} }
]; ];