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: fix calling constructor without new #1385

Closed
wants to merge 1 commit into from
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
5 changes: 4 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ exports.createInterface = function(input, output, completer, terminal) {

function Interface(input, output, completer, terminal) {
if (!(this instanceof Interface)) {
return new Interface(input, output, completer, terminal);
// call the constructor preserving original number of arguments
const self = Object.create(Interface.prototype);
Interface.apply(self, arguments);
return self;
}

this._sawReturn = false;
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ function isWarned(emitter) {
fi.emit('data', ''); // removes listener
});

// calling readline without `new`
fi = new FakeInput();
rli = readline.Interface({ input: fi, output: fi, terminal: terminal });
called = false;
rli.on('line', function(line) {
called = true;
assert.equal(line, 'asdf');
});
fi.emit('data', 'asdf\n');
assert.ok(called);
rli.close();

if (terminal) {
// question
fi = new FakeInput();
Expand Down