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:
parent
3635a9a333
commit
67b658e8d5
@ -179,6 +179,10 @@
|
|||||||
"selector": "CallExpression[callee.name='assert'][arguments.length!=2]",
|
"selector": "CallExpression[callee.name='assert'][arguments.length!=2]",
|
||||||
"message": "`assert()` must always be invoked with two arguments.",
|
"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']",
|
"selector": "NewExpression[callee.name='Cmd']",
|
||||||
"message": "Use `Cmd.get()` rather than `new Cmd()`.",
|
"message": "Use `Cmd.get()` rather than `new Cmd()`.",
|
||||||
|
@ -20,7 +20,7 @@ import {
|
|||||||
unreachable,
|
unreachable,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} 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 { BaseStream } from "./base_stream.js";
|
||||||
import { Lexer } from "./parser.js";
|
import { Lexer } from "./parser.js";
|
||||||
import { MissingDataException } from "./core_utils.js";
|
import { MissingDataException } from "./core_utils.js";
|
||||||
@ -920,7 +920,7 @@ const CMapFactory = (function CMapFactoryClosure() {
|
|||||||
parseCMapName(cMap, lexer);
|
parseCMapName(cMap, lexer);
|
||||||
}
|
}
|
||||||
previous = obj;
|
previous = obj;
|
||||||
} else if (isCmd(obj)) {
|
} else if (obj instanceof Cmd) {
|
||||||
switch (obj.cmd) {
|
switch (obj.cmd) {
|
||||||
case "endcmap":
|
case "endcmap":
|
||||||
break objLoop;
|
break objLoop;
|
||||||
|
@ -496,6 +496,8 @@ describe("primitives", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("isCmd", function () {
|
describe("isCmd", function () {
|
||||||
|
/* eslint-disable no-restricted-syntax */
|
||||||
|
|
||||||
it("handles non-commands", function () {
|
it("handles non-commands", function () {
|
||||||
const nonCmd = {};
|
const nonCmd = {};
|
||||||
expect(isCmd(nonCmd)).toEqual(false);
|
expect(isCmd(nonCmd)).toEqual(false);
|
||||||
@ -511,6 +513,8 @@ describe("primitives", function () {
|
|||||||
expect(isCmd(cmd, "BT")).toEqual(true);
|
expect(isCmd(cmd, "BT")).toEqual(true);
|
||||||
expect(isCmd(cmd, "ET")).toEqual(false);
|
expect(isCmd(cmd, "ET")).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* eslint-enable no-restricted-syntax */
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("isDict", function () {
|
describe("isDict", function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user