diff --git a/lib/repl.js b/lib/repl.js index 4c44e2aa4d47a7..a6123d93896d06 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -22,7 +22,7 @@ /* A repl library that you can include in your own code to get a runtime * interface to your program. * - * var repl = require("repl"); + * const repl = require("repl"); * // start repl on stdin * repl.start("prompt> "); * @@ -372,7 +372,7 @@ function REPLServer(prompt, // After executing the current expression, store the values of RegExp // predefined properties back in `savedRegExMatches` - for (var idx = 1; idx < savedRegExMatches.length; idx += 1) { + for (let idx = 1; idx < savedRegExMatches.length; idx += 1) { savedRegExMatches[idx] = RegExp[`$${idx}`]; } @@ -636,8 +636,8 @@ function REPLServer(prompt, self.emit('exit'); }); - var sawSIGINT = false; - var sawCtrlD = false; + let sawSIGINT = false; + let sawCtrlD = false; const prioritizedSigintQueue = new Set(); self.on('SIGINT', function onSigInt() { if (prioritizedSigintQueue.size > 0) { @@ -877,7 +877,7 @@ REPLServer.prototype.close = function close() { }; REPLServer.prototype.createContext = function() { - var context; + let context; if (this.useGlobal) { context = global; } else { @@ -963,7 +963,7 @@ REPLServer.prototype.resetContext = function() { }; REPLServer.prototype.displayPrompt = function(preserveCursor) { - var prompt = this._initialPrompt; + let prompt = this._initialPrompt; if (this[kBufferedCommandSymbol].length) { prompt = '...'; const len = this.lines.level.length ? this.lines.level.length - 1 : 0; @@ -993,7 +993,7 @@ function ArrayStream() { Stream.call(this); this.run = function(data) { - for (var n = 0; n < data.length; n++) + for (let n = 0; n < data.length; n++) this.emit('data', `${data[n]}\n`); }; } @@ -1018,7 +1018,7 @@ function isIdentifier(str) { return false; } const firstLen = first > 0xffff ? 2 : 1; - for (var i = firstLen; i < str.length; i += 1) { + for (let i = firstLen; i < str.length; i += 1) { const cp = str.codePointAt(i); if (!isIdentifierChar(cp)) { return false; @@ -1056,7 +1056,7 @@ REPLServer.prototype.complete = function() { // given to the readline interface for handling tab completion. // // Example: -// complete('var foo = util.') +// complete('let foo = util.') // -> [['util.print', 'util.debug', 'util.log', 'util.inspect'], // 'util.' ] // @@ -1067,16 +1067,16 @@ function complete(line, callback) { if (this[kBufferedCommandSymbol] !== undefined && this[kBufferedCommandSymbol].length) { // Get a new array of inputted lines - var tmp = this.lines.slice(); + const tmp = this.lines.slice(); // Kill off all function declarations to push all local variables into // global scope - for (var n = 0; n < this.lines.level.length; n++) { - var kill = this.lines.level[n]; + for (let n = 0; n < this.lines.level.length; n++) { + const kill = this.lines.level[n]; if (kill.isFunction) tmp[kill.line] = ''; } - var flat = new ArrayStream(); // Make a new "input" stream. - var magic = new REPLServer('', flat); // Make a nested REPL. + const flat = new ArrayStream(); // Make a new "input" stream. + const magic = new REPLServer('', flat); // Make a nested REPL. replMap.set(magic, replMap.get(this)); flat.run(tmp); // `eval` the flattened code. // All this is only profitable if the nested REPL does not have a @@ -1087,13 +1087,12 @@ function complete(line, callback) { } } - var completions; // List of completion lists, one for each inheritance "level" - var completionGroups = []; - var completeOn, group, c; + let completionGroups = []; + let completeOn, group; // REPL commands (e.g. ".break"). - var filter; + let filter; let match = line.match(/^\s*\.(\w*)$/); if (match) { completionGroups.push(Object.keys(this.commands)); @@ -1106,14 +1105,14 @@ function complete(line, callback) { } else if (match = line.match(requireRE)) { // require('...') const exts = Object.keys(this.context.require.extensions); - var indexRe = new RegExp('^index(?:' + exts.map(regexpEscape).join('|') + + const indexRe = new RegExp('^index(?:' + exts.map(regexpEscape).join('|') + ')$'); - var versionedFileNamesRe = /-\d+\.\d+/; + const versionedFileNamesRe = /-\d+\.\d+/; completeOn = match[1]; - var subdir = match[2] || ''; + const subdir = match[2] || ''; filter = match[1]; - var dir, files, name, base, ext, abs, subfiles, isDirectory; + let dir, files, name, base, ext, abs, subfiles, isDirectory; group = []; let paths = []; @@ -1211,7 +1210,7 @@ function complete(line, callback) { } else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) { match = simpleExpressionRE.exec(line); if (line.length === 0 || match) { - var expr; + let expr; completeOn = (match ? match[0] : ''); if (line.length === 0) { filter = ''; @@ -1220,19 +1219,19 @@ function complete(line, callback) { filter = ''; expr = match[0].slice(0, match[0].length - 1); } else { - var bits = match[0].split('.'); + const bits = match[0].split('.'); filter = bits.pop(); expr = bits.join('.'); } // Resolve expr and get its completions. - var memberGroups = []; + const memberGroups = []; if (!expr) { // If context is instance of vm.ScriptContext // Get global vars synchronously if (this.useGlobal || vm.isContext(this.context)) { completionGroups.push(getGlobalLexicalScopeNames(this[kContextId])); - var contextProto = this.context; + let contextProto = this.context; while (contextProto = Object.getPrototypeOf(contextProto)) { completionGroups.push( filteredOwnPropertyNames.call(this, contextProto)); @@ -1247,7 +1246,7 @@ function complete(line, callback) { if (filter !== '') addCommonWords(completionGroups); } else if (Array.isArray(globals[0])) { // Add grouped globals - for (var n = 0; n < globals.length; n++) + for (let n = 0; n < globals.length; n++) completionGroups.push(globals[n]); } else { completionGroups.push(globals); @@ -1272,8 +1271,8 @@ function complete(line, callback) { } // Works for non-objects try { - var sentinel = 5; - var p; + let sentinel = 5; + let p; if (typeof obj === 'object' || typeof obj === 'function') { p = Object.getPrototypeOf(obj); } else { @@ -1316,7 +1315,7 @@ function complete(line, callback) { function completionGroupsLoaded() { // Filter, sort (within each group), uniq and merge the completion groups. if (completionGroups.length && filter) { - var newCompletionGroups = []; + const newCompletionGroups = []; for (let i = 0; i < completionGroups.length; i++) { group = completionGroups[i] .filter((elem) => elem.indexOf(filter) === 0); @@ -1327,8 +1326,10 @@ function complete(line, callback) { completionGroups = newCompletionGroups; } + let completions; + if (completionGroups.length) { - var uniq = {}; // Unique completions across all groups + const uniq = {}; // Unique completions across all groups completions = []; // Completion group 0 is the "closest" // (least far up the inheritance chain) @@ -1336,8 +1337,8 @@ function complete(line, callback) { for (let i = 0; i < completionGroups.length; i++) { group = completionGroups[i]; group.sort(); - for (var j = group.length - 1; j >= 0; j--) { - c = group[j]; + for (let j = group.length - 1; j >= 0; j--) { + const c = group[j]; if (!ObjectPrototype.hasOwnProperty(uniq, c)) { completions.unshift(c); uniq[c] = true; @@ -1361,9 +1362,9 @@ function longestCommonPrefix(arr = []) { const first = arr[0]; // complexity: O(m * n) - for (var m = 0; m < first.length; m++) { + for (let m = 0; m < first.length; m++) { const c = first[m]; - for (var n = 1; n < cnt; n++) { + for (let n = 1; n < cnt; n++) { const entry = arr[n]; if (m >= entry.length || c !== entry[m]) { return first.substring(0, m); @@ -1506,7 +1507,7 @@ function defineDefaultCommands(repl) { } }); - var clearMessage; + let clearMessage; if (repl.useGlobal) { clearMessage = 'Alias for .break'; } else { @@ -1539,11 +1540,11 @@ function defineDefaultCommands(repl) { (max, name) => Math.max(max, name.length), 0 ); - for (var n = 0; n < names.length; n++) { - var name = names[n]; - var cmd = this.commands[name]; - var spaces = ' '.repeat(longestNameLength - name.length + 3); - var line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; + for (let n = 0; n < names.length; n++) { + const name = names[n]; + const cmd = this.commands[name]; + const spaces = ' '.repeat(longestNameLength - name.length + 3); + const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; this.outputStream.write(line); } this.outputStream.write('\nPress ^C to abort current expression, ' +