Skip to content

Commit 62e7261

Browse files
jasnellitaloacasas
authored andcommitted
repl: avoid using forEach
PR-URL: #11582 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 90be5a1 commit 62e7261

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

lib/repl.js

+33-30
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ const GLOBAL_OBJECT_PROPERTIES = [
4747
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
4848
];
4949
const GLOBAL_OBJECT_PROPERTY_MAP = {};
50-
GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p);
50+
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
51+
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
52+
GLOBAL_OBJECT_PROPERTIES[n];
53+
}
5154

5255
try {
5356
// hack for require.resolve("./relative") to work properly.
@@ -692,13 +695,17 @@ REPLServer.prototype.createContext = function() {
692695
enumerable: true,
693696
get: () => _console
694697
});
695-
Object.getOwnPropertyNames(global).filter((name) => {
696-
if (name === 'console' || name === 'global') return false;
697-
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
698-
}).forEach((name) => {
699-
Object.defineProperty(context, name,
700-
Object.getOwnPropertyDescriptor(global, name));
701-
});
698+
699+
var names = Object.getOwnPropertyNames(global);
700+
for (var n = 0; n < names.length; n++) {
701+
var name = names[n];
702+
if (name === 'console' || name === 'global')
703+
continue;
704+
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
705+
Object.defineProperty(context, name,
706+
Object.getOwnPropertyDescriptor(global, name));
707+
}
708+
}
702709
}
703710

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

771778
this.run = function(data) {
772-
var self = this;
773-
data.forEach(function(line) {
774-
self.emit('data', line + '\n');
775-
});
779+
for (var n = 0; n < data.length; n++)
780+
this.emit('data', `${data[n]}\n`);
776781
};
777782
}
778783
util.inherits(ArrayStream, Stream);
@@ -816,11 +821,11 @@ function complete(line, callback) {
816821
var tmp = this.lines.slice();
817822
// Kill off all function declarations to push all local variables into
818823
// global scope
819-
this.lines.level.forEach(function(kill) {
820-
if (kill.isFunction) {
824+
for (var n = 0; n < this.lines.level.length; n++) {
825+
var kill = this.lines.level[n];
826+
if (kill.isFunction)
821827
tmp[kill.line] = '';
822-
}
823-
});
828+
}
824829
var flat = new ArrayStream(); // make a new "input" stream
825830
var magic = new REPLServer('', flat); // make a nested REPL
826831
replMap.set(magic, replMap.get(this));
@@ -954,9 +959,8 @@ function complete(line, callback) {
954959
addStandardGlobals(completionGroups, filter);
955960
} else if (Array.isArray(globals[0])) {
956961
// Add grouped globals
957-
globals.forEach(function(group) {
958-
completionGroups.push(group);
959-
});
962+
for (var n = 0; n < globals.length; n++)
963+
completionGroups.push(globals[n]);
960964
} else {
961965
completionGroups.push(globals);
962966
addStandardGlobals(completionGroups, filter);
@@ -1264,12 +1268,13 @@ function defineDefaultCommands(repl) {
12641268
(max, name) => Math.max(max, name.length),
12651269
0
12661270
);
1267-
names.forEach((name) => {
1268-
const cmd = this.commands[name];
1269-
const spaces = ' '.repeat(longestNameLength - name.length + 3);
1270-
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
1271+
for (var n = 0; n < names.length; n++) {
1272+
var name = names[n];
1273+
var cmd = this.commands[name];
1274+
var spaces = ' '.repeat(longestNameLength - name.length + 3);
1275+
var line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
12711276
this.outputStream.write(line);
1272-
});
1277+
}
12731278
this.displayPrompt();
12741279
}
12751280
});
@@ -1293,15 +1298,13 @@ function defineDefaultCommands(repl) {
12931298
try {
12941299
var stats = fs.statSync(file);
12951300
if (stats && stats.isFile()) {
1296-
var self = this;
12971301
var data = fs.readFileSync(file, 'utf8');
12981302
var lines = data.split('\n');
12991303
this.displayPrompt();
1300-
lines.forEach(function(line) {
1301-
if (line) {
1302-
self.write(line + '\n');
1303-
}
1304-
});
1304+
for (var n = 0; n < lines.length; n++) {
1305+
if (lines[n])
1306+
this.write(`${lines[n]}\n`);
1307+
}
13051308
} else {
13061309
this.outputStream.write('Failed to load:' + file +
13071310
' is not a valid file\n');

0 commit comments

Comments
 (0)