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

@ -136,7 +136,7 @@
"new-cap": ["error", { "newIsCap": true, "capIsNew": false, }], "new-cap": ["error", { "newIsCap": true, "capIsNew": false, }],
"no-array-constructor": "error", "no-array-constructor": "error",
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 0, "maxBOF": 1, }], "no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 0, "maxBOF": 1, }],
"no-nested-ternary": "off", "no-nested-ternary": "error",
"no-new-object": "error", "no-new-object": "error",
"no-unneeded-ternary": "error", "no-unneeded-ternary": "error",
"spaced-comment": ["error", "always", { "spaced-comment": ["error", "always", {

View File

@ -109,11 +109,12 @@ limitations under the License.
} }
// Migration code for https://github.com/mozilla/pdf.js/pull/9479. // Migration code for https://github.com/mozilla/pdf.js/pull/9479.
if (typeof items.disableTextLayer === "boolean") { if (typeof items.disableTextLayer === "boolean") {
var textLayerMode = items.disableTextLayer var textLayerMode = 1;
? 0 if (items.disableTextLayer) {
: items.enhanceTextSelection textLayerMode = 0;
? 2 } else if (items.enhanceTextSelection) {
: 1; textLayerMode = 2;
}
if (textLayerMode !== 1) { if (textLayerMode !== 1) {
// Overwrite if computed textLayerMode is not the default value (1). // Overwrite if computed textLayerMode is not the default value (1).
storageSync.set( storageSync.set(

View File

@ -113,12 +113,12 @@ Promise.all([
// Automatically update the UI when the preferences were changed elsewhere. // Automatically update the UI when the preferences were changed elsewhere.
chrome.storage.onChanged.addListener(function(changes, areaName) { chrome.storage.onChanged.addListener(function(changes, areaName) {
var prefs = var prefs = null;
areaName === storageAreaName if (areaName === storageAreaName) {
? userPrefs prefs = userPrefs;
: areaName === "managed" } else if (areaName === "managed") {
? managedPrefs prefs = managedPrefs;
: null; }
if (prefs) { if (prefs) {
renderedPrefNames.forEach(function(prefName) { renderedPrefNames.forEach(function(prefName) {
var prefChanges = changes[prefName]; var prefChanges = changes[prefName];

View File

@ -104,11 +104,14 @@ function reverseValues(arr, start, end) {
} }
} }
function createBidiText(str, isLTR, vertical) { function createBidiText(str, isLTR, vertical = false) {
return { let dir = "ltr";
str, if (vertical) {
dir: vertical ? "ttb" : isLTR ? "ltr" : "rtl", dir = "ttb";
}; } else if (!isLTR) {
dir = "rtl";
}
return { str, dir };
} }
// These are used in bidi(), which is called frequently. We re-use them on // These are used in bidi(), which is called frequently. We re-use them on

View File

@ -1331,8 +1331,16 @@ const LabCS = (function LabCSClosure() {
} }
// Adjust limits of 'as' and 'bs' // Adjust limits of 'as' and 'bs'
as = as > cs.amax ? cs.amax : as < cs.amin ? cs.amin : as; if (as > cs.amax) {
bs = bs > cs.bmax ? cs.bmax : bs < cs.bmin ? cs.bmin : bs; as = cs.amax;
} else if (as < cs.amin) {
as = cs.amin;
}
if (bs > cs.bmax) {
bs = cs.bmax;
} else if (bs < cs.bmin) {
bs = cs.bmin;
}
// Computes intermediate variables X,Y,Z as per spec // Computes intermediate variables X,Y,Z as per spec
let M = (Ls + 16) / 116; let M = (Ls + 16) / 116;

View File

@ -752,11 +752,13 @@ var Font = (function FontClosure() {
this.type = type; this.type = type;
this.subtype = subtype; this.subtype = subtype;
this.fallbackName = this.isMonospace let fallbackName = "sans-serif";
? "monospace" if (this.isMonospace) {
: this.isSerifFont fallbackName = "monospace";
? "serif" } else if (this.isSerifFont) {
: "sans-serif"; fallbackName = "serif";
}
this.fallbackName = fallbackName;
this.differences = properties.differences; this.differences = properties.differences;
this.widths = properties.widths; this.widths = properties.widths;
@ -901,7 +903,11 @@ var Font = (function FontClosure() {
function safeString16(value) { function safeString16(value) {
// clamp value to the 16-bit int range // clamp value to the 16-bit int range
value = value > 0x7fff ? 0x7fff : value < -0x8000 ? -0x8000 : value; if (value > 0x7fff) {
value = 0x7fff;
} else if (value < -0x8000) {
value = -0x8000;
}
return String.fromCharCode((value >> 8) & 0xff, value & 0xff); return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
} }
@ -2075,9 +2081,19 @@ var Font = (function FontClosure() {
// reserved flags must be zero, cleaning up // reserved flags must be zero, cleaning up
glyf[j - 1] = flag & 0x3f; glyf[j - 1] = flag & 0x3f;
} }
var xyLength = let xLength = 2;
(flag & 2 ? 1 : flag & 16 ? 0 : 2) + if (flag & 2) {
(flag & 4 ? 1 : flag & 32 ? 0 : 2); xLength = 1;
} else if (flag & 16) {
xLength = 0;
}
let yLength = 2;
if (flag & 4) {
yLength = 1;
} else if (flag & 32) {
yLength = 0;
}
const xyLength = xLength + yLength;
coordinatesLength += xyLength; coordinatesLength += xyLength;
if (flag & 8) { if (flag & 8) {
var repeat = glyf[j++]; var repeat = glyf[j++];
@ -2620,14 +2636,14 @@ var Font = (function FontClosure() {
} }
// Adjusting stack not extactly, but just enough to get function id // Adjusting stack not extactly, but just enough to get function id
if (!inFDEF && !inELSE) { if (!inFDEF && !inELSE) {
var stackDelta = let stackDelta = 0;
op <= 0x8e if (op <= 0x8e) {
? TTOpsStackDeltas[op] stackDelta = TTOpsStackDeltas[op];
: op >= 0xc0 && op <= 0xdf } else if (op >= 0xc0 && op <= 0xdf) {
? -1 stackDelta = -1;
: op >= 0xe0 } else if (op >= 0xe0) {
? -2 stackDelta = -2;
: 0; }
if (op >= 0x71 && op <= 0x75) { if (op >= 0x71 && op <= 0x75) {
n = stack.pop(); n = stack.pop();
if (!isNaN(n)) { if (!isNaN(n)) {

View File

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

View File

@ -79,6 +79,7 @@ var Jbig2Image = (function Jbig2ImageClosure() {
var sign = readBits(1); var sign = readBits(1);
// prettier-ignore // prettier-ignore
/* eslint-disable no-nested-ternary */
var value = readBits(1) ? var value = readBits(1) ?
(readBits(1) ? (readBits(1) ?
(readBits(1) ? (readBits(1) ?
@ -90,7 +91,13 @@ var Jbig2Image = (function Jbig2ImageClosure() {
readBits(6) + 20) : readBits(6) + 20) :
readBits(4) + 4) : readBits(4) + 4) :
readBits(2); readBits(2);
return sign === 0 ? value : value > 0 ? -value : null; /* eslint-enable no-nested-ternary */
if (sign === 0) {
return value;
} else if (value > 0) {
return -value;
}
return null;
} }
// A.3 The IAID decoding procedure // A.3 The IAID decoding procedure
@ -1176,17 +1183,24 @@ var Jbig2Image = (function Jbig2ImageClosure() {
} }
segmentHeader.retainBits = retainBits; segmentHeader.retainBits = retainBits;
var referredToSegmentNumberSize =
segmentHeader.number <= 256 ? 1 : segmentHeader.number <= 65536 ? 2 : 4; let referredToSegmentNumberSize = 4;
if (segmentHeader.number <= 256) {
referredToSegmentNumberSize = 1;
} else if (segmentHeader.number <= 65536) {
referredToSegmentNumberSize = 2;
}
var referredTo = []; var referredTo = [];
var i, ii; var i, ii;
for (i = 0; i < referredToCount; i++) { for (i = 0; i < referredToCount; i++) {
var number = let number;
referredToSegmentNumberSize === 1 if (referredToSegmentNumberSize === 1) {
? data[position] number = data[position];
: referredToSegmentNumberSize === 2 } else if (referredToSegmentNumberSize === 2) {
? readUint16(data, position) number = readUint16(data, position);
: readUint32(data, position); } else {
number = readUint32(data, position);
}
referredTo.push(number); referredTo.push(number);
position += referredToSegmentNumberSize; position += referredToSegmentNumberSize;
} }

View File

@ -557,8 +557,14 @@ var JpegImage = (function JpegImageClosure() {
// check for all-zero AC coefficients // check for all-zero AC coefficients
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) { if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
t = (dctSqrt2 * p0 + 8192) >> 14; t = (dctSqrt2 * p0 + 8192) >> 14;
// convert to 8 bit // Convert to 8-bit.
t = t < -2040 ? 0 : t >= 2024 ? 255 : (t + 2056) >> 4; if (t < -2040) {
t = 0;
} else if (t >= 2024) {
t = 255;
} else {
t = (t + 2056) >> 4;
}
blockData[blockBufferOffset + col] = t; blockData[blockBufferOffset + col] = t;
blockData[blockBufferOffset + col + 8] = t; blockData[blockBufferOffset + col + 8] = t;
blockData[blockBufferOffset + col + 16] = t; blockData[blockBufferOffset + col + 16] = t;
@ -615,15 +621,63 @@ var JpegImage = (function JpegImageClosure() {
p3 = v3 + v4; p3 = v3 + v4;
p4 = v3 - v4; p4 = v3 - v4;
// convert to 8-bit integers // Convert to 8-bit integers.
p0 = p0 < 16 ? 0 : p0 >= 4080 ? 255 : p0 >> 4; if (p0 < 16) {
p1 = p1 < 16 ? 0 : p1 >= 4080 ? 255 : p1 >> 4; p0 = 0;
p2 = p2 < 16 ? 0 : p2 >= 4080 ? 255 : p2 >> 4; } else if (p0 >= 4080) {
p3 = p3 < 16 ? 0 : p3 >= 4080 ? 255 : p3 >> 4; p0 = 255;
p4 = p4 < 16 ? 0 : p4 >= 4080 ? 255 : p4 >> 4; } else {
p5 = p5 < 16 ? 0 : p5 >= 4080 ? 255 : p5 >> 4; p0 >>= 4;
p6 = p6 < 16 ? 0 : p6 >= 4080 ? 255 : p6 >> 4; }
p7 = p7 < 16 ? 0 : p7 >= 4080 ? 255 : p7 >> 4; if (p1 < 16) {
p1 = 0;
} else if (p1 >= 4080) {
p1 = 255;
} else {
p1 >>= 4;
}
if (p2 < 16) {
p2 = 0;
} else if (p2 >= 4080) {
p2 = 255;
} else {
p2 >>= 4;
}
if (p3 < 16) {
p3 = 0;
} else if (p3 >= 4080) {
p3 = 255;
} else {
p3 >>= 4;
}
if (p4 < 16) {
p4 = 0;
} else if (p4 >= 4080) {
p4 = 255;
} else {
p4 >>= 4;
}
if (p5 < 16) {
p5 = 0;
} else if (p5 >= 4080) {
p5 = 255;
} else {
p5 >>= 4;
}
if (p6 < 16) {
p6 = 0;
} else if (p6 >= 4080) {
p6 = 255;
} else {
p6 >>= 4;
}
if (p7 < 16) {
p7 = 0;
} else if (p7 >= 4080) {
p7 = 255;
} else {
p7 >>= 4;
}
// store block data // store block data
blockData[blockBufferOffset + col] = p0; blockData[blockBufferOffset + col] = p0;

View File

@ -1762,12 +1762,15 @@ var JpxImage = (function JpxImageClosure() {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.contextLabelTable = let contextLabelTable;
subband === "HH" if (subband === "HH") {
? HHContextLabel contextLabelTable = HHContextLabel;
: subband === "HL" } else if (subband === "HL") {
? HLContextLabel contextLabelTable = HLContextLabel;
: LLAndLHContextsLabel; } else {
contextLabelTable = LLAndLHContextsLabel;
}
this.contextLabelTable = contextLabelTable;
var coefficientCount = width * height; var coefficientCount = width * height;
@ -1775,12 +1778,15 @@ var JpxImage = (function JpxImageClosure() {
// add border state cells for significanceState // add border state cells for significanceState
this.neighborsSignificance = new Uint8Array(coefficientCount); this.neighborsSignificance = new Uint8Array(coefficientCount);
this.coefficentsSign = new Uint8Array(coefficientCount); this.coefficentsSign = new Uint8Array(coefficientCount);
this.coefficentsMagnitude = let coefficentsMagnitude;
mb > 14 if (mb > 14) {
? new Uint32Array(coefficientCount) coefficentsMagnitude = new Uint32Array(coefficientCount);
: mb > 6 } else if (mb > 6) {
? new Uint16Array(coefficientCount) coefficentsMagnitude = new Uint16Array(coefficientCount);
: new Uint8Array(coefficientCount); } else {
coefficentsMagnitude = new Uint8Array(coefficientCount);
}
this.coefficentsMagnitude = coefficentsMagnitude;
this.processingFlags = new Uint8Array(coefficientCount); this.processingFlags = new Uint8Array(coefficientCount);
var bitsDecoded = new Uint8Array(coefficientCount); var bitsDecoded = new Uint8Array(coefficientCount);

View File

@ -510,7 +510,13 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
return; return;
} }
style.fontWeight = font.black ? "900" : font.bold ? "bold" : "normal"; let bold = "normal";
if (font.black) {
bold = "900";
} else if (font.bold) {
bold = "bold";
}
style.fontWeight = bold;
style.fontStyle = font.italic ? "italic" : "normal"; style.fontStyle = font.italic ? "italic" : "normal";
// Use a reasonable default font if the font doesn't specify a fallback. // Use a reasonable default font if the font doesn't specify a fallback.

View File

@ -1441,7 +1441,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
var name = fontObj.loadedName || "sans-serif"; var name = fontObj.loadedName || "sans-serif";
var bold = fontObj.black ? "900" : fontObj.bold ? "bold" : "normal";
let bold = "normal";
if (fontObj.black) {
bold = "900";
} else if (fontObj.bold) {
bold = "bold";
}
var italic = fontObj.italic ? "italic" : "normal"; var italic = fontObj.italic ? "italic" : "normal";
var typeface = `"${name}", ${fontObj.fallbackName}`; var typeface = `"${name}", ${fontObj.fallbackName}`;
@ -1449,12 +1455,12 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// Keeping the font at minimal size and using the fontSizeScale to change // Keeping the font at minimal size and using the fontSizeScale to change
// the current transformation matrix before the fillText/strokeText. // the current transformation matrix before the fillText/strokeText.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=726227 // See https://bugzilla.mozilla.org/show_bug.cgi?id=726227
var browserFontSize = let browserFontSize = size;
size < MIN_FONT_SIZE if (size < MIN_FONT_SIZE) {
? MIN_FONT_SIZE browserFontSize = MIN_FONT_SIZE;
: size > MAX_FONT_SIZE } else if (size > MAX_FONT_SIZE) {
? MAX_FONT_SIZE browserFontSize = MAX_FONT_SIZE;
: size; }
this.current.fontSizeScale = size / browserFontSize; this.current.fontSizeScale = size / browserFontSize;
this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`; this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`;

View File

@ -113,22 +113,43 @@ var createMeshCanvas = (function createMeshCanvasClosure() {
maxY = Math.round(y3); maxY = Math.round(y3);
var xa, car, cag, cab; var xa, car, cag, cab;
var xb, cbr, cbg, cbb; var xb, cbr, cbg, cbb;
var k;
for (var y = minY; y <= maxY; y++) { for (var y = minY; y <= maxY; y++) {
if (y < y2) { if (y < y2) {
k = y < y1 ? 0 : y1 === y2 ? 1 : (y1 - y) / (y1 - y2); let k;
if (y < y1) {
k = 0;
} else if (y1 === y2) {
k = 1;
} else {
k = (y1 - y) / (y1 - y2);
}
xa = x1 - (x1 - x2) * k; xa = x1 - (x1 - x2) * k;
car = c1r - (c1r - c2r) * k; car = c1r - (c1r - c2r) * k;
cag = c1g - (c1g - c2g) * k; cag = c1g - (c1g - c2g) * k;
cab = c1b - (c1b - c2b) * k; cab = c1b - (c1b - c2b) * k;
} else { } else {
k = y > y3 ? 1 : y2 === y3 ? 0 : (y2 - y) / (y2 - y3); let k;
if (y > y3) {
k = 1;
} else if (y2 === y3) {
k = 0;
} else {
k = (y2 - y) / (y2 - y3);
}
xa = x2 - (x2 - x3) * k; xa = x2 - (x2 - x3) * k;
car = c2r - (c2r - c3r) * k; car = c2r - (c2r - c3r) * k;
cag = c2g - (c2g - c3g) * k; cag = c2g - (c2g - c3g) * k;
cab = c2b - (c2b - c3b) * k; cab = c2b - (c2b - c3b) * k;
} }
k = y < y1 ? 0 : y > y3 ? 1 : (y1 - y) / (y1 - y3);
let k;
if (y < y1) {
k = 0;
} else if (y > y3) {
k = 1;
} else {
k = (y1 - y) / (y1 - y3);
}
xb = x1 - (x1 - x3) * k; xb = x1 - (x1 - x3) * k;
cbr = c1r - (c1r - c3r) * k; cbr = c1r - (c1r - c3r) * k;
cbg = c1g - (c1g - c3g) * k; cbg = c1g - (c1g - c3g) * k;
@ -137,8 +158,12 @@ var createMeshCanvas = (function createMeshCanvasClosure() {
var x2_ = Math.round(Math.max(xa, xb)); var x2_ = Math.round(Math.max(xa, xb));
var j = rowSize * y + x1_ * 4; var j = rowSize * y + x1_ * 4;
for (var x = x1_; x <= x2_; x++) { for (var x = x1_; x <= x2_; x++) {
k = (xa - x) / (xa - xb); let k = (xa - x) / (xa - xb);
k = k < 0 ? 0 : k > 1 ? 1 : k; if (k < 0) {
k = 0;
} else if (k > 1) {
k = 1;
}
bytes[j++] = (car - (car - cbr) * k) | 0; bytes[j++] = (car - (car - cbr) * k) | 0;
bytes[j++] = (cag - (cag - cbg) * k) | 0; bytes[j++] = (cag - (cag - cbg) * k) | 0;
bytes[j++] = (cab - (cab - cbb) * k) | 0; bytes[j++] = (cab - (cab - cbb) * k) | 0;

View File

@ -942,7 +942,12 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
? fontObj.fontMatrix ? fontObj.fontMatrix
: FONT_IDENTITY_MATRIX; : FONT_IDENTITY_MATRIX;
const bold = fontObj.black ? "900" : fontObj.bold ? "bold" : "normal"; let bold = "normal";
if (fontObj.black) {
bold = "900";
} else if (fontObj.bold) {
bold = "bold";
}
const italic = fontObj.italic ? "italic" : "normal"; const italic = fontObj.italic ? "italic" : "normal";
if (size < 0) { if (size < 0) {

View File

@ -7,9 +7,6 @@
// Plugins // Plugins
"import/no-unresolved": ["error", { "ignore": ["pdfjs-lib"]}], "import/no-unresolved": ["error", { "ignore": ["pdfjs-lib"]}],
// Stylistic Issues
"no-nested-ternary": "error",
// ECMAScript 6 // ECMAScript 6
"no-var": "error", "no-var": "error",
"prefer-const": "error", "prefer-const": "error",