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