Skip to content

Commit 834423b

Browse files
committed
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).
1 parent 5da507f commit 834423b

6 files changed

Lines changed: 33 additions & 65 deletions

File tree

src/core/annotation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,8 +3488,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
34883488
// always make the field value an array with zero, one or multiple items.
34893489
if (typeof this.data.fieldValue === "string") {
34903490
this.data.fieldValue = [this.data.fieldValue];
3491-
} else if (!this.data.fieldValue) {
3492-
this.data.fieldValue = [];
3491+
} else {
3492+
this.data.fieldValue ||= [];
34933493
}
34943494
} else {
34953495
// The specs say that we should have an indices array only with

src/core/catalog.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,7 @@ class Catalog {
998998
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
999999
continue;
10001000
}
1001-
if (!prefs) {
1002-
prefs = Object.create(null);
1003-
}
1001+
prefs ??= Object.create(null);
10041002
prefs[key] = prefValue;
10051003
}
10061004
return shadow(this, "viewerPreferences", prefs);
@@ -1042,9 +1040,7 @@ class Catalog {
10421040
const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
10431041
for (const [key, value] of nameTree.getAll()) {
10441042
const fs = new FileSpec(value, this.xref);
1045-
if (!attachments) {
1046-
attachments = Object.create(null);
1047-
}
1043+
attachments ??= Object.create(null);
10481044
attachments[stringToPDFString(key)] = fs.serializable;
10491045
}
10501046
}
@@ -1058,9 +1054,7 @@ class Catalog {
10581054
if (obj instanceof Dict && obj.has("XFAImages")) {
10591055
const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref);
10601056
for (const [key, value] of nameTree.getAll()) {
1061-
if (!xfaImages) {
1062-
xfaImages = new Dict(this.xref);
1063-
}
1057+
xfaImages ??= new Dict(this.xref);
10641058
xfaImages.set(stringToPDFString(key), value);
10651059
}
10661060
}

src/core/document.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,7 @@ class PDFDocument {
15321532
warn(`Bad value, for custom key "${key}", in Info: ${value}.`);
15331533
continue;
15341534
}
1535-
if (!docInfo.Custom) {
1536-
docInfo.Custom = Object.create(null);
1537-
}
1535+
docInfo.Custom ??= Object.create(null);
15381536
docInfo.Custom[key] = customValue;
15391537
continue;
15401538
}

src/core/xfa/parser.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ class XFAParser extends XMLParserBase {
9191
}
9292
} else if (name.startsWith("xmlns:")) {
9393
const prefix = name.substring("xmlns:".length);
94-
if (!prefixes) {
95-
prefixes = [];
96-
}
94+
prefixes ??= [];
9795
prefixes.push({ prefix, value });
9896
} else {
9997
const i = name.indexOf(":");
@@ -102,10 +100,7 @@ class XFAParser extends XMLParserBase {
102100
} else {
103101
// Attributes can have their own namespace.
104102
// For example in data, we can have <foo xfa:dataNode="dataGroup"/>
105-
let nsAttrs = attributeObj[$nsAttributes];
106-
if (!nsAttrs) {
107-
nsAttrs = attributeObj[$nsAttributes] = Object.create(null);
108-
}
103+
const nsAttrs = (attributeObj[$nsAttributes] ??= Object.create(null));
109104
const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
110105
const attrs = (nsAttrs[ns] ||= Object.create(null));
111106
attrs[attrName] = value;

src/core/xfa/template.js

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,9 +2462,7 @@ class ExclGroup extends XFAObject {
24622462

24632463
setAccess(this, attributes.class);
24642464

2465-
if (!this[$extra]) {
2466-
this[$extra] = Object.create(null);
2467-
}
2465+
this[$extra] ||= Object.create(null);
24682466

24692467
Object.assign(this[$extra], {
24702468
children,
@@ -2953,9 +2951,7 @@ class Field extends XFAObject {
29532951
}
29542952
}
29552953

2956-
if (!ui.attributes.style) {
2957-
ui.attributes.style = Object.create(null);
2958-
}
2954+
ui.attributes.style ||= Object.create(null);
29592955

29602956
let aElement = null;
29612957

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

3051-
if (!ui.attributes.class) {
3052-
ui.attributes.class = [];
3053-
}
3047+
ui.attributes.class ||= [];
30543048

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

@@ -4067,11 +4061,9 @@ class PageArea extends XFAObject {
40674061
}
40684062

40694063
[$getNextPage]() {
4070-
if (!this[$extra]) {
4071-
this[$extra] = {
4072-
numberOfUse: 0,
4073-
};
4074-
}
4064+
this[$extra] ||= {
4065+
numberOfUse: 0,
4066+
};
40754067

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

40914083
[$toHTML]() {
40924084
// TODO: incomplete.
4093-
if (!this[$extra]) {
4094-
this[$extra] = {
4095-
numberOfUse: 1,
4096-
};
4097-
}
4085+
this[$extra] ||= {
4086+
numberOfUse: 1,
4087+
};
40984088

40994089
const children = [];
41004090
this[$extra].children = children;
@@ -4186,13 +4176,11 @@ class PageSet extends XFAObject {
41864176
}
41874177

41884178
[$getNextPage]() {
4189-
if (!this[$extra]) {
4190-
this[$extra] = {
4191-
numberOfUse: 1,
4192-
pageIndex: -1,
4193-
pageSetIndex: -1,
4194-
};
4195-
}
4179+
this[$extra] ||= {
4180+
numberOfUse: 1,
4181+
pageIndex: -1,
4182+
pageSetIndex: -1,
4183+
};
41964184

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

50685056
setAccess(this, attributes.class);
50695057

5070-
if (!this[$extra]) {
5071-
this[$extra] = Object.create(null);
5072-
}
5058+
this[$extra] ||= Object.create(null);
50735059

50745060
Object.assign(this[$extra], {
50755061
children,
@@ -5495,9 +5481,7 @@ class Template extends XFAObject {
54955481
}
54965482
}
54975483

5498-
if (!pageArea) {
5499-
pageArea = pageAreas[0];
5500-
}
5484+
pageArea ||= pageAreas[0];
55015485

55025486
pageArea[$extra] = {
55035487
numberOfUse: 1,

src/scripting_api/app.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,15 @@ class App extends PDFObject {
202202
}
203203

204204
get constants() {
205-
if (!this._constants) {
206-
this._constants = Object.freeze({
207-
align: Object.freeze({
208-
left: 0,
209-
center: 1,
210-
right: 2,
211-
top: 3,
212-
bottom: 4,
213-
}),
214-
});
215-
}
216-
return this._constants;
205+
return (this._constants ??= Object.freeze({
206+
align: Object.freeze({
207+
left: 0,
208+
center: 1,
209+
right: 2,
210+
top: 3,
211+
bottom: 4,
212+
}),
213+
}));
217214
}
218215

219216
set constants(_) {

0 commit comments

Comments
 (0)