1
0
Fork 0
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:
Jonas Jenwald 2019-12-25 15:59:37 +01:00
parent 8ec1dfde49
commit de36b2aaba
205 changed files with 40024 additions and 31859 deletions

View file

@ -32,7 +32,7 @@ const XMLParserErrorCode = {
function isWhitespace(s, index) {
const ch = s[index];
return ch === ' ' || ch === '\n' || ch === '\r' || ch === '\t';
return ch === " " || ch === "\n" || ch === "\r" || ch === "\t";
}
function isWhitespaceString(s) {
@ -47,27 +47,29 @@ function isWhitespaceString(s) {
class XMLParserBase {
_resolveEntities(s) {
return s.replace(/&([^;]+);/g, (all, entity) => {
if (entity.substring(0, 2) === '#x') {
if (entity.substring(0, 2) === "#x") {
return String.fromCharCode(parseInt(entity.substring(2), 16));
} else if (entity.substring(0, 1) === '#') {
} else if (entity.substring(0, 1) === "#") {
return String.fromCharCode(parseInt(entity.substring(1), 10));
}
switch (entity) {
case 'lt':
return '<';
case 'gt':
return '>';
case 'amp':
return '&';
case 'quot':
return '\"';
case "lt":
return "<";
case "gt":
return ">";
case "amp":
return "&";
case "quot":
return '"';
}
return this.onResolveEntity(entity);
});
}
_parseContent(s, start) {
let pos = start, name, attributes = [];
let pos = start,
name,
attributes = [];
function skipWs() {
while (pos < s.length && isWhitespace(s, pos)) {
@ -75,28 +77,37 @@ class XMLParserBase {
}
}
while (pos < s.length && !isWhitespace(s, pos) &&
s[pos] !== '>' && s[pos] !== '/') {
while (
pos < s.length &&
!isWhitespace(s, pos) &&
s[pos] !== ">" &&
s[pos] !== "/"
) {
++pos;
}
name = s.substring(start, pos);
skipWs();
while (pos < s.length && s[pos] !== '>' &&
s[pos] !== '/' && s[pos] !== '?') {
while (
pos < s.length &&
s[pos] !== ">" &&
s[pos] !== "/" &&
s[pos] !== "?"
) {
skipWs();
let attrName = '', attrValue = '';
while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '=') {
let attrName = "",
attrValue = "";
while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== "=") {
attrName += s[pos];
++pos;
}
skipWs();
if (s[pos] !== '=') {
if (s[pos] !== "=") {
return null;
}
++pos;
skipWs();
const attrEndChar = s[pos];
if (attrEndChar !== '\"' && attrEndChar !== '\'') {
if (attrEndChar !== '"' && attrEndChar !== "'") {
return null;
}
const attrEndIndex = s.indexOf(attrEndChar, ++pos);
@ -119,7 +130,9 @@ class XMLParserBase {
}
_parseProcessingInstruction(s, start) {
let pos = start, name, value;
let pos = start,
name,
value;
function skipWs() {
while (pos < s.length && isWhitespace(s, pos)) {
@ -127,14 +140,18 @@ class XMLParserBase {
}
}
while (pos < s.length && !isWhitespace(s, pos) &&
s[pos] !== '>' && s[pos] !== '/') {
while (
pos < s.length &&
!isWhitespace(s, pos) &&
s[pos] !== ">" &&
s[pos] !== "/"
) {
++pos;
}
name = s.substring(start, pos);
skipWs();
const attrStart = pos;
while (pos < s.length && (s[pos] !== '?' || s[pos + 1] !== '>')) {
while (pos < s.length && (s[pos] !== "?" || s[pos + 1] !== ">")) {
++pos;
}
value = s.substring(attrStart, pos);
@ -150,14 +167,14 @@ class XMLParserBase {
while (i < s.length) {
const ch = s[i];
let j = i;
if (ch === '<') {
if (ch === "<") {
++j;
const ch2 = s[j];
let q;
switch (ch2) {
case '/':
case "/":
++j;
q = s.indexOf('>', j);
q = s.indexOf(">", j);
if (q < 0) {
this.onError(XMLParserErrorCode.UnterminatedElement);
return;
@ -165,52 +182,55 @@ class XMLParserBase {
this.onEndElement(s.substring(j, q));
j = q + 1;
break;
case '?':
case "?":
++j;
const pi = this._parseProcessingInstruction(s, j);
if (s.substring(j + pi.parsed, j + pi.parsed + 2) !== '?>') {
if (s.substring(j + pi.parsed, j + pi.parsed + 2) !== "?>") {
this.onError(XMLParserErrorCode.UnterminatedXmlDeclaration);
return;
}
this.onPi(pi.name, pi.value);
j += pi.parsed + 2;
break;
case '!':
if (s.substring(j + 1, j + 3) === '--') {
q = s.indexOf('-->', j + 3);
case "!":
if (s.substring(j + 1, j + 3) === "--") {
q = s.indexOf("-->", j + 3);
if (q < 0) {
this.onError(XMLParserErrorCode.UnterminatedComment);
return;
}
this.onComment(s.substring(j + 3, q));
j = q + 3;
} else if (s.substring(j + 1, j + 8) === '[CDATA[') {
q = s.indexOf(']]>', j + 8);
} else if (s.substring(j + 1, j + 8) === "[CDATA[") {
q = s.indexOf("]]>", j + 8);
if (q < 0) {
this.onError(XMLParserErrorCode.UnterminatedCdat);
return;
}
this.onCdata(s.substring(j + 8, q));
j = q + 3;
} else if (s.substring(j + 1, j + 8) === 'DOCTYPE') {
const q2 = s.indexOf('[', j + 8);
} else if (s.substring(j + 1, j + 8) === "DOCTYPE") {
const q2 = s.indexOf("[", j + 8);
let complexDoctype = false;
q = s.indexOf('>', j + 8);
q = s.indexOf(">", j + 8);
if (q < 0) {
this.onError(XMLParserErrorCode.UnterminatedDoctypeDeclaration);
return;
}
if (q2 > 0 && q > q2) {
q = s.indexOf(']>', j + 8);
q = s.indexOf("]>", j + 8);
if (q < 0) {
this.onError(
XMLParserErrorCode.UnterminatedDoctypeDeclaration);
XMLParserErrorCode.UnterminatedDoctypeDeclaration
);
return;
}
complexDoctype = true;
}
const doctypeContent =
s.substring(j + 8, q + (complexDoctype ? 1 : 0));
const doctypeContent = s.substring(
j + 8,
q + (complexDoctype ? 1 : 0)
);
this.onDoctype(doctypeContent);
j = q + (complexDoctype ? 2 : 1);
} else {
@ -225,11 +245,13 @@ class XMLParserBase {
return;
}
let isClosed = false;
if (s.substring(j + content.parsed,
j + content.parsed + 2) === '/>') {
if (
s.substring(j + content.parsed, j + content.parsed + 2) === "/>"
) {
isClosed = true;
} else if (s.substring(j + content.parsed,
j + content.parsed + 1) !== '>') {
} else if (
s.substring(j + content.parsed, j + content.parsed + 1) !== ">"
) {
this.onError(XMLParserErrorCode.UnterminatedElement);
return;
}
@ -238,7 +260,7 @@ class XMLParserBase {
break;
}
} else {
while (j < s.length && s[j] !== '<') {
while (j < s.length && s[j] !== "<") {
j++;
}
const text = s.substring(i, j);
@ -252,21 +274,21 @@ class XMLParserBase {
return `&${name};`;
}
onPi(name, value) { }
onPi(name, value) {}
onComment(text) { }
onComment(text) {}
onCdata(text) { }
onCdata(text) {}
onDoctype(doctypeContent) { }
onDoctype(doctypeContent) {}
onText(text) { }
onText(text) {}
onBeginElement(name, attributes, isEmpty) { }
onBeginElement(name, attributes, isEmpty) {}
onEndElement(name) { }
onEndElement(name) {}
onError(code) { }
onError(code) {}
}
class SimpleDOMNode {
@ -274,7 +296,7 @@ class SimpleDOMNode {
this.nodeName = nodeName;
this.nodeValue = nodeValue;
Object.defineProperty(this, 'parentNode', { value: null, writable: true, });
Object.defineProperty(this, "parentNode", { value: null, writable: true });
}
get firstChild() {
@ -295,11 +317,13 @@ class SimpleDOMNode {
get textContent() {
if (!this.childNodes) {
return this.nodeValue || '';
return this.nodeValue || "";
}
return this.childNodes.map(function(child) {
return child.textContent;
}).join('');
return this.childNodes
.map(function(child) {
return child.textContent;
})
.join("");
}
hasChildNodes() {
@ -331,13 +355,13 @@ class SimpleXMLParser extends XMLParserBase {
if (!documentElement) {
return undefined; // Return undefined if no root was found.
}
return { documentElement, };
return { documentElement };
}
onResolveEntity(name) {
switch (name) {
case 'apos':
return '\'';
case "apos":
return "'";
}
return super.onResolveEntity(name);
}
@ -346,12 +370,12 @@ class SimpleXMLParser extends XMLParserBase {
if (isWhitespaceString(text)) {
return;
}
const node = new SimpleDOMNode('#text', text);
const node = new SimpleDOMNode("#text", text);
this._currentFragment.push(node);
}
onCdata(text) {
const node = new SimpleDOMNode('#text', text);
const node = new SimpleDOMNode("#text", text);
this._currentFragment.push(node);
}
@ -382,6 +406,4 @@ class SimpleXMLParser extends XMLParserBase {
}
}
export {
SimpleXMLParser,
};
export { SimpleXMLParser };