Enable the unicorn/prefer-optional-catch-binding
ESLint plugin rule
According to MDN this format is available in all browsers/environments that we currently support, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#browser_compatibility Please also see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-optional-catch-binding.md
This commit is contained in:
parent
9af50dc358
commit
fee850737b
@ -72,6 +72,7 @@
|
||||
"unicorn/prefer-logical-operator-over-ternary": "error",
|
||||
"unicorn/prefer-modern-dom-apis": "error",
|
||||
"unicorn/prefer-negative-index": "error",
|
||||
"unicorn/prefer-optional-catch-binding": "error",
|
||||
"unicorn/prefer-regexp-test": "error",
|
||||
"unicorn/prefer-string-replace-all": "error",
|
||||
"unicorn/prefer-string-starts-ends-with": "error",
|
||||
|
@ -174,7 +174,7 @@ const PDFViewerApplication = {
|
||||
let title = pdfjsLib.getFilenameFromUrl(url) || url;
|
||||
try {
|
||||
title = decodeURIComponent(title);
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// decodeURIComponent may throw URIError,
|
||||
// fall back to using the unprocessed url in that case
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ var extraInfoSpecWithHeaders; // = ['requestHeaders', 'extraHeaders']
|
||||
}
|
||||
try {
|
||||
registerListener(["requestHeaders", "extraHeaders"]);
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// "extraHeaders" is not supported in Chrome 71 and earlier.
|
||||
registerListener(["requestHeaders"]);
|
||||
}
|
||||
|
@ -66,7 +66,7 @@
|
||||
if (element.dataset.l10nArgs) {
|
||||
try {
|
||||
args = JSON.parse(element.dataset.l10nArgs);
|
||||
} catch (e) {
|
||||
} catch {
|
||||
console.warn("[l10n] could not parse arguments for #" + key + "");
|
||||
}
|
||||
}
|
||||
|
@ -594,7 +594,7 @@ function checkFile(filePath) {
|
||||
try {
|
||||
const stat = fs.lstatSync(filePath);
|
||||
return stat.isFile();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -603,7 +603,7 @@ function checkDir(dirPath) {
|
||||
try {
|
||||
const stat = fs.lstatSync(dirPath);
|
||||
return stat.isDirectory();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ class CFFFont {
|
||||
this.seacs = this.cff.seacs;
|
||||
try {
|
||||
this.data = compiler.compile();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
warn("Failed to compile font " + properties.loadedName);
|
||||
// There may have just been an issue with the compiler, set the data
|
||||
// anyway and hope the font loaded.
|
||||
|
@ -1734,7 +1734,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
|
||||
if (revision === 6) {
|
||||
try {
|
||||
password = utf8StringToString(password);
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
warn(
|
||||
"CipherTransformFactory: Unable to convert UTF8 encoded password."
|
||||
);
|
||||
|
@ -53,7 +53,7 @@ class DatasetReader {
|
||||
const parser = new DatasetXMLParser({ hasAttributes: true });
|
||||
try {
|
||||
parser.parseFromString(data["xdp:xdp"]);
|
||||
} catch (_) {}
|
||||
} catch {}
|
||||
this.node = parser.node;
|
||||
}
|
||||
}
|
||||
|
@ -1057,7 +1057,7 @@ class PDFDocument {
|
||||
const str = stringToUTF8String(stream.getString());
|
||||
const data = { [key]: str };
|
||||
return shadow(this, "xfaDatasets", new DatasetReader(data));
|
||||
} catch (_) {
|
||||
} catch {
|
||||
warn("XFA - Invalid utf-8 string.");
|
||||
break;
|
||||
}
|
||||
@ -1077,7 +1077,7 @@ class PDFDocument {
|
||||
}
|
||||
try {
|
||||
data[key] = stringToUTF8String(stream.getString());
|
||||
} catch (_) {
|
||||
} catch {
|
||||
warn("XFA - Invalid utf-8 string.");
|
||||
return null;
|
||||
}
|
||||
|
@ -1498,7 +1498,7 @@ class PartialEvaluator {
|
||||
);
|
||||
operatorList.addOp(fn, tilingPatternIR);
|
||||
return undefined;
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// Handle any errors during normal TilingPattern parsing.
|
||||
}
|
||||
}
|
||||
|
@ -3045,7 +3045,7 @@ class Font {
|
||||
cff.duplicateFirstGlyph();
|
||||
const compiler = new CFFCompiler(cff);
|
||||
tables["CFF "].data = compiler.compile();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
warn("Failed to compile font " + properties.loadedName);
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ class ImageResizer {
|
||||
const opacity = ctx.getImageData(0, 0, 1, 1).data[3];
|
||||
canvas.width = canvas.height = 1;
|
||||
return opacity !== 0;
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ function getHeaderBlock(stream, suggestedLength) {
|
||||
try {
|
||||
headerBytes = stream.getBytes(suggestedLength);
|
||||
headerBytesLength = headerBytes.length;
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// Ignore errors if the `suggestedLength` is huge enough that a Uint8Array
|
||||
// cannot hold the result of `getBytes`, and fallback to simply checking
|
||||
// the entire stream (fixes issue3928.pdf).
|
||||
|
@ -644,7 +644,7 @@ class XFAObject {
|
||||
for (const $symbol of Object.getOwnPropertySymbols(this)) {
|
||||
try {
|
||||
clone[$symbol] = this[$symbol];
|
||||
} catch (_) {
|
||||
} catch {
|
||||
shadow(clone, $symbol, this[$symbol]);
|
||||
}
|
||||
}
|
||||
|
@ -523,7 +523,7 @@ function getUrlProp(val) {
|
||||
try {
|
||||
// The full path is required in the 'url' field.
|
||||
return new URL(val, window.location).href;
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
if (
|
||||
typeof PDFJSDev !== "undefined" &&
|
||||
PDFJSDev.test("GENERIC") &&
|
||||
@ -2011,7 +2011,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||
if (!base.origin || base.origin === "null") {
|
||||
return false; // non-HTTP url
|
||||
}
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -2187,7 +2187,7 @@ class PDFWorker {
|
||||
}
|
||||
try {
|
||||
sendTest();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// We need fallback to a faked worker.
|
||||
this._setupFakeWorker();
|
||||
}
|
||||
@ -2204,7 +2204,7 @@ class PDFWorker {
|
||||
// The worker shall process only the first received "test" message.
|
||||
sendTest();
|
||||
return;
|
||||
} catch (e) {
|
||||
} catch {
|
||||
info("The worker has been disabled.");
|
||||
}
|
||||
}
|
||||
@ -2305,7 +2305,7 @@ class PDFWorker {
|
||||
static get _mainThreadWorkerMessageHandler() {
|
||||
try {
|
||||
return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
|
||||
const buffer = stringToBytes(value);
|
||||
value = decoder.decode(buffer);
|
||||
needsEncodingFixup = false;
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// TextDecoder constructor threw - unrecognized encoding.
|
||||
}
|
||||
}
|
||||
@ -207,7 +207,7 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
|
||||
} // else encoding is b or B - base64 (RFC 2047 section 4.1)
|
||||
try {
|
||||
text = atob(text);
|
||||
} catch (e) {}
|
||||
} catch {}
|
||||
return textdecode(charset, text);
|
||||
}
|
||||
);
|
||||
|
@ -648,7 +648,7 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
|
||||
suggestedFilename = reFilename.exec(
|
||||
decodeURIComponent(suggestedFilename)
|
||||
)[0];
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// Possible (extremely rare) errors:
|
||||
// URIError "Malformed URI", e.g. for "%AA.pdf"
|
||||
// TypeError "null has no properties", e.g. for "%2F.pdf"
|
||||
@ -702,7 +702,7 @@ function isValidFetchUrl(url, baseUrl) {
|
||||
const { protocol } = baseUrl ? new URL(url, baseUrl) : new URL(url);
|
||||
// The Fetch API only supports the http/https protocols, and not file/ftp.
|
||||
return protocol === "http:" || protocol === "https:";
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
return false; // `new URL()` will throw on incorrect data.
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ function extractFilenameFromHeader(getResponseHeader) {
|
||||
if (filename.includes("%")) {
|
||||
try {
|
||||
filename = decodeURIComponent(filename);
|
||||
} catch (ex) {}
|
||||
} catch {}
|
||||
}
|
||||
if (isPdfFile(filename)) {
|
||||
return filename;
|
||||
|
@ -144,7 +144,7 @@ class AForm {
|
||||
let date = null;
|
||||
try {
|
||||
date = this._util.scand(cFormat, cDate);
|
||||
} catch (error) {}
|
||||
} catch {}
|
||||
if (!date) {
|
||||
date = Date.parse(cDate);
|
||||
if (isNaN(date)) {
|
||||
|
@ -434,7 +434,7 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
|
||||
if (options.tryConvertEncoding) {
|
||||
try {
|
||||
url = stringToUTF8String(url);
|
||||
} catch (ex) {}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -442,7 +442,7 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
|
||||
if (_isValidProtocol(absoluteUrl)) {
|
||||
return absoluteUrl;
|
||||
}
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
/* `new URL()` will throw on incorrect data. */
|
||||
}
|
||||
return null;
|
||||
@ -605,7 +605,7 @@ function isEvalSupported() {
|
||||
try {
|
||||
new Function(""); // eslint-disable-line no-new, no-new-func
|
||||
return true;
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -471,7 +471,7 @@ describe("Interaction", () => {
|
||||
await page._client.send("Page.setDownloadBehavior", {
|
||||
behavior: "deny",
|
||||
});
|
||||
} catch (_) {}
|
||||
} catch {}
|
||||
await clearInput(page, getSelector("47R"));
|
||||
await page.evaluate(_ => {
|
||||
window.document.activeElement.blur();
|
||||
|
@ -168,7 +168,7 @@ describe("api", function () {
|
||||
|
||||
// Shouldn't get here.
|
||||
expect(false).toEqual(true);
|
||||
} catch (reason) {
|
||||
} catch {
|
||||
expect(true).toEqual(true);
|
||||
await destroyed;
|
||||
}
|
||||
|
@ -549,7 +549,7 @@ describe("CipherTransformFactory", function () {
|
||||
try {
|
||||
const factory = new CipherTransformFactory(dict, fileId, password);
|
||||
expect("createCipherTransform" in factory).toEqual(true);
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// Shouldn't get here.
|
||||
expect(false).toEqual(true);
|
||||
}
|
||||
|
@ -368,7 +368,7 @@ describe("evaluator", function () {
|
||||
|
||||
// Shouldn't get here.
|
||||
expect(false).toEqual(true);
|
||||
} catch (_) {
|
||||
} catch {
|
||||
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
||||
expect(result.fnArray.length).toEqual(0);
|
||||
}
|
||||
@ -389,7 +389,7 @@ describe("evaluator", function () {
|
||||
|
||||
// Shouldn't get here.
|
||||
expect(false).toEqual(true);
|
||||
} catch (_) {
|
||||
} catch {
|
||||
expect(true).toEqual(true);
|
||||
}
|
||||
});
|
||||
|
@ -90,7 +90,7 @@ WebServer.prototype = {
|
||||
// Windows paths cause issues in statFile and serverDirectoryIndex.
|
||||
// Converting to unix path would avoid platform checks in said functions.
|
||||
pathPart = pathPart.replaceAll("\\", "/");
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// If the URI cannot be decoded, a `URIError` is thrown. This happens for
|
||||
// malformed URIs such as `http://localhost:8888/%s%s` and should be
|
||||
// handled as a bad request.
|
||||
|
@ -815,7 +815,7 @@ const PDFViewerApplication = {
|
||||
if (!title) {
|
||||
try {
|
||||
title = decodeURIComponent(getFilenameFromUrl(url)) || url;
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// decodeURIComponent may throw URIError,
|
||||
// fall back to using the unprocessed url in that case
|
||||
title = url;
|
||||
@ -876,7 +876,7 @@ const PDFViewerApplication = {
|
||||
try {
|
||||
// Trigger saving, to prevent data loss in forms; see issue 12257.
|
||||
await this.save();
|
||||
} catch (reason) {
|
||||
} catch {
|
||||
// Ignoring errors, to ensure that document closing won't break.
|
||||
}
|
||||
}
|
||||
@ -1044,7 +1044,7 @@ const PDFViewerApplication = {
|
||||
const blob = new Blob([data], { type: "application/pdf" });
|
||||
|
||||
await this.downloadManager.download(blob, url, filename, options);
|
||||
} catch (reason) {
|
||||
} catch {
|
||||
// When the PDF document isn't ready, or the PDF file is still
|
||||
// downloading, simply download using the URL.
|
||||
await this.downloadManager.downloadUrl(url, filename, options);
|
||||
|
@ -150,7 +150,7 @@ function isRuntimeAvailable() {
|
||||
if (chrome.runtime?.getManifest()) {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch {}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ class GrabToPan {
|
||||
try {
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
event.originalTarget.tagName;
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// Mozilla-specific: element is a scrollbar (XUL element)
|
||||
return;
|
||||
}
|
||||
|
@ -445,7 +445,7 @@ class PDFLinkService {
|
||||
// e.g. "4.3" or "true", because `JSON.parse` converted its type.
|
||||
dest = dest.toString();
|
||||
}
|
||||
} catch (ex) {}
|
||||
} catch {}
|
||||
|
||||
if (
|
||||
typeof dest === "string" ||
|
||||
|
@ -341,7 +341,7 @@ class PDFOutlineViewer extends BaseTreeViewer {
|
||||
return null; // The document was closed while the data resolved.
|
||||
}
|
||||
this.linkService.cachePageRef(pageNumber, destRef);
|
||||
} catch (ex) {
|
||||
} catch {
|
||||
// Invalid page reference, ignore it and continue parsing.
|
||||
}
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ class PDFPresentationMode {
|
||||
await promise;
|
||||
pdfViewer.focus(); // Fixes bug 1787456.
|
||||
return true;
|
||||
} catch (reason) {
|
||||
} catch {
|
||||
this.#removeFullscreenChangeListeners();
|
||||
this.#notifyStateChange(PresentationModeState.NORMAL);
|
||||
}
|
||||
|
@ -487,7 +487,7 @@ class PDFScriptingManager {
|
||||
|
||||
try {
|
||||
await this._scripting.destroySandbox();
|
||||
} catch (ex) {}
|
||||
} catch {}
|
||||
|
||||
for (const [name, listener] of this._internalEvents) {
|
||||
this._eventBus._off(name, listener);
|
||||
|
Loading…
Reference in New Issue
Block a user