Skip to content

Commit

Permalink
repl: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #37345
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Feb 16, 2021
1 parent 799b2d5 commit 6d53e79
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/internal/repl/await.js
Expand Up @@ -2,6 +2,7 @@

const {
ArrayFrom,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypePop,
ArrayPrototypePush,
Expand Down Expand Up @@ -61,10 +62,10 @@ const visitorsWithoutAncestors = {
state.replace(node.start, node.start + node.kind.length, 'void (');
}

for (const decl of node.declarations) {
ArrayPrototypeForEach(node.declarations, (decl) => {
state.prepend(decl, '(');
state.append(decl, decl.init ? ')' : '=undefined)');
}
});

if (node.declarations.length !== 1) {
state.append(node.declarations[node.declarations.length - 1], ')');
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/repl/utils.js
Expand Up @@ -103,6 +103,10 @@ function isRecoverableError(e, code) {
staticClassFeatures,
(Parser) => {
return class extends Parser {
// eslint-disable-next-line no-useless-constructor
constructor(options, input, startPos) {
super(options, input, startPos);
}
nextToken() {
super.nextToken();
if (this.type === tt.eof)
Expand Down
6 changes: 3 additions & 3 deletions lib/repl.js
Expand Up @@ -62,7 +62,7 @@ const {
Boolean,
Error,
FunctionPrototypeBind,
MathMax,
MathMaxApply,
NumberIsNaN,
NumberParseFloat,
ObjectAssign,
Expand Down Expand Up @@ -1647,8 +1647,8 @@ function defineDefaultCommands(repl) {
help: 'Print this help message',
action: function() {
const names = ArrayPrototypeSort(ObjectKeys(this.commands));
const longestNameLength = MathMax(
...ArrayPrototypeMap(names, (name) => name.length)
const longestNameLength = MathMaxApply(
ArrayPrototypeMap(names, (name) => name.length)
);
ArrayPrototypeForEach(names, (name) => {
const cmd = this.commands[name];
Expand Down

0 comments on commit 6d53e79

Please sign in to comment.