mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
Enable auto-formatting of the entire code-base using Prettier (issue 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.)
This commit is contained in:
parent
8ec1dfde49
commit
de36b2aaba
205 changed files with 40024 additions and 31859 deletions
153
external/builder/builder.js
vendored
153
external/builder/builder.js
vendored
|
@ -1,8 +1,8 @@
|
|||
'use strict';
|
||||
"use strict";
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
vm = require('vm');
|
||||
var fs = require("fs"),
|
||||
path = require("path"),
|
||||
vm = require("vm");
|
||||
|
||||
/**
|
||||
* A simple preprocessor that is based on the Firefox preprocessor
|
||||
|
@ -34,9 +34,12 @@ var fs = require('fs'),
|
|||
*/
|
||||
function preprocess(inFilename, outFilename, defines) {
|
||||
// TODO make this really read line by line.
|
||||
var lines = fs.readFileSync(inFilename).toString().split('\n');
|
||||
var lines = fs
|
||||
.readFileSync(inFilename)
|
||||
.toString()
|
||||
.split("\n");
|
||||
var totalLines = lines.length;
|
||||
var out = '';
|
||||
var out = "";
|
||||
var i = 0;
|
||||
function readLine() {
|
||||
if (i < totalLines) {
|
||||
|
@ -44,19 +47,29 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
var writeLine = (typeof outFilename === 'function' ? outFilename :
|
||||
function(line) {
|
||||
out += line + '\n';
|
||||
});
|
||||
var writeLine =
|
||||
typeof outFilename === "function"
|
||||
? outFilename
|
||||
: function(line) {
|
||||
out += line + "\n";
|
||||
};
|
||||
function evaluateCondition(code) {
|
||||
if (!code || !code.trim()) {
|
||||
throw new Error('No JavaScript expression given at ' + loc());
|
||||
throw new Error("No JavaScript expression given at " + loc());
|
||||
}
|
||||
try {
|
||||
return vm.runInNewContext(code, defines, { displayErrors: false, });
|
||||
return vm.runInNewContext(code, defines, { displayErrors: false });
|
||||
} catch (e) {
|
||||
throw new Error('Could not evaluate "' + code + '" at ' + loc() + '\n' +
|
||||
e.name + ': ' + e.message);
|
||||
throw new Error(
|
||||
'Could not evaluate "' +
|
||||
code +
|
||||
'" at ' +
|
||||
loc() +
|
||||
"\n" +
|
||||
e.name +
|
||||
": " +
|
||||
e.message
|
||||
);
|
||||
}
|
||||
}
|
||||
function include(file) {
|
||||
|
@ -64,15 +77,18 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
var dir = path.dirname(realPath);
|
||||
try {
|
||||
var fullpath;
|
||||
if (file.indexOf('$ROOT/') === 0) {
|
||||
fullpath = path.join(__dirname, '../..',
|
||||
file.substring('$ROOT/'.length));
|
||||
if (file.indexOf("$ROOT/") === 0) {
|
||||
fullpath = path.join(
|
||||
__dirname,
|
||||
"../..",
|
||||
file.substring("$ROOT/".length)
|
||||
);
|
||||
} else {
|
||||
fullpath = path.join(dir, file);
|
||||
}
|
||||
preprocess(fullpath, writeLine, defines);
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
if (e.code === "ENOENT") {
|
||||
throw new Error('Failed to include "' + file + '" at ' + loc());
|
||||
}
|
||||
throw e; // Some other error
|
||||
|
@ -84,7 +100,7 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
if (variable in defines) {
|
||||
return defines[variable];
|
||||
}
|
||||
return '';
|
||||
return "";
|
||||
});
|
||||
writeLine(line);
|
||||
}
|
||||
|
@ -104,104 +120,107 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
var line;
|
||||
var state = STATE_NONE;
|
||||
var stack = [];
|
||||
var control = // eslint-disable-next-line max-len
|
||||
/^(?:\/\/|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:-->)?$)?/;
|
||||
var control = /^(?:\/\/|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:-->)?$)?/; // eslint-disable-next-line max-len
|
||||
var lineNumber = 0;
|
||||
var loc = function() {
|
||||
return fs.realpathSync(inFilename) + ':' + lineNumber;
|
||||
return fs.realpathSync(inFilename) + ":" + lineNumber;
|
||||
};
|
||||
while ((line = readLine()) !== null) {
|
||||
++lineNumber;
|
||||
var m = control.exec(line);
|
||||
if (m) {
|
||||
switch (m[1]) {
|
||||
case 'if':
|
||||
case "if":
|
||||
stack.push(state);
|
||||
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
||||
break;
|
||||
case 'elif':
|
||||
case "elif":
|
||||
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
||||
state = STATE_ELSE_FALSE;
|
||||
} else if (state === STATE_IF_FALSE) {
|
||||
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
||||
} else if (state === STATE_ELSE_TRUE) {
|
||||
throw new Error('Found #elif after #else at ' + loc());
|
||||
throw new Error("Found #elif after #else at " + loc());
|
||||
} else {
|
||||
throw new Error('Found #elif without matching #if at ' + loc());
|
||||
throw new Error("Found #elif without matching #if at " + loc());
|
||||
}
|
||||
break;
|
||||
case 'else':
|
||||
case "else":
|
||||
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
||||
state = STATE_ELSE_FALSE;
|
||||
} else if (state === STATE_IF_FALSE) {
|
||||
state = STATE_ELSE_TRUE;
|
||||
} else {
|
||||
throw new Error('Found #else without matching #if at ' + loc());
|
||||
throw new Error("Found #else without matching #if at " + loc());
|
||||
}
|
||||
break;
|
||||
case 'endif':
|
||||
case "endif":
|
||||
if (state === STATE_NONE) {
|
||||
throw new Error('Found #endif without #if at ' + loc());
|
||||
throw new Error("Found #endif without #if at " + loc());
|
||||
}
|
||||
state = stack.pop();
|
||||
break;
|
||||
case 'expand':
|
||||
case "expand":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
expand(m[2]);
|
||||
}
|
||||
break;
|
||||
case 'include':
|
||||
case "include":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
include(m[2]);
|
||||
}
|
||||
break;
|
||||
case 'error':
|
||||
case "error":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
throw new Error('Found #error ' + m[2] + ' at ' + loc());
|
||||
throw new Error("Found #error " + m[2] + " at " + loc());
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (state === STATE_NONE) {
|
||||
writeLine(line);
|
||||
} else if ((state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
|
||||
!stack.includes(STATE_IF_FALSE) &&
|
||||
!stack.includes(STATE_ELSE_FALSE)) {
|
||||
writeLine(line.replace(/^\/\/|^<!--|-->$/g, ' '));
|
||||
} else if (
|
||||
(state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
|
||||
!stack.includes(STATE_IF_FALSE) &&
|
||||
!stack.includes(STATE_ELSE_FALSE)
|
||||
) {
|
||||
writeLine(line.replace(/^\/\/|^<!--|-->$/g, " "));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (state !== STATE_NONE || stack.length !== 0) {
|
||||
throw new Error('Missing #endif in preprocessor for ' +
|
||||
fs.realpathSync(inFilename));
|
||||
throw new Error(
|
||||
"Missing #endif in preprocessor for " + fs.realpathSync(inFilename)
|
||||
);
|
||||
}
|
||||
if (typeof outFilename !== 'function') {
|
||||
if (typeof outFilename !== "function") {
|
||||
fs.writeFileSync(outFilename, out);
|
||||
}
|
||||
}
|
||||
exports.preprocess = preprocess;
|
||||
|
||||
var deprecatedInMozcentral = new RegExp('(^|\\W)(' + [
|
||||
'-moz-box-sizing',
|
||||
'-moz-grab',
|
||||
'-moz-grabbing'
|
||||
].join('|') + ')');
|
||||
var deprecatedInMozcentral = new RegExp(
|
||||
"(^|\\W)(" + ["-moz-box-sizing", "-moz-grab", "-moz-grabbing"].join("|") + ")"
|
||||
);
|
||||
|
||||
function preprocessCSS(mode, source, destination) {
|
||||
function hasPrefixedFirefox(line) {
|
||||
return (/(^|\W)-(ms|o|webkit)-\w/.test(line));
|
||||
return /(^|\W)-(ms|o|webkit)-\w/.test(line);
|
||||
}
|
||||
|
||||
function hasPrefixedMozcentral(line) {
|
||||
return (/(^|\W)-(ms|o|webkit)-\w/.test(line) ||
|
||||
deprecatedInMozcentral.test(line));
|
||||
return (
|
||||
/(^|\W)-(ms|o|webkit)-\w/.test(line) || deprecatedInMozcentral.test(line)
|
||||
);
|
||||
}
|
||||
|
||||
function expandImports(content, baseUrl) {
|
||||
return content.replace(/^\s*@import\s+url\(([^\)]+)\);\s*$/gm,
|
||||
function(all, url) {
|
||||
return content.replace(/^\s*@import\s+url\(([^\)]+)\);\s*$/gm, function(
|
||||
all,
|
||||
url
|
||||
) {
|
||||
var file = path.join(path.dirname(baseUrl), url);
|
||||
var imported = fs.readFileSync(file, 'utf8').toString();
|
||||
var imported = fs.readFileSync(file, "utf8").toString();
|
||||
return expandImports(imported, file);
|
||||
});
|
||||
}
|
||||
|
@ -221,9 +240,9 @@ function preprocessCSS(mode, source, destination) {
|
|||
while (j < lines.length && bracketLevel > 0) {
|
||||
var checkBracket = /([{}])\s*$/.exec(lines[j]);
|
||||
if (checkBracket) {
|
||||
if (checkBracket[1] === '{') {
|
||||
if (checkBracket[1] === "{") {
|
||||
bracketLevel++;
|
||||
} else if (!lines[j].includes('{')) {
|
||||
} else if (!lines[j].includes("{")) {
|
||||
bracketLevel--;
|
||||
}
|
||||
}
|
||||
|
@ -236,30 +255,34 @@ function preprocessCSS(mode, source, destination) {
|
|||
// multiline? skipping until next directive or bracket
|
||||
do {
|
||||
lines.splice(i, 1);
|
||||
} while (i < lines.length &&
|
||||
!/\}\s*$/.test(lines[i]) &&
|
||||
!lines[i].includes(':'));
|
||||
} while (
|
||||
i < lines.length &&
|
||||
!/\}\s*$/.test(lines[i]) &&
|
||||
!lines[i].includes(":")
|
||||
);
|
||||
if (i < lines.length && /\S\s*}\s*$/.test(lines[i])) {
|
||||
lines[i] = lines[i].substring(lines[i].indexOf('}'));
|
||||
lines[i] = lines[i].substring(lines[i].indexOf("}"));
|
||||
}
|
||||
}
|
||||
// collapse whitespaces
|
||||
while (lines[i] === '' && lines[i - 1] === '') {
|
||||
while (lines[i] === "" && lines[i - 1] === "") {
|
||||
lines.splice(i, 1);
|
||||
}
|
||||
}
|
||||
return lines.join('\n');
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
if (!mode) {
|
||||
throw new Error('Invalid CSS preprocessor mode');
|
||||
throw new Error("Invalid CSS preprocessor mode");
|
||||
}
|
||||
|
||||
var content = fs.readFileSync(source, 'utf8').toString();
|
||||
var content = fs.readFileSync(source, "utf8").toString();
|
||||
content = expandImports(content, source);
|
||||
if (mode === 'mozcentral' || mode === 'firefox') {
|
||||
content = removePrefixed(content, mode === 'mozcentral' ?
|
||||
hasPrefixedMozcentral : hasPrefixedFirefox);
|
||||
if (mode === "mozcentral" || mode === "firefox") {
|
||||
content = removePrefixed(
|
||||
content,
|
||||
mode === "mozcentral" ? hasPrefixedMozcentral : hasPrefixedFirefox
|
||||
);
|
||||
}
|
||||
fs.writeFileSync(destination, content);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue