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 <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
jasnell committed Mar 17, 2017
1 parent 53cf483 commit ae9e640
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 @@ -585,13 +588,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 @@ -662,10 +669,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 @@ -709,11 +714,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 @@ -847,9 +852,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 @@ -1155,12 +1159,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 @@ -1184,15 +1189,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 ae9e640

Please sign in to comment.