Merge pull request #21672 from Snuffleupagus/more-ternary

Replace simple return `if` statements with ternary operators
This commit is contained in:
Tim van der Meij 2026-07-31 19:59:46 +02:00 committed by GitHub
commit bb1a948397
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 81 additions and 152 deletions

View File

@ -73,10 +73,7 @@ function preprocess(inFilename, outFilename, defines) {
const out = []; const out = [];
let i = 0; let i = 0;
function readLine() { function readLine() {
if (i < totalLines) { return i < totalLines ? lines[i++] : null;
return lines[i++];
}
return null;
} }
const writeLine = const writeLine =
typeof outFilename === "function" typeof outFilename === "function"
@ -127,10 +124,7 @@ function preprocess(inFilename, outFilename, defines) {
function expand(line) { function expand(line) {
line = line.replaceAll(/__\w+__/g, function (variable) { line = line.replaceAll(/__\w+__/g, function (variable) {
variable = variable.substring(2, variable.length - 2); variable = variable.substring(2, variable.length - 2);
if (variable in defines) { return variable in defines ? defines[variable] : "";
return defines[variable];
}
return "";
}); });
writeLine(line); writeLine(line);
} }

View File

@ -1428,10 +1428,9 @@ class CFFFDSelect {
} }
getFDIndex(glyphIndex) { getFDIndex(glyphIndex) {
if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) { return glyphIndex < 0 || glyphIndex >= this.fdSelect.length
return -1; ? -1
} : this.fdSelect[glyphIndex];
return this.fdSelect[glyphIndex];
} }
} }

View File

@ -239,10 +239,10 @@ class ChunkedStream extends Stream {
}; };
Object.defineProperty(ChunkedStreamSubstream.prototype, "isDataLoaded", { Object.defineProperty(ChunkedStreamSubstream.prototype, "isDataLoaded", {
get() { get() {
if (this.numChunksLoaded === this.numChunks) { return (
return true; this.numChunksLoaded === this.numChunks ||
} this.getMissingChunks().length === 0
return this.getMissingChunks().length === 0; );
}, },
configurable: true, configurable: true,
}); });

View File

@ -111,10 +111,9 @@ class DecodeStream extends BaseStream {
async getImageData(length, decoderOptions) { async getImageData(length, decoderOptions) {
if (!this.canAsyncDecodeImageFromBuffer) { if (!this.canAsyncDecodeImageFromBuffer) {
if (this.isAsyncDecoder) { return this.isAsyncDecoder
return this.decodeImage(null, length, decoderOptions); ? this.decodeImage(null, length, decoderOptions)
} : this.getBytes(length, decoderOptions);
return this.getBytes(length, decoderOptions);
} }
const data = await this.stream.asyncGetBytes(); const data = await this.stream.asyncGetBytes();
return this.decodeImage(data, length, decoderOptions); return this.decodeImage(data, length, decoderOptions);

View File

@ -186,10 +186,7 @@ class Parser {
} }
if (typeof buf1 === "string") { if (typeof buf1 === "string") {
if (cipherTransform) { return cipherTransform ? cipherTransform.decryptString(buf1) : buf1;
return cipherTransform.decryptString(buf1);
}
return buf1;
} }
// simple object // simple object

View File

@ -39,10 +39,7 @@ class Stream extends BaseStream {
} }
getByte() { getByte() {
if (this.pos >= this.end) { return this.pos >= this.end ? -1 : this.bytes[this.pos++];
return -1;
}
return this.bytes[this.pos++];
} }
getBytes(length) { getBytes(length) {

View File

@ -16,19 +16,15 @@
import { stringToBytes, Util, warn } from "../shared/util.js"; import { stringToBytes, Util, warn } from "../shared/util.js";
function isAscii(str) { function isAscii(str) {
return ( // eslint-disable-next-line no-control-regex
typeof str === "string" && return typeof str === "string" && (!str || /^[\x00-\x7F]*$/.test(str));
// eslint-disable-next-line no-control-regex
(!str || /^[\x00-\x7F]*$/.test(str))
);
} }
// If the string is null or undefined then it is returned as is. // If the string is null or undefined then it is returned as is.
function stringToAsciiOrUTF16BE(str) { function stringToAsciiOrUTF16BE(str) {
if (str === null || str === undefined) { return str === null || str === undefined || isAscii(str)
return str; ? str
} : stringToUTF16String(str, /* bigEndian = */ true);
return isAscii(str) ? str : stringToUTF16String(str, /* bigEndian = */ true);
} }
function stringToUTF16HexString(str) { function stringToUTF16HexString(str) {

View File

@ -83,10 +83,9 @@ class IdentityToUnicodeMap {
} }
get(i) { get(i) {
if (this.firstChar <= i && i <= this.lastChar) { return this.firstChar <= i && i <= this.lastChar
return String.fromCharCode(i); ? String.fromCharCode(i)
} : undefined;
return undefined;
} }
charCodeOf(v) { charCodeOf(v) {

View File

@ -154,10 +154,7 @@ class FontFinder {
function selectFont(xfaFont, typeface) { function selectFont(xfaFont, typeface) {
if (xfaFont.posture === "italic") { if (xfaFont.posture === "italic") {
if (xfaFont.weight === "bold") { return xfaFont.weight === "bold" ? typeface.bolditalic : typeface.italic;
return typeface.bolditalic;
}
return typeface.italic;
} else if (xfaFont.weight === "bold") { } else if (xfaFont.weight === "bold") {
return typeface.bold; return typeface.bold;
} }

View File

@ -6073,10 +6073,9 @@ class Value extends XFAObject {
[$text]() { [$text]() {
if (this.exData) { if (this.exData) {
if (typeof this.exData[$content] === "string") { return typeof this.exData[$content] === "string"
return this.exData[$content].trim(); ? this.exData[$content].trim()
} : this.exData[$content][$text]().trim();
return this.exData[$content][$text]().trim();
} }
for (const name of Object.getOwnPropertyNames(this)) { for (const name of Object.getOwnPropertyNames(this)) {
if (name === "image") { if (name === "image") {

View File

@ -284,10 +284,9 @@ class XFAObject {
} }
[$text]() { [$text]() {
if (this[_children].length === 0) { return this[_children].length === 0
return this[$content]; ? this[$content]
} : this[_children].map(c => c[$text]()).join("");
return this[_children].map(c => c[$text]()).join("");
} }
get [_attributeNames]() { get [_attributeNames]() {
@ -329,11 +328,7 @@ class XFAObject {
} }
[$getChildren](name = null) { [$getChildren](name = null) {
if (!name) { return !name ? this[_children] : this[name];
return this[_children];
}
return this[name];
} }
[$dump]() { [$dump]() {
@ -680,11 +675,9 @@ class XFAObject {
} }
[$getChildren](name = null) { [$getChildren](name = null) {
if (!name) { return !name
return this[_children]; ? this[_children]
} : this[_children].filter(c => c[$nodeName] === name);
return this[_children].filter(c => c[$nodeName] === name);
} }
[$getChildrenByClass](name) { [$getChildrenByClass](name) {
@ -909,11 +902,9 @@ class XmlObject extends XFAObject {
} }
[$getChildren](name = null) { [$getChildren](name = null) {
if (!name) { return !name
return this[_children]; ? this[_children]
} : this[_children].filter(c => c[$nodeName] === name);
return this[_children].filter(c => c[$nodeName] === name);
} }
[$getAttributes]() { [$getAttributes]() {

View File

@ -324,10 +324,9 @@ class SimpleDOMNode {
} }
get textContent() { get textContent() {
if (!this.childNodes) { return !this.childNodes
return this.nodeValue || ""; ? this.nodeValue || ""
} : this.childNodes.map(child => child.textContent).join("");
return this.childNodes.map(child => child.textContent).join("");
} }
get children() { get children() {

View File

@ -3122,10 +3122,7 @@ class PopupElement {
} }
get isVisible() { get isVisible() {
if (this.#commentManager) { return !this.#commentManager && this.#container.hidden === false;
return false;
}
return this.#container.hidden === false;
} }
} }

View File

@ -188,10 +188,7 @@ class CanvasBBoxTracker {
} }
getOpenMarker() { getOpenMarker() {
if (this._savesStack.length === 0) { return this._savesStack.length === 0 ? null : this._savesStack.at(-1);
return null;
}
return this._savesStack.at(-1);
} }
recordCloseMarker(opIdx, onSavePopped) { recordCloseMarker(opIdx, onSavePopped) {

View File

@ -131,17 +131,15 @@ class AltText {
} }
isEmpty() { isEmpty() {
if (this.#useNewAltTextFlow) { return this.#useNewAltTextFlow
return this.#altText === null; ? this.#altText === null
} : !this.#altText && !this.#altTextDecorative;
return !this.#altText && !this.#altTextDecorative;
} }
hasData() { hasData() {
if (this.#useNewAltTextFlow) { return this.#useNewAltTextFlow
return this.#altText !== null || !!this.#guessedText; ? this.#altText !== null || !!this.#guessedText
} : this.isEmpty();
return this.isEmpty();
} }
get guessedText() { get guessedText() {

View File

@ -622,10 +622,7 @@ class InkDrawOutline extends Outline {
} }
updateProperty(name, value) { updateProperty(name, value) {
if (name === "stroke-width") { return name === "stroke-width" ? this.#updateThickness(value) : null;
return this.#updateThickness(value);
}
return null;
} }
#updateThickness(thickness) { #updateThickness(thickness) {

View File

@ -254,10 +254,9 @@ class SignatureEditor extends DrawingEditor {
/** @inheritdoc */ /** @inheritdoc */
get toolbarButtons() { get toolbarButtons() {
if (this._uiManager.signatureManager) { return this._uiManager.signatureManager
return [["editSignature", this._uiManager.signatureManager]]; ? [["editSignature", this._uiManager.signatureManager]]
} : super.toolbarButtons;
return super.toolbarButtons;
} }
addSignature(data, heightInPage, description, uuid) { addSignature(data, heightInPage, description, uuid) {

View File

@ -130,18 +130,12 @@ export class SandboxSupportBase {
} }
this.win.alert(cMsg); this.win.alert(cMsg);
}, },
confirm: cMsg => { confirm: cMsg =>
if (typeof cMsg !== "string") { typeof cMsg !== "string" ? false : this.win.confirm(cMsg),
return false; prompt: (cQuestion, cDefault) =>
} typeof cQuestion !== "string" || typeof cDefault !== "string"
return this.win.confirm(cMsg); ? null
}, : this.win.prompt(cQuestion, cDefault),
prompt: (cQuestion, cDefault) => {
if (typeof cQuestion !== "string" || typeof cDefault !== "string") {
return null;
}
return this.win.prompt(cQuestion, cDefault);
},
parseURL: cUrl => { parseURL: cUrl => {
const url = new this.win.URL(cUrl); const url = new this.win.URL(cUrl);
const props = [ const props = [

View File

@ -52,11 +52,9 @@ class AForm {
} }
AFMergeChange(event = globalThis.event) { AFMergeChange(event = globalThis.event) {
if (event.willCommit) { return event.willCommit
return event.value.toString(); ? event.value.toString()
} : this._app._eventDispatcher.mergeChange(event);
return this._app._eventDispatcher.mergeChange(event);
} }
AFParseDateEx(cString, cOrder) { AFParseDateEx(cString, cOrder) {
@ -102,10 +100,7 @@ class AForm {
} }
AFMakeArrayFromList(string) { AFMakeArrayFromList(string) {
if (typeof string === "string") { return typeof string === "string" ? string.split(/, ?/g) : string;
return string.split(/, ?/g);
}
return string;
} }
AFNumber_Format( AFNumber_Format(
@ -616,11 +611,9 @@ class AForm {
} }
AFExactMatch(rePatterns, str) { AFExactMatch(rePatterns, str) {
if (rePatterns instanceof RegExp) { return rePatterns instanceof RegExp
return str.match(rePatterns)?.[0] === str || 0; ? str.match(rePatterns)?.[0] === str || 0
} : rePatterns.findIndex(re => str.match(re)?.[0] === str) + 1;
return rePatterns.findIndex(re => str.match(re)?.[0] === str) + 1;
} }
} }

View File

@ -98,10 +98,7 @@ class Field extends PDFObject {
} }
get currentValueIndices() { get currentValueIndices() {
if (!this._isChoice) { return !this._isChoice ? 0 : this._currentValueIndices;
return 0;
}
return this._currentValueIndices;
} }
set currentValueIndices(indices) { set currentValueIndices(indices) {
@ -683,17 +680,13 @@ class CheckboxField extends RadioButtonField {
} }
isBoxChecked(nWidget) { isBoxChecked(nWidget) {
if (this._value === "Off") { return this._value === "Off" ? false : super.isBoxChecked(nWidget);
return false;
}
return super.isBoxChecked(nWidget);
} }
isDefaultChecked(nWidget) { isDefaultChecked(nWidget) {
if (this.defaultValue === "Off") { return this.defaultValue === "Off"
return this._value === "Off"; ? this._value === "Off"
} : super.isDefaultChecked(nWidget);
return super.isDefaultChecked(nWidget);
} }
checkThisBox(nWidget, bCheckIt = true) { checkThisBox(nWidget, bCheckIt = true) {

View File

@ -252,10 +252,9 @@ class Util extends PDFObject {
const patterns = const patterns =
/(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t|\\.)/g; /(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t|\\.)/g;
return cFormat.replaceAll(patterns, function (match, pattern) { return cFormat.replaceAll(patterns, function (match, pattern) {
if (pattern in handlers) { return pattern in handlers
return handlers[pattern](data); ? handlers[pattern](data)
} : pattern.charCodeAt(1);
return pattern.charCodeAt(1);
}); });
} }

View File

@ -1007,10 +1007,9 @@ class Driver {
} }
_getLastPageNumber(task) { _getLastPageNumber(task) {
if (!task.pdfDoc) { return !task.pdfDoc
return task.firstPage || 1; ? task.firstPage || 1
} : task.lastPage || task.pdfDoc.numPages;
return task.lastPage || task.pdfDoc.numPages;
} }
_nextPage(task, loadError) { _nextPage(task, loadError) {

View File

@ -38,10 +38,9 @@ const WASM_URL = isNodeJS
class DefaultFileReaderFactory { class DefaultFileReaderFactory {
static async fetch(params) { static async fetch(params) {
if (isNodeJS) { return isNodeJS
return fetchDataNode(params.path); ? fetchDataNode(params.path)
} : fetchDataDOM(params.path, /* type = */ "bytes");
return fetchDataDOM(params.path, /* type = */ "bytes");
} }
} }

View File

@ -1600,13 +1600,10 @@ class PDFViewer {
} }
get #pageWidthScaleFactor() { get #pageWidthScaleFactor() {
if ( return this._spreadMode !== SpreadMode.NONE &&
this._spreadMode !== SpreadMode.NONE &&
this._scrollMode !== ScrollMode.HORIZONTAL this._scrollMode !== ScrollMode.HORIZONTAL
) { ? 2
return 2; : 1;
}
return 1;
} }
#setScale(value, options) { #setScale(value, options) {