Skip to content

Commit 088a55f

Browse files
committed
Enable the no-var rule in the src/core/xref.js file
1 parent bc828cd commit 088a55f

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

src/core/xref.js

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
/* eslint-disable no-var */
1615

1716
import {
1817
assert,
@@ -71,7 +70,7 @@ class XRef {
7170
}
7271

7372
parse(recoveryMode = false) {
74-
var trailerDict;
73+
let trailerDict;
7574
if (!recoveryMode) {
7675
trailerDict = this.readXRef();
7776
} else {
@@ -91,8 +90,8 @@ class XRef {
9190
warn(`XRef.parse - Invalid "Encrypt" reference: "${ex}".`);
9291
}
9392
if (isDict(encrypt)) {
94-
var ids = trailerDict.get("ID");
95-
var fileId = ids && ids.length ? ids[0] : "";
93+
const ids = trailerDict.get("ID");
94+
const fileId = ids && ids.length ? ids[0] : "";
9695
// The 'Encrypt' dictionary itself should not be encrypted, and by
9796
// setting `suppressEncryption` we can prevent an infinite loop inside
9897
// of `XRef_fetchUncompressed` if the dictionary contains indirect
@@ -137,7 +136,7 @@ class XRef {
137136
};
138137
}
139138

140-
var obj = this.readXRefTable(parser);
139+
const obj = this.readXRefTable(parser);
141140

142141
// Sanity check
143142
if (!isCmd(obj, "trailer")) {
@@ -154,7 +153,7 @@ class XRef {
154153
// >>
155154
// The parser goes through the entire stream << ... >> and provides
156155
// a getter interface for the key-value table
157-
var dict = parser.getObj();
156+
let dict = parser.getObj();
158157

159158
// The pdflib PDF generator can generate a nested trailer dictionary
160159
if (!isDict(dict) && dict.dict) {
@@ -181,14 +180,14 @@ class XRef {
181180
// trailer
182181
// ...
183182

184-
var stream = parser.lexer.stream;
185-
var tableState = this.tableState;
183+
const stream = parser.lexer.stream;
184+
const tableState = this.tableState;
186185
stream.pos = tableState.streamPos;
187186
parser.buf1 = tableState.parserBuf1;
188187
parser.buf2 = tableState.parserBuf2;
189188

190189
// Outer loop is over subsection headers
191-
var obj;
190+
let obj;
192191

193192
while (true) {
194193
if (!("firstEntryNum" in tableState) || !("entryCount" in tableState)) {
@@ -199,24 +198,24 @@ class XRef {
199198
tableState.entryCount = parser.getObj();
200199
}
201200

202-
var first = tableState.firstEntryNum;
203-
var count = tableState.entryCount;
201+
let first = tableState.firstEntryNum;
202+
const count = tableState.entryCount;
204203
if (!Number.isInteger(first) || !Number.isInteger(count)) {
205204
throw new FormatError(
206205
"Invalid XRef table: wrong types in subsection header"
207206
);
208207
}
209208
// Inner loop is over objects themselves
210-
for (var i = tableState.entryNum; i < count; i++) {
209+
for (let i = tableState.entryNum; i < count; i++) {
211210
tableState.streamPos = stream.pos;
212211
tableState.entryNum = i;
213212
tableState.parserBuf1 = parser.buf1;
214213
tableState.parserBuf2 = parser.buf2;
215214

216-
var entry = {};
215+
const entry = {};
217216
entry.offset = parser.getObj();
218217
entry.gen = parser.getObj();
219-
var type = parser.getObj();
218+
const type = parser.getObj();
220219

221220
if (type instanceof Cmd) {
222221
switch (type.cmd) {
@@ -270,9 +269,9 @@ class XRef {
270269
if (!("streamState" in this)) {
271270
// Stores state of the stream as we process it so we can resume
272271
// from middle of stream in case of missing data error
273-
var streamParameters = stream.dict;
274-
var byteWidths = streamParameters.get("W");
275-
var range = streamParameters.get("Index");
272+
const streamParameters = stream.dict;
273+
const byteWidths = streamParameters.get("W");
274+
let range = streamParameters.get("Index");
276275
if (!range) {
277276
range = [0, streamParameters.get("Size")];
278277
}
@@ -291,19 +290,19 @@ class XRef {
291290
}
292291

293292
readXRefStream(stream) {
294-
var i, j;
295-
var streamState = this.streamState;
293+
let i, j;
294+
const streamState = this.streamState;
296295
stream.pos = streamState.streamPos;
297296

298-
var byteWidths = streamState.byteWidths;
299-
var typeFieldWidth = byteWidths[0];
300-
var offsetFieldWidth = byteWidths[1];
301-
var generationFieldWidth = byteWidths[2];
297+
const byteWidths = streamState.byteWidths;
298+
const typeFieldWidth = byteWidths[0];
299+
const offsetFieldWidth = byteWidths[1];
300+
const generationFieldWidth = byteWidths[2];
302301

303-
var entryRanges = streamState.entryRanges;
302+
const entryRanges = streamState.entryRanges;
304303
while (entryRanges.length > 0) {
305-
var first = entryRanges[0];
306-
var n = entryRanges[1];
304+
const first = entryRanges[0];
305+
const n = entryRanges[1];
307306

308307
if (!Number.isInteger(first) || !Number.isInteger(n)) {
309308
throw new FormatError(`Invalid XRef range fields: ${first}, ${n}`);
@@ -321,7 +320,7 @@ class XRef {
321320
streamState.entryNum = i;
322321
streamState.streamPos = stream.pos;
323322

324-
var type = 0,
323+
let type = 0,
325324
offset = 0,
326325
generation = 0;
327326
for (j = 0; j < typeFieldWidth; ++j) {
@@ -337,7 +336,7 @@ class XRef {
337336
for (j = 0; j < generationFieldWidth; ++j) {
338337
generation = (generation << 8) | stream.getByte();
339338
}
340-
var entry = {};
339+
const entry = {};
341340
entry.offset = offset;
342341
entry.gen = generation;
343342
switch (type) {
@@ -366,15 +365,15 @@ class XRef {
366365
indexObjects() {
367366
// Simple scan through the PDF content to find objects,
368367
// trailers and XRef streams.
369-
var TAB = 0x9,
368+
const TAB = 0x9,
370369
LF = 0xa,
371370
CR = 0xd,
372371
SPACE = 0x20;
373-
var PERCENT = 0x25,
372+
const PERCENT = 0x25,
374373
LT = 0x3c;
375374

376375
function readToken(data, offset) {
377-
var token = "",
376+
let token = "",
378377
ch = data[offset];
379378
while (ch !== LF && ch !== CR && ch !== LT) {
380379
if (++offset >= data.length) {
@@ -386,12 +385,12 @@ class XRef {
386385
return token;
387386
}
388387
function skipUntil(data, offset, what) {
389-
var length = what.length,
388+
const length = what.length,
390389
dataLength = data.length;
391-
var skipped = 0;
390+
let skipped = 0;
392391
// finding byte sequence
393392
while (offset < dataLength) {
394-
var i = 0;
393+
let i = 0;
395394
while (i < length && data[offset + i] === what[i]) {
396395
++i;
397396
}
@@ -403,30 +402,30 @@ class XRef {
403402
}
404403
return skipped;
405404
}
406-
var objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
405+
const objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
407406
const endobjRegExp = /\bendobj[\b\s]$/;
408407
const nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s<])$/;
409408
const CHECK_CONTENT_LENGTH = 25;
410409

411-
var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
410+
const trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
412411
// prettier-ignore
413-
var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114,
412+
const startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114,
414413
101, 102]);
415414
const objBytes = new Uint8Array([111, 98, 106]);
416-
var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
415+
const xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
417416

418417
// Clear out any existing entries, since they may be bogus.
419418
this.entries.length = 0;
420419

421-
var stream = this.stream;
420+
const stream = this.stream;
422421
stream.pos = 0;
423-
var buffer = stream.getBytes();
424-
var position = stream.start,
422+
const buffer = stream.getBytes(),
425423
length = buffer.length;
426-
var trailers = [],
424+
let position = stream.start;
425+
const trailers = [],
427426
xrefStms = [];
428427
while (position < length) {
429-
var ch = buffer[position];
428+
let ch = buffer[position];
430429
if (ch === TAB || ch === LF || ch === CR || ch === SPACE) {
431430
++position;
432431
continue;
@@ -442,8 +441,8 @@ class XRef {
442441
} while (ch !== LF && ch !== CR);
443442
continue;
444443
}
445-
var token = readToken(buffer, position);
446-
var m;
444+
const token = readToken(buffer, position);
445+
let m;
447446
if (
448447
token.startsWith("xref") &&
449448
(token.length === 4 || /\s/.test(token[4]))
@@ -497,7 +496,7 @@ class XRef {
497496

498497
// checking XRef stream suspect
499498
// (it shall have '/XRef' and next char is not a letter)
500-
var xrefTagOffset = skipUntil(content, 0, xrefBytes);
499+
const xrefTagOffset = skipUntil(content, 0, xrefBytes);
501500
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
502501
xrefStms.push(position - stream.start);
503502
this.xrefstms[position - stream.start] = 1; // Avoid recursion
@@ -529,7 +528,7 @@ class XRef {
529528
allowStreams: true,
530529
recoveryMode: true,
531530
});
532-
var obj = parser.getObj();
531+
const obj = parser.getObj();
533532
if (!isCmd(obj, "trailer")) {
534533
continue;
535534
}
@@ -572,15 +571,15 @@ class XRef {
572571
}
573572

574573
readXRef(recoveryMode = false) {
575-
var stream = this.stream;
574+
const stream = this.stream;
576575
// Keep track of already parsed XRef tables, to prevent an infinite loop
577576
// when parsing corrupt PDF files where e.g. the /Prev entries create a
578577
// circular dependency between tables (fixes bug1393476.pdf).
579578
const startXRefParsedCache = new Set();
580579

581580
try {
582581
while (this.startXRefQueue.length) {
583-
var startXRef = this.startXRefQueue[0];
582+
const startXRef = this.startXRefQueue[0];
584583

585584
if (startXRefParsedCache.has(startXRef)) {
586585
warn("readXRef - skipping XRef table since it was already parsed.");
@@ -596,8 +595,8 @@ class XRef {
596595
xref: this,
597596
allowStreams: true,
598597
});
599-
var obj = parser.getObj();
600-
var dict;
598+
let obj = parser.getObj();
599+
let dict;
601600

602601
// Get dictionary
603602
if (isCmd(obj, "xref")) {
@@ -610,7 +609,7 @@ class XRef {
610609
// Recursively get other XRefs 'XRefStm', if any
611610
obj = dict.get("XRefStm");
612611
if (Number.isInteger(obj)) {
613-
var pos = obj;
612+
const pos = obj;
614613
// ignore previously loaded xref streams
615614
// (possible infinite recursion)
616615
if (!(pos in this.xrefstms)) {
@@ -666,7 +665,7 @@ class XRef {
666665
}
667666

668667
getEntry(i) {
669-
var xrefEntry = this.entries[i];
668+
const xrefEntry = this.entries[i];
670669
if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
671670
return xrefEntry;
672671
}
@@ -720,22 +719,22 @@ class XRef {
720719
}
721720

722721
fetchUncompressed(ref, xrefEntry, suppressEncryption = false) {
723-
var gen = ref.gen;
724-
var num = ref.num;
722+
const gen = ref.gen;
723+
let num = ref.num;
725724
if (xrefEntry.gen !== gen) {
726725
throw new XRefEntryException(`Inconsistent generation in XRef: ${ref}`);
727726
}
728-
var stream = this.stream.makeSubStream(
727+
const stream = this.stream.makeSubStream(
729728
xrefEntry.offset + this.stream.start
730729
);
731730
const parser = new Parser({
732731
lexer: new Lexer(stream),
733732
xref: this,
734733
allowStreams: true,
735734
});
736-
var obj1 = parser.getObj();
737-
var obj2 = parser.getObj();
738-
var obj3 = parser.getObj();
735+
const obj1 = parser.getObj();
736+
const obj2 = parser.getObj();
737+
const obj3 = parser.getObj();
739738

740739
if (obj1 !== num || obj2 !== gen || !(obj3 instanceof Cmd)) {
741740
throw new XRefEntryException(`Bad (uncompressed) XRef entry: ${ref}`);

0 commit comments

Comments
 (0)