Skip to content

Commit 170599f

Browse files
committed
Enable the unicorn/prefer-class-fields ESLint plugin rule
This leads to slightly shorter code[1] when initializing classes, and in some cases we can even remove the constructors, which shouldn't hurt; see https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-class-fields.md It's probably possible to also change a lot of these class fields to private ones[2], however it's often difficult to tell at a glance if that's safe hence this patch only does this for the `PDFRenderingQueue`. --- [1] This reduces the size of the `gulp mozcentral` output by 999 bytes, for a mostly mechanical code change. [2] That sort of re-factoring should generally be done separately, on a class-by-class basis, to reduce the risk of regressions.
1 parent fa908e4 commit 170599f

11 files changed

Lines changed: 285 additions & 212 deletions

File tree

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export default [
148148
"unicorn/prefer-array-index-of": "error",
149149
"unicorn/prefer-array-some": "error",
150150
"unicorn/prefer-at": "error",
151+
"unicorn/prefer-class-fields": "error",
151152
"unicorn/prefer-classlist-toggle": "error",
152153
"unicorn/prefer-date-now": "error",
153154
"unicorn/prefer-dom-node-append": "error",

src/core/annotation.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,14 +1462,17 @@ class Annotation {
14621462
* Contains all data regarding an annotation's border style.
14631463
*/
14641464
class AnnotationBorderStyle {
1465-
constructor() {
1466-
this.width = 1;
1467-
this.rawWidth = 1;
1468-
this.style = AnnotationBorderStyleType.SOLID;
1469-
this.dashArray = [3];
1470-
this.horizontalCornerRadius = 0;
1471-
this.verticalCornerRadius = 0;
1472-
}
1465+
width = 1;
1466+
1467+
rawWidth = 1;
1468+
1469+
style = AnnotationBorderStyleType.SOLID;
1470+
1471+
dashArray = [3];
1472+
1473+
horizontalCornerRadius = 0;
1474+
1475+
verticalCornerRadius = 0;
14731476

14741477
/**
14751478
* Set the width.

src/core/cff_parser.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -991,24 +991,31 @@ class CFFParser {
991991

992992
// Compact Font Format
993993
class CFF {
994-
constructor() {
995-
this.header = null;
996-
this.names = [];
997-
this.topDict = null;
998-
this.strings = new CFFStrings();
999-
this.globalSubrIndex = null;
1000-
1001-
// The following could really be per font, but since we only have one font
1002-
// store them here.
1003-
this.encoding = null;
1004-
this.charset = null;
1005-
this.charStrings = null;
1006-
this.fdArray = [];
1007-
this.fdSelect = null;
1008-
1009-
this.isCIDFont = false;
1010-
this.charStringCount = 0;
1011-
}
994+
header = null;
995+
996+
names = [];
997+
998+
topDict = null;
999+
1000+
strings = new CFFStrings();
1001+
1002+
globalSubrIndex = null;
1003+
1004+
// The following could really be per font, but since we only have one font
1005+
// store them here.
1006+
encoding = null;
1007+
1008+
charset = null;
1009+
1010+
charStrings = null;
1011+
1012+
fdArray = [];
1013+
1014+
fdSelect = null;
1015+
1016+
isCIDFont = false;
1017+
1018+
charStringCount = 0;
10121019

10131020
duplicateFirstGlyph() {
10141021
// Browsers will not display a glyph at position 0. Typically glyph 0 is

src/core/crypto.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import { calculateSHA256 } from "./calculate_sha256.js";
3232
import { DecryptStream } from "./decrypt_stream.js";
3333

3434
class ARCFourCipher {
35+
a = 0;
36+
37+
b = 0;
38+
3539
constructor(key) {
36-
this.a = 0;
37-
this.b = 0;
3840
const s = new Uint8Array(256);
3941
const keyLength = key.length;
4042

src/core/editor/pdf_editor.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,52 @@ class XRefWrapper {
7474
}
7575

7676
class PDFEditor {
77+
hasSingleFile = false;
78+
79+
currentDocument = null;
80+
81+
oldPages = [];
82+
83+
newPages = [];
84+
85+
xref = [null];
86+
87+
xrefWrapper = new XRefWrapper(this.xref);
88+
89+
newRefCount = 1;
90+
91+
namesDict = null;
92+
93+
version = "1.7";
94+
95+
pageLabels = null;
96+
97+
namedDestinations = new Map();
98+
99+
parentTree = new Map();
100+
101+
structTreeKids = [];
102+
103+
idTree = new Map();
104+
105+
classMap = new Dict();
106+
107+
roleMap = new Dict();
108+
109+
namespaces = new Map();
110+
111+
structTreeAF = [];
112+
113+
structTreePronunciationLexicon = [];
114+
77115
constructor({ useObjectStreams = true, title = "", author = "" } = {}) {
78-
this.hasSingleFile = false;
79-
this.currentDocument = null;
80-
this.oldPages = [];
81-
this.newPages = [];
82-
this.xref = [null];
83-
this.xrefWrapper = new XRefWrapper(this.xref);
84-
this.newRefCount = 1;
85116
[this.rootRef, this.rootDict] = this.newDict;
86117
[this.infoRef, this.infoDict] = this.newDict;
87118
[this.pagesRef, this.pagesDict] = this.newDict;
88-
this.namesDict = null;
89119
this.useObjectStreams = useObjectStreams;
90120
this.objStreamRefs = useObjectStreams ? new Set() : null;
91-
this.version = "1.7";
92121
this.title = title;
93122
this.author = author;
94-
this.pageLabels = null;
95-
this.namedDestinations = new Map();
96-
this.parentTree = new Map();
97-
this.structTreeKids = [];
98-
this.idTree = new Map();
99-
this.classMap = new Dict();
100-
this.roleMap = new Dict();
101-
this.namespaces = new Map();
102-
this.structTreeAF = [];
103-
this.structTreePronunciationLexicon = [];
104123
}
105124

106125
/**

src/core/type1_parser.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ const COMMAND_MAP = {
7979
* the charStrings.
8080
*/
8181
class Type1CharString {
82-
constructor() {
83-
this.width = 0;
84-
this.lsb = 0;
85-
this.flexing = false;
86-
this.output = [];
87-
this.stack = [];
88-
}
82+
width = 0;
83+
84+
lsb = 0;
85+
86+
flexing = false;
87+
88+
output = [];
89+
90+
stack = [];
8991

9092
convert(encoded, subrs, seacAnalysisEnabled) {
9193
const count = encoded.length;

src/display/annotation_storage.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ class AnnotationStorage {
3535

3636
#storage = new Map();
3737

38-
constructor() {
39-
// Callbacks to signal when the modification state is set or reset.
40-
// This is used by the viewer to only bind on `beforeunload` if forms
41-
// are actually edited to prevent doing so unconditionally since that
42-
// can have undesirable effects.
43-
this.onSetModified = null;
44-
this.onResetModified = null;
45-
this.onAnnotationEditor = null;
38+
// Callbacks to signal when the modification state is set or reset.
39+
// This is used by the viewer to only bind on `beforeunload` if forms
40+
// are actually edited to prevent doing so unconditionally since that
41+
// can have undesirable effects.
42+
onSetModified = null;
43+
44+
onResetModified = null;
4645

46+
onAnnotationEditor = null;
47+
48+
constructor() {
4749
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
4850
// For testing purposes.
4951
Object.defineProperty(this, "_setValues", {

0 commit comments

Comments
 (0)