1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Enable the ESLint no-shadow rule

This rule is *not* currently enabled in mozilla-central, but it appears commented out[1] in the ESLint definition file; see https://searchfox.org/mozilla-central/rev/c80fa7258c935223fe319c5345b58eae85d4c6ae/tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js#238-239

Unfortunately this rule is, for fairly obvious reasons, impossible to `--fix` automatically (even partially) and each case thus required careful manual analysis.
Hence this ESLint rule is, by some margin, probably the most difficult one that we've enabled thus far. However, using this rule does seem like a good idea in general since allowing variable shadowing could lead to subtle (and difficult to find) bugs or at the very least confusing code.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/no-shadow

---
[1] Most likely, a very large number of lint errors have prevented this rule from being enabled thus far.
This commit is contained in:
Jonas Jenwald 2020-03-25 09:42:50 +01:00
parent 475fa1f97f
commit 1d2f787d6a
12 changed files with 95 additions and 87 deletions

View file

@ -22,9 +22,9 @@ if (!fs.existsSync(file)) {
throw new Error(`PDF file does not exist '${file}'.`);
}
function calculateMD5(file, callback) {
function calculateMD5(pdfFile, callback) {
var hash = crypto.createHash("md5");
var stream = fs.createReadStream(file);
var stream = fs.createReadStream(pdfFile);
stream.on("data", function(data) {
hash.update(data);
});

View file

@ -228,10 +228,10 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
for (var i = 0, ii = data.length; i < ii; i++) {
images[i].src = data[i];
loadedPromises.push(
new Promise(function(resolve, reject) {
images[i].onload = resolve;
new Promise(function(resolveImage, rejectImage) {
images[i].onload = resolveImage;
images[i].onerror = function(e) {
reject(new Error("Error loading image " + e));
rejectImage(new Error("Error loading image " + e));
};
})
);

View file

@ -24,10 +24,10 @@ var ttxResourcesHome = path.join(__dirname, "..", "ttx");
var nextTTXTaskId = Date.now();
function runTtx(ttxResourcesHome, fontPath, registerOnCancel, callback) {
fs.realpath(ttxResourcesHome, function(err, ttxResourcesHome) {
var fontToolsHome = path.join(ttxResourcesHome, "fonttools-code");
fs.realpath(fontPath, function(err, fontPath) {
function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
fs.realpath(ttxResourcesHomePath, function(error, realTtxResourcesHomePath) {
var fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
fs.realpath(fontPath, function(errorFontPath, realFontPath) {
var ttxPath = path.join("Tools", "ttx");
if (!fs.existsSync(path.join(fontToolsHome, ttxPath))) {
callback("TTX was not found, please checkout PDF.js submodules");
@ -38,7 +38,7 @@ function runTtx(ttxResourcesHome, fontPath, registerOnCancel, callback) {
PYTHONDONTWRITEBYTECODE: true,
};
var ttxStdioMode = "ignore";
var ttx = spawn("python", [ttxPath, fontPath], {
var ttx = spawn("python", [ttxPath, realFontPath], {
cwd: fontToolsHome,
stdio: ttxStdioMode,
env: ttxEnv,
@ -49,8 +49,8 @@ function runTtx(ttxResourcesHome, fontPath, registerOnCancel, callback) {
callback(reason);
ttx.kill();
});
ttx.on("error", function(err) {
ttxRunError = err;
ttx.on("error", function(errorTtx) {
ttxRunError = errorTtx;
callback("Unable to execute ttx");
});
ttx.on("close", function(code) {

View file

@ -37,16 +37,16 @@ function parseOptions() {
function group(stats, groupBy) {
var vals = [];
for (var i = 0; i < stats.length; i++) {
var stat = stats[i];
var curStat = stats[i];
var keyArr = [];
for (var j = 0; j < groupBy.length; j++) {
keyArr.push(stat[groupBy[j]]);
keyArr.push(curStat[groupBy[j]]);
}
var key = keyArr.join(",");
if (vals[key] === undefined) {
vals[key] = [];
}
vals[key].push(stat["time"]);
vals[key].push(curStat["time"]);
}
return vals;
}
@ -57,13 +57,13 @@ function group(stats, groupBy) {
*/
function flatten(stats) {
var rows = [];
stats.forEach(function(stat) {
stat["stats"].forEach(function(s) {
stats.forEach(function(curStat) {
curStat["stats"].forEach(function(s) {
rows.push({
browser: stat["browser"],
page: stat["page"],
pdf: stat["pdf"],
round: stat["round"],
browser: curStat["browser"],
page: curStat["page"],
pdf: curStat["pdf"],
round: curStat["round"],
stat: s["name"],
time: s["end"] - s["start"],
});

View file

@ -619,8 +619,8 @@ function refTestPostHandler(req, res) {
if (pathname === "/tellMeToQuit") {
// finding by path
var browserPath = parsedUrl.query.path;
session = sessions.filter(function(session) {
return session.config.path === browserPath;
session = sessions.filter(function(curSession) {
return curSession.config.path === browserPath;
})[0];
monitorBrowserTimeout(session, null);
closeSession(session.name);
@ -689,7 +689,7 @@ function refTestPostHandler(req, res) {
return true;
}
function startUnitTest(url, name) {
function startUnitTest(testUrl, name) {
var startTime = Date.now();
startServer();
server.hooks["POST"].push(unitTestPostHandler);
@ -712,7 +712,7 @@ function startUnitTest(url, name) {
var runtime = (Date.now() - startTime) / 1000;
console.log(name + " tests runtime was " + runtime.toFixed(1) + " seconds");
};
startBrowsers(url, function(session) {
startBrowsers(testUrl, function(session) {
session.numRuns = 0;
session.numErrors = 0;
});
@ -784,7 +784,7 @@ function unitTestPostHandler(req, res) {
return true;
}
function startBrowsers(url, initSessionCallback) {
function startBrowsers(testUrl, initSessionCallback) {
var browsers;
if (options.browserManifestFile) {
browsers = JSON.parse(fs.readFileSync(options.browserManifestFile));
@ -801,7 +801,7 @@ function startBrowsers(url, initSessionCallback) {
var browser = WebBrowser.create(b);
var startUrl =
getServerBaseAddress() +
url +
testUrl +
"?browser=" +
encodeURIComponent(b.name) +
"&manifestFile=" +

View file

@ -26,9 +26,9 @@ var crypto = require("crypto");
var tempDirPrefix = "pdfjs_";
function WebBrowser(name, path, headless) {
function WebBrowser(name, execPath, headless) {
this.name = name;
this.path = path;
this.path = execPath;
this.headless = headless;
this.tmpDir = null;
this.profileDir = null;
@ -197,7 +197,7 @@ WebBrowser.prototype = {
// Note: First process' output it shown, the later outputs are suppressed.
execAsyncNoStdin(
cmdKillAll,
function checkAlive(exitCode, firstStdout) {
function checkAlive(firstExitCode, firstStdout) {
execAsyncNoStdin(
cmdCheckAllKilled,
function(exitCode, stdout) {
@ -227,14 +227,14 @@ WebBrowser.prototype = {
var firefoxResourceDir = path.join(__dirname, "resources", "firefox");
function FirefoxBrowser(name, path, headless) {
function FirefoxBrowser(name, execPath, headless) {
if (os.platform() === "darwin") {
var m = /([^.\/]+)\.app(\/?)$/.exec(path);
var m = /([^.\/]+)\.app(\/?)$/.exec(execPath);
if (m) {
path += (m[2] ? "" : "/") + "Contents/MacOS/firefox";
execPath += (m[2] ? "" : "/") + "Contents/MacOS/firefox";
}
}
WebBrowser.call(this, name, path, headless);
WebBrowser.call(this, name, execPath, headless);
}
FirefoxBrowser.prototype = Object.create(WebBrowser.prototype);
FirefoxBrowser.prototype.buildArguments = function(url) {
@ -253,15 +253,15 @@ FirefoxBrowser.prototype.setupProfileDir = function(dir) {
testUtils.copySubtreeSync(firefoxResourceDir, dir);
};
function ChromiumBrowser(name, path, headless) {
function ChromiumBrowser(name, execPath, headless) {
if (os.platform() === "darwin") {
var m = /([^.\/]+)\.app(\/?)$/.exec(path);
var m = /([^.\/]+)\.app(\/?)$/.exec(execPath);
if (m) {
path += (m[2] ? "" : "/") + "Contents/MacOS/" + m[1];
console.log(path);
execPath += (m[2] ? "" : "/") + "Contents/MacOS/" + m[1];
console.log(execPath);
}
}
WebBrowser.call(this, name, path, headless);
WebBrowser.call(this, name, execPath, headless);
}
ChromiumBrowser.prototype = Object.create(WebBrowser.prototype);
ChromiumBrowser.prototype.buildArguments = function(url) {
@ -291,18 +291,18 @@ ChromiumBrowser.prototype.buildArguments = function(url) {
WebBrowser.create = function(desc) {
var name = desc.name;
var path = fs.realpathSync(desc.path);
if (!path) {
var execPath = fs.realpathSync(desc.path);
if (!execPath) {
throw new Error("Browser executable not found: " + desc.path);
}
if (/firefox/i.test(name)) {
return new FirefoxBrowser(name, path, desc.headless);
return new FirefoxBrowser(name, execPath, desc.headless);
}
if (/(chrome|chromium|opera)/i.test(name)) {
return new ChromiumBrowser(name, path, desc.headless);
return new ChromiumBrowser(name, execPath, desc.headless);
}
return new WebBrowser(name, path, desc.headless);
return new WebBrowser(name, execPath, desc.headless);
};
exports.WebBrowser = WebBrowser;

View file

@ -283,15 +283,15 @@ WebServer.prototype = {
});
}
function serveRequestedFile(filePath) {
var stream = fs.createReadStream(filePath, { flags: "rs" });
function serveRequestedFile(reqFilePath) {
var stream = fs.createReadStream(reqFilePath, { flags: "rs" });
stream.on("error", function(error) {
res.writeHead(500);
res.end();
});
var ext = path.extname(filePath).toLowerCase();
var ext = path.extname(reqFilePath).toLowerCase();
var contentType = mimeTypes[ext] || defaultMimeType;
if (!disableRangeRequests) {
@ -309,8 +309,8 @@ WebServer.prototype = {
stream.pipe(res);
}
function serveRequestedFileRange(filePath, start, end) {
var stream = fs.createReadStream(filePath, {
function serveRequestedFileRange(reqFilePath, start, end) {
var stream = fs.createReadStream(reqFilePath, {
flags: "rs",
start: start,
end: end - 1,
@ -321,7 +321,7 @@ WebServer.prototype = {
res.end();
});
var ext = path.extname(filePath).toLowerCase();
var ext = path.extname(reqFilePath).toLowerCase();
var contentType = mimeTypes[ext] || defaultMimeType;
res.setHeader("Accept-Ranges", "bytes");