Skip to content

Commit

Permalink
Enable auto-formatting of the entire code-base using Prettier (issue …
Browse files Browse the repository at this point in the history
…11444)

Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).

Prettier is being used for a couple of reasons:

 - To be consistent with `mozilla-central`, where Prettier is already in use across the tree.

 - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.

Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.

*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.

(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
  • Loading branch information
Snuffleupagus committed Dec 25, 2019
1 parent f7ae422 commit 281fd40
Show file tree
Hide file tree
Showing 205 changed files with 39,686 additions and 31,417 deletions.
51 changes: 6 additions & 45 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"unicorn",
],

"extends": [
"plugin:prettier/recommended"
],

"env": {
"browser": true,
"es6": true,
Expand Down Expand Up @@ -47,7 +51,6 @@
"no-empty": ["error", { "allowEmptyCatch": true, }],
"no-ex-assign": "error",
"no-extra-boolean-cast": "error",
"no-extra-semi": "error",
"no-func-assign": "error",
"no-inner-declarations": ["error", "functions"],
"no-invalid-regexp": "error",
Expand Down Expand Up @@ -84,9 +87,6 @@
"no-implied-eval": "error",
"no-iterator": "error",
"no-lone-blocks": "error",
"no-multi-spaces": ["error", {
"ignoreEOLComments": true,
}],
"no-multi-str": "error",
"no-new-func": "error",
"no-new-wrappers": "error",
Expand Down Expand Up @@ -127,61 +127,23 @@
}],

// Stylistic Issues
"array-bracket-spacing": ["error", "never"],
"block-spacing": ["error", "always"],
"brace-style": ["error", "1tbs", {
"allowSingleLine": false,
}],
"comma-dangle": ["error", {
"arrays": "ignore",
"objects": "always",
"imports": "never",
"exports": "always",
"functions": "never",
}],
"comma-spacing": ["error", { "before": false, "after": true, }],
"comma-style": ["error", "last"],
"eol-last": "error",
"func-call-spacing": ["error", "never"],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true, "mode": "strict", }],
"keyword-spacing": ["error", { "before": true, "after": true, }],
"linebreak-style": ["error", "unix"],
"lines-between-class-members": ["error", "always"],
"max-len": ["error", {
"code": 80,
"code": 1000,
"comments": 80,
"ignoreUrls": true
}],
"new-cap": ["error", { "newIsCap": true, "capIsNew": false, }],
"new-parens": "error",
"no-array-constructor": "error",
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 0, "maxBOF": 1, }],
"no-new-object": "error",
"no-tabs": "error",
"no-trailing-spaces": ["error", { "skipBlankLines": false, }],
"no-whitespace-before-property": "error",
"object-curly-spacing": ["error", "always"],
"operator-linebreak": ["error", "after", { "overrides": { ":": "ignore", } }],
"quotes": ["error", "single"],
"semi-spacing": ["error", { "before": false, "after": true, }],
"semi": ["error", "always"],
"space-before-blocks": ["error", "always"],
"space-before-function-paren": ["error", { "anonymous": "ignore", "named": "never", }],
"space-in-parens": ["error", "never"],
"space-infix-ops": ["error", { "int32Hint": false }],
"space-unary-ops": ["error", { "words": true, "nonwords": false, }],
"spaced-comment": ["error", "always", {
"block": {
"balanced": true,
}
}],

// ECMAScript 6
"arrow-body-style": ["error", "always"],
"arrow-parens": ["error", "always"],
"arrow-spacing": ["error", {
"before": true,
"after": true,
}],
"constructor-super": "error",
"no-class-assign": "error",
"no-confusing-arrow": "error",
Expand All @@ -197,7 +159,6 @@
"avoidQuotes": true,
}],
"prefer-const": "off",
"rest-spread-spacing": ["error", "never"],
"sort-imports": ["error", {
"ignoreCase": true,
}],
Expand Down
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"endOfLine": "lf",
"printWidth": 80,
"semi": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false
}
43 changes: 22 additions & 21 deletions examples/acroforms/acroforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* limitations under the License.
*/

'use strict';
"use strict";

pdfjsLib.GlobalWorkerOptions.workerSrc =
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
"../../node_modules/pdfjs-dist/build/pdf.worker.js";

var DEFAULT_URL = '../../test/pdfs/f1040.pdf';
var DEFAULT_URL = "../../test/pdfs/f1040.pdf";
var DEFAULT_SCALE = 1.0;

var container = document.getElementById('pageContainer');
var container = document.getElementById("pageContainer");

// Fetch the PDF document from the URL using promises.
var loadingTask = pdfjsLib.getDocument(DEFAULT_URL);
Expand All @@ -30,23 +30,24 @@ loadingTask.promise.then(function(doc) {
var promise = Promise.resolve();

for (var i = 1; i <= doc.numPages; i++) {
promise = promise.then(function (pageNum) {
return doc.getPage(pageNum).then(function (pdfPage) {
// Create the page view.
var pdfPageView = new pdfjsViewer.PDFPageView({
container: container,
id: pageNum,
scale: DEFAULT_SCALE,
defaultViewport: pdfPage.getViewport({ scale: DEFAULT_SCALE, }),
annotationLayerFactory:
new pdfjsViewer.DefaultAnnotationLayerFactory(),
renderInteractiveForms: true,
});
promise = promise.then(
function(pageNum) {
return doc.getPage(pageNum).then(function(pdfPage) {
// Create the page view.
var pdfPageView = new pdfjsViewer.PDFPageView({
container: container,
id: pageNum,
scale: DEFAULT_SCALE,
defaultViewport: pdfPage.getViewport({ scale: DEFAULT_SCALE }),
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
renderInteractiveForms: true,
});

// Associate the actual page with the view and draw it.
pdfPageView.setPdfPage(pdfPage);
return pdfPageView.draw();
});
}.bind(null, i));
// Associate the actual page with the view and draw it.
pdfPageView.setPdfPage(pdfPage);
return pdfPageView.draw();
});
}.bind(null, i)
);
}
});
52 changes: 28 additions & 24 deletions examples/browserify/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
var gulp = require('gulp');
var browserify = require('browserify');
var streamify = require('gulp-streamify');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');
var gulp = require("gulp");
var browserify = require("browserify");
var streamify = require("gulp-streamify");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var source = require("vinyl-source-stream");

var OUTPUT_PATH = '../../build/browserify';
var TMP_FILE_PREFIX = '../../build/browserify_';
var OUTPUT_PATH = "../../build/browserify";
var TMP_FILE_PREFIX = "../../build/browserify_";

gulp.task('build-bundle', function() {
return browserify('main.js', { output: TMP_FILE_PREFIX + 'main.tmp', })
.ignore(require.resolve('pdfjs-dist/build/pdf.worker')) // Reducing size
gulp.task("build-bundle", function() {
return browserify("main.js", { output: TMP_FILE_PREFIX + "main.tmp" })
.ignore(require.resolve("pdfjs-dist/build/pdf.worker")) // Reducing size
.bundle()
.pipe(source(TMP_FILE_PREFIX + 'main.tmp'))
.pipe(source(TMP_FILE_PREFIX + "main.tmp"))
.pipe(streamify(uglify()))
.pipe(rename('main.bundle.js'))
.pipe(rename("main.bundle.js"))
.pipe(gulp.dest(OUTPUT_PATH));
});

gulp.task('build-worker', function() {
gulp.task("build-worker", function() {
// We can create our own viewer (see worker.js) or use already defined one.
var workerSrc = require.resolve('pdfjs-dist/build/pdf.worker.entry');
return browserify(workerSrc, { output: TMP_FILE_PREFIX + 'worker.tmp', })
var workerSrc = require.resolve("pdfjs-dist/build/pdf.worker.entry");
return browserify(workerSrc, { output: TMP_FILE_PREFIX + "worker.tmp" })
.bundle()
.pipe(source(TMP_FILE_PREFIX + 'worker.tmp'))
.pipe(streamify(uglify({
compress: {
sequences: false, // Chrome has issue with the generated code if true
},
})))
.pipe(rename('pdf.worker.bundle.js'))
.pipe(source(TMP_FILE_PREFIX + "worker.tmp"))
.pipe(
streamify(
uglify({
compress: {
sequences: false, // Chrome has issue with the generated code if true
},
})
)
)
.pipe(rename("pdf.worker.bundle.js"))
.pipe(gulp.dest(OUTPUT_PATH));
});

gulp.task('build', gulp.series('build-bundle', 'build-worker'));
gulp.task("build", gulp.series("build-bundle", "build-worker"));
40 changes: 21 additions & 19 deletions examples/browserify/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@

// Hello world example for browserify.

var pdfjsLib = require('pdfjs-dist');
var pdfjsLib = require("pdfjs-dist");

var pdfPath = '../learning/helloworld.pdf';
var pdfPath = "../learning/helloworld.pdf";

// Setting worker path to worker bundle.
pdfjsLib.GlobalWorkerOptions.workerSrc =
'../../build/browserify/pdf.worker.bundle.js';
"../../build/browserify/pdf.worker.bundle.js";

// Loading a document.
var loadingTask = pdfjsLib.getDocument(pdfPath);
loadingTask.promise.then(function (pdfDocument) {
// Request a first page
return pdfDocument.getPage(1).then(function (pdfPage) {
// Display page on the existing canvas with 100% scale.
var viewport = pdfPage.getViewport({ scale: 1.0, });
var canvas = document.getElementById('theCanvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
var ctx = canvas.getContext('2d');
var renderTask = pdfPage.render({
canvasContext: ctx,
viewport: viewport,
loadingTask.promise
.then(function(pdfDocument) {
// Request a first page
return pdfDocument.getPage(1).then(function(pdfPage) {
// Display page on the existing canvas with 100% scale.
var viewport = pdfPage.getViewport({ scale: 1.0 });
var canvas = document.getElementById("theCanvas");
canvas.width = viewport.width;
canvas.height = viewport.height;
var ctx = canvas.getContext("2d");
var renderTask = pdfPage.render({
canvasContext: ctx,
viewport: viewport,
});
return renderTask.promise;
});
return renderTask.promise;
})
.catch(function(reason) {
console.error("Error: " + reason);
});
}).catch(function (reason) {
console.error('Error: ' + reason);
});
6 changes: 4 additions & 2 deletions examples/browserify/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@

// Hello world example for browserify: worker bundle.

(typeof window !== 'undefined' ? window : {}).pdfjsWorker =
require('pdfjs-dist/build/pdf.worker');
(typeof window !== "undefined"
? window
: {}
).pdfjsWorker = require("pdfjs-dist/build/pdf.worker");
19 changes: 10 additions & 9 deletions examples/components/pageviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@
* limitations under the License.
*/

'use strict';
"use strict";

if (!pdfjsLib.getDocument || !pdfjsViewer.PDFPageView) {
alert('Please build the pdfjs-dist library using\n' +
' `gulp dist-install`');
alert(
"Please build the pdfjs-dist library using\n" + " `gulp dist-install`"
);
}

// The workerSrc property shall be specified.
//
pdfjsLib.GlobalWorkerOptions.workerSrc =
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
"../../node_modules/pdfjs-dist/build/pdf.worker.js";

// Some PDFs need external cmaps.
//
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
var CMAP_PACKED = true;

var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
var PAGE_TO_VIEW = 1;
var SCALE = 1.0;

var container = document.getElementById('pageContainer');
var container = document.getElementById("pageContainer");

// Loading document.
var loadingTask = pdfjsLib.getDocument({
Expand All @@ -44,13 +45,13 @@ var loadingTask = pdfjsLib.getDocument({
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, retrieving the page.
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
return pdfDocument.getPage(PAGE_TO_VIEW).then(function(pdfPage) {
// Creating the page view with default parameters.
var pdfPageView = new pdfjsViewer.PDFPageView({
container: container,
id: PAGE_TO_VIEW,
scale: SCALE,
defaultViewport: pdfPage.getViewport({ scale: SCALE, }),
defaultViewport: pdfPage.getViewport({ scale: SCALE }),
// We can enable text/annotations layers, if needed
textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(),
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
Expand Down
Loading

0 comments on commit 281fd40

Please sign in to comment.