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

Enable the no-nested-ternary ESLint rule (PR 11488 follow-up)

This rule is already enabled in mozilla-central, and helps avoid some confusing formatting, see https://searchfox.org/mozilla-central/rev/9e45d74b956be046e5021a746b0c8912f1c27318/tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js#209-210

With the recent introduction of Prettier some of the existing nested ternary statements became even more difficult to read, since any possibly helpful indentation was removed.
This particular ESLint rule wasn't entirely straightforward to enable, and I do recognize that there's a certain amount of subjectivity in the changes being made. Generally, the changes in this patch fall into three categories:
 - Cases where a value is only clamped to a certain range (the easiest ones to update).
 - Cases where the values involved are "simple", such as Numbers and Strings, which are re-factored to initialize the variable with the *default* value and only update it when necessary by using `if`/`else if` statements.
 - Cases with more complex and/or larger values, such as TypedArrays, which are re-factored to let the variable be (implicitly) undefined and where all values are then set through `if`/`else if`/`else` statements.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/no-nested-ternary
This commit is contained in:
Jonas Jenwald 2020-01-12 19:47:13 +01:00
parent 78917bab91
commit c591826f3b
15 changed files with 256 additions and 101 deletions

View file

@ -45,7 +45,12 @@ var PDFImage = (function PDFImageClosure() {
function decodeAndClamp(value, addend, coefficient, max) {
value = addend + value * coefficient;
// Clamp the value to the range
return value < 0 ? 0 : value > max ? max : value;
if (value < 0) {
value = 0;
} else if (value > max) {
value = max;
}
return value;
}
/**
@ -60,12 +65,14 @@ var PDFImage = (function PDFImageClosure() {
*/
function resizeImageMask(src, bpc, w1, h1, w2, h2) {
var length = w2 * h2;
var dest =
bpc <= 8
? new Uint8Array(length)
: bpc <= 16
? new Uint16Array(length)
: new Uint32Array(length);
let dest;
if (bpc <= 8) {
dest = new Uint8Array(length);
} else if (bpc <= 16) {
dest = new Uint16Array(length);
} else {
dest = new Uint32Array(length);
}
var xRatio = w1 / w2;
var yRatio = h1 / h2;
var i,
@ -421,12 +428,14 @@ var PDFImage = (function PDFImageClosure() {
var length = width * height * numComps;
var bufferPos = 0;
var output =
bpc <= 8
? new Uint8Array(length)
: bpc <= 16
? new Uint16Array(length)
: new Uint32Array(length);
let output;
if (bpc <= 8) {
output = new Uint8Array(length);
} else if (bpc <= 16) {
output = new Uint16Array(length);
} else {
output = new Uint32Array(length);
}
var rowComps = width * numComps;
var max = (1 << bpc) - 1;
@ -481,8 +490,13 @@ var PDFImage = (function PDFImageClosure() {
}
var remainingBits = bits - bpc;
var value = buf >> remainingBits;
output[i] = value < 0 ? 0 : value > max ? max : value;
let value = buf >> remainingBits;
if (value < 0) {
value = 0;
} else if (value > max) {
value = max;
}
output[i] = value;
buf = buf & ((1 << remainingBits) - 1);
bits = remainingBits;
}