Skip to content

Commit

Permalink
Merge pull request #17550 from Snuffleupagus/arrow-fn-shorter
Browse files Browse the repository at this point in the history
Use shorter arrow functions where possible
  • Loading branch information
Snuffleupagus committed Jan 21, 2024
2 parents 10eef37 + f9a384d commit fce822c
Show file tree
Hide file tree
Showing 37 changed files with 156 additions and 252 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@
}],

// ECMAScript 6
"arrow-body-style": ["error", "as-needed"],
"constructor-super": "error",
"no-class-assign": "error",
"no-confusing-arrow": "error",
"no-const-assign": "error",
"no-dupe-class-members": "error",
"no-duplicate-imports": "error",
Expand Down
12 changes: 4 additions & 8 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class AnnotationFactory {
// with "GoToE" actions, from throwing and thus breaking parsing:
pdfManager.ensureCatalog("attachments"),
]).then(
// eslint-disable-next-line arrow-body-style
([acroForm, xfaDatasets, structTreeRoot, baseUrl, attachments]) => {
return {
pdfManager,
Expand Down Expand Up @@ -2133,11 +2134,8 @@ class WidgetAnnotation extends Annotation {
value,
};

const encoder = val => {
return isAscii(val)
? val
: stringToUTF16String(val, /* bigEndian = */ true);
};
const encoder = val =>
isAscii(val) ? val : stringToUTF16String(val, /* bigEndian = */ true);
dict.set("V", Array.isArray(value) ? value.map(encoder) : encoder(value));
this.amendSavedDict(annotationStorage, dict);

Expand Down Expand Up @@ -4824,9 +4822,7 @@ class StampAnnotation extends MarkupAnnotation {

const jpegBufferPromise = canvas
.convertToBlob({ type: "image/jpeg", quality: 1 })
.then(blob => {
return blob.arrayBuffer();
});
.then(blob => blob.arrayBuffer());

const xobjectName = Name.get("XObject");
const imageName = Name.get("Image");
Expand Down
7 changes: 3 additions & 4 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,13 @@ class Catalog {
case "PrintPageRange":
// The number of elements must be even.
if (Array.isArray(value) && value.length % 2 === 0) {
const isValid = value.every((page, i, arr) => {
return (
const isValid = value.every(
(page, i, arr) =>
Number.isInteger(page) &&
page > 0 &&
(i === 0 || page >= arr[i - 1]) &&
page <= this.numPages
);
});
);
if (isValid) {
prefValue = value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,9 @@ async function createBuiltInCMap(name, fetchBuiltInCMap) {
const cMap = new CMap(true);

if (compressionType === CMapCompressionType.BINARY) {
return new BinaryCMapReader().process(cMapData, cMap, useCMap => {
return extendCMap(cMap, fetchBuiltInCMap, useCMap);
});
return new BinaryCMapReader().process(cMapData, cMap, useCMap =>
extendCMap(cMap, fetchBuiltInCMap, useCMap)
);
}
if (compressionType === CMapCompressionType.NONE) {
const lexer = new Lexer(new Stream(cMapData));
Expand Down
1 change: 1 addition & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@ class PDFDocument {
} else {
promise = catalog.getPageDict(pageIndex);
}
// eslint-disable-next-line arrow-body-style
promise = promise.then(([pageDict, ref]) => {
return new Page({
pdfManager: this.pdfManager,
Expand Down
20 changes: 12 additions & 8 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,14 +1039,15 @@ class PartialEvaluator {

return translated;
})
.catch(reason => {
return new TranslatedFont({
loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font,
evaluatorOptions: this.options,
});
});
.catch(
reason =>
new TranslatedFont({
loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font,
evaluatorOptions: this.options,
})
);
})
.then(translated => {
state.font = translated.font;
Expand Down Expand Up @@ -1129,6 +1130,7 @@ class PartialEvaluator {
case "Font":
isSimpleGState = false;

// eslint-disable-next-line arrow-body-style
promise = promise.then(() => {
return this.handleSetFont(
resources,
Expand All @@ -1154,6 +1156,7 @@ class PartialEvaluator {
if (value instanceof Dict) {
isSimpleGState = false;

// eslint-disable-next-line arrow-body-style
promise = promise.then(() => {
return this.handleSMask(
value,
Expand Down Expand Up @@ -1214,6 +1217,7 @@ class PartialEvaluator {
fallbackFontDict = null,
cssFontInfo = null
) {
// eslint-disable-next-line arrow-body-style
const errorFont = async () => {
return new TranslatedFont({
loadedName: "g_font_error",
Expand Down
8 changes: 2 additions & 6 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2093,19 +2093,15 @@ class Font {
endOffset: 0,
});
}
locaEntries.sort((a, b) => {
return a.offset - b.offset;
});
locaEntries.sort((a, b) => a.offset - b.offset);
// Now the offsets are sorted, calculate the end offset of each glyph.
// The last loca entry's endOffset is not calculated since it's the end
// of the data and will be stored on the previous entry's endOffset.
for (i = 0; i < numGlyphs; i++) {
locaEntries[i].endOffset = locaEntries[i + 1].offset;
}
// Re-sort so glyphs aren't out of order.
locaEntries.sort((a, b) => {
return a.index - b.index;
});
locaEntries.sort((a, b) => a.index - b.index);
// Calculate the endOffset of the "first" glyph correctly when there are
// *multiple* empty ones at the start of the data (fixes issue14618.pdf).
for (i = 0; i < numGlyphs; i++) {
Expand Down
7 changes: 3 additions & 4 deletions src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,9 @@ async function incrementalUpdate({

// Add a ref for the new xref and sort them
newRefs.push({ ref: refForXrefTable, data: "" });
newRefs = newRefs.sort((a, b) => {
// compare the refs
return a.ref.num - b.ref.num;
});
newRefs = newRefs.sort(
(a, b) => /* compare the refs */ a.ref.num - b.ref.num
);

const xrefTableData = [[0, 1, 0xffff]];
const indexes = [0, 1];
Expand Down
10 changes: 4 additions & 6 deletions src/core/xfa/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ function getRelevant(data) {
return data
.trim()
.split(/\s+/)
.map(e => {
return {
excluded: e[0] === "-",
viewname: e.substring(1),
};
});
.map(e => ({
excluded: e[0] === "-",
viewname: e.substring(1),
}));
}

function getColor(data, def = [0, 0, 0]) {
Expand Down
7 changes: 4 additions & 3 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1838,9 +1838,10 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {

const getItems = event => {
const options = event.target.options;
return Array.prototype.map.call(options, option => {
return { displayValue: option.textContent, exportValue: option.value };
});
return Array.prototype.map.call(options, option => ({
displayValue: option.textContent,
exportValue: option.value,
}));
};

if (this.enableScripting && this.hasJSActions) {
Expand Down
59 changes: 19 additions & 40 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,19 +771,13 @@ class PDFDocumentProxy {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getXFADatasets", {
value: () => {
return this._transport.getXFADatasets();
},
value: () => this._transport.getXFADatasets(),
});
Object.defineProperty(this, "getXRefPrevValue", {
value: () => {
return this._transport.getXRefPrevValue();
},
value: () => this._transport.getXRefPrevValue(),
});
Object.defineProperty(this, "getAnnotArray", {
value: pageIndex => {
return this._transport.getAnnotArray(pageIndex);
},
value: pageIndex => this._transport.getAnnotArray(pageIndex),
});
}
}
Expand Down Expand Up @@ -1628,9 +1622,7 @@ class PDFPageProxy {
if (this._transport._htmlForXfa) {
// TODO: We need to revisit this once the XFA foreground patch lands and
// only do this for non-foreground XFA.
return this.getXfa().then(xfa => {
return XfaText.textContent(xfa);
});
return this.getXfa().then(xfa => XfaText.textContent(xfa));
}
const readableStream = this.streamTextContent(params);

Expand Down Expand Up @@ -2358,21 +2350,16 @@ class WorkerTransport {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getXFADatasets", {
value: () => {
return this.messageHandler.sendWithPromise("GetXFADatasets", null);
},
value: () =>
this.messageHandler.sendWithPromise("GetXFADatasets", null),
});
Object.defineProperty(this, "getXRefPrevValue", {
value: () => {
return this.messageHandler.sendWithPromise("GetXRefPrevValue", null);
},
value: () =>
this.messageHandler.sendWithPromise("GetXRefPrevValue", null),
});
Object.defineProperty(this, "getAnnotArray", {
value: pageIndex => {
return this.messageHandler.sendWithPromise("GetAnnotArray", {
pageIndex,
});
},
value: pageIndex =>
this.messageHandler.sendWithPromise("GetAnnotArray", { pageIndex }),
});
}
}
Expand Down Expand Up @@ -2737,9 +2724,7 @@ class WorkerTransport {

this.fontLoader
.bind(font)
.catch(reason => {
return messageHandler.sendWithPromise("FontFallback", { id });
})
.catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
.finally(() => {
if (!params.fontExtraProperties && font.data) {
// Immediately release the `font.data` property once the font
Expand Down Expand Up @@ -3013,9 +2998,7 @@ class WorkerTransport {
getOptionalContentConfig() {
return this.messageHandler
.sendWithPromise("GetOptionalContentConfig", null)
.then(results => {
return new OptionalContentConfig(results);
});
.then(results => new OptionalContentConfig(results));
}

getPermissions() {
Expand All @@ -3030,14 +3013,12 @@ class WorkerTransport {
}
const promise = this.messageHandler
.sendWithPromise(name, null)
.then(results => {
return {
info: results[0],
metadata: results[1] ? new Metadata(results[1]) : null,
contentDispositionFilename: this._fullReader?.filename ?? null,
contentLength: this._fullReader?.contentLength ?? null,
};
});
.then(results => ({
info: results[0],
metadata: results[1] ? new Metadata(results[1]) : null,
contentDispositionFilename: this._fullReader?.filename ?? null,
contentLength: this._fullReader?.contentLength ?? null,
}));
this.#methodPromises.set(name, promise);
return promise;
}
Expand Down Expand Up @@ -3192,9 +3173,7 @@ class RenderTask {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getOperatorList", {
value: () => {
return this.#internalRenderTask.operatorList;
},
value: () => this.#internalRenderTask.operatorList,
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2359,8 +2359,8 @@ class CanvasGraphics {
const color = IR[1];
const baseTransform = this.baseTransform || getCurrentTransform(this.ctx);
const canvasGraphicsFactory = {
createCanvasGraphics: ctx => {
return new CanvasGraphics(
createCanvasGraphics: ctx =>
new CanvasGraphics(
ctx,
this.commonObjs,
this.objs,
Expand All @@ -2370,8 +2370,7 @@ class CanvasGraphics {
optionalContentConfig: this.optionalContentConfig,
markedContentStack: this.markedContentStack,
}
);
},
),
};
pattern = new TilingPattern(
IR,
Expand Down
22 changes: 10 additions & 12 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,13 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
return fetchData(
url,
/* type = */ this.isCompressed ? "arraybuffer" : "text"
).then(data => {
return {
cMapData:
data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data),
compressionType,
};
});
).then(data => ({
cMapData:
data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data),
compressionType,
}));
}
}

Expand All @@ -483,9 +481,9 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
* @ignore
*/
_fetchData(url) {
return fetchData(url, /* type = */ "arraybuffer").then(data => {
return new Uint8Array(data);
});
return fetchData(url, /* type = */ "arraybuffer").then(
data => new Uint8Array(data)
);
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,14 @@ class AnnotationEditorUIManager {
static get _keyboardManager() {
const proto = AnnotationEditorUIManager.prototype;

const arrowChecker = self => {
// If the focused element is an input, we don't want to handle the arrow.
// For example, sliders can be controlled with the arrow keys.
return (
self.#container.contains(document.activeElement) &&
document.activeElement.tagName !== "BUTTON" &&
self.hasSomethingToControl()
);
};
/**
* If the focused element is an input, we don't want to handle the arrow.
* For example, sliders can be controlled with the arrow keys.
*/
const arrowChecker = self =>
self.#container.contains(document.activeElement) &&
document.activeElement.tagName !== "BUTTON" &&
self.hasSomethingToControl();

const textInputChecker = (_self, { target: el }) => {
if (el instanceof HTMLInputElement) {
Expand Down
Loading

0 comments on commit fce822c

Please sign in to comment.