Skip to content

Add more logical assignment in the src/ folder #19806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3488,8 +3488,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
// always make the field value an array with zero, one or multiple items.
if (typeof this.data.fieldValue === "string") {
this.data.fieldValue = [this.data.fieldValue];
} else if (!this.data.fieldValue) {
this.data.fieldValue = [];
} else {
this.data.fieldValue ||= [];
}
} else {
// The specs say that we should have an indices array only with
Expand Down
12 changes: 3 additions & 9 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,7 @@ class Catalog {
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
continue;
}
if (!prefs) {
prefs = Object.create(null);
}
prefs ??= Object.create(null);
prefs[key] = prefValue;
}
return shadow(this, "viewerPreferences", prefs);
Expand Down Expand Up @@ -1042,9 +1040,7 @@ class Catalog {
const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
for (const [key, value] of nameTree.getAll()) {
const fs = new FileSpec(value, this.xref);
if (!attachments) {
attachments = Object.create(null);
}
attachments ??= Object.create(null);
attachments[stringToPDFString(key)] = fs.serializable;
}
}
Expand All @@ -1058,9 +1054,7 @@ class Catalog {
if (obj instanceof Dict && obj.has("XFAImages")) {
const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref);
for (const [key, value] of nameTree.getAll()) {
if (!xfaImages) {
xfaImages = new Dict(this.xref);
}
xfaImages ??= new Dict(this.xref);
xfaImages.set(stringToPDFString(key), value);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,9 +1532,7 @@ class PDFDocument {
warn(`Bad value, for custom key "${key}", in Info: ${value}.`);
continue;
}
if (!docInfo.Custom) {
docInfo.Custom = Object.create(null);
}
docInfo.Custom ??= Object.create(null);
docInfo.Custom[key] = customValue;
continue;
}
Expand Down
9 changes: 2 additions & 7 deletions src/core/xfa/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ class XFAParser extends XMLParserBase {
}
} else if (name.startsWith("xmlns:")) {
const prefix = name.substring("xmlns:".length);
if (!prefixes) {
prefixes = [];
}
prefixes ??= [];
prefixes.push({ prefix, value });
} else {
const i = name.indexOf(":");
Expand All @@ -102,10 +100,7 @@ class XFAParser extends XMLParserBase {
} else {
// Attributes can have their own namespace.
// For example in data, we can have <foo xfa:dataNode="dataGroup"/>
let nsAttrs = attributeObj[$nsAttributes];
if (!nsAttrs) {
nsAttrs = attributeObj[$nsAttributes] = Object.create(null);
}
const nsAttrs = (attributeObj[$nsAttributes] ??= Object.create(null));
const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
const attrs = (nsAttrs[ns] ||= Object.create(null));
attrs[attrName] = value;
Expand Down
48 changes: 16 additions & 32 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2462,9 +2462,7 @@ class ExclGroup extends XFAObject {

setAccess(this, attributes.class);

if (!this[$extra]) {
this[$extra] = Object.create(null);
}
this[$extra] ||= Object.create(null);

Object.assign(this[$extra], {
children,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -3048,9 +3044,7 @@ class Field extends XFAObject {
caption.attributes.class[0] = "xfaCaptionForCheckButton";
}

if (!ui.attributes.class) {
ui.attributes.class = [];
}
ui.attributes.class ||= [];

ui.children.splice(0, 0, caption);

Expand Down Expand Up @@ -4067,11 +4061,9 @@ class PageArea extends XFAObject {
}

[$getNextPage]() {
if (!this[$extra]) {
this[$extra] = {
numberOfUse: 0,
};
}
this[$extra] ||= {
numberOfUse: 0,
};

const parent = this[$getParent]();
if (parent.relation === "orderedOccurrence") {
Expand All @@ -4090,11 +4082,9 @@ class PageArea extends XFAObject {

[$toHTML]() {
// TODO: incomplete.
if (!this[$extra]) {
this[$extra] = {
numberOfUse: 1,
};
}
this[$extra] ||= {
numberOfUse: 1,
};

const children = [];
this[$extra].children = children;
Expand Down Expand Up @@ -4186,13 +4176,11 @@ class PageSet extends XFAObject {
}

[$getNextPage]() {
if (!this[$extra]) {
this[$extra] = {
numberOfUse: 1,
pageIndex: -1,
pageSetIndex: -1,
};
}
this[$extra] ||= {
numberOfUse: 1,
pageIndex: -1,
pageSetIndex: -1,
};

if (this.relation === "orderedOccurrence") {
if (this[$extra].pageIndex + 1 < this.pageArea.children.length) {
Expand Down Expand Up @@ -5067,9 +5055,7 @@ class Subform extends XFAObject {

setAccess(this, attributes.class);

if (!this[$extra]) {
this[$extra] = Object.create(null);
}
this[$extra] ||= Object.create(null);

Object.assign(this[$extra], {
children,
Expand Down Expand Up @@ -5495,9 +5481,7 @@ class Template extends XFAObject {
}
}

if (!pageArea) {
pageArea = pageAreas[0];
}
pageArea ||= pageAreas[0];

pageArea[$extra] = {
numberOfUse: 1,
Expand Down
21 changes: 9 additions & 12 deletions src/scripting_api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,15 @@ class App extends PDFObject {
}

get constants() {
if (!this._constants) {
this._constants = Object.freeze({
align: Object.freeze({
left: 0,
center: 1,
right: 2,
top: 3,
bottom: 4,
}),
});
}
return this._constants;
return (this._constants ??= Object.freeze({
align: Object.freeze({
left: 0,
center: 1,
right: 2,
top: 3,
bottom: 4,
}),
}));
}

set constants(_) {
Expand Down