Skip to content

Commit

Permalink
repl: avoid using forEach
Browse files Browse the repository at this point in the history
PR-URL: #11582
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
jasnell authored and italoacasas committed Mar 20, 2017
1 parent 90be5a1 commit 62e7261
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions lib/repl.js
Expand Up @@ -47,7 +47,10 @@ const GLOBAL_OBJECT_PROPERTIES = [
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
];
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 {
// hack for require.resolve("./relative") to work properly.
Expand Down Expand Up @@ -692,13 +695,17 @@ REPLServer.prototype.createContext = function() {
enumerable: true,
get: () => _console
});
Object.getOwnPropertyNames(global).filter((name) => {
if (name === 'console' || name === 'global') return false;
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
}).forEach((name) => {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
});

var names = Object.getOwnPropertyNames(global);
for (var n = 0; n < names.length; n++) {
var name = names[n];
if (name === 'console' || name === 'global')
continue;
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
}
}

const module = new Module('<repl>');
Expand Down Expand Up @@ -769,10 +776,8 @@ function ArrayStream() {
Stream.call(this);

this.run = function(data) {
var self = this;
data.forEach(function(line) {
self.emit('data', line + '\n');
});
for (var n = 0; n < data.length; n++)
this.emit('data', `${data[n]}\n`);
};
}
util.inherits(ArrayStream, Stream);
Expand Down Expand Up @@ -816,11 +821,11 @@ function complete(line, callback) {
var tmp = this.lines.slice();
// Kill off all function declarations to push all local variables into
// global scope
this.lines.level.forEach(function(kill) {
if (kill.isFunction) {
for (var n = 0; n < this.lines.level.length; n++) {
var 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
replMap.set(magic, replMap.get(this));
Expand Down Expand Up @@ -954,9 +959,8 @@ function complete(line, callback) {
addStandardGlobals(completionGroups, filter);
} else if (Array.isArray(globals[0])) {
// Add grouped globals
globals.forEach(function(group) {
completionGroups.push(group);
});
for (var n = 0; n < globals.length; n++)
completionGroups.push(globals[n]);
} else {
completionGroups.push(globals);
addStandardGlobals(completionGroups, filter);
Expand Down Expand Up @@ -1264,12 +1268,13 @@ function defineDefaultCommands(repl) {
(max, name) => Math.max(max, name.length),
0
);
names.forEach((name) => {
const cmd = this.commands[name];
const spaces = ' '.repeat(longestNameLength - name.length + 3);
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
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`;
this.outputStream.write(line);
});
}
this.displayPrompt();
}
});
Expand All @@ -1293,15 +1298,13 @@ function defineDefaultCommands(repl) {
try {
var stats = fs.statSync(file);
if (stats && stats.isFile()) {
var self = this;
var data = fs.readFileSync(file, 'utf8');
var lines = data.split('\n');
this.displayPrompt();
lines.forEach(function(line) {
if (line) {
self.write(line + '\n');
}
});
for (var n = 0; n < lines.length; n++) {
if (lines[n])
this.write(`${lines[n]}\n`);
}
} else {
this.outputStream.write('Failed to load:' + file +
' is not a valid file\n');
Expand Down

0 comments on commit 62e7261

Please sign in to comment.