From c4bd1b7e4f02b60523f54be89b786111e7a890b9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 14 Nov 2024 12:29:52 +0100 Subject: [PATCH] Enable the ESLint `no-console` rule in parts of the code-base The purpose of these changes is to make it more difficult to accidentally include logging statements, used during development and debugging, when submitting patches for review. For (almost) all code residing in the `src/` folder we should use our existing helper functions to ensure that all logging can be controlled via the `verbosity` API-option. For the `test/unit/` respectively `test/integration/` folders we should not need any "normal" logging, but it should be OK to print the *occasional* warning/error message. Please find additional details about the ESLint rule at https://eslint.org/docs/latest/rules/no-console --- eslint.config.mjs | 8 ++++++++ src/display/display_utils.js | 1 + src/display/editor/tools.js | 2 +- src/pdf.sandbox.js | 1 + src/shared/util.js | 2 ++ test/integration/test_utils.mjs | 6 ++++-- 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index e0c2b8b014e31..16bba1b99fbc5 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -313,6 +313,12 @@ export default [ "template-curly-spacing": ["error", "never"], }, }, + { + files: jsFiles("src"), + rules: { + "no-console": "error", + }, + }, /* ======================================================================== *\ Test-specific rules @@ -354,11 +360,13 @@ export default [ files: jsFiles("test/unit"), rules: { "import/no-unresolved": ["error", { ignore: ["pdfjs/"] }], + "no-console": ["error", { allow: ["warn", "error"] }], }, }, { files: jsFiles("test/integration"), rules: { + "no-console": ["error", { allow: ["warn", "error"] }], "no-restricted-syntax": [ "error", { diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 218a0f33bdd03..16fc7093e8238 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -413,6 +413,7 @@ function noContextMenu(e) { // Deprecated API function -- display regardless of the `verbosity` setting. function deprecated(details) { + // eslint-disable-next-line no-console console.log("Deprecated API usage: " + details); } diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index 9d4fd87476720..40b96897babba 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -166,7 +166,7 @@ class ImageManager { } data.refCounter = 1; } catch (e) { - console.error(e); + warn(e); data = null; } this.#cache.set(key, data); diff --git a/src/pdf.sandbox.js b/src/pdf.sandbox.js index adced961f5426..79cb05ec1afff 100644 --- a/src/pdf.sandbox.js +++ b/src/pdf.sandbox.js @@ -84,6 +84,7 @@ class Sandbox { [buf, this._alertOnError] ); } catch (error) { + // eslint-disable-next-line no-console console.error(error); } finally { if (buf) { diff --git a/src/shared/util.js b/src/shared/util.js index 60ce504889c5b..c4752522fcde9 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -363,6 +363,7 @@ function getVerbosityLevel() { // end users. function info(msg) { if (verbosity >= VerbosityLevel.INFOS) { + // eslint-disable-next-line no-console console.log(`Info: ${msg}`); } } @@ -370,6 +371,7 @@ function info(msg) { // Non-fatal warnings. function warn(msg) { if (verbosity >= VerbosityLevel.WARNINGS) { + // eslint-disable-next-line no-console console.log(`Warning: ${msg}`); } } diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index 7e6ec548b0f22..84c74ed43541f 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -309,9 +309,11 @@ async function waitForEvent({ const success = await awaitPromise(handle); if (success === null) { - console.log(`waitForEvent: ${eventName} didn't trigger within the timeout`); + console.warn( + `waitForEvent: ${eventName} didn't trigger within the timeout` + ); } else if (!success) { - console.log(`waitForEvent: ${eventName} triggered, but validation failed`); + console.warn(`waitForEvent: ${eventName} triggered, but validation failed`); } }