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

lib: replace var with let and const in readline.js #30377

Closed
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
92 changes: 45 additions & 47 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ function Interface(input, output, completer, terminal) {
this.escapeCodeTimeout = ESCAPE_CODE_TIMEOUT;

EventEmitter.call(this);
var historySize;
var removeHistoryDuplicates = false;
let historySize;
let removeHistoryDuplicates = false;
let crlfDelay;
let prompt = '> ';

Expand Down Expand Up @@ -258,10 +258,9 @@ Object.defineProperty(Interface.prototype, 'columns', {
configurable: true,
enumerable: true,
get: function() {
var columns = Infinity;
if (this.output && this.output.columns)
columns = this.output.columns;
return columns;
return this.output.columns;
return Infinity;
}
});

Expand Down Expand Up @@ -308,7 +307,7 @@ Interface.prototype.question = function(query, cb) {

Interface.prototype._onLine = function(line) {
if (this._questionCallback) {
var cb = this._questionCallback;
const cb = this._questionCallback;
this._questionCallback = null;
this.setPrompt(this._oldPrompt);
cb(line);
Expand Down Expand Up @@ -435,7 +434,7 @@ Interface.prototype._normalWrite = function(b) {
if (b === undefined) {
return;
}
var string = this._decoder.write(b);
let string = this._decoder.write(b);
if (this._sawReturnAt &&
Date.now() - this._sawReturnAt <= this.crlfDelay) {
string = string.replace(/^\n/, '');
Expand All @@ -453,11 +452,11 @@ Interface.prototype._normalWrite = function(b) {
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;

// Got one or more newlines; process into "line" events
var lines = string.split(lineEnding);
const lines = string.split(lineEnding);
// Either '' or (conceivably) the unfinished portion of the next line
string = lines.pop();
this._line_buffer = string;
for (var n = 0; n < lines.length; n++)
for (let n = 0; n < lines.length; n++)
this._onLine(lines[n]);
} else if (string) {
// No newlines this time, save what we have for next time
Expand All @@ -467,8 +466,8 @@ Interface.prototype._normalWrite = function(b) {

Interface.prototype._insertString = function(c) {
if (this.cursor < this.line.length) {
var beg = this.line.slice(0, this.cursor);
var end = this.line.slice(this.cursor, this.line.length);
const beg = this.line.slice(0, this.cursor);
const end = this.line.slice(this.cursor, this.line.length);
this.line = beg + c + end;
this.cursor += c.length;
this._refreshLine();
Expand Down Expand Up @@ -505,16 +504,16 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
// Apply/show completions.
if (lastKeypressWasTab) {
self._writeToOutput('\r\n');
var width = completions.reduce(function completionReducer(a, b) {
const width = completions.reduce(function completionReducer(a, b) {
return a.length > b.length ? a : b;
}).length + 2; // 2 space padding
var maxColumns = Math.floor(self.columns / width);
let maxColumns = Math.floor(self.columns / width);
if (!maxColumns || maxColumns === Infinity) {
maxColumns = 1;
}
var group = [];
for (var i = 0; i < completions.length; i++) {
var c = completions[i];
let group = [];
for (let i = 0; i < completions.length; i++) {
const c = completions[i];
if (c === '') {
handleGroup(self, group, width, maxColumns);
group = [];
Expand All @@ -526,8 +525,8 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
}

// If there is a common prefix to all matches, then apply that portion.
var f = completions.filter((e) => e);
var prefix = commonPrefix(f);
const f = completions.filter((e) => e);
const prefix = commonPrefix(f);
if (prefix.length > completeOn.length) {
self._insertString(prefix.slice(completeOn.length));
}
Expand All @@ -543,16 +542,16 @@ function handleGroup(self, group, width, maxColumns) {
return;
}
const minRows = Math.ceil(group.length / maxColumns);
for (var row = 0; row < minRows; row++) {
for (var col = 0; col < maxColumns; col++) {
var idx = row * maxColumns + col;
for (let row = 0; row < minRows; row++) {
for (let col = 0; col < maxColumns; col++) {
const idx = row * maxColumns + col;
if (idx >= group.length) {
break;
}
var item = group[idx];
const item = group[idx];
self._writeToOutput(item);
if (col < maxColumns - 1) {
for (var s = 0; s < width - item.length; s++) {
for (let s = 0; s < width - item.length; s++) {
self._writeToOutput(' ');
}
}
Expand All @@ -570,7 +569,7 @@ function commonPrefix(strings) {
const sorted = strings.slice().sort();
const min = sorted[0];
const max = sorted[sorted.length - 1];
for (var i = 0, len = min.length; i < len; i++) {
for (let i = 0, len = min.length; i < len; i++) {
if (min[i] !== max[i]) {
return min.slice(0, i);
}
Expand All @@ -583,18 +582,18 @@ Interface.prototype._wordLeft = function() {
if (this.cursor > 0) {
// Reverse the string and match a word near beginning
// to avoid quadratic time complexity
var leading = this.line.slice(0, this.cursor);
var reversed = leading.split('').reverse().join('');
var match = reversed.match(/^\s*(?:[^\w\s]+|\w+)?/);
const leading = this.line.slice(0, this.cursor);
const reversed = leading.split('').reverse().join('');
const match = reversed.match(/^\s*(?:[^\w\s]+|\w+)?/);
this._moveCursor(-match[0].length);
}
};


Interface.prototype._wordRight = function() {
if (this.cursor < this.line.length) {
var trailing = this.line.slice(this.cursor);
var match = trailing.match(/^(?:\s+|[^\w\s]+|\w+)\s*/);
const trailing = this.line.slice(this.cursor);
const match = trailing.match(/^(?:\s+|[^\w\s]+|\w+)\s*/);
this._moveCursor(match[0].length);
}
};
Expand Down Expand Up @@ -643,9 +642,9 @@ Interface.prototype._deleteWordLeft = function() {
if (this.cursor > 0) {
// Reverse the string and match a word near beginning
// to avoid quadratic time complexity
var leading = this.line.slice(0, this.cursor);
var reversed = leading.split('').reverse().join('');
var match = reversed.match(/^\s*(?:[^\w\s]+|\w+)?/);
let leading = this.line.slice(0, this.cursor);
const reversed = leading.split('').reverse().join('');
const match = reversed.match(/^\s*(?:[^\w\s]+|\w+)?/);
leading = leading.slice(0, leading.length - match[0].length);
this.line = leading + this.line.slice(this.cursor, this.line.length);
this.cursor = leading.length;
Expand All @@ -656,8 +655,8 @@ Interface.prototype._deleteWordLeft = function() {

Interface.prototype._deleteWordRight = function() {
if (this.cursor < this.line.length) {
var trailing = this.line.slice(this.cursor);
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
const trailing = this.line.slice(this.cursor);
const match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
this.line = this.line.slice(0, this.cursor) +
trailing.slice(match[0].length);
this._refreshLine();
Expand Down Expand Up @@ -723,13 +722,12 @@ Interface.prototype._historyPrev = function() {

// Returns the last character's display position of the given string
Interface.prototype._getDisplayPos = function(str) {
var offset = 0;
let offset = 0;
const col = this.columns;
var row = 0;
var code;
let row = 0;
str = stripVTControlCharacters(str);
for (var i = 0, len = str.length; i < len; i++) {
code = str.codePointAt(i);
for (let i = 0, len = str.length; i < len; i++) {
const code = str.codePointAt(i);
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
i++;
}
Expand Down Expand Up @@ -761,8 +759,8 @@ Interface.prototype._getCursorPos = function() {
const strBeforeCursor = this._prompt + this.line.substring(0, this.cursor);
const dispPos = this._getDisplayPos(
stripVTControlCharacters(strBeforeCursor));
var cols = dispPos.cols;
var rows = dispPos.rows;
let cols = dispPos.cols;
let rows = dispPos.rows;
// If the cursor is on a full-width character which steps over the line,
// move the cursor to the beginning of the next line.
if (cols + 1 === columns &&
Expand Down Expand Up @@ -790,8 +788,8 @@ Interface.prototype._moveCursor = function(dx) {

// Check if cursors are in the same line
if (oldPos.rows === newPos.rows) {
var diffCursor = this.cursor - oldcursor;
var diffWidth;
const diffCursor = this.cursor - oldcursor;
let diffWidth;
if (diffCursor < 0) {
diffWidth = -getStringWidth(
this.line.substring(this.cursor, oldcursor)
Expand Down Expand Up @@ -1072,8 +1070,8 @@ Interface.prototype._ttyWrite = function(s, key) {

default:
if (typeof s === 'string' && s) {
var lines = s.split(/\r\n|\n|\r/);
for (var i = 0, len = lines.length; i < len; i++) {
const lines = s.split(/\r\n|\n|\r/);
for (let i = 0, len = lines.length; i < len; i++) {
if (i > 0) {
this._line();
}
Expand Down Expand Up @@ -1136,15 +1134,15 @@ function emitKeypressEvents(stream, iface) {

function onData(b) {
if (stream.listenerCount('keypress') > 0) {
var r = stream[KEYPRESS_DECODER].write(b);
const r = stream[KEYPRESS_DECODER].write(b);
if (r) {
clearTimeout(timeoutId);

if (iface) {
iface._sawKeyPress = r.length === 1;
}

for (var i = 0; i < r.length; i++) {
for (let i = 0; i < r.length; i++) {
if (r[i] === '\t' && typeof r[i + 1] === 'string' && iface) {
iface.isCompletionEnabled = false;
}
Expand Down