From 67b658e8d5202a2da20ff7ca724ae090057589fd Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 21 Feb 2022 12:44:51 +0100 Subject: [PATCH] Prefer `instanceof Cmd` rather than calling `isCmd()` with *one* argument Unless you actually need to check that something is both a `Cmd` and also of the *correct* type, using `instanceof Cmd` directly should be a tiny bit more efficient since it avoids one function call and an unnecessary `undefined` check. This patch uses ESLint to enforce this, since we obviously still want to keep the `isCmd` helper function for where it makes sense. --- .eslintrc | 4 ++++ src/core/cmap.js | 4 ++-- test/unit/primitives_spec.js | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.eslintrc b/.eslintrc index ef0fe2cd3..37f9f057b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -179,6 +179,10 @@ "selector": "CallExpression[callee.name='assert'][arguments.length!=2]", "message": "`assert()` must always be invoked with two arguments.", }, + { + "selector": "CallExpression[callee.name='isCmd'][arguments.length<2]", + "message": "Use `instanceof Cmd` rather than `isCmd()` with one argument.", + }, { "selector": "NewExpression[callee.name='Cmd']", "message": "Use `Cmd.get()` rather than `new Cmd()`.", diff --git a/src/core/cmap.js b/src/core/cmap.js index 9c0c373ff..ab2228a9e 100644 --- a/src/core/cmap.js +++ b/src/core/cmap.js @@ -20,7 +20,7 @@ import { unreachable, warn, } from "../shared/util.js"; -import { EOF, isCmd, isName } from "./primitives.js"; +import { Cmd, EOF, isCmd, isName } from "./primitives.js"; import { BaseStream } from "./base_stream.js"; import { Lexer } from "./parser.js"; import { MissingDataException } from "./core_utils.js"; @@ -920,7 +920,7 @@ const CMapFactory = (function CMapFactoryClosure() { parseCMapName(cMap, lexer); } previous = obj; - } else if (isCmd(obj)) { + } else if (obj instanceof Cmd) { switch (obj.cmd) { case "endcmap": break objLoop; diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index fbcdaa62f..6604517df 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -496,6 +496,8 @@ describe("primitives", function () { }); describe("isCmd", function () { + /* eslint-disable no-restricted-syntax */ + it("handles non-commands", function () { const nonCmd = {}; expect(isCmd(nonCmd)).toEqual(false); @@ -511,6 +513,8 @@ describe("primitives", function () { expect(isCmd(cmd, "BT")).toEqual(true); expect(isCmd(cmd, "ET")).toEqual(false); }); + + /* eslint-enable no-restricted-syntax */ }); describe("isDict", function () {