Skip to content

Commit

Permalink
Replace instanceof Object with typeof checks
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
Snuffleupagus committed Jul 3, 2021
1 parent d80651e commit 909ff8e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
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 @@ -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;
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 @@ -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) {
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 @@ -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) {
Expand Down

0 comments on commit 909ff8e

Please sign in to comment.