Skip to content

Commit

Permalink
Merge pull request #16544 from Snuffleupagus/eslint-prefer-optional-c…
Browse files Browse the repository at this point in the history
…atch-binding

Enable the `unicorn/prefer-optional-catch-binding` ESLint plugin rule
  • Loading branch information
Snuffleupagus authored Jun 15, 2023
2 parents 9af50dc + fee8507 commit 033228a
Show file tree
Hide file tree
Showing 32 changed files with 45 additions and 44 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/chromium/preserve-referer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/firefox/tools/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "");
}
}
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ function checkFile(filePath) {
try {
const stat = fs.lstatSync(filePath);
return stat.isFile();
} catch (e) {
} catch {
return false;
}
}
Expand All @@ -603,7 +603,7 @@ function checkDir(dirPath) {
try {
const stat = fs.lstatSync(dirPath);
return stat.isDirectory();
} catch (e) {
} catch {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/cff_font.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/core/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
Expand Down
2 changes: 1 addition & 1 deletion src/core/dataset_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DatasetReader {
const parser = new DatasetXMLParser({ hasAttributes: true });
try {
parser.parseFromString(data["xdp:xdp"]);
} catch (_) {}
} catch {}
this.node = parser.node;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -1077,7 +1077,7 @@ class PDFDocument {
}
try {
data[key] = stringToUTF8String(stream.getString());
} catch (_) {
} catch {
warn("XFA - Invalid utf-8 string.");
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ class PartialEvaluator {
);
operatorList.addOp(fn, tilingPatternIR);
return undefined;
} catch (ex) {
} catch {
// Handle any errors during normal TilingPattern parsing.
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/image_resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/type1_font.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/xfa_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -2187,7 +2187,7 @@ class PDFWorker {
}
try {
sendTest();
} catch (e) {
} catch {
// We need fallback to a faked worker.
this._setupFakeWorker();
}
Expand All @@ -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.");
}
}
Expand Down Expand Up @@ -2305,7 +2305,7 @@ class PDFWorker {
static get _mainThreadWorkerMessageHandler() {
try {
return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
} catch (ex) {
} catch {
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/display/content_disposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Expand Down Expand Up @@ -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);
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/display/network_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function extractFilenameFromHeader(getResponseHeader) {
if (filename.includes("%")) {
try {
filename = decodeURIComponent(filename);
} catch (ex) {}
} catch {}
}
if (isPdfFile(filename)) {
return filename;
Expand Down
2 changes: 1 addition & 1 deletion src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
if (options.tryConvertEncoding) {
try {
url = stringToUTF8String(url);
} catch (ex) {}
} catch {}
}
}

const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
if (_isValidProtocol(absoluteUrl)) {
return absoluteUrl;
}
} catch (ex) {
} catch {
/* `new URL()` will throw on incorrect data. */
}
return null;
Expand Down Expand Up @@ -605,7 +605,7 @@ function isEvalSupported() {
try {
new Function(""); // eslint-disable-line no-new, no-new-func
return true;
} catch (e) {
} catch {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe("api", function () {

// Shouldn't get here.
expect(false).toEqual(true);
} catch (reason) {
} catch {
expect(true).toEqual(true);
await destroyed;
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/crypto_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/evaluator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -389,7 +389,7 @@ describe("evaluator", function () {

// Shouldn't get here.
expect(false).toEqual(true);
} catch (_) {
} catch {
expect(true).toEqual(true);
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function isRuntimeAvailable() {
if (chrome.runtime?.getManifest()) {
return true;
}
} catch (e) {}
} catch {}
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion web/grab_to_pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_outline_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_presentation_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PDFPresentationMode {
await promise;
pdfViewer.focus(); // Fixes bug 1787456.
return true;
} catch (reason) {
} catch {
this.#removeFullscreenChangeListeners();
this.#notifyStateChange(PresentationModeState.NORMAL);
}
Expand Down
Loading

0 comments on commit 033228a

Please sign in to comment.