lint: Run eslint --fix on src/

This commit is contained in:
Richard Hansen 2020-11-23 13:24:19 -05:00 committed by John McLear
parent b8d07a42eb
commit 8e5fd19db2
109 changed files with 9061 additions and 10572 deletions

View file

@ -1,7 +1,7 @@
/* global exports, require */
var _ = require("underscore");
var pluginDefs = require('./plugin_defs');
const _ = require('underscore');
const pluginDefs = require('./plugin_defs');
// Maps the name of a server-side hook to a string explaining the deprecation
// (e.g., 'use the foo hook instead').
@ -24,26 +24,24 @@ function checkDeprecation(hook) {
deprecationWarned[hook.hook_fn_name] = true;
}
exports.bubbleExceptions = true
exports.bubbleExceptions = true;
var hookCallWrapper = function (hook, hook_name, args, cb) {
const hookCallWrapper = function (hook, hook_name, args, cb) {
if (cb === undefined) cb = function (x) { return x; };
checkDeprecation(hook);
// Normalize output to list for both sync and async cases
var normalize = function(x) {
const normalize = function (x) {
if (x === undefined) return [];
return x;
}
var normalizedhook = function () {
return normalize(hook.hook_fn(hook_name, args, function (x) {
return cb(normalize(x));
}));
}
};
const normalizedhook = function () {
return normalize(hook.hook_fn(hook_name, args, (x) => cb(normalize(x))));
};
if (exports.bubbleExceptions) {
return normalizedhook();
return normalizedhook();
} else {
try {
return normalizedhook();
@ -51,32 +49,32 @@ var hookCallWrapper = function (hook, hook_name, args, cb) {
console.error([hook_name, hook.part.full_name, ex.stack || ex]);
}
}
}
};
exports.syncMapFirst = function (lst, fn) {
var i;
var result;
let i;
let result;
for (i = 0; i < lst.length; i++) {
result = fn(lst[i])
result = fn(lst[i]);
if (result.length) return result;
}
return [];
}
};
exports.mapFirst = function (lst, fn, cb, predicate) {
if (predicate == null) predicate = (x) => (x != null && x.length > 0);
var i = 0;
let i = 0;
var next = function () {
if (i >= lst.length) return cb(null, []);
fn(lst[i++], function (err, result) {
fn(lst[i++], (err, result) => {
if (err) return cb(err);
if (predicate(result)) return cb(null, result);
next();
});
}
};
next();
}
};
// Calls the hook function synchronously and returns the value provided by the hook function (via
// callback or return value).
@ -350,11 +348,9 @@ async function callHookFnAsync(hook, context) {
exports.aCallAll = async (hookName, context, cb) => {
if (context == null) context = {};
const hooks = pluginDefs.hooks[hookName] || [];
let resultsPromise = Promise.all(hooks.map((hook) => {
return callHookFnAsync(hook, context)
// `undefined` (but not `null`!) is treated the same as [].
.then((result) => (result === undefined) ? [] : result);
})).then((results) => _.flatten(results, 1));
let resultsPromise = Promise.all(hooks.map((hook) => callHookFnAsync(hook, context)
// `undefined` (but not `null`!) is treated the same as [].
.then((result) => (result === undefined) ? [] : result))).then((results) => _.flatten(results, 1));
if (cb != null) resultsPromise = resultsPromise.then((val) => cb(null, val), cb);
return await resultsPromise;
};
@ -362,49 +358,45 @@ exports.aCallAll = async (hookName, context, cb) => {
exports.callFirst = function (hook_name, args) {
if (!args) args = {};
if (pluginDefs.hooks[hook_name] === undefined) return [];
return exports.syncMapFirst(pluginDefs.hooks[hook_name], function(hook) {
return hookCallWrapper(hook, hook_name, args);
});
}
return exports.syncMapFirst(pluginDefs.hooks[hook_name], (hook) => hookCallWrapper(hook, hook_name, args));
};
function aCallFirst(hook_name, args, cb, predicate) {
if (!args) args = {};
if (!cb) cb = function () {};
if (pluginDefs.hooks[hook_name] === undefined) return cb(null, []);
exports.mapFirst(
pluginDefs.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},
cb,
predicate
pluginDefs.hooks[hook_name],
(hook, cb) => {
hookCallWrapper(hook, hook_name, args, (res) => { cb(null, res); });
},
cb,
predicate,
);
}
/* return a Promise if cb is not supplied */
exports.aCallFirst = function (hook_name, args, cb, predicate) {
if (cb === undefined) {
return new Promise(function(resolve, reject) {
aCallFirst(hook_name, args, function(err, res) {
return err ? reject(err) : resolve(res);
}, predicate);
return new Promise((resolve, reject) => {
aCallFirst(hook_name, args, (err, res) => err ? reject(err) : resolve(res), predicate);
});
} else {
return aCallFirst(hook_name, args, cb, predicate);
}
}
};
exports.callAllStr = function(hook_name, args, sep, pre, post) {
exports.callAllStr = function (hook_name, args, sep, pre, post) {
if (sep == undefined) sep = '';
if (pre == undefined) pre = '';
if (post == undefined) post = '';
var newCallhooks = [];
var callhooks = exports.callAll(hook_name, args);
for (var i = 0, ii = callhooks.length; i < ii; i++) {
const newCallhooks = [];
const callhooks = exports.callAll(hook_name, args);
for (let i = 0, ii = callhooks.length; i < ii; i++) {
newCallhooks[i] = pre + callhooks[i] + post;
}
return newCallhooks.join(sep || "");
}
return newCallhooks.join(sep || '');
};
exports.exportedForTestingOnly = {
callHookFnAsync,