2023-07-08 23:07:54 +09:00
|
|
|
import * as acorn from "acorn";
|
|
|
|
import escodegen from "@javascript-obfuscator/escodegen";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import vm from "vm";
|
2016-05-11 08:05:29 +09:00
|
|
|
|
2021-03-14 01:37:27 +09:00
|
|
|
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
|
|
|
|
const ROOT_PREFIX = "$ROOT/";
|
2021-10-17 20:30:09 +09:00
|
|
|
const ACORN_ECMA_VERSION = 2022;
|
2016-05-11 08:05:29 +09:00
|
|
|
|
|
|
|
function isLiteral(obj, value) {
|
|
|
|
return obj.type === "Literal" && obj.value === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPDFJSPreprocessor(obj) {
|
|
|
|
return obj.type === "Identifier" && obj.name === PDFJS_PREPROCESSOR_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evalWithDefines(code, defines, loc) {
|
|
|
|
if (!code || !code.trim()) {
|
|
|
|
throw new Error("No JavaScript expression given");
|
|
|
|
}
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return vm.runInNewContext(code, defines, { displayErrors: false });
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function handlePreprocessorAction(ctx, actionName, args, loc) {
|
|
|
|
try {
|
2021-03-14 01:37:27 +09:00
|
|
|
let arg;
|
2016-05-11 08:05:29 +09:00
|
|
|
switch (actionName) {
|
|
|
|
case "test":
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") {
|
|
|
|
throw new Error("No code for testing is given");
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
const isTrue = !!evalWithDefines(arg.value, ctx.defines);
|
2021-01-23 01:38:26 +09:00
|
|
|
return { type: "Literal", value: isTrue, loc };
|
2016-05-11 08:05:29 +09:00
|
|
|
case "eval":
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") {
|
|
|
|
throw new Error("No code for eval is given");
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
const result = evalWithDefines(arg.value, ctx.defines);
|
2016-05-11 08:05:29 +09:00
|
|
|
if (
|
|
|
|
typeof result === "boolean" ||
|
|
|
|
typeof result === "string" ||
|
|
|
|
typeof result === "number"
|
|
|
|
) {
|
2021-01-23 01:38:26 +09:00
|
|
|
return { type: "Literal", value: result, loc };
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
if (typeof result === "object") {
|
2020-10-06 20:34:45 +09:00
|
|
|
const parsedObj = acorn.parse("(" + JSON.stringify(result) + ")", {
|
|
|
|
ecmaVersion: ACORN_ECMA_VERSION,
|
|
|
|
});
|
2016-05-11 08:05:29 +09:00
|
|
|
parsedObj.body[0].expression.loc = loc;
|
|
|
|
return parsedObj.body[0].expression;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "json":
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") {
|
|
|
|
throw new Error("Path to JSON is not provided");
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
let jsonPath = arg.value;
|
2016-05-11 08:05:29 +09:00
|
|
|
if (jsonPath.indexOf(ROOT_PREFIX) === 0) {
|
|
|
|
jsonPath = path.join(
|
|
|
|
ctx.rootPath,
|
|
|
|
jsonPath.substring(ROOT_PREFIX.length)
|
|
|
|
);
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
const jsonContent = fs.readFileSync(jsonPath).toString();
|
2020-10-06 20:34:45 +09:00
|
|
|
const parsedJSON = acorn.parse("(" + jsonContent + ")", {
|
|
|
|
ecmaVersion: ACORN_ECMA_VERSION,
|
|
|
|
});
|
2016-05-11 08:05:29 +09:00
|
|
|
parsedJSON.body[0].expression.loc = loc;
|
|
|
|
return parsedJSON.body[0].expression;
|
|
|
|
}
|
|
|
|
throw new Error("Unsupported action");
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(
|
|
|
|
"Could not process " +
|
|
|
|
PDFJS_PREPROCESSOR_NAME +
|
|
|
|
"." +
|
|
|
|
actionName +
|
|
|
|
" at " +
|
|
|
|
JSON.stringify(loc) +
|
|
|
|
"\n" +
|
|
|
|
e.name +
|
|
|
|
": " +
|
|
|
|
e.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postprocessNode(ctx, node) {
|
|
|
|
switch (node.type) {
|
2017-05-31 10:38:22 +09:00
|
|
|
case "ExportNamedDeclaration":
|
|
|
|
case "ImportDeclaration":
|
|
|
|
if (
|
|
|
|
node.source &&
|
|
|
|
node.source.type === "Literal" &&
|
|
|
|
ctx.map &&
|
|
|
|
ctx.map[node.source.value]
|
|
|
|
) {
|
2021-03-14 01:37:27 +09:00
|
|
|
const newValue = ctx.map[node.source.value];
|
2017-05-31 10:38:22 +09:00
|
|
|
node.source.value = node.source.raw = newValue;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
case "IfStatement":
|
|
|
|
if (isLiteral(node.test, true)) {
|
|
|
|
// if (true) stmt1; => stmt1
|
|
|
|
return node.consequent;
|
|
|
|
} else if (isLiteral(node.test, false)) {
|
|
|
|
// if (false) stmt1; else stmt2; => stmt2
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return node.alternate || { type: "EmptyStatement", loc: node.loc };
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ConditionalExpression":
|
|
|
|
if (isLiteral(node.test, true)) {
|
|
|
|
// true ? stmt1 : stmt2 => stmt1
|
|
|
|
return node.consequent;
|
|
|
|
} else if (isLiteral(node.test, false)) {
|
|
|
|
// false ? stmt1 : stmt2 => stmt2
|
|
|
|
return node.alternate;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "UnaryExpression":
|
|
|
|
if (node.operator === "typeof" && isPDFJSPreprocessor(node.argument)) {
|
|
|
|
// typeof PDFJSDev => 'object'
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return { type: "Literal", value: "object", loc: node.loc };
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
node.operator === "!" &&
|
|
|
|
node.argument.type === "Literal" &&
|
|
|
|
typeof node.argument.value === "boolean"
|
|
|
|
) {
|
|
|
|
// !true => false, !false => true
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return { type: "Literal", value: !node.argument.value, loc: node.loc };
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "LogicalExpression":
|
|
|
|
switch (node.operator) {
|
|
|
|
case "&&":
|
|
|
|
if (isLiteral(node.left, true)) {
|
|
|
|
return node.right;
|
|
|
|
}
|
|
|
|
if (isLiteral(node.left, false)) {
|
|
|
|
return node.left;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "||":
|
|
|
|
if (isLiteral(node.left, true)) {
|
|
|
|
return node.left;
|
|
|
|
}
|
|
|
|
if (isLiteral(node.left, false)) {
|
|
|
|
return node.right;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "BinaryExpression":
|
|
|
|
switch (node.operator) {
|
|
|
|
case "==":
|
|
|
|
case "===":
|
|
|
|
case "!=":
|
|
|
|
case "!==":
|
|
|
|
if (
|
|
|
|
node.left.type === "Literal" &&
|
|
|
|
node.right.type === "Literal" &&
|
|
|
|
typeof node.left.value === typeof node.right.value
|
|
|
|
) {
|
|
|
|
// folding two literals == and != check
|
|
|
|
switch (typeof node.left.value) {
|
|
|
|
case "string":
|
|
|
|
case "boolean":
|
|
|
|
case "number":
|
2021-03-14 01:40:44 +09:00
|
|
|
const equal = node.left.value === node.right.value;
|
2016-05-11 08:05:29 +09:00
|
|
|
return {
|
|
|
|
type: "Literal",
|
|
|
|
value: (node.operator[0] === "=") === equal,
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
loc: node.loc,
|
2016-05-11 08:05:29 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "CallExpression":
|
|
|
|
if (
|
|
|
|
node.callee.type === "MemberExpression" &&
|
|
|
|
isPDFJSPreprocessor(node.callee.object) &&
|
|
|
|
node.callee.property.type === "Identifier"
|
|
|
|
) {
|
2018-04-02 06:20:41 +09:00
|
|
|
// PDFJSDev.xxxx(arg1, arg2, ...) => transform
|
2021-03-14 01:37:27 +09:00
|
|
|
const action = node.callee.property.name;
|
2016-05-11 08:05:29 +09:00
|
|
|
return handlePreprocessorAction(ctx, action, node.arguments, node.loc);
|
|
|
|
}
|
2017-05-31 10:38:22 +09:00
|
|
|
// require('string')
|
|
|
|
if (
|
|
|
|
node.callee.type === "Identifier" &&
|
|
|
|
node.callee.name === "require" &&
|
|
|
|
node.arguments.length === 1 &&
|
|
|
|
node.arguments[0].type === "Literal" &&
|
|
|
|
ctx.map &&
|
|
|
|
ctx.map[node.arguments[0].value]
|
|
|
|
) {
|
2021-03-14 01:37:27 +09:00
|
|
|
const requireName = node.arguments[0];
|
2017-05-31 10:38:22 +09:00
|
|
|
requireName.value = requireName.raw = ctx.map[requireName.value];
|
|
|
|
}
|
2016-05-11 08:05:29 +09:00
|
|
|
break;
|
|
|
|
case "BlockStatement":
|
2021-03-14 01:40:44 +09:00
|
|
|
let subExpressionIndex = 0;
|
2016-05-11 08:05:29 +09:00
|
|
|
while (subExpressionIndex < node.body.length) {
|
2017-01-10 04:15:09 +09:00
|
|
|
switch (node.body[subExpressionIndex].type) {
|
|
|
|
case "EmptyStatement":
|
|
|
|
// Removing empty statements from the blocks.
|
|
|
|
node.body.splice(subExpressionIndex, 1);
|
|
|
|
continue;
|
|
|
|
case "BlockStatement":
|
|
|
|
// Block statements inside a block are moved to the parent one.
|
2021-03-14 01:40:44 +09:00
|
|
|
const subChildren = node.body[subExpressionIndex].body;
|
2022-06-19 19:26:48 +09:00
|
|
|
Array.prototype.splice.apply(node.body, [
|
|
|
|
subExpressionIndex,
|
|
|
|
1,
|
|
|
|
...subChildren,
|
|
|
|
]);
|
2017-01-10 04:15:09 +09:00
|
|
|
subExpressionIndex += Math.max(subChildren.length - 1, 0);
|
|
|
|
continue;
|
|
|
|
case "ReturnStatement":
|
|
|
|
case "ThrowStatement":
|
|
|
|
// Removing dead code after return or throw.
|
|
|
|
node.body.splice(
|
|
|
|
subExpressionIndex + 1,
|
|
|
|
node.body.length - subExpressionIndex - 1
|
|
|
|
);
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
subExpressionIndex++;
|
|
|
|
}
|
|
|
|
break;
|
2017-01-10 04:15:09 +09:00
|
|
|
case "FunctionDeclaration":
|
|
|
|
case "FunctionExpression":
|
2021-03-14 01:40:44 +09:00
|
|
|
const block = node.body;
|
2017-01-10 04:15:09 +09:00
|
|
|
if (
|
|
|
|
block.body.length > 0 &&
|
2023-04-26 00:19:59 +09:00
|
|
|
block.body.at(-1).type === "ReturnStatement" &&
|
|
|
|
!block.body.at(-1).argument
|
2017-01-10 04:15:09 +09:00
|
|
|
) {
|
|
|
|
// Function body ends with return without arg -- removing it.
|
|
|
|
block.body.pop();
|
|
|
|
}
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixComments(ctx, node) {
|
|
|
|
if (!ctx.saveComments) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Fixes double comments in the escodegen output.
|
|
|
|
delete node.trailingComments;
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
// Removes ESLint and other service comments.
|
2016-05-11 08:05:29 +09:00
|
|
|
if (node.leadingComments) {
|
2021-03-14 01:37:27 +09:00
|
|
|
const CopyrightRegExp = /\bcopyright\b/i;
|
|
|
|
const BlockCommentRegExp = /^\s*(globals|eslint|falls through)\b/;
|
|
|
|
const LineCommentRegExp = /^\s*eslint\b/;
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
|
2021-03-14 01:37:27 +09:00
|
|
|
let i = 0;
|
2016-05-11 08:05:29 +09:00
|
|
|
while (i < node.leadingComments.length) {
|
2021-03-14 01:37:27 +09:00
|
|
|
const type = node.leadingComments[i].type;
|
|
|
|
const value = node.leadingComments[i].value;
|
2016-11-12 06:05:17 +09:00
|
|
|
|
|
|
|
if (ctx.saveComments === "copyright") {
|
|
|
|
// Remove all comments, except Copyright notices and License headers.
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
if (!(type === "Block" && CopyrightRegExp.test(value))) {
|
2016-11-12 06:05:17 +09:00
|
|
|
node.leadingComments.splice(i, 1);
|
|
|
|
continue;
|
|
|
|
}
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
} else if (
|
|
|
|
(type === "Block" && BlockCommentRegExp.test(value)) ||
|
|
|
|
(type === "Line" && LineCommentRegExp.test(value))
|
|
|
|
) {
|
2016-05-11 08:05:29 +09:00
|
|
|
node.leadingComments.splice(i, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverseTree(ctx, node) {
|
|
|
|
// generic node processing
|
2021-03-14 01:37:27 +09:00
|
|
|
for (const i in node) {
|
2021-03-14 01:40:44 +09:00
|
|
|
const child = node[i];
|
2016-05-11 08:05:29 +09:00
|
|
|
if (typeof child === "object" && child !== null && child.type) {
|
2020-03-25 17:42:50 +09:00
|
|
|
const result = traverseTree(ctx, child);
|
2016-05-11 08:05:29 +09:00
|
|
|
if (result !== child) {
|
|
|
|
node[i] = result;
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(child)) {
|
2020-04-14 19:28:14 +09:00
|
|
|
child.forEach(function (childItem, index) {
|
2016-05-11 08:05:29 +09:00
|
|
|
if (
|
|
|
|
typeof childItem === "object" &&
|
|
|
|
childItem !== null &&
|
|
|
|
childItem.type
|
|
|
|
) {
|
2020-03-25 17:42:50 +09:00
|
|
|
const result = traverseTree(ctx, childItem);
|
2016-05-11 08:05:29 +09:00
|
|
|
if (result !== childItem) {
|
|
|
|
child[index] = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = postprocessNode(ctx, node) || node;
|
|
|
|
|
|
|
|
fixComments(ctx, node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
function preprocessPDFJSCode(ctx, code) {
|
2021-03-14 01:37:27 +09:00
|
|
|
const format = ctx.format || {
|
2016-05-11 08:05:29 +09:00
|
|
|
indent: {
|
2016-11-12 06:05:17 +09:00
|
|
|
style: " ",
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
},
|
2016-05-11 08:05:29 +09:00
|
|
|
};
|
2021-03-14 01:37:27 +09:00
|
|
|
const parseOptions = {
|
2020-10-06 20:34:45 +09:00
|
|
|
ecmaVersion: ACORN_ECMA_VERSION,
|
2017-03-06 23:42:48 +09:00
|
|
|
locations: true,
|
2017-05-04 02:17:18 +09:00
|
|
|
sourceFile: ctx.sourceFile,
|
2017-03-06 23:42:48 +09:00
|
|
|
sourceType: "module",
|
2016-05-11 08:05:29 +09:00
|
|
|
};
|
2021-03-14 01:37:27 +09:00
|
|
|
const codegenOptions = {
|
2021-01-23 01:38:26 +09:00
|
|
|
format,
|
|
|
|
parse(input) {
|
2020-10-06 20:34:45 +09:00
|
|
|
return acorn.parse(input, { ecmaVersion: ACORN_ECMA_VERSION });
|
|
|
|
},
|
2017-05-04 02:17:18 +09:00
|
|
|
sourceMap: ctx.sourceMap,
|
|
|
|
sourceMapWithCode: ctx.sourceMap,
|
2016-05-11 08:05:29 +09:00
|
|
|
};
|
2021-03-14 01:37:27 +09:00
|
|
|
const syntax = acorn.parse(code, parseOptions);
|
2016-05-11 08:05:29 +09:00
|
|
|
traverseTree(ctx, syntax);
|
|
|
|
return escodegen.generate(syntax, codegenOptions);
|
|
|
|
}
|
|
|
|
|
2023-07-08 23:07:54 +09:00
|
|
|
export { preprocessPDFJSCode };
|