repl: avoid using forEach
PR-URL: https://github.com/nodejs/node/pull/11582 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
53cf483401
commit
ae9e640f30
55
lib/repl.js
55
lib/repl.js
@ -68,7 +68,10 @@ const GLOBAL_OBJECT_PROPERTIES = [
|
|||||||
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
|
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
|
||||||
];
|
];
|
||||||
const GLOBAL_OBJECT_PROPERTY_MAP = {};
|
const GLOBAL_OBJECT_PROPERTY_MAP = {};
|
||||||
GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p);
|
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
|
||||||
|
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
|
||||||
|
GLOBAL_OBJECT_PROPERTIES[n];
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// hack for require.resolve("./relative") to work properly.
|
// hack for require.resolve("./relative") to work properly.
|
||||||
@ -585,13 +588,17 @@ REPLServer.prototype.createContext = function() {
|
|||||||
enumerable: true,
|
enumerable: true,
|
||||||
get: () => _console
|
get: () => _console
|
||||||
});
|
});
|
||||||
Object.getOwnPropertyNames(global).filter((name) => {
|
|
||||||
if (name === 'console' || name === 'global') return false;
|
var names = Object.getOwnPropertyNames(global);
|
||||||
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
|
for (var n = 0; n < names.length; n++) {
|
||||||
}).forEach((name) => {
|
var name = names[n];
|
||||||
|
if (name === 'console' || name === 'global')
|
||||||
|
continue;
|
||||||
|
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
|
||||||
Object.defineProperty(context, name,
|
Object.defineProperty(context, name,
|
||||||
Object.getOwnPropertyDescriptor(global, name));
|
Object.getOwnPropertyDescriptor(global, name));
|
||||||
});
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const module = new Module('<repl>');
|
const module = new Module('<repl>');
|
||||||
@ -662,10 +669,8 @@ function ArrayStream() {
|
|||||||
Stream.call(this);
|
Stream.call(this);
|
||||||
|
|
||||||
this.run = function(data) {
|
this.run = function(data) {
|
||||||
var self = this;
|
for (var n = 0; n < data.length; n++)
|
||||||
data.forEach(function(line) {
|
this.emit('data', `${data[n]}\n`);
|
||||||
self.emit('data', line + '\n');
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
util.inherits(ArrayStream, Stream);
|
util.inherits(ArrayStream, Stream);
|
||||||
@ -709,11 +714,11 @@ function complete(line, callback) {
|
|||||||
var tmp = this.lines.slice();
|
var 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
|
||||||
this.lines.level.forEach(function(kill) {
|
for (var n = 0; n < this.lines.level.length; n++) {
|
||||||
if (kill.isFunction) {
|
var kill = this.lines.level[n];
|
||||||
|
if (kill.isFunction)
|
||||||
tmp[kill.line] = '';
|
tmp[kill.line] = '';
|
||||||
}
|
}
|
||||||
});
|
|
||||||
var flat = new ArrayStream(); // make a new "input" stream
|
var flat = new ArrayStream(); // make a new "input" stream
|
||||||
var magic = new REPLServer('', flat); // make a nested REPL
|
var magic = new REPLServer('', flat); // make a nested REPL
|
||||||
replMap.set(magic, replMap.get(this));
|
replMap.set(magic, replMap.get(this));
|
||||||
@ -847,9 +852,8 @@ function complete(line, callback) {
|
|||||||
addStandardGlobals(completionGroups, filter);
|
addStandardGlobals(completionGroups, filter);
|
||||||
} else if (Array.isArray(globals[0])) {
|
} else if (Array.isArray(globals[0])) {
|
||||||
// Add grouped globals
|
// Add grouped globals
|
||||||
globals.forEach(function(group) {
|
for (var n = 0; n < globals.length; n++)
|
||||||
completionGroups.push(group);
|
completionGroups.push(globals[n]);
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
completionGroups.push(globals);
|
completionGroups.push(globals);
|
||||||
addStandardGlobals(completionGroups, filter);
|
addStandardGlobals(completionGroups, filter);
|
||||||
@ -1155,12 +1159,13 @@ function defineDefaultCommands(repl) {
|
|||||||
(max, name) => Math.max(max, name.length),
|
(max, name) => Math.max(max, name.length),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
names.forEach((name) => {
|
for (var n = 0; n < names.length; n++) {
|
||||||
const cmd = this.commands[name];
|
var name = names[n];
|
||||||
const spaces = ' '.repeat(longestNameLength - name.length + 3);
|
var cmd = this.commands[name];
|
||||||
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
|
var spaces = ' '.repeat(longestNameLength - name.length + 3);
|
||||||
|
var line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
|
||||||
this.outputStream.write(line);
|
this.outputStream.write(line);
|
||||||
});
|
}
|
||||||
this.displayPrompt();
|
this.displayPrompt();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -1184,15 +1189,13 @@ function defineDefaultCommands(repl) {
|
|||||||
try {
|
try {
|
||||||
var stats = fs.statSync(file);
|
var stats = fs.statSync(file);
|
||||||
if (stats && stats.isFile()) {
|
if (stats && stats.isFile()) {
|
||||||
var self = this;
|
|
||||||
var data = fs.readFileSync(file, 'utf8');
|
var data = fs.readFileSync(file, 'utf8');
|
||||||
var lines = data.split('\n');
|
var lines = data.split('\n');
|
||||||
this.displayPrompt();
|
this.displayPrompt();
|
||||||
lines.forEach(function(line) {
|
for (var n = 0; n < lines.length; n++) {
|
||||||
if (line) {
|
if (lines[n])
|
||||||
self.write(line + '\n');
|
this.write(`${lines[n]}\n`);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
this.outputStream.write('Failed to load:' + file +
|
this.outputStream.write('Failed to load:' + file +
|
||||||
' is not a valid file\n');
|
' is not a valid file\n');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user