From 7bee0c2aa392b145d5cc23ac62d06b30fdf0af14 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 25 Apr 2017 16:07:59 +0200 Subject: [PATCH 1/2] Enable the `object-shorthand` ESLint rule in `src/shared` Please see http://eslint.org/docs/rules/object-shorthand. For the most part, these changes are of the search-and-replace kind, and the previously enabled `no-undef` rule should complement the tests in helping ensure that no stupid errors crept into to the patch. --- src/shared/compatibility.js | 56 ++++++++++++++++++------------------- src/shared/util.js | 27 +++++++++--------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index e01b95ce6..7b9be12b5 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -105,12 +105,12 @@ PDFJS.compatibilityChecked = true; var uint32ArrayViewSetters = 0; function createUint32ArrayProp(index) { return { - get: function () { + get() { var buffer = this.buffer, offset = index << 2; return (buffer[offset] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24)) >>> 0; }, - set: function (value) { + set(value) { var buffer = this.buffer, offset = index << 2; buffer[offset] = value & 255; buffer[offset + 1] = (value >> 8) & 255; @@ -190,14 +190,14 @@ PDFJS.compatibilityChecked = true; } // Trying to fake CanvasPixelArray as Uint8ClampedArray. Object.defineProperty(cpaProto, 'buffer', { - get: function () { + get() { return this; }, enumerable: false, configurable: true }); Object.defineProperty(cpaProto, 'byteLength', { - get: function () { + get() { return this.length; }, enumerable: false, @@ -411,7 +411,7 @@ PDFJS.compatibilityChecked = true; } Object.defineProperty(HTMLElement.prototype, 'dataset', { - get: function() { + get() { if (this._dataset) { return this._dataset; } @@ -470,22 +470,22 @@ PDFJS.compatibilityChecked = true; } var classListPrototype = { - add: function(name) { + add(name) { changeList(this.element, name, true, false); }, - contains: function(name) { + contains(name) { return changeList(this.element, name, false, false); }, - remove: function(name) { + remove(name) { changeList(this.element, name, false, true); }, - toggle: function(name) { + toggle(name) { changeList(this.element, name, true, true); } }; Object.defineProperty(HTMLElement.prototype, 'classList', { - get: function() { + get() { if (this._classList) { return this._classList; } @@ -562,9 +562,9 @@ PDFJS.compatibilityChecked = true; } if (!('console' in window)) { window.console = { - log: function() {}, - error: function() {}, - warn: function() {} + log() {}, + error() {}, + warn() {} }; return; } @@ -772,7 +772,7 @@ PDFJS.compatibilityChecked = true; return; } Object.defineProperty(document, 'currentScript', { - get: function () { + get() { var scripts = document.getElementsByTagName('script'); return scripts[scripts.length - 1]; }, @@ -794,10 +794,10 @@ PDFJS.compatibilityChecked = true; var inputProto = el.constructor.prototype; var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type'); Object.defineProperty(inputProto, 'type', { - get: function () { + get() { return typeProperty.get.call(this); }, - set: function (value) { + set(value) { typeProperty.set.call(this, value === 'number' ? 'text' : value); }, enumerable: true, @@ -819,11 +819,11 @@ PDFJS.compatibilityChecked = true; var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState'); Object.defineProperty(documentProto, 'readyState', { - get: function () { + get() { var value = readyStateProto.get.call(this); return value === 'interactive' ? 'loading' : value; }, - set: function (value) { + set(value) { readyStateProto.set.call(this, value); }, enumerable: true, @@ -979,7 +979,7 @@ PDFJS.compatibilityChecked = true; addUnhandledRejection: function addUnhandledRejection(promise) { this.unhandledRejections.push({ - promise: promise, + promise, time: Date.now() }); this.scheduleRejectionCheck(); @@ -1160,9 +1160,9 @@ PDFJS.compatibilityChecked = true; }); this._handlers.push({ thisPromise: this, - onResolve: onResolve, - onReject: onReject, - nextPromise: nextPromise + onResolve, + onReject, + nextPromise, }); HandlerManager.scheduleHandlers(this); return nextPromise; @@ -1186,20 +1186,20 @@ PDFJS.compatibilityChecked = true; this.id = '$weakmap' + (id++); } WeakMap.prototype = { - has: function(obj) { + has(obj) { return !!Object.getOwnPropertyDescriptor(obj, this.id); }, - get: function(obj, defaultValue) { + get(obj, defaultValue) { return this.has(obj) ? obj[this.id] : defaultValue; }, - set: function(obj, value) { + set(obj, value) { Object.defineProperty(obj, this.id, { - value: value, + value, enumerable: false, configurable: true }); }, - delete: function(obj) { + delete(obj) { delete obj[this.id]; } }; @@ -1703,7 +1703,7 @@ PDFJS.compatibilityChecked = true; } JURL.prototype = { - toString: function() { + toString() { return this.href; }, get href() { diff --git a/src/shared/util.js b/src/shared/util.js index f6f7f3974..f17cc92cb 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -376,7 +376,7 @@ function createValidAbsoluteUrl(url, baseUrl) { } function shadow(obj, prop, value) { - Object.defineProperty(obj, prop, { value: value, + Object.defineProperty(obj, prop, { value, enumerable: true, configurable: true, writable: false }); @@ -1254,8 +1254,8 @@ function MessageHandler(sourceName, targetName, comObj) { return action[0].call(action[1], data.data); }).then(function (result) { comObj.postMessage({ - sourceName: sourceName, - targetName: targetName, + sourceName, + targetName, isReply: true, callbackId: data.callbackId, data: result @@ -1266,8 +1266,8 @@ function MessageHandler(sourceName, targetName, comObj) { reason = reason + ''; } comObj.postMessage({ - sourceName: sourceName, - targetName: targetName, + sourceName, + targetName, isReply: true, callbackId: data.callbackId, error: reason @@ -1284,7 +1284,7 @@ function MessageHandler(sourceName, targetName, comObj) { } MessageHandler.prototype = { - on: function messageHandlerOn(actionName, handler, scope) { + on(actionName, handler, scope) { var ah = this.actionHandler; if (ah[actionName]) { error('There is already an actionName called "' + actionName + '"'); @@ -1297,12 +1297,12 @@ MessageHandler.prototype = { * @param {JSON} data JSON data to send. * @param {Array} [transfers] Optional list of transfers/ArrayBuffers */ - send: function messageHandlerSend(actionName, data, transfers) { + send(actionName, data, transfers) { var message = { sourceName: this.sourceName, targetName: this.targetName, action: actionName, - data: data + data, }; this.postMessage(message, transfers); }, @@ -1314,15 +1314,14 @@ MessageHandler.prototype = { * @param {Array} [transfers] Optional list of transfers/ArrayBuffers. * @returns {Promise} Promise to be resolved with response data. */ - sendWithPromise: - function messageHandlerSendWithPromise(actionName, data, transfers) { + sendWithPromise(actionName, data, transfers) { var callbackId = this.callbackIndex++; var message = { sourceName: this.sourceName, targetName: this.targetName, action: actionName, - data: data, - callbackId: callbackId + data, + callbackId, }; var capability = createPromiseCapability(); this.callbacksCapabilities[callbackId] = capability; @@ -1339,7 +1338,7 @@ MessageHandler.prototype = { * @param message {Object} Raw message. * @param transfers List of transfers/ArrayBuffers, or undefined. */ - postMessage: function (message, transfers) { + postMessage(message, transfers) { if (transfers && this.postMessageTransfers) { this.comObj.postMessage(message, transfers); } else { @@ -1347,7 +1346,7 @@ MessageHandler.prototype = { } }, - destroy: function () { + destroy() { this.comObj.removeEventListener('message', this._onComObjOnMessage); } }; From 07b55740067738ac25bc2e08207c3dacda32106d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 25 Apr 2017 16:17:18 +0200 Subject: [PATCH 2/2] Enable the `object-shorthand` ESLint rule in `src/display` Please see http://eslint.org/docs/rules/object-shorthand. For the most part, these changes are of the search-and-replace kind, and the previously enabled `no-undef` rule should complement the tests in helping ensure that no stupid errors crept into to the patch. --- src/display/annotation_layer.js | 6 ++-- src/display/api.js | 53 +++++++++++++++++---------------- src/display/canvas.js | 35 +++++++++++----------- src/display/dom_utils.js | 6 ++-- src/display/font_loader.js | 6 ++-- src/display/global.js | 8 ++--- src/display/pattern_helper.js | 15 ++++++---- src/display/text_layer.js | 2 +- src/display/webgl.js | 4 +-- 9 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index efd05765b..73e9e2842 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -237,8 +237,8 @@ var AnnotationElement = (function AnnotationElementClosure() { } var popupElement = new PopupElement({ - container: container, - trigger: trigger, + container, + trigger, color: data.color, title: data.title, contents: data.contents, @@ -1154,7 +1154,7 @@ var AnnotationLayer = (function AnnotationLayerClosure() { continue; } var element = annotationElementFactory.create({ - data: data, + data, layer: parameters.div, page: parameters.page, viewport: parameters.viewport, diff --git a/src/display/api.js b/src/display/api.js index ee52b8c96..35baccb91 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -284,8 +284,8 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) { source.initialData = pdfDataRangeTransport.initialData; } return worker.messageHandler.sendWithPromise('GetDocRequest', { - docId: docId, - source: source, + docId, + source, disableRange: getDefaultSetting('disableRange'), maxImageSize: getDefaultSetting('maxImageSize'), disableFontFace: getDefaultSetting('disableFontFace'), @@ -364,7 +364,7 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { * @return {Promise} A promise that is resolved after destruction activity * is completed. */ - destroy: function () { + destroy() { this.destroyed = true; var transportDestroyed = !this._transport ? Promise.resolve() : @@ -968,7 +968,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() { /** * Cleans up resources allocated by the page. (deprecated) */ - destroy: function() { + destroy() { deprecated('page destroy method, use cleanup() instead'); this.cleanup(); }, @@ -1119,7 +1119,7 @@ var PDFWorker = (function PDFWorkerClosure() { this._deferred = Promise.resolve(undefined); } FakeWorkerPort.prototype = { - postMessage: function (obj, transfers) { + postMessage(obj, transfers) { function cloneValue(value) { // Trying to perform a structured clone close to the spec, including // transfers. @@ -1179,14 +1179,14 @@ var PDFWorker = (function PDFWorkerClosure() { }, this); }.bind(this)); }, - addEventListener: function (name, listener) { + addEventListener(name, listener) { this._listeners.push(listener); }, - removeEventListener: function (name, listener) { + removeEventListener(name, listener) { var i = this._listeners.indexOf(listener); this._listeners.splice(i, 1); }, - terminate: function () { + terminate() { this._listeners = []; } }; @@ -1495,20 +1495,20 @@ var WorkerTransport = (function WorkerTransportClosure() { if (pdfDataRangeTransport) { pdfDataRangeTransport.addRangeListener(function(begin, chunk) { messageHandler.send('OnDataRange', { - begin: begin, - chunk: chunk + begin, + chunk, }); }); pdfDataRangeTransport.addProgressListener(function(loaded) { messageHandler.send('OnDataProgress', { - loaded: loaded + loaded, }); }); pdfDataRangeTransport.addProgressiveReadListener(function(chunk) { messageHandler.send('OnDataRange', { - chunk: chunk + chunk, }); }); @@ -1532,12 +1532,11 @@ var WorkerTransport = (function WorkerTransportClosure() { this._passwordCapability = createPromiseCapability(); if (loadingTask.onPassword) { - var updatePassword = function (password) { + var updatePassword = (password) => { this._passwordCapability.resolve({ - password: password, + password, }); - }.bind(this); - + }; loadingTask.onPassword(updatePassword, exception.code); } else { this._passwordCapability.reject( @@ -1628,7 +1627,7 @@ var WorkerTransport = (function WorkerTransportClosure() { if (getDefaultSetting('pdfBug') && globalScope.FontInspector && globalScope['FontInspector'].enabled) { fontRegistry = { - registerFont: function (font, url) { + registerFont(font, url) { globalScope['FontInspector'].fontAdded(font, url); } }; @@ -1636,7 +1635,7 @@ var WorkerTransport = (function WorkerTransportClosure() { var font = new FontFaceObject(exportedData, { isEvalSuported: getDefaultSetting('isEvalSupported'), disableFontFace: getDefaultSetting('disableFontFace'), - fontRegistry: fontRegistry + fontRegistry, }); this.fontLoader.bind( @@ -1784,7 +1783,7 @@ var WorkerTransport = (function WorkerTransportClosure() { buf[j] = data[i]; } } - resolve({ data: buf, width: width, height: height}); + resolve({ data: buf, width, height, }); }; img.onerror = function () { reject(new Error('JpegDecode failed to load image')); @@ -1817,7 +1816,7 @@ var WorkerTransport = (function WorkerTransportClosure() { return this.pagePromises[pageIndex]; } var promise = this.messageHandler.sendWithPromise('GetPage', { - pageIndex: pageIndex + pageIndex, }).then(function (pageInfo) { if (this.destroyed) { throw new Error('Transport destroyed'); @@ -1832,7 +1831,7 @@ var WorkerTransport = (function WorkerTransportClosure() { getPageIndex: function WorkerTransport_getPageIndexByRef(ref) { return this.messageHandler.sendWithPromise('GetPageIndex', { - ref: ref, + ref, }).catch(function (reason) { return Promise.reject(new Error(reason)); }); @@ -1840,8 +1839,8 @@ var WorkerTransport = (function WorkerTransportClosure() { getAnnotations: function WorkerTransport_getAnnotations(pageIndex, intent) { return this.messageHandler.sendWithPromise('GetAnnotations', { - pageIndex: pageIndex, - intent: intent, + pageIndex, + intent, }); }, @@ -1850,7 +1849,9 @@ var WorkerTransport = (function WorkerTransportClosure() { }, getDestination: function WorkerTransport_getDestination(id) { - return this.messageHandler.sendWithPromise('GetDestination', { id: id }); + return this.messageHandler.sendWithPromise('GetDestination', { + id, + }); }, getPageLabels: function WorkerTransport_getPageLabels() { @@ -2194,12 +2195,12 @@ var InternalRenderTask = (function InternalRenderTaskClosure() { var _UnsupportedManager = (function UnsupportedManagerClosure() { var listeners = []; return { - listen: function (cb) { + listen(cb) { deprecated('Global UnsupportedManager.listen is used: ' + ' use PDFDocumentLoadingTask.onUnsupportedFeature instead'); listeners.push(cb); }, - notify: function (featureId) { + notify(featureId) { for (var i = 0, ii = listeners.length; i < ii; i++) { listeners[i](featureId); } diff --git a/src/display/canvas.js b/src/display/canvas.js index 91d7a6bcf..c3c9c2be7 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -182,7 +182,7 @@ var CachedCanvases = (function CachedCanvasesClosure() { } return canvasEntry; }, - clear: function () { + clear() { for (var id in this.cache) { var canvasEntry = this.cache[id]; this.canvasFactory.destroy(canvasEntry); @@ -688,7 +688,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var backdrop = smask.backdrop || null; if (!smask.transferMap && WebGLUtils.isEnabled) { var composed = WebGLUtils.composeSMask(layerCtx.canvas, mask, - {subtype: smask.subtype, backdrop: backdrop}); + { subtype: smask.subtype, backdrop, }); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(composed, smask.offsetX, smask.offsetY); return; @@ -1376,10 +1376,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var paths = this.pendingTextPaths || (this.pendingTextPaths = []); paths.push({ transform: ctx.mozCurrentTransform, - x: x, - y: y, - fontSize: fontSize, - addToPath: addToPath + x, + y, + fontSize, + addToPath, }); } }, @@ -1630,11 +1630,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var color = IR[1]; var baseTransform = this.baseTransform || this.ctx.mozCurrentTransform.slice(); - var self = this; var canvasGraphicsFactory = { - createCanvasGraphics: function (ctx) { - return new CanvasGraphics(ctx, self.commonObjs, self.objs, - self.canvasFactory); + createCanvasGraphics: (ctx) => { + return new CanvasGraphics(ctx, this.commonObjs, this.objs, + this.canvasFactory); } }; pattern = new TilingPattern(IR, color, this.ctx, canvasGraphicsFactory, @@ -1812,10 +1811,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { this.smaskStack.push({ canvas: scratchCanvas.canvas, context: groupCtx, - offsetX: offsetX, - offsetY: offsetY, - scaleX: scaleX, - scaleY: scaleY, + offsetX, + offsetY, + scaleX, + scaleY, subtype: group.smask.subtype, backdrop: group.smask.backdrop, transferMap: group.smask.transferMap || null, @@ -1915,7 +1914,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var currentTransform = ctx.mozCurrentTransformInverse; var position = this.getCanvasPosition(0, 0); this.imageLayer.appendImage({ - objId: objId, + objId, left: position[0], top: position[1], width: w / currentTransform[0], @@ -1936,7 +1935,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) { if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) { glyph.compiled = - compileType3Glyph({data: img.data, width: width, height: height}); + compileType3Glyph({ data: img.data, width, height, }); } else { glyph.compiled = null; } @@ -2123,7 +2122,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { if (this.imageLayer) { var position = this.getCanvasPosition(0, -height); this.imageLayer.appendImage({ - imgData: imgData, + imgData, left: position[0], top: position[1], width: width / currentTransform[0], @@ -2153,7 +2152,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { if (this.imageLayer) { var position = this.getCanvasPosition(entry.x, entry.y); this.imageLayer.appendImage({ - imgData: imgData, + imgData, left: position[0], top: position[1], width: w, diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index d3975c7fe..b2f61c3ba 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -29,8 +29,8 @@ DOMCanvasFactory.prototype = { canvas.width = width; canvas.height = height; return { - canvas: canvas, - context: context, + canvas, + context, }; }, @@ -59,7 +59,7 @@ var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() { } DOMCMapReaderFactory.prototype = { - fetch: function(params) { + fetch(params) { var name = params.name; if (!name) { return Promise.reject(new Error('CMap name must be specified.')); diff --git a/src/display/font_loader.js b/src/display/font_loader.js index a63cba594..25144ea40 100644 --- a/src/display/font_loader.js +++ b/src/display/font_loader.js @@ -87,7 +87,7 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) { 'ABAAAAAAAAAAAD6AAAAAAAAA=='); }; Object.defineProperty(FontLoader.prototype, 'loadTestFont', { - get: function () { + get() { return shadow(this, 'loadTestFont', getLoadTestFont()); }, configurable: true @@ -170,7 +170,7 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) { var request = { id: requestId, complete: LoadLoader_completeRequest, - callback: callback, + callback, started: Date.now() }; context.requests.push(request); @@ -320,7 +320,7 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL || CHROME')) { return supported; }; Object.defineProperty(FontLoader, 'isSyncFontLoadingSupported', { - get: function () { + get() { return shadow(FontLoader, 'isSyncFontLoadingSupported', isSyncFontLoadingSupported()); }, diff --git a/src/display/global.js b/src/display/global.js index e8d60a3c4..afa1f6e56 100644 --- a/src/display/global.js +++ b/src/display/global.js @@ -55,10 +55,10 @@ if (PDFJS.verbosity !== undefined) { } delete PDFJS.verbosity; Object.defineProperty(PDFJS, 'verbosity', { - get: function () { + get() { return getVerbosityLevel(); }, - set: function (level) { + set(level) { setVerbosityLevel(level); }, enumerable: true, @@ -250,10 +250,10 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) { var savedOpenExternalLinksInNewWindow = PDFJS.openExternalLinksInNewWindow; delete PDFJS.openExternalLinksInNewWindow; Object.defineProperty(PDFJS, 'openExternalLinksInNewWindow', { - get: function () { + get() { return PDFJS.externalLinkTarget === LinkTarget.BLANK; }, - set: function (value) { + set(value) { if (value) { deprecated('PDFJS.openExternalLinksInNewWindow, please use ' + '"PDFJS.externalLinkTarget = PDFJS.LinkTarget.BLANK" instead.'); diff --git a/src/display/pattern_helper.js b/src/display/pattern_helper.js index 994150445..13c44aae3 100644 --- a/src/display/pattern_helper.js +++ b/src/display/pattern_helper.js @@ -169,8 +169,8 @@ var createMeshCanvas = (function createMeshCanvasClosure() { var scaleY = boundsHeight / height; var context = { - coords: coords, - colors: colors, + coords, + colors, offsetX: -offsetX, offsetY: -offsetY, scaleX: 1 / scaleX, @@ -212,10 +212,13 @@ var createMeshCanvas = (function createMeshCanvasClosure() { canvas = tmpCanvas.canvas; } - return {canvas: canvas, - offsetX: offsetX - BORDER_SIZE * scaleX, - offsetY: offsetY - BORDER_SIZE * scaleY, - scaleX: scaleX, scaleY: scaleY}; + return { + canvas, + offsetX: offsetX - BORDER_SIZE * scaleX, + offsetY: offsetY - BORDER_SIZE * scaleY, + scaleX, + scaleY, + }; } return createMeshCanvas; })(); diff --git a/src/display/text_layer.js b/src/display/text_layer.js index fda53a779..961a1684e 100644 --- a/src/display/text_layer.js +++ b/src/display/text_layer.js @@ -148,7 +148,7 @@ var renderTextLayer = (function renderTextLayerClosure() { bottom: b[3], div: textDiv, size: [divWidth, divHeight], - m: m + m, }); } } diff --git a/src/display/webgl.js b/src/display/webgl.js index d305bb2bf..effe1ded0 100644 --- a/src/display/webgl.js +++ b/src/display/webgl.js @@ -430,8 +430,8 @@ var WebGLUtils = (function WebGLUtilsClosure() { } catch (e) { } return shadow(this, 'isEnabled', enabled); }, - composeSMask: composeSMask, - drawFigures: drawFigures, + composeSMask, + drawFigures, clear: cleanup }; })();