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.
This commit is contained in:
Jonas Jenwald 2022-02-21 12:44:51 +01:00
parent 3635a9a333
commit 67b658e8d5
3 changed files with 10 additions and 2 deletions

View File

@ -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()`.",

View File

@ -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;

View File

@ -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 () {