From c42029489ec1f4a24acb3f07c147bd1fbf2587e7 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 29 Nov 2020 09:59:03 +0100 Subject: [PATCH] Run `gulp lint --fix`, to account for changes in Prettier version `2.2.1` Please refer to https://github.com/prettier/prettier/blob/master/CHANGELOG.md#221 for additional details. --- external/builder/builder.js | 16 +- gulpfile.js | 95 +++++----- src/core/cmap.js | 12 +- src/core/worker.js | 177 +++++++++--------- src/scripting_api/util.js | 254 +++++++++++++------------- test/unit/evaluator_spec.js | 108 ++++++----- test/unit/pdf_find_controller_spec.js | 41 +++-- 7 files changed, 358 insertions(+), 345 deletions(-) diff --git a/external/builder/builder.js b/external/builder/builder.js index 997be77ad..2454f3da5 100644 --- a/external/builder/builder.js +++ b/external/builder/builder.js @@ -202,14 +202,14 @@ function preprocessCSS(mode, source, destination) { } function expandImports(content, baseUrl) { - return content.replace(/^\s*@import\s+url\(([^)]+)\);\s*$/gm, function ( - all, - url - ) { - var file = path.join(path.dirname(baseUrl), url); - var imported = fs.readFileSync(file, "utf8").toString(); - return expandImports(imported, file); - }); + return content.replace( + /^\s*@import\s+url\(([^)]+)\);\s*$/gm, + function (all, url) { + var file = path.join(path.dirname(baseUrl), url); + var imported = fs.readFileSync(file, "utf8").toString(); + return expandImports(imported, file); + } + ); } function removePrefixed(content, hasPrefixedFilter) { diff --git a/gulpfile.js b/gulpfile.js index f21e64589..8e95a5257 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -577,45 +577,46 @@ gulp.task("buildnumber", function (done) { console.log(); console.log("### Getting extension build number"); - exec("git log --format=oneline " + config.baseVersion + "..", function ( - err, - stdout, - stderr - ) { - var buildNumber = 0; - if (!err) { - // Build number is the number of commits since base version - buildNumber = stdout ? stdout.match(/\n/g).length : 0; - } else { - console.log("This is not a Git repository; using default build number."); - } - - console.log("Extension build number: " + buildNumber); - - var version = config.versionPrefix + buildNumber; - - exec('git log --format="%h" -n 1', function (err2, stdout2, stderr2) { - var buildCommit = ""; - if (!err2) { - buildCommit = stdout2.replace("\n", ""); + exec( + "git log --format=oneline " + config.baseVersion + "..", + function (err, stdout, stderr) { + var buildNumber = 0; + if (!err) { + // Build number is the number of commits since base version + buildNumber = stdout ? stdout.match(/\n/g).length : 0; + } else { + console.log( + "This is not a Git repository; using default build number." + ); } - createStringSource( - "version.json", - JSON.stringify( - { - version: version, - build: buildNumber, - commit: buildCommit, - }, - null, - 2 + console.log("Extension build number: " + buildNumber); + + var version = config.versionPrefix + buildNumber; + + exec('git log --format="%h" -n 1', function (err2, stdout2, stderr2) { + var buildCommit = ""; + if (!err2) { + buildCommit = stdout2.replace("\n", ""); + } + + createStringSource( + "version.json", + JSON.stringify( + { + version: version, + build: buildNumber, + commit: buildCommit, + }, + null, + 2 + ) ) - ) - .pipe(gulp.dest(BUILD_DIR)) - .on("end", done); - }); - }); + .pipe(gulp.dest(BUILD_DIR)) + .on("end", done); + }); + } + ); }); gulp.task("default_preferences-pre", function () { @@ -1619,17 +1620,19 @@ gulp.task("baseline", function (done) { return; } - exec("git checkout " + baselineCommit, { cwd: workingDirectory }, function ( - error2 - ) { - if (error2) { - done(new Error("Baseline commit checkout failed.")); - return; - } + exec( + "git checkout " + baselineCommit, + { cwd: workingDirectory }, + function (error2) { + if (error2) { + done(new Error("Baseline commit checkout failed.")); + return; + } - console.log('Baseline commit "' + baselineCommit + '" checked out.'); - done(); - }); + console.log('Baseline commit "' + baselineCommit + '" checked out.'); + done(); + } + ); }); }); diff --git a/src/core/cmap.js b/src/core/cmap.js index 71c4d9fd9..838b4a332 100644 --- a/src/core/cmap.js +++ b/src/core/cmap.js @@ -994,11 +994,13 @@ var CMapFactory = (function CMapFactoryClosure() { var cMap = new CMap(true); if (compressionType === CMapCompressionType.BINARY) { - return new BinaryCMapReader().process(cMapData, cMap, function ( - useCMap - ) { - return extendCMap(cMap, fetchBuiltInCMap, useCMap); - }); + return new BinaryCMapReader().process( + cMapData, + cMap, + function (useCMap) { + return extendCMap(cMap, fetchBuiltInCMap, useCMap); + } + ); } if (compressionType === CMapCompressionType.NONE) { var lexer = new Lexer(new Stream(cMapData)); diff --git a/src/core/worker.js b/src/core/worker.js index ba0c51d1c..a45c0d46b 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -533,101 +533,100 @@ class WorkerMessageHandler { return pdfManager.ensureDoc("calculationOrderIds"); }); - handler.on("SaveDocument", function ({ - numPages, - annotationStorage, - filename, - }) { - pdfManager.requestLoadedStream(); - const promises = [ - pdfManager.onLoadedStream(), - pdfManager.ensureCatalog("acroForm"), - pdfManager.ensureDoc("xref"), - pdfManager.ensureDoc("startXRef"), - ]; + handler.on( + "SaveDocument", + function ({ numPages, annotationStorage, filename }) { + pdfManager.requestLoadedStream(); + const promises = [ + pdfManager.onLoadedStream(), + pdfManager.ensureCatalog("acroForm"), + pdfManager.ensureDoc("xref"), + pdfManager.ensureDoc("startXRef"), + ]; - for (let pageIndex = 0; pageIndex < numPages; pageIndex++) { - promises.push( - pdfManager.getPage(pageIndex).then(function (page) { - const task = new WorkerTask(`Save: page ${pageIndex}`); - startWorkerTask(task); + for (let pageIndex = 0; pageIndex < numPages; pageIndex++) { + promises.push( + pdfManager.getPage(pageIndex).then(function (page) { + const task = new WorkerTask(`Save: page ${pageIndex}`); + startWorkerTask(task); - return page - .save(handler, task, annotationStorage) - .finally(function () { - finishWorkerTask(task); - }); - }) - ); - } - - return Promise.all(promises).then(function ([ - stream, - acroForm, - xref, - startXRef, - ...refs - ]) { - let newRefs = []; - for (const ref of refs) { - newRefs = ref - .filter(x => x !== null) - .reduce((a, b) => a.concat(b), newRefs); + return page + .save(handler, task, annotationStorage) + .finally(function () { + finishWorkerTask(task); + }); + }) + ); } - if (newRefs.length === 0) { - // No new refs so just return the initial bytes - return stream.bytes; - } - - const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || []; - let xfaDatasets = null; - if (Array.isArray(xfa)) { - for (let i = 0, ii = xfa.length; i < ii; i += 2) { - if (xfa[i] === "datasets") { - xfaDatasets = xfa[i + 1]; - } - } - } else { - // TODO: Support XFA streams. - warn("Unsupported XFA type."); - } - - let newXrefInfo = Object.create(null); - if (xref.trailer) { - // Get string info from Info in order to compute fileId. - const infoObj = Object.create(null); - const xrefInfo = xref.trailer.get("Info") || null; - if (xrefInfo instanceof Dict) { - xrefInfo.forEach((key, value) => { - if (isString(key) && isString(value)) { - infoObj[key] = stringToPDFString(value); - } - }); - } - - newXrefInfo = { - rootRef: xref.trailer.getRaw("Root") || null, - encrypt: xref.trailer.getRaw("Encrypt") || null, - newRef: xref.getNewRef(), - infoRef: xref.trailer.getRaw("Info") || null, - info: infoObj, - fileIds: xref.trailer.getRaw("ID") || null, - startXRef, - filename, - }; - } - xref.resetNewRef(); - - return incrementalUpdate({ - originalData: stream.bytes, - xrefInfo: newXrefInfo, - newRefs, + return Promise.all(promises).then(function ([ + stream, + acroForm, xref, - datasetsRef: xfaDatasets, + startXRef, + ...refs + ]) { + let newRefs = []; + for (const ref of refs) { + newRefs = ref + .filter(x => x !== null) + .reduce((a, b) => a.concat(b), newRefs); + } + + if (newRefs.length === 0) { + // No new refs so just return the initial bytes + return stream.bytes; + } + + const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || []; + let xfaDatasets = null; + if (Array.isArray(xfa)) { + for (let i = 0, ii = xfa.length; i < ii; i += 2) { + if (xfa[i] === "datasets") { + xfaDatasets = xfa[i + 1]; + } + } + } else { + // TODO: Support XFA streams. + warn("Unsupported XFA type."); + } + + let newXrefInfo = Object.create(null); + if (xref.trailer) { + // Get string info from Info in order to compute fileId. + const infoObj = Object.create(null); + const xrefInfo = xref.trailer.get("Info") || null; + if (xrefInfo instanceof Dict) { + xrefInfo.forEach((key, value) => { + if (isString(key) && isString(value)) { + infoObj[key] = stringToPDFString(value); + } + }); + } + + newXrefInfo = { + rootRef: xref.trailer.getRaw("Root") || null, + encrypt: xref.trailer.getRaw("Encrypt") || null, + newRef: xref.getNewRef(), + infoRef: xref.trailer.getRaw("Info") || null, + info: infoObj, + fileIds: xref.trailer.getRaw("ID") || null, + startXRef, + filename, + }; + } + xref.resetNewRef(); + + return incrementalUpdate({ + originalData: stream.bytes, + xrefInfo: newXrefInfo, + newRefs, + xref, + datasetsRef: xfaDatasets, + }); }); - }); - }); + } + ); handler.on("GetOperatorList", function wphSetupRenderPage(data, sink) { var pageIndex = data.pageIndex; diff --git a/src/scripting_api/util.js b/src/scripting_api/util.js index 5a8a7c4e3..8805a48ee 100644 --- a/src/scripting_api/util.js +++ b/src/scripting_api/util.js @@ -70,140 +70,136 @@ class Util extends PDFObject { const ZERO = 4; const HASH = 8; let i = 0; - return args[0].replace(pattern, function ( - match, - nDecSep, - cFlags, - nWidth, - nPrecision, - cConvChar - ) { - // cConvChar must be one of d, f, s, x - if ( - cConvChar !== "d" && - cConvChar !== "f" && - cConvChar !== "s" && - cConvChar !== "x" - ) { - const buf = ["%"]; - for (const str of [nDecSep, cFlags, nWidth, nPrecision, cConvChar]) { - if (str) { - buf.push(str); + return args[0].replace( + pattern, + function (match, nDecSep, cFlags, nWidth, nPrecision, cConvChar) { + // cConvChar must be one of d, f, s, x + if ( + cConvChar !== "d" && + cConvChar !== "f" && + cConvChar !== "s" && + cConvChar !== "x" + ) { + const buf = ["%"]; + for (const str of [nDecSep, cFlags, nWidth, nPrecision, cConvChar]) { + if (str) { + buf.push(str); + } + } + return buf.join(""); + } + + i++; + if (i === args.length) { + throw new Error("Not enough arguments in printf"); + } + const arg = args[i]; + + if (cConvChar === "s") { + return arg.toString(); + } + + let flags = 0; + if (cFlags) { + for (const flag of cFlags) { + switch (flag) { + case "+": + flags |= PLUS; + break; + case " ": + flags |= SPACE; + break; + case "0": + flags |= ZERO; + break; + case "#": + flags |= HASH; + break; + } } } - return buf.join(""); - } + cFlags = flags; - i++; - if (i === args.length) { - throw new Error("Not enough arguments in printf"); - } - const arg = args[i]; + if (nWidth) { + nWidth = parseInt(nWidth); + } - if (cConvChar === "s") { - return arg.toString(); - } + let intPart = Math.trunc(arg); - let flags = 0; - if (cFlags) { - for (const flag of cFlags) { - switch (flag) { - case "+": - flags |= PLUS; - break; - case " ": - flags |= SPACE; - break; - case "0": - flags |= ZERO; - break; - case "#": - flags |= HASH; - break; + if (cConvChar === "x") { + let hex = Math.abs(intPart).toString(16).toUpperCase(); + if (nWidth !== undefined) { + hex = hex.padStart(nWidth, cFlags & ZERO ? "0" : " "); + } + if (cFlags & HASH) { + hex = `0x${hex}`; + } + return hex; + } + + if (nPrecision) { + nPrecision = parseInt(nPrecision.substring(1)); + } + + nDecSep = nDecSep ? nDecSep.substring(1) : "0"; + const separators = { + 0: [",", "."], + 1: ["", "."], + 2: [".", ","], + 3: ["", ","], + 4: ["'", "."], + }; + const [thousandSep, decimalSep] = separators[nDecSep]; + + let decPart = ""; + if (cConvChar === "f") { + if (nPrecision !== undefined) { + decPart = (arg - intPart).toFixed(nPrecision); + } else { + decPart = (arg - intPart).toString(); + } + if (decPart.length > 2) { + decPart = `${decimalSep}${decPart.substring(2)}`; + } else if (cFlags & HASH) { + decPart = "."; + } else { + decPart = ""; } } - } - cFlags = flags; - if (nWidth) { - nWidth = parseInt(nWidth); - } + let sign = ""; + if (intPart < 0) { + sign = "-"; + intPart = -intPart; + } else if (cFlags & PLUS) { + sign = "+"; + } else if (cFlags & SPACE) { + sign = " "; + } - let intPart = Math.trunc(arg); + if (thousandSep && intPart >= 1000) { + const buf = []; + while (true) { + buf.push((intPart % 1000).toString().padStart(3, "0")); + intPart = Math.trunc(intPart / 1000); + if (intPart < 1000) { + buf.push(intPart.toString()); + break; + } + } + intPart = buf.reverse().join(thousandSep); + } else { + intPart = intPart.toString(); + } - if (cConvChar === "x") { - let hex = Math.abs(intPart).toString(16).toUpperCase(); + let n = `${intPart}${decPart}`; if (nWidth !== undefined) { - hex = hex.padStart(nWidth, cFlags & ZERO ? "0" : " "); + n = n.padStart(nWidth - sign.length, cFlags & ZERO ? "0" : " "); } - if (cFlags & HASH) { - hex = `0x${hex}`; - } - return hex; + + return `${sign}${n}`; } - - if (nPrecision) { - nPrecision = parseInt(nPrecision.substring(1)); - } - - nDecSep = nDecSep ? nDecSep.substring(1) : "0"; - const separators = { - 0: [",", "."], - 1: ["", "."], - 2: [".", ","], - 3: ["", ","], - 4: ["'", "."], - }; - const [thousandSep, decimalSep] = separators[nDecSep]; - - let decPart = ""; - if (cConvChar === "f") { - if (nPrecision !== undefined) { - decPart = (arg - intPart).toFixed(nPrecision); - } else { - decPart = (arg - intPart).toString(); - } - if (decPart.length > 2) { - decPart = `${decimalSep}${decPart.substring(2)}`; - } else if (cFlags & HASH) { - decPart = "."; - } else { - decPart = ""; - } - } - - let sign = ""; - if (intPart < 0) { - sign = "-"; - intPart = -intPart; - } else if (cFlags & PLUS) { - sign = "+"; - } else if (cFlags & SPACE) { - sign = " "; - } - - if (thousandSep && intPart >= 1000) { - const buf = []; - while (true) { - buf.push((intPart % 1000).toString().padStart(3, "0")); - intPart = Math.trunc(intPart / 1000); - if (intPart < 1000) { - buf.push(intPart.toString()); - break; - } - } - intPart = buf.reverse().join(thousandSep); - } else { - intPart = intPart.toString(); - } - - let n = `${intPart}${decPart}`; - if (nWidth !== undefined) { - n = n.padStart(nWidth - sign.length, cFlags & ZERO ? "0" : " "); - } - - return `${sign}${n}`; - }); + ); } iconStreamFromIcon() { @@ -525,14 +521,14 @@ class Util extends PDFObject { const patterns = /(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t)/g; const actions = []; - const re = escapedFormat.replace(patterns, function ( - match, - patternElement - ) { - const { pattern, action } = handlers[patternElement]; - actions.push(action); - return pattern; - }); + const re = escapedFormat.replace( + patterns, + function (match, patternElement) { + const { pattern, action } = handlers[patternElement]; + actions.push(action); + return pattern; + } + ); this._scandCache.set(cFormat, [new RegExp(re, "g"), actions]); } diff --git a/test/unit/evaluator_spec.js b/test/unit/evaluator_spec.js index 62d43580b..cef3568f8 100644 --- a/test/unit/evaluator_spec.js +++ b/test/unit/evaluator_spec.js @@ -121,19 +121,22 @@ describe("evaluator", function () { resources.XObject = xObject; const stream = new StringStream("/Res1 DoQ"); - runOperatorListCheck(partialEvaluator, stream, resources, function ( - result - ) { - expect(result.fnArray.length).toEqual(3); - expect(result.fnArray[0]).toEqual(OPS.dependency); - expect(result.fnArray[1]).toEqual(OPS.paintImageXObject); - expect(result.fnArray[2]).toEqual(OPS.restore); - expect(result.argsArray.length).toEqual(3); - expect(result.argsArray[0]).toEqual(["img_p0_1"]); - expect(result.argsArray[1]).toEqual(["img_p0_1", 1, 1]); - expect(result.argsArray[2]).toEqual(null); - done(); - }); + runOperatorListCheck( + partialEvaluator, + stream, + resources, + function (result) { + expect(result.fnArray.length).toEqual(3); + expect(result.fnArray[0]).toEqual(OPS.dependency); + expect(result.fnArray[1]).toEqual(OPS.paintImageXObject); + expect(result.fnArray[2]).toEqual(OPS.restore); + expect(result.argsArray.length).toEqual(3); + expect(result.argsArray[0]).toEqual(["img_p0_1"]); + expect(result.argsArray[1]).toEqual(["img_p0_1", 1, 1]); + expect(result.argsArray[2]).toEqual(null); + done(); + } + ); }); it("should handle three glued operations", function (done) { @@ -157,16 +160,19 @@ describe("evaluator", function () { const resources = new ResourcesMock(); resources.Res1 = {}; const stream = new StringStream("B*Bf*"); - runOperatorListCheck(partialEvaluator, stream, resources, function ( - result - ) { - expect(!!result.fnArray && !!result.argsArray).toEqual(true); - expect(result.fnArray.length).toEqual(3); - expect(result.fnArray[0]).toEqual(OPS.eoFillStroke); - expect(result.fnArray[1]).toEqual(OPS.fillStroke); - expect(result.fnArray[2]).toEqual(OPS.eoFill); - done(); - }); + runOperatorListCheck( + partialEvaluator, + stream, + resources, + function (result) { + expect(!!result.fnArray && !!result.argsArray).toEqual(true); + expect(result.fnArray.length).toEqual(3); + expect(result.fnArray[0]).toEqual(OPS.eoFillStroke); + expect(result.fnArray[1]).toEqual(OPS.fillStroke); + expect(result.fnArray[2]).toEqual(OPS.eoFill); + done(); + } + ); }); it("should handle glued operations and operands", function (done) { @@ -253,24 +259,27 @@ describe("evaluator", function () { resources.ExtGState = extGState; const stream = new StringStream("/F2 /GS2 gs 5.711 Tf"); - runOperatorListCheck(partialEvaluator, stream, resources, function ( - result - ) { - expect(result.fnArray.length).toEqual(3); - expect(result.fnArray[0]).toEqual(OPS.setGState); - expect(result.fnArray[1]).toEqual(OPS.dependency); - expect(result.fnArray[2]).toEqual(OPS.setFont); - expect(result.argsArray.length).toEqual(3); - expect(result.argsArray[0]).toEqual([ - [ - ["LW", 2], - ["CA", 0.5], - ], - ]); - expect(result.argsArray[1]).toEqual(["g_font_error"]); - expect(result.argsArray[2]).toEqual(["g_font_error", 5.711]); - done(); - }); + runOperatorListCheck( + partialEvaluator, + stream, + resources, + function (result) { + expect(result.fnArray.length).toEqual(3); + expect(result.fnArray[0]).toEqual(OPS.setGState); + expect(result.fnArray[1]).toEqual(OPS.dependency); + expect(result.fnArray[2]).toEqual(OPS.setFont); + expect(result.argsArray.length).toEqual(3); + expect(result.argsArray[0]).toEqual([ + [ + ["LW", 2], + ["CA", 0.5], + ], + ]); + expect(result.argsArray[1]).toEqual(["g_font_error"]); + expect(result.argsArray[2]).toEqual(["g_font_error", 5.711]); + done(); + } + ); }); it("should skip if too few arguments", function (done) { const stream = new StringStream("5 d0"); @@ -369,13 +378,16 @@ describe("evaluator", function () { resources.set("XObject", xobjs); const stream = new StringStream("/Res1 Do"); - runOperatorListCheck(partialEvaluator, stream, resources, function ( - result - ) { - expect(result.argsArray).toEqual([]); - expect(result.fnArray).toEqual([]); - done(); - }); + runOperatorListCheck( + partialEvaluator, + stream, + resources, + function (result) { + expect(result.argsArray).toEqual([]); + expect(result.fnArray).toEqual([]); + done(); + } + ); }); }); diff --git a/test/unit/pdf_find_controller_spec.js b/test/unit/pdf_find_controller_spec.js index aef775e97..f4ae3a35b 100644 --- a/test/unit/pdf_find_controller_spec.js +++ b/test/unit/pdf_find_controller_spec.js @@ -98,29 +98,30 @@ describe("pdf_find_controller", function () { return a + b; }); - eventBus.on("updatefindmatchescount", function onUpdateFindMatchesCount( - evt - ) { - if (pdfFindController.pageMatches.length !== totalPages) { - return; - } - eventBus.off("updatefindmatchescount", onUpdateFindMatchesCount); + eventBus.on( + "updatefindmatchescount", + function onUpdateFindMatchesCount(evt) { + if (pdfFindController.pageMatches.length !== totalPages) { + return; + } + eventBus.off("updatefindmatchescount", onUpdateFindMatchesCount); - expect(evt.matchesCount.total).toBe(totalMatches); - for (let i = 0; i < totalPages; i++) { - expect(pdfFindController.pageMatches[i].length).toEqual( - matchesPerPage[i] + expect(evt.matchesCount.total).toBe(totalMatches); + for (let i = 0; i < totalPages; i++) { + expect(pdfFindController.pageMatches[i].length).toEqual( + matchesPerPage[i] + ); + } + expect(pdfFindController.selected.pageIdx).toEqual( + selectedMatch.pageIndex + ); + expect(pdfFindController.selected.matchIdx).toEqual( + selectedMatch.matchIndex ); - } - expect(pdfFindController.selected.pageIdx).toEqual( - selectedMatch.pageIndex - ); - expect(pdfFindController.selected.matchIdx).toEqual( - selectedMatch.matchIndex - ); - resolve(); - }); + resolve(); + } + ); }); }