Skip to content

switch lib/readline to arrow functions #24599

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

Closed
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
72 changes: 36 additions & 36 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ const ESCAPE_DECODER = Symbol('escape-decoder');
// GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500;

function createInterface(input, output, completer, terminal) {
const createInterface = (input, output, completer, terminal) => {
return new Interface(input, output, completer, terminal);
}
};


function Interface(input, output, completer, terminal) {
Expand Down Expand Up @@ -152,7 +152,7 @@ function Interface(input, output, completer, terminal) {
if (typeof completer === 'function') {
this.completer = completer.length === 2 ?
completer :
function completerWrapper(v, cb) {
(v, cb) => {
cb(null, completer(v));
};
}
Expand All @@ -161,26 +161,26 @@ function Interface(input, output, completer, terminal) {

this.terminal = !!terminal;

function ondata(data) {
const ondata = (data) => {
self._normalWrite(data);
}
};

function onend() {
const onend = () => {
if (typeof self._line_buffer === 'string' &&
self._line_buffer.length > 0) {
self.emit('line', self._line_buffer);
}
self.close();
}
};

function ontermend() {
const ontermend = () => {
if (typeof self.line === 'string' && self.line.length > 0) {
self.emit('line', self.line);
}
self.close();
}
};

function onkeypress(s, key) {
const onkeypress = (s, key) => {
self._ttyWrite(s, key);
if (key && key.sequence) {
// if the key.sequence is half of a surrogate pair
Expand All @@ -190,32 +190,32 @@ function Interface(input, output, completer, terminal) {
if (ch >= 0xd800 && ch <= 0xdfff)
self._refreshLine();
}
}
};

function onresize() {
const onresize = () => {
self._refreshLine();
}
};

this[kLineObjectStream] = undefined;

if (!this.terminal) {
function onSelfCloseWithoutTerminal() {
const onSelfCloseWithoutTerminal = () => {
input.removeListener('data', ondata);
input.removeListener('end', onend);
}
};

input.on('data', ondata);
input.on('end', onend);
self.once('close', onSelfCloseWithoutTerminal);
this._decoder = new StringDecoder('utf8');
} else {
function onSelfCloseWithTerminal() {
const onSelfCloseWithTerminal = () => {
input.removeListener('keypress', onkeypress);
input.removeListener('end', ontermend);
if (output !== null && output !== undefined) {
output.removeListener('resize', onresize);
}
}
};

emitKeypressEvents(input, this);

Expand Down Expand Up @@ -553,7 +553,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
};

// this = Interface instance
function handleGroup(self, group, width, maxColumns) {
const handleGroup = (self, group, width, maxColumns) => {
if (group.length === 0) {
return;
}
Expand All @@ -575,9 +575,9 @@ function handleGroup(self, group, width, maxColumns) {
self._writeToOutput('\r\n');
}
self._writeToOutput('\r\n');
}
};

function commonPrefix(strings) {
const commonPrefix = (strings) => {
if (!strings || strings.length === 0) {
return '';
}
Expand All @@ -591,7 +591,7 @@ function commonPrefix(strings) {
}
}
return min;
}
};


Interface.prototype._wordLeft = function() {
Expand Down Expand Up @@ -887,7 +887,7 @@ Interface.prototype._ttyWrite = function(s, key) {
this.emit('SIGTSTP');
} else {
process.once('SIGCONT', (function continueProcess(self) {
return function() {
return () => {
// Don't raise events if stream has already been abandoned.
if (!self.paused) {
// Stream must be paused and resumed after SIGCONT to catch
Expand Down Expand Up @@ -1066,7 +1066,7 @@ Interface.prototype[Symbol.asyncIterator] = function() {
* accepts a readable Stream instance and makes it emit "keypress" events
*/

function emitKeypressEvents(stream, iface) {
const emitKeypressEvents = (stream, iface) => {
if (stream[KEYPRESS_DECODER]) return;

if (StringDecoder === undefined)
Expand All @@ -1079,7 +1079,7 @@ function emitKeypressEvents(stream, iface) {
const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
let timeoutId;

function onData(b) {
const onData = (b) => {
if (stream.listenerCount('keypress') > 0) {
var r = stream[KEYPRESS_DECODER].write(b);
if (r) {
Expand Down Expand Up @@ -1121,27 +1121,27 @@ function emitKeypressEvents(stream, iface) {
stream.removeListener('data', onData);
stream.on('newListener', onNewListener);
}
}
};

function onNewListener(event) {
const onNewListener = (event) => {
if (event === 'keypress') {
stream.on('data', onData);
stream.removeListener('newListener', onNewListener);
}
}
};

if (stream.listenerCount('keypress') > 0) {
stream.on('data', onData);
} else {
stream.on('newListener', onNewListener);
}
}
};

/**
* moves the cursor to the x and y coordinate on the given stream
*/

function cursorTo(stream, x, y) {
const cursorTo = (stream, x, y) => {
if (stream === null || stream === undefined)
return;

Expand All @@ -1156,13 +1156,13 @@ function cursorTo(stream, x, y) {
} else {
stream.write(CSI`${y + 1};${x + 1}H`);
}
}
};

/**
* moves the cursor relative to its current location
*/

function moveCursor(stream, dx, dy) {
const moveCursor = (stream, dx, dy) => {
if (stream === null || stream === undefined)
return;

Expand All @@ -1177,7 +1177,7 @@ function moveCursor(stream, dx, dy) {
} else if (dy > 0) {
stream.write(CSI`${dy}B`);
}
}
};

/**
* clears the current line the cursor is on:
Expand All @@ -1186,7 +1186,7 @@ function moveCursor(stream, dx, dy) {
* 0 for the entire line
*/

function clearLine(stream, dir) {
const clearLine = (stream, dir) => {
if (stream === null || stream === undefined)
return;

Expand All @@ -1200,18 +1200,18 @@ function clearLine(stream, dir) {
// entire line
stream.write(kClearLine);
}
}
};

/**
* clears the screen from the current position of the cursor down
*/

function clearScreenDown(stream) {
const clearScreenDown = (stream) => {
if (stream === null || stream === undefined)
return;

stream.write(kClearScreenDown);
}
};

module.exports = {
Interface,
Expand Down