chore: eslint no-restricted-syntax

v1.18.x
Peter Jaszkowiak 4 years ago committed by Julian Lam
parent 115d19e289
commit 5c2f0f0557

@ -65,22 +65,21 @@
"AssignmentExpression": { "array": false, "object": false }
}],
// identical to airbnb rule, except for allowing for..of, because we want to use it
// "no-restricted-syntax": [
// "error",
// {
// "selector": "ForInStatement",
// "message": "for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array."
// },
// {
// "selector": "LabeledStatement",
// "message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
// },
// {
// "selector": "WithStatement",
// "message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
// }
// ],
"no-restricted-syntax": "off",
"no-restricted-syntax": [
"error",
{
"selector": "ForInStatement",
"message": "for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array."
},
{
"selector": "LabeledStatement",
"message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
},
{
"selector": "WithStatement",
"message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
}
],
// allow lines of up to 120 characters
// "max-len": ["error", { "code": 120, "tabWidth": 2, "ignoreUrls": true, "ignoreStrings": true, "ignoreTemplateLiterals": true, "ignoreRegExpLiterals": true }],
"max-len": "off", // do this LAST

@ -151,9 +151,9 @@ function install(req, res) {
req.setTimeout(0);
installing = true;
const setupEnvVars = nconf.get();
for (const i in req.body) {
if (req.body.hasOwnProperty(i) && !process.env.hasOwnProperty(i)) {
setupEnvVars[i.replace(':', '__')] = req.body[i];
for (const [key, value] of Object.entries(req.body)) {
if (!process.env.hasOwnProperty(key)) {
setupEnvVars[key.replace(':', '__')] = value;
}
}
@ -161,12 +161,12 @@ function install(req, res) {
const pushToRoot = function (parentKey, key) {
setupEnvVars[`${parentKey}__${key}`] = setupEnvVars[parentKey][key];
};
for (const j in setupEnvVars) {
if (setupEnvVars.hasOwnProperty(j) && typeof setupEnvVars[j] === 'object' && setupEnvVars[j] !== null && !Array.isArray(setupEnvVars[j])) {
Object.keys(setupEnvVars[j]).forEach(pushToRoot.bind(null, j));
delete setupEnvVars[j];
} else if (Array.isArray(setupEnvVars[j])) {
setupEnvVars[j] = JSON.stringify(setupEnvVars[j]);
for (const [parentKey, value] of Object.entries(setupEnvVars)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
Object.keys(value).forEach(key => pushToRoot(parentKey, key));
delete setupEnvVars[parentKey];
} else if (Array.isArray(value)) {
setupEnvVars[parentKey] = JSON.stringify(value);
}
}

@ -128,14 +128,10 @@ Analytics.writeData = async function () {
uniqueIPCount = 0;
}
if (Object.keys(counters).length > 0) {
for (const key in counters) {
if (counters.hasOwnProperty(key)) {
dbQueue.push(db.sortedSetIncrBy(`analytics:${key}`, counters[key], today.getTime()));
for (const [key, value] of Object.entries(counters)) {
dbQueue.push(db.sortedSetIncrBy(`analytics:${key}`, value, today.getTime()));
delete counters[key];
}
}
}
try {
await Promise.all(dbQueue);
} catch (err) {

@ -28,9 +28,9 @@ helpers.fieldToString = function (field) {
helpers.serializeData = function (data) {
const serialized = {};
for (const field in data) {
if (data.hasOwnProperty(field) && field !== '') {
serialized[helpers.fieldToString(field)] = data[field];
for (const [field, value] of Object.entries(data)) {
if (field !== '') {
serialized[helpers.fieldToString(field)] = value;
}
}
return serialized;
@ -38,10 +38,8 @@ helpers.serializeData = function (data) {
helpers.deserializeData = function (data) {
const deserialized = {};
for (const field in data) {
if (data.hasOwnProperty(field)) {
deserialized[field.replace(/\uff0E/g, '.')] = data[field];
}
for (const [field, value] of Object.entries(data)) {
deserialized[field.replace(/\uff0E/g, '.')] = value;
}
return deserialized;
};

@ -135,15 +135,13 @@ Flags.getFlagIdsWithFilters = async function ({ filters, uid }) {
filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1;
filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20;
for (const type in filters) {
if (filters.hasOwnProperty(type)) {
for (const type of Object.keys(filters)) {
if (Flags._filters.hasOwnProperty(type)) {
Flags._filters[type](sets, orSets, filters[type], uid);
} else {
winston.warn(`[flags/list] No flag filter type found: ${type}`);
}
}
}
sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; // No filter default
let flagIds = [];
@ -586,8 +584,7 @@ Flags.update = async function (flagId, uid, changeset) {
// Retrieve existing flag data to compare for history-saving/reference purposes
const tasks = [];
for (const prop in changeset) {
if (changeset.hasOwnProperty(prop)) {
for (const prop of Object.keys(changeset)) {
if (current[prop] === changeset[prop]) {
delete changeset[prop];
} else if (prop === 'state') {
@ -610,7 +607,6 @@ Flags.update = async function (flagId, uid, changeset) {
}
}
}
}
if (!Object.keys(changeset).length) {
return;

@ -150,12 +150,7 @@ async function setupConfig() {
async function completeConfigSetup(config) {
// Add CI object
if (install.ciVals) {
config.test_database = {};
for (const prop in install.ciVals) {
if (install.ciVals.hasOwnProperty(prop)) {
config.test_database[prop] = install.ciVals[prop];
}
}
config.test_database = { ...install.ciVals };
}
// Add package_manager object if set

@ -47,14 +47,12 @@ Settings.set = async function (hash, values, quiet) {
({ plugin: hash, settings: values, quiet } = await plugins.hooks.fire('filter:settings.set', { plugin: hash, settings: values, quiet }));
const sortedListData = {};
for (const key in values) {
if (values.hasOwnProperty(key)) {
if (Array.isArray(values[key]) && typeof values[key][0] !== 'string') {
sortedListData[key] = values[key];
for (const [key, value] of Object.entries(values)) {
if (Array.isArray(value) && typeof value[0] !== 'string') {
sortedListData[key] = value;
delete values[key];
}
}
}
const sortedLists = Object.keys(sortedListData);
if (sortedLists.length) {

@ -64,9 +64,9 @@ module.exports = function (middleware) {
headers['X-Upstream-Hostname'] = os.hostname();
}
for (const key in headers) {
if (headers.hasOwnProperty(key) && headers[key]) {
res.setHeader(key, headers[key]);
for (const [key, value] of Object.entries(headers)) {
if (value) {
res.setHeader(key, value);
}
}

@ -241,13 +241,7 @@ Plugins.normalise = async function (apiReturn) {
pluginMap[plugin.id].outdated = semver.gt(pluginMap[plugin.id].latest, pluginMap[plugin.id].version);
});
const pluginArray = [];
for (const key in pluginMap) {
if (pluginMap.hasOwnProperty(key)) {
pluginArray.push(pluginMap[key]);
}
}
const pluginArray = Object.values(pluginMap);
pluginArray.sort((a, b) => {
if (a.name > b.name) {

@ -4,17 +4,11 @@ const meta = require('./meta');
const pubsub = require('./pubsub');
function expandObjBy(obj1, obj2) {
let key;
let val1;
let val2;
let xorValIsArray;
let changed = false;
for (key in obj2) {
if (obj2.hasOwnProperty(key)) {
val2 = obj2[key];
val1 = obj1[key];
xorValIsArray = Array.isArray(val1) === Array.isArray(val2);
if (xorValIsArray || !obj1.hasOwnProperty(key) || typeof val2 !== typeof val1) {
for (const [key, val2] of Object.entries(obj2)) {
const val1 = obj1[key];
const xorIsArray = Array.isArray(val1) === Array.isArray(val2);
if (xorIsArray || !obj1.hasOwnProperty(key) || typeof val2 !== typeof val1) {
obj1[key] = val2;
changed = true;
} else if (typeof val2 === 'object' && !Array.isArray(val2)) {
@ -23,16 +17,11 @@ function expandObjBy(obj1, obj2) {
}
}
}
}
return changed;
}
function trim(obj1, obj2) {
let key;
let val1;
for (key in obj1) {
if (obj1.hasOwnProperty(key)) {
val1 = obj1[key];
for (const [key, val1] of Object.entries(obj1)) {
if (!obj2.hasOwnProperty(key)) {
delete obj1[key];
} else if (typeof val1 === 'object' && !Array.isArray(val1)) {
@ -40,7 +29,6 @@ function trim(obj1, obj2) {
}
}
}
}
function mergeSettings(cfg, defCfg) {
if (typeof defCfg !== 'object') {

@ -32,16 +32,11 @@ Config.setMultiple = async function (socket, data) {
}
});
await meta.configs.setMultiple(data);
for (const field in data) {
if (data.hasOwnProperty(field)) {
const setting = {
key: field,
value: data[field],
};
for (const [key, value] of Object.entries(data)) {
const setting = { key, value };
plugins.hooks.fire('action:config.set', setting);
logger.monitorConfig({ io: index.server }, setting);
}
}
if (Object.keys(changes).length) {
changes.type = 'config-change';
changes.uid = socket.uid;

@ -64,23 +64,21 @@ SocketRooms.getAll = async function () {
category: 0,
};
for (const instance in stats) {
if (stats.hasOwnProperty(instance)) {
totals.onlineGuestCount += stats[instance].onlineGuestCount;
totals.onlineRegisteredCount += stats[instance].onlineRegisteredCount;
totals.socketCount += stats[instance].socketCount;
totals.users.categories += stats[instance].users.categories;
totals.users.recent += stats[instance].users.recent;
totals.users.unread += stats[instance].users.unread;
totals.users.topics += stats[instance].users.topics;
totals.users.category += stats[instance].users.category;
stats[instance].topics.forEach((topic) => {
for (const instance of Object.values(stats)) {
totals.onlineGuestCount += instance.onlineGuestCount;
totals.onlineRegisteredCount += instance.onlineRegisteredCount;
totals.socketCount += instance.socketCount;
totals.users.categories += instance.users.categories;
totals.users.recent += instance.users.recent;
totals.users.unread += instance.users.unread;
totals.users.topics += instance.users.topics;
totals.users.category += instance.users.category;
instance.topics.forEach((topic) => {
totals.topics[topic.tid] = totals.topics[topic.tid] || { count: 0, tid: topic.tid };
totals.topics[topic.tid].count += topic.count;
});
}
}
let topTenTopics = [];
Object.keys(totals.topics).forEach((tid) => {

@ -285,10 +285,8 @@ module.exports = function (User) {
User.setUserFields = async function (uid, data) {
await db.setObject(`user:${uid}`, data);
for (const field in data) {
if (data.hasOwnProperty(field)) {
plugins.hooks.fire('action:user.set', { uid: uid, field: field, value: data[field], type: 'set' });
}
for (const [field, value] of Object.entries(data)) {
plugins.hooks.fire('action:user.set', { uid, field, value, type: 'set' });
}
};

@ -46,14 +46,12 @@ module.exports = function (User) {
User.stopJobs = function () {
let terminated = 0;
// Terminate any active cron jobs
for (const jobId in jobs) {
if (jobs.hasOwnProperty(jobId)) {
for (const jobId of Object.keys(jobs)) {
winston.verbose(`[user/jobs] Terminating job (${jobId})`);
jobs[jobId].stop();
delete jobs[jobId];
terminated += 1;
}
}
if (terminated > 0) {
winston.verbose(`[user/jobs] ${terminated} jobs terminated`);
}

@ -131,8 +131,8 @@ UserReset.cleanByUid = async function (uid) {
await batch.processSortedSet('reset:issueDate', async (tokens) => {
const results = await db.getObjectFields('reset:uid', tokens);
for (const code in results) {
if (results.hasOwnProperty(code) && parseInt(results[code], 10) === uid) {
for (const [code, result] of Object.entries(results)) {
if (parseInt(result, 10) === uid) {
tokensToClean.push(code);
}
}

@ -70,10 +70,8 @@ server.on('connection', (conn) => {
exports.destroy = function (callback) {
server.close(callback);
for (const key in connections) {
if (connections.hasOwnProperty(key)) {
connections[key].destroy();
}
for (const connection of Object.values(connections)) {
connection.destroy();
}
};

@ -196,13 +196,8 @@ widgets.reset = async function () {
};
widgets.resetTemplate = async function (template) {
let toBeDrafted = [];
const area = await db.getObject(`widgets:${template}.tpl`);
for (const location in area) {
if (area.hasOwnProperty(location)) {
toBeDrafted = toBeDrafted.concat(JSON.parse(area[location]));
}
}
const toBeDrafted = _.flatMap(Object.values(area), value => JSON.parse(value));
await db.delete(`widgets:${template}.tpl`);
let draftWidgets = await db.getObjectField('widgets:global', 'drafts');
draftWidgets = JSON.parse(draftWidgets).concat(toBeDrafted);

@ -420,12 +420,10 @@ describe('API', async () => {
return memo;
}, {});
for (const header in expectedHeaders) {
if (expectedHeaders.hasOwnProperty(header)) {
for (const header of Object.keys(expectedHeaders)) {
assert(response.headers[header.toLowerCase()]);
assert.strictEqual(response.headers[header.toLowerCase()], expectedHeaders[header]);
}
}
return;
}

@ -55,12 +55,10 @@ describe('Flags', () => {
target_readable: 'Post 1',
};
assert(flagData);
for (const key in compare) {
if (compare.hasOwnProperty(key)) {
for (const key of Object.keys(compare)) {
assert.ok(flagData[key], `undefined key ${key}`);
assert.equal(flagData[key], compare[key]);
}
}
done();
});
@ -131,12 +129,10 @@ describe('Flags', () => {
target_readable: 'Post 1',
};
assert(flagData);
for (const key in compare) {
if (compare.hasOwnProperty(key)) {
for (const key of Object.keys(compare)) {
assert.ok(flagData[key], `undefined key ${key}`);
assert.equal(flagData[key], compare[key]);
}
}
done();
});
@ -483,12 +479,10 @@ describe('Flags', () => {
content: 'This is flaggable content',
};
for (const key in compare) {
if (compare.hasOwnProperty(key)) {
for (const key of Object.keys(compare)) {
assert.ok(data[key]);
assert.equal(data[key], compare[key]);
}
}
done();
});
@ -503,12 +497,10 @@ describe('Flags', () => {
email: 'b@c.com',
};
for (const key in compare) {
if (compare.hasOwnProperty(key)) {
for (const key of Object.keys(compare)) {
assert.ok(data[key]);
assert.equal(data[key], compare[key]);
}
}
done();
});
@ -644,12 +636,10 @@ describe('Flags', () => {
};
const data = notes[1];
for (const key in compare) {
if (compare.hasOwnProperty(key)) {
for (const key of Object.keys(compare)) {
assert.ok(data[key]);
assert.strictEqual(data[key], compare[key]);
}
}
done();
});

Loading…
Cancel
Save