diff --git a/lib/readline.js b/lib/readline.js index e7c5a04bb03eed..e847ce961f53ad 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -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 = '> '; @@ -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; } }); @@ -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); @@ -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/, ''); @@ -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 @@ -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(); @@ -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 = []; @@ -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)); } @@ -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(' '); } } @@ -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); } @@ -583,9 +582,9 @@ 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); } }; @@ -593,8 +592,8 @@ Interface.prototype._wordLeft = function() { 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); } }; @@ -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; @@ -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(); @@ -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++; } @@ -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 && @@ -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) @@ -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(); } @@ -1136,7 +1134,7 @@ 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); @@ -1144,7 +1142,7 @@ function emitKeypressEvents(stream, 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; }