Add more logical assignment in the src/ folder

This patch uses nullish coalescing assignment in cases where it's immediately obvious from surrounding code that doing so is safe, and logical OR assignment elsewhere (mostly the changes in XFA code).
This commit is contained in:
Jonas Jenwald 2025-04-12 17:13:39 +02:00
parent 5da507f279
commit 834423b51d
6 changed files with 33 additions and 65 deletions

View File

@ -3488,8 +3488,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
// always make the field value an array with zero, one or multiple items. // always make the field value an array with zero, one or multiple items.
if (typeof this.data.fieldValue === "string") { if (typeof this.data.fieldValue === "string") {
this.data.fieldValue = [this.data.fieldValue]; this.data.fieldValue = [this.data.fieldValue];
} else if (!this.data.fieldValue) { } else {
this.data.fieldValue = []; this.data.fieldValue ||= [];
} }
} else { } else {
// The specs say that we should have an indices array only with // The specs say that we should have an indices array only with

View File

@ -998,9 +998,7 @@ class Catalog {
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`); warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
continue; continue;
} }
if (!prefs) { prefs ??= Object.create(null);
prefs = Object.create(null);
}
prefs[key] = prefValue; prefs[key] = prefValue;
} }
return shadow(this, "viewerPreferences", prefs); return shadow(this, "viewerPreferences", prefs);
@ -1042,9 +1040,7 @@ class Catalog {
const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref); const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
for (const [key, value] of nameTree.getAll()) { for (const [key, value] of nameTree.getAll()) {
const fs = new FileSpec(value, this.xref); const fs = new FileSpec(value, this.xref);
if (!attachments) { attachments ??= Object.create(null);
attachments = Object.create(null);
}
attachments[stringToPDFString(key)] = fs.serializable; attachments[stringToPDFString(key)] = fs.serializable;
} }
} }
@ -1058,9 +1054,7 @@ class Catalog {
if (obj instanceof Dict && obj.has("XFAImages")) { if (obj instanceof Dict && obj.has("XFAImages")) {
const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref); const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref);
for (const [key, value] of nameTree.getAll()) { for (const [key, value] of nameTree.getAll()) {
if (!xfaImages) { xfaImages ??= new Dict(this.xref);
xfaImages = new Dict(this.xref);
}
xfaImages.set(stringToPDFString(key), value); xfaImages.set(stringToPDFString(key), value);
} }
} }

View File

@ -1532,9 +1532,7 @@ class PDFDocument {
warn(`Bad value, for custom key "${key}", in Info: ${value}.`); warn(`Bad value, for custom key "${key}", in Info: ${value}.`);
continue; continue;
} }
if (!docInfo.Custom) { docInfo.Custom ??= Object.create(null);
docInfo.Custom = Object.create(null);
}
docInfo.Custom[key] = customValue; docInfo.Custom[key] = customValue;
continue; continue;
} }

View File

@ -91,9 +91,7 @@ class XFAParser extends XMLParserBase {
} }
} else if (name.startsWith("xmlns:")) { } else if (name.startsWith("xmlns:")) {
const prefix = name.substring("xmlns:".length); const prefix = name.substring("xmlns:".length);
if (!prefixes) { prefixes ??= [];
prefixes = [];
}
prefixes.push({ prefix, value }); prefixes.push({ prefix, value });
} else { } else {
const i = name.indexOf(":"); const i = name.indexOf(":");
@ -102,10 +100,7 @@ class XFAParser extends XMLParserBase {
} else { } else {
// Attributes can have their own namespace. // Attributes can have their own namespace.
// For example in data, we can have <foo xfa:dataNode="dataGroup"/> // For example in data, we can have <foo xfa:dataNode="dataGroup"/>
let nsAttrs = attributeObj[$nsAttributes]; const nsAttrs = (attributeObj[$nsAttributes] ??= Object.create(null));
if (!nsAttrs) {
nsAttrs = attributeObj[$nsAttributes] = Object.create(null);
}
const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)]; const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
const attrs = (nsAttrs[ns] ||= Object.create(null)); const attrs = (nsAttrs[ns] ||= Object.create(null));
attrs[attrName] = value; attrs[attrName] = value;

View File

@ -2462,9 +2462,7 @@ class ExclGroup extends XFAObject {
setAccess(this, attributes.class); setAccess(this, attributes.class);
if (!this[$extra]) { this[$extra] ||= Object.create(null);
this[$extra] = Object.create(null);
}
Object.assign(this[$extra], { Object.assign(this[$extra], {
children, children,
@ -2953,9 +2951,7 @@ class Field extends XFAObject {
} }
} }
if (!ui.attributes.style) { ui.attributes.style ||= Object.create(null);
ui.attributes.style = Object.create(null);
}
let aElement = null; let aElement = null;
@ -3048,9 +3044,7 @@ class Field extends XFAObject {
caption.attributes.class[0] = "xfaCaptionForCheckButton"; caption.attributes.class[0] = "xfaCaptionForCheckButton";
} }
if (!ui.attributes.class) { ui.attributes.class ||= [];
ui.attributes.class = [];
}
ui.children.splice(0, 0, caption); ui.children.splice(0, 0, caption);
@ -4067,11 +4061,9 @@ class PageArea extends XFAObject {
} }
[$getNextPage]() { [$getNextPage]() {
if (!this[$extra]) { this[$extra] ||= {
this[$extra] = {
numberOfUse: 0, numberOfUse: 0,
}; };
}
const parent = this[$getParent](); const parent = this[$getParent]();
if (parent.relation === "orderedOccurrence") { if (parent.relation === "orderedOccurrence") {
@ -4090,11 +4082,9 @@ class PageArea extends XFAObject {
[$toHTML]() { [$toHTML]() {
// TODO: incomplete. // TODO: incomplete.
if (!this[$extra]) { this[$extra] ||= {
this[$extra] = {
numberOfUse: 1, numberOfUse: 1,
}; };
}
const children = []; const children = [];
this[$extra].children = children; this[$extra].children = children;
@ -4186,13 +4176,11 @@ class PageSet extends XFAObject {
} }
[$getNextPage]() { [$getNextPage]() {
if (!this[$extra]) { this[$extra] ||= {
this[$extra] = {
numberOfUse: 1, numberOfUse: 1,
pageIndex: -1, pageIndex: -1,
pageSetIndex: -1, pageSetIndex: -1,
}; };
}
if (this.relation === "orderedOccurrence") { if (this.relation === "orderedOccurrence") {
if (this[$extra].pageIndex + 1 < this.pageArea.children.length) { if (this[$extra].pageIndex + 1 < this.pageArea.children.length) {
@ -5067,9 +5055,7 @@ class Subform extends XFAObject {
setAccess(this, attributes.class); setAccess(this, attributes.class);
if (!this[$extra]) { this[$extra] ||= Object.create(null);
this[$extra] = Object.create(null);
}
Object.assign(this[$extra], { Object.assign(this[$extra], {
children, children,
@ -5495,9 +5481,7 @@ class Template extends XFAObject {
} }
} }
if (!pageArea) { pageArea ||= pageAreas[0];
pageArea = pageAreas[0];
}
pageArea[$extra] = { pageArea[$extra] = {
numberOfUse: 1, numberOfUse: 1,

View File

@ -202,8 +202,7 @@ class App extends PDFObject {
} }
get constants() { get constants() {
if (!this._constants) { return (this._constants ??= Object.freeze({
this._constants = Object.freeze({
align: Object.freeze({ align: Object.freeze({
left: 0, left: 0,
center: 1, center: 1,
@ -211,9 +210,7 @@ class App extends PDFObject {
top: 3, top: 3,
bottom: 4, bottom: 4,
}), }),
}); }));
}
return this._constants;
} }
set constants(_) { set constants(_) {