From 37ebc2875603c19e600f5851ef83f15c2637839e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 17 Jul 2022 15:48:39 +0200 Subject: [PATCH] Use more `for...of` loops in the code-base Note that these cases, which are all in older code, were found using the [`unicorn/no-for-loop`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-for-loop.md) ESLint plugin rule. However, note that I've opted not to enable this rule by default since there's still *some* cases where I do think that it makes sense to allow "regular" for-loops. --- extensions/chromium/extension-router.js | 4 ++-- extensions/chromium/pdfHandler.js | 3 +-- extensions/chromium/telemetry.js | 3 +-- external/cmapscompress/compress.js | 4 ++-- external/cmapscompress/optimize.js | 6 +++--- gulpfile.js | 3 +-- src/core/cff_parser.js | 4 +--- src/core/type1_font.js | 5 ++--- src/core/type1_parser.js | 10 ++++------ src/core/worker.js | 4 ++-- src/display/canvas.js | 3 +-- test/resources/reftest-analyzer.js | 10 +++++----- test/stats/statcmp.js | 18 ++++++------------ test/unit/function_spec.js | 8 ++++---- 14 files changed, 35 insertions(+), 50 deletions(-) diff --git a/extensions/chromium/extension-router.js b/extensions/chromium/extension-router.js index b1ee148d5..2660da684 100644 --- a/extensions/chromium/extension-router.js +++ b/extensions/chromium/extension-router.js @@ -95,8 +95,8 @@ limitations under the License. url: CRX_BASE_URL + "*:*", }, function (tabsFromLastSession) { - for (var i = 0; i < tabsFromLastSession.length; ++i) { - chrome.tabs.reload(tabsFromLastSession[i].id); + for (const { id } of tabsFromLastSession) { + chrome.tabs.reload(id); } } ); diff --git a/extensions/chromium/pdfHandler.js b/extensions/chromium/pdfHandler.js index d12081b68..3b014db5c 100644 --- a/extensions/chromium/pdfHandler.js +++ b/extensions/chromium/pdfHandler.js @@ -54,8 +54,7 @@ function isPdfDownloadable(details) { * @returns {undefined|{name: string, value: string}} The header, if found. */ function getHeaderFromHeaders(headers, headerName) { - for (var i = 0; i < headers.length; ++i) { - var header = headers[i]; + for (const header of headers) { if (header.name.toLowerCase() === headerName) { return header; } diff --git a/extensions/chromium/telemetry.js b/extensions/chromium/telemetry.js index d6fe1c0a4..a142a19aa 100644 --- a/extensions/chromium/telemetry.js +++ b/extensions/chromium/telemetry.js @@ -110,8 +110,7 @@ limitations under the License. id = ""; var buf = new Uint8Array(5); crypto.getRandomValues(buf); - for (var i = 0; i < buf.length; ++i) { - var c = buf[i]; + for (const c of buf) { id += (c >>> 4).toString(16) + (c & 0xf).toString(16); } localStorage.telemetryDeduplicationId = id; diff --git a/external/cmapscompress/compress.js b/external/cmapscompress/compress.js index 81316fb47..2fe83e078 100644 --- a/external/cmapscompress/compress.js +++ b/external/cmapscompress/compress.js @@ -218,8 +218,8 @@ function parseCMap(binaryData) { const sign = fromHexDigit(num[num.length - 1]) & 1 ? 15 : 0; let c = 0; let result = ""; - for (let i = 0; i < num.length; i++) { - c = (c << 4) | fromHexDigit(num[i]); + for (const digit of num) { + c = (c << 4) | fromHexDigit(digit); result += toHexDigit(sign ? (c >> 1) ^ sign : c >> 1); c &= 1; } diff --git a/external/cmapscompress/optimize.js b/external/cmapscompress/optimize.js index 231912bb5..69840af7d 100644 --- a/external/cmapscompress/optimize.js +++ b/external/cmapscompress/optimize.js @@ -196,13 +196,13 @@ exports.optimizeCMap = function (data) { i++; data.body.splice(i, 0, newItem); } - for (let j = 0; j < subitems.length; j++) { - const code = subitems[j].code; + for (const subitem of subitems) { + const { code } = subitem; let q = 0; while (q < groups.length && groups[q] <= code) { q++; } - buckets[q].push(subitems[j]); + buckets[q].push(subitem); } } } diff --git a/gulpfile.js b/gulpfile.js index c98ff7e2b..936be8376 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -782,8 +782,7 @@ gulp.task("locale", function () { subfolders.sort(); let viewerOutput = ""; const locales = []; - for (let i = 0; i < subfolders.length; i++) { - const locale = subfolders[i]; + for (const locale of subfolders) { const dirPath = L10N_DIR + locale; if (!checkDir(dirPath)) { continue; diff --git a/src/core/cff_parser.js b/src/core/cff_parser.js index d80899bf4..78ebb167b 100644 --- a/src/core/cff_parser.js +++ b/src/core/cff_parser.js @@ -1682,9 +1682,7 @@ class CFFCompiler { compileDict(dict, offsetTracker) { const out = []; // The dictionary keys must be in a certain order. - const order = dict.order; - for (let i = 0; i < order.length; ++i) { - const key = order[i]; + for (const key of dict.order) { if (!(key in dict.values)) { continue; } diff --git a/src/core/type1_font.js b/src/core/type1_font.js index bde53e9a1..3ff2240c5 100644 --- a/src/core/type1_font.js +++ b/src/core/type1_font.js @@ -219,9 +219,8 @@ class Type1Font { getCharset() { const charset = [".notdef"]; - const charstrings = this.charstrings; - for (let glyphId = 0; glyphId < charstrings.length; glyphId++) { - charset.push(charstrings[glyphId].glyphName); + for (const { glyphName } of this.charstrings) { + charset.push(glyphName); } return charset; } diff --git a/src/core/type1_parser.js b/src/core/type1_parser.js index 2efac2d9b..dea6cf63b 100644 --- a/src/core/type1_parser.js +++ b/src/core/type1_parser.js @@ -576,7 +576,7 @@ const Type1Parser = (function Type1ParserClosure() { privateData, }, }; - let token, length, data, lenIV, encoded; + let token, length, data, lenIV; while ((token = this.getToken()) !== null) { if (token !== "/") { continue; @@ -604,7 +604,7 @@ const Type1Parser = (function Type1ParserClosure() { this.getToken(); // read in 'RD' or '-|' data = length > 0 ? stream.getBytes(length) : new Uint8Array(0); lenIV = program.properties.privateData.lenIV; - encoded = this.readCharStrings(data, lenIV); + const encoded = this.readCharStrings(data, lenIV); this.nextChar(); token = this.getToken(); // read in 'ND' or '|-' if (token === "noaccess") { @@ -629,7 +629,7 @@ const Type1Parser = (function Type1ParserClosure() { this.getToken(); // read in 'RD' or '-|' data = length > 0 ? stream.getBytes(length) : new Uint8Array(0); lenIV = program.properties.privateData.lenIV; - encoded = this.readCharStrings(data, lenIV); + const encoded = this.readCharStrings(data, lenIV); this.nextChar(); token = this.getToken(); // read in 'NP' or '|' if (token === "noaccess") { @@ -675,9 +675,7 @@ const Type1Parser = (function Type1ParserClosure() { } } - for (let i = 0; i < charstrings.length; i++) { - const glyph = charstrings[i].glyph; - encoded = charstrings[i].encoded; + for (const { encoded, glyph } of charstrings) { const charString = new Type1CharString(); const error = charString.convert( encoded, diff --git a/src/core/worker.js b/src/core/worker.js index 0d80cbbb8..3a9b6c60e 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -263,8 +263,8 @@ class WorkerMessageHandler { // There may be a chance that `newPdfManager` is not initialized for // the first few runs of `readchunk` block of code. Be sure to send // all cached chunks, if any, to chunked_stream via pdf_manager. - for (let i = 0; i < cachedChunks.length; i++) { - newPdfManager.sendProgressiveData(cachedChunks[i]); + for (const chunk of cachedChunks) { + newPdfManager.sendProgressiveData(chunk); } cachedChunks = []; diff --git a/src/display/canvas.js b/src/display/canvas.js index 433ffb7a1..5e8816d08 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -2211,8 +2211,7 @@ class CanvasGraphics { ctx.save(); ctx.beginPath(); - for (let i = 0; i < paths.length; i++) { - const path = paths[i]; + for (const path of paths) { ctx.setTransform.apply(ctx, path.transform); ctx.translate(path.x, path.y); path.addToPath(ctx, path.fontSize); diff --git a/test/resources/reftest-analyzer.js b/test/resources/reftest-analyzer.js index 6a6ed2488..e8bf06573 100644 --- a/test/resources/reftest-analyzer.js +++ b/test/resources/reftest-analyzer.js @@ -301,8 +301,8 @@ window.onload = function () { // Bind an event handler to each image link const images = document.getElementsByClassName("image"); - for (let i = 0; i < images.length; i++) { - images[i].addEventListener( + for (const image of images) { + image.addEventListener( "click", function (e) { showImages(e.target.id); @@ -407,9 +407,9 @@ window.onload = function () { function flashPixels(on) { const stroke = on ? "#FF0000" : "#CCC"; const strokeWidth = on ? "2px" : "1px"; - for (let i = 0; i < gFlashingPixels.length; i++) { - gFlashingPixels[i].setAttribute("stroke", stroke); - gFlashingPixels[i].setAttribute("stroke-width", strokeWidth); + for (const pixel of gFlashingPixels) { + pixel.setAttribute("stroke", stroke); + pixel.setAttribute("stroke-width", strokeWidth); } } diff --git a/test/stats/statcmp.js b/test/stats/statcmp.js index 22d535a5a..1ddf9d2c0 100644 --- a/test/stats/statcmp.js +++ b/test/stats/statcmp.js @@ -30,17 +30,13 @@ function parseOptions() { function group(stats, groupBy) { const vals = []; - for (let i = 0; i < stats.length; i++) { - const curStat = stats[i]; + for (const curStat of stats) { const keyArr = []; - for (let j = 0; j < groupBy.length; j++) { - keyArr.push(curStat[groupBy[j]]); + for (const entry of groupBy) { + keyArr.push(curStat[entry]); } const key = keyArr.join(","); - if (vals[key] === undefined) { - vals[key] = []; - } - vals[key].push(curStat.time); + (vals[key] ||= []).push(curStat.time); } return vals; } @@ -134,8 +130,7 @@ function stat(baseline, current) { return s.length; }); rows.push(labels); - for (let k = 0; k < keys.length; k++) { - const key = keys[k]; + for (const key of keys) { const baselineMean = mean(baselineGroup[key]); const currentMean = mean(currentGroup[key]); const row = key.split(","); @@ -172,8 +167,7 @@ function stat(baseline, current) { // print output console.log("-- Grouped By " + options.groupBy.join(", ") + " --"); const groupCount = options.groupBy.length; - for (let r = 0; r < rows.length; r++) { - const row = rows[r]; + for (const row of rows) { for (let i = 0; i < row.length; i++) { row[i] = pad(row[i], width[i], i < groupCount ? "right" : "left"); } diff --git a/test/unit/function_spec.js b/test/unit/function_spec.js index ead609579..ac67e0d4d 100644 --- a/test/unit/function_spec.js +++ b/test/unit/function_spec.js @@ -416,10 +416,10 @@ describe("function", function () { "destOffset", compiledCode ); - for (let i = 0; i < samples.length; i++) { - const out = new Float32Array(samples[i].output.length); - fn(samples[i].input, 0, out, 0); - expect(Array.prototype.slice.call(out, 0)).toEqual(samples[i].output); + for (const { input, output } of samples) { + const out = new Float32Array(output.length); + fn(input, 0, out, 0); + expect(Array.prototype.slice.call(out, 0)).toEqual(output); } } }