From 909ff8e29fa653be95ec36430971fe4b9d3394e4 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 3 Jul 2021 11:15:13 +0200 Subject: [PATCH] Replace `instanceof Object` with `typeof` checks Using `instanceof Object` is generally problematic, since it's not guaranteed to always do the right thing for all Objects. (I stumbled upon this while working on another patch, when I noticed that the `outlineView` was broken with workers disabled.) --- .eslintrc | 4 ++++ src/core/xfa/xfa_object.js | 2 +- web/pdf_link_service.js | 2 +- web/pdf_outline_viewer.js | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index 1692ccf16..982f9e4af 100644 --- a/.eslintrc +++ b/.eslintrc @@ -166,6 +166,10 @@ "no-nested-ternary": "error", "no-new-object": "error", "no-restricted-syntax": ["error", + { + "selector": "BinaryExpression[operator='instanceof'][right.name='Object']", + "message": "Use `typeof` rather than `instanceof Object`.", + }, { "selector": "CallExpression[callee.name='assert'][arguments.length!=2]", "message": "`assert()` must always be invoked with two arguments.", diff --git a/src/core/xfa/xfa_object.js b/src/core/xfa/xfa_object.js index a890bcc05..d8fbaa09a 100644 --- a/src/core/xfa/xfa_object.js +++ b/src/core/xfa/xfa_object.js @@ -586,7 +586,7 @@ class XFAObject { if (Array.isArray(obj)) { return obj.map(x => XFAObject[_cloneAttribute](x)); } - if (obj instanceof Object) { + if (typeof obj === "object" && obj !== null) { return Object.assign({}, obj); } return obj; diff --git a/web/pdf_link_service.js b/web/pdf_link_service.js index 54ff04594..e73c11fe1 100644 --- a/web/pdf_link_service.js +++ b/web/pdf_link_service.js @@ -114,7 +114,7 @@ class PDFLinkService { const destRef = explicitDest[0]; let pageNumber; - if (destRef instanceof Object) { + if (typeof destRef === "object" && destRef !== null) { pageNumber = this._cachedPageNumber(destRef); if (pageNumber === null) { diff --git a/web/pdf_outline_viewer.js b/web/pdf_outline_viewer.js index 1017fe737..00cabc81d 100644 --- a/web/pdf_outline_viewer.js +++ b/web/pdf_outline_viewer.js @@ -304,7 +304,7 @@ class PDFOutlineViewer extends BaseTreeViewer { if (Array.isArray(explicitDest)) { const [destRef] = explicitDest; - if (destRef instanceof Object) { + if (typeof destRef === "object" && destRef !== null) { pageNumber = this.linkService._cachedPageNumber(destRef); if (!pageNumber) {