From 708f04947100474790a3804ef703000f2b82ad9b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 12 Feb 2021 12:59:21 +0100 Subject: [PATCH] repl: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/37345 Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- lib/internal/repl/await.js | 5 +++-- lib/internal/repl/utils.js | 4 ++++ lib/repl.js | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/internal/repl/await.js b/lib/internal/repl/await.js index 8dc1f5de4c7ee6..9d36c7147509b4 100644 --- a/lib/internal/repl/await.js +++ b/lib/internal/repl/await.js @@ -2,6 +2,7 @@ const { ArrayFrom, + ArrayPrototypeForEach, ArrayPrototypeJoin, ArrayPrototypePop, ArrayPrototypePush, @@ -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], ')'); diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 8fee6d40123c49..a1c7ef8a81a5a1 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -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) diff --git a/lib/repl.js b/lib/repl.js index acbfe8a9b709ae..e277da3b8763cd 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -62,7 +62,7 @@ const { Boolean, Error, FunctionPrototypeBind, - MathMax, + MathMaxApply, NumberIsNaN, NumberParseFloat, ObjectAssign, @@ -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];