1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Use the ESLint no-restricted-syntax rule to ensure that assert is always called with two arguments

Having `assert` calls without a message string isn't very helpful when debugging, and it turns out that it's easy enough to make use of ESLint to enforce better `assert` call-sites.
In a couple of cases the `assert` calls were changed to "regular" throwing of errors instead, since that seemed more appropriate.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/no-restricted-syntax
This commit is contained in:
Jonas Jenwald 2020-05-05 12:40:01 +02:00
parent 491904d30a
commit e1f340a0c2
14 changed files with 73 additions and 22 deletions

View file

@ -767,7 +767,15 @@ class PDFDocument {
_getLinearizationPage(pageIndex) {
const { catalog, linearization } = this;
assert(linearization && linearization.pageFirst === pageIndex);
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
linearization && linearization.pageFirst === pageIndex,
"_getLinearizationPage - invalid pageIndex argument."
);
}
const ref = Ref.get(linearization.objectNumberFirst, 0);
return this.xref

View file

@ -578,7 +578,11 @@ class Catalog {
}
break;
default:
assert(typeof value === "boolean");
if (typeof value !== "boolean") {
throw new FormatError(
`viewerPreferences - expected a boolean value for: ${key}`
);
}
prefValue = value;
}

View file

@ -216,7 +216,7 @@ class Parser {
} else if (state === 1) {
state = ch === I ? 2 : 0;
} else {
assert(state === 2);
assert(state === 2, "findDefaultInlineStreamEnd - invalid state.");
if (ch === SPACE || ch === LF || ch === CR) {
maybeEIPos = stream.pos;
// Let's check that the next `n` bytes are ASCII... just to be sure.

View file

@ -26,7 +26,10 @@ class PDFWorkerStream {
}
getFullReader() {
assert(!this._fullRequestReader);
assert(
!this._fullRequestReader,
"PDFWorkerStream.getFullReader can only be called once."
);
this._fullRequestReader = new PDFWorkerStreamReader(this._msgHandler);
return this._fullRequestReader;
}