From 09e868ce5fe27bdb02802711677de9b4a38b6e60 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Sat, 18 Feb 2017 14:27:26 -0700
Subject: [PATCH] ESlint no-useless-escape, no-else-return
---
.eslintrc | 4 ++--
public/src/admin/manage/flags.js | 3 +--
public/src/ajaxify.js | 3 +--
public/src/client/account/edit.js | 15 ++++++------
public/src/installer/install.js | 3 +--
public/src/modules/components.js | 6 ++---
public/src/modules/helpers.js | 27 ++++++++-------------
public/src/modules/settings.js | 21 +++++++---------
public/src/modules/settings/key.js | 6 ++---
public/src/utils.js | 37 ++++++++++++++---------------
src/admin/search.js | 4 ++--
src/controllers/accounts/session.js | 3 +--
src/controllers/authentication.js | 7 +++---
src/controllers/index.js | 3 +--
src/database/redis/sorted.js | 4 ++--
src/groups/search.js | 3 +--
src/logger.js | 3 +--
src/meta/languages.js | 2 +-
src/middleware/index.js | 10 ++++----
src/middleware/user.js | 9 ++++---
src/plugins.js | 3 +--
src/plugins/hooks.js | 5 ++--
src/plugins/load.js | 9 ++++---
src/socket.io/index.js | 3 +--
src/socket.io/modules.js | 4 ++--
src/topics/recent.js | 3 +--
test/utils.js | 2 +-
27 files changed, 85 insertions(+), 117 deletions(-)
diff --git a/.eslintrc b/.eslintrc
index 95b90ba253..a6229e5bda 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -71,8 +71,8 @@
"new-cap": "off",
"no-lonely-if": "off",
"radix": "off",
- "no-else-return": "off",
- "no-useless-escape": "off",
+ // "no-else-return": "off",
+ // "no-useless-escape": "off",
// "block-scoped-var": "off",
// "operator-assignment": "off",
// "yoda": "off",
diff --git a/public/src/admin/manage/flags.js b/public/src/admin/manage/flags.js
index a420540f99..2e4dca281d 100644
--- a/public/src/admin/manage/flags.js
+++ b/public/src/admin/manage/flags.js
@@ -165,9 +165,8 @@ define('admin/manage/flags', [
}, function (err) {
if (err) {
return app.alertError(err.message);
- } else {
- app.alertSuccess('[[topic:flag_manage_saved]]');
}
+ app.alertSuccess('[[topic:flag_manage_saved]]');
});
}
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index 3125373d02..9cc3711701 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -377,9 +377,8 @@ $(document).ready(function () {
if ($(this).attr('data-ajaxify') === 'false') {
if (!internalLink) {
return;
- } else {
- return e.preventDefault();
}
+ return e.preventDefault();
}
// Default behaviour for rss feeds
diff --git a/public/src/client/account/edit.js b/public/src/client/account/edit.js
index ddee32f3f4..4de3d61be3 100644
--- a/public/src/client/account/edit.js
+++ b/public/src/client/account/edit.js
@@ -166,15 +166,14 @@ define('forum/account/edit', ['forum/account/header', 'translator', 'components'
if ($('#confirm-username').val() !== app.user.username) {
app.alertError('[[error:invalid-username]]');
return false;
- } else {
- socket.emit('user.deleteAccount', {}, function (err) {
- if (err) {
- return app.alertError(err.message);
- }
-
- window.location.href = config.relative_path + '/';
- });
}
+ socket.emit('user.deleteAccount', {}, function (err) {
+ if (err) {
+ return app.alertError(err.message);
+ }
+
+ window.location.href = config.relative_path + '/';
+ });
});
modal.on('shown.bs.modal', function () {
diff --git a/public/src/installer/install.js b/public/src/installer/install.js
index 4dddbf9360..fffc3e62a5 100644
--- a/public/src/installer/install.js
+++ b/public/src/installer/install.js
@@ -45,9 +45,8 @@ $('document').ready(function () {
$('html, body').animate({ scrollTop: '0px' }, 400);
return false;
- } else {
- $('#submit .fa-spin').removeClass('hide');
}
+ $('#submit .fa-spin').removeClass('hide');
}
function activate(type, el) {
diff --git a/public/src/modules/components.js b/public/src/modules/components.js
index 9ddec2b7a9..29d9e883a1 100644
--- a/public/src/modules/components.js
+++ b/public/src/modules/components.js
@@ -7,9 +7,8 @@ define('components', function () {
'topic/teaser': function (tid) {
if (tid) {
return $('[component="category/topic"][data-tid="' + tid + '"] [component="topic/teaser"]');
- } else {
- return $('[component="topic/teaser"]');
}
+ return $('[component="topic/teaser"]');
},
topic: function (name, value) {
return $('[component="topic"][data-' + name + '="' + value + '"]');
@@ -61,9 +60,8 @@ define('components', function () {
if (components.core[arguments[0]] && args.length) {
return components.core[arguments[0]].apply(this, args);
- } else {
- return $('[component="' + arguments[0] + '"]');
}
+ return $('[component="' + arguments[0] + '"]');
};
return components;
diff --git a/public/src/modules/helpers.js b/public/src/modules/helpers.js
index 2796b8d94e..3e06438fd0 100644
--- a/public/src/modules/helpers.js
+++ b/public/src/modules/helpers.js
@@ -56,18 +56,16 @@
helpers.escape = function (str) {
if (typeof utils !== 'undefined') {
return utils.escapeHTML(str);
- } else {
- return require('../utils').escapeHTML(str);
}
+ return require('../utils').escapeHTML(str);
};
helpers.stripTags = function (str) {
if (typeof window !== 'undefined' && window.S) {
return window.S(String(str)).stripTags().s;
- } else {
- var S = require('string');
- return S(String(str)).stripTags().s;
}
+ var S = require('string');
+ return S(String(str)).stripTags().s;
};
helpers.generateCategoryBackground = function (category) {
@@ -157,9 +155,8 @@
return '';
} else if (!groupObj.disableJoinRequests && groupObj.name !== 'administrators') {
return '';
- } else {
- return '';
}
+ return '';
};
helpers.spawnPrivilegeStates = function (member, privileges) {
@@ -184,25 +181,21 @@
helpers.renderTopicImage = function (topicObj) {
if (topicObj.thumb) {
return '';
- } else {
- return '';
}
+ return '';
};
helpers.renderDigestAvatar = function (block) {
if (block.teaser) {
if (block.teaser.user.picture) {
return '';
- } else {
- return '' + block.teaser.user['icon:text'] + '
';
- }
- } else {
- if (block.user.picture) {
- return '';
- } else {
- return '' + block.user['icon:text'] + '
';
}
+ return '' + block.teaser.user['icon:text'] + '
';
+ }
+ if (block.user.picture) {
+ return '';
}
+ return '' + block.user['icon:text'] + '
';
};
helpers.userAgentIcons = function (data) {
diff --git a/public/src/modules/settings.js b/public/src/modules/settings.js
index b8883f2958..35ae29eb9b 100644
--- a/public/src/modules/settings.js
+++ b/public/src/modules/settings.js
@@ -36,9 +36,8 @@ define('settings', function () {
hook = plugin[name];
if (typeof hook === 'function') {
return hook;
- } else {
- return null;
}
+ return null;
}
helper = {
@@ -48,9 +47,8 @@ define('settings', function () {
deepClone: function (obj) {
if (typeof obj === 'object') {
return JSON.parse(JSON.stringify(obj));
- } else {
- return obj;
}
+ return obj;
},
/**
Creates a new Element with given data.
@@ -169,14 +167,13 @@ define('settings', function () {
value = element.val();
var array = (value != null && value.split(split || ',')) || [];
return helper.cleanArray(array, trim, empty);
- } else {
- value = element.val();
- if (trim && value != null && typeof value.trim === 'function') {
- value = value.trim();
- }
- if (empty || (value !== undefined && (value == null || value.length !== 0))) {
- return value;
- }
+ }
+ value = element.val();
+ if (trim && value != null && typeof value.trim === 'function') {
+ value = value.trim();
+ }
+ if (empty || (value !== undefined && (value == null || value.length !== 0))) {
+ return value;
}
},
/**
diff --git a/public/src/modules/settings/key.js b/public/src/modules/settings/key.js
index 52b116b2e3..eac0f9e3c0 100644
--- a/public/src/modules/settings/key.js
+++ b/public/src/modules/settings/key.js
@@ -78,9 +78,8 @@ define('settings/key', function () {
return String.fromCharCode(code).toUpperCase();
} else if (code >= 112 && code <= 123) {
return 'F' + (code - 111);
- } else {
- return keyMap[code] || ('#' + code);
}
+ return keyMap[code] || ('#' + code);
}
/**
@@ -99,9 +98,8 @@ define('settings/key', function () {
if (!key.char) {
if (human) {
return 'Enter a key';
- } else {
- return '';
}
+ return '';
}
if (!separator || /CtrlAShifMea#/.test(separator)) {
separator = human ? ' + ' : '+';
diff --git a/public/src/utils.js b/public/src/utils.js
index 292ba3694a..de5c3dc59d 100644
--- a/public/src/utils.js
+++ b/public/src/utils.js
@@ -111,7 +111,7 @@
tag = tag.trim().toLowerCase();
// see https://github.com/NodeBB/NodeBB/issues/4378
tag = tag.replace(/\u202E/gi, '');
- tag = tag.replace(/[,\/#!$%\^\*;:{}=_`<>'"~()?\|]/g, '');
+ tag = tag.replace(/[,/#!$%^*;:{}=_`<>'"~()?|]/g, '');
tag = tag.substr(0, maxLength || 15).trim();
var matches = tag.match(/^[.-]*(.+?)[.-]*$/);
if (matches && matches.length > 1) {
@@ -121,7 +121,7 @@
},
removePunctuation: function (str) {
- return str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`<>'"~()?]/g, '');
+ return str.replace(/[.,-/#!$%^&*;:{}=\-_`<>'"~()?]/g, '');
},
isEmailValid: function (email) {
@@ -129,7 +129,7 @@
},
isUserNameValid: function (name) {
- return (name && name !== '' && (/^['"\s\-\+.*0-9\u00BF-\u1FFF\u2C00-\uD7FF\w]+$/.test(name)));
+ return (name && name !== '' && (/^['"\s\-+.*0-9\u00BF-\u1FFF\u2C00-\uD7FF\w]+$/.test(name)));
},
isPasswordValid: function (password) {
@@ -390,24 +390,23 @@
var type = typeof str;
if (type !== 'string') {
return str;
- } else {
- var nb = parseFloat(str);
- if (!isNaN(nb) && isFinite(str)) {
- return nb;
- }
- if (str === 'false') {
- return false;
- }
- if (str === 'true') {
- return true;
- }
+ }
+ var nb = parseFloat(str);
+ if (!isNaN(nb) && isFinite(str)) {
+ return nb;
+ }
+ if (str === 'false') {
+ return false;
+ }
+ if (str === 'true') {
+ return true;
+ }
- try {
- str = JSON.parse(str);
- } catch (e) {}
+ try {
+ str = JSON.parse(str);
+ } catch (e) {}
- return str;
- }
+ return str;
},
// Safely get/set chained properties on an object
diff --git a/src/admin/search.js b/src/admin/search.js
index 44f9a53a9f..3a6f52cfd0 100644
--- a/src/admin/search.js
+++ b/src/admin/search.js
@@ -19,7 +19,7 @@ function filterDirectories(directories) {
// exclude category.tpl, group.tpl, category-analytics.tpl
return !dir.includes('/partials/') &&
/\/.*\//.test(dir) &&
- !/manage\/(category|group|category\-analytics)$/.test(dir);
+ !/manage\/(category|group|category-analytics)$/.test(dir);
});
}
@@ -45,7 +45,7 @@ function sanitize(html) {
function simplify(translations) {
return translations
// remove all mustaches
- .replace(/(?:\{{1,2}[^\}]*?\}{1,2})/g, '')
+ .replace(/(?:\{{1,2}[^}]*?\}{1,2})/g, '')
// collapse whitespace
.replace(/(?:[ \t]*[\n\r]+[ \t]*)+/g, '\n')
.replace(/[\t ]+/g, ' ');
diff --git a/src/controllers/accounts/session.js b/src/controllers/accounts/session.js
index 67fae0bef3..809cdb6dad 100644
--- a/src/controllers/accounts/session.js
+++ b/src/controllers/accounts/session.js
@@ -46,9 +46,8 @@ sessionController.revoke = function (req, res, next) {
], function (err) {
if (err) {
return res.status(500).send(err.message);
- } else {
- return res.sendStatus(200);
}
+ return res.sendStatus(200);
});
};
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index 3a44f7b134..e83cb56a33 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -113,11 +113,10 @@ function registerAndLoginUser(req, res, userData, callback) {
if (!deferRegistration) {
return next();
- } else {
- userData.register = true;
- req.session.registration = userData;
- return res.json({ referrer: nconf.get('relative_path') + '/register/complete' });
}
+ userData.register = true;
+ req.session.registration = userData;
+ return res.json({ referrer: nconf.get('relative_path') + '/register/complete' });
});
},
function (next) {
diff --git a/src/controllers/index.js b/src/controllers/index.js
index 14600263de..57e23e069f 100644
--- a/src/controllers/index.js
+++ b/src/controllers/index.js
@@ -131,9 +131,8 @@ Controllers.login = function (req, res, next) {
return helpers.redirect(res, {
external: data.authentication[0].url,
});
- } else {
- return res.redirect(nconf.get('relative_path') + data.authentication[0].url);
}
+ return res.redirect(nconf.get('relative_path') + data.authentication[0].url);
}
if (req.uid) {
user.getUserFields(req.uid, ['username', 'email'], function (err, user) {
diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js
index 2fd3220eaa..a2c18296db 100644
--- a/src/database/redis/sorted.js
+++ b/src/database/redis/sorted.js
@@ -207,10 +207,10 @@ module.exports = function (redisClient, module) {
maxmax = '+';
}
- if (min !== minmin && !min.match(/^[\[\(]/)) {
+ if (min !== minmin && !min.match(/^[[(]/)) {
min = '[' + min;
}
- if (max !== maxmax && !max.match(/^[\[\(]/)) {
+ if (max !== maxmax && !max.match(/^[[(]/)) {
max = '[' + max;
}
diff --git a/src/groups/search.js b/src/groups/search.js
index 44bc835d5c..3c580b5e8d 100644
--- a/src/groups/search.js
+++ b/src/groups/search.js
@@ -121,9 +121,8 @@ module.exports = function (Groups) {
return -1;
} else if (!a.isOwner && b.isOwner) {
return 1;
- } else {
- return 0;
}
+ return 0;
});
next(null, results);
},
diff --git a/src/logger.js b/src/logger.js
index c40ae49ae1..73180d7add 100644
--- a/src/logger.js
+++ b/src/logger.js
@@ -132,9 +132,8 @@ var opts = {
*/
if (meta.config.loggerStatus > 0) {
return opts.express.ofn(req, res, next);
- } else {
- return next();
}
+ return next();
};
Logger.prepare_io_string = function (_type, _uid, _args) {
diff --git a/src/meta/languages.js b/src/meta/languages.js
index 381c22c34f..30d8d599e9 100644
--- a/src/meta/languages.js
+++ b/src/meta/languages.js
@@ -44,7 +44,7 @@ function getTranslationTree(callback) {
// pull languages and namespaces from paths
function extrude(languageDir, paths) {
paths.forEach(function (p) {
- var rel = p.split(languageDir)[1].split(/[\/\\]/).slice(1);
+ var rel = p.split(languageDir)[1].split(/[/\\]/).slice(1);
var language = rel.shift().replace('_', '-').replace('@', '-x-');
var namespace = rel.join('/').replace(/\.json$/, '');
diff --git a/src/middleware/index.js b/src/middleware/index.js
index 798067429a..df11d8a830 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -64,9 +64,8 @@ middleware.ensureSelfOrGlobalPrivilege = function (req, res, next) {
return next(err);
} else if (ok) {
return next();
- } else {
- controllers.helpers.notAllowed(req, res);
}
+ controllers.helpers.notAllowed(req, res);
});
} else {
controllers.helpers.notAllowed(req, res);
@@ -121,11 +120,10 @@ middleware.prepareAPI = function (req, res, next) {
middleware.routeTouchIcon = function (req, res) {
if (meta.config['brand:touchIcon'] && validator.isURL(meta.config['brand:touchIcon'])) {
return res.redirect(meta.config['brand:touchIcon']);
- } else {
- return res.sendFile(path.join(__dirname, '../../public', meta.config['brand:touchIcon'] || '/logo.png'), {
- maxAge: req.app.enabled('cache') ? 5184000000 : 0,
- });
}
+ return res.sendFile(path.join(__dirname, '../../public', meta.config['brand:touchIcon'] || '/logo.png'), {
+ maxAge: req.app.enabled('cache') ? 5184000000 : 0,
+ });
};
middleware.privateTagListing = function (req, res, next) {
diff --git a/src/middleware/user.js b/src/middleware/user.js
index ed56ad6d9e..85b5d6e808 100644
--- a/src/middleware/user.js
+++ b/src/middleware/user.js
@@ -148,12 +148,11 @@ module.exports = function (middleware) {
// If the user's session contains registration data, redirect the user to complete registration
if (!req.session.hasOwnProperty('registration')) {
return next();
+ }
+ if (!req.path.endsWith('/register/complete')) {
+ controllers.helpers.redirect(res, '/register/complete');
} else {
- if (!req.path.endsWith('/register/complete')) {
- controllers.helpers.redirect(res, '/register/complete');
- } else {
- return next();
- }
+ return next();
}
};
};
diff --git a/src/plugins.js b/src/plugins.js
index 915f9fdca6..99656312e9 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -316,9 +316,8 @@ var middleware;
return 1;
} else if (a.name < b.name) {
return -1;
- } else {
- return 0;
}
+ return 0;
});
callback(null, pluginArray);
diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js
index ef82046bbf..4db26fb560 100644
--- a/src/plugins/hooks.js
+++ b/src/plugins/hooks.js
@@ -57,10 +57,9 @@ module.exports = function (Plugins) {
method = data.method.split('.').reduce(function (memo, prop) {
if (memo && memo[prop]) {
return memo[prop];
- } else {
- // Couldn't find method by path, aborting
- return null;
}
+ // Couldn't find method by path, aborting
+ return null;
}, Plugins.libraries[data.id]);
// Write the actual method reference to the hookObj
diff --git a/src/plugins/load.js b/src/plugins/load.js
index 56f3ff563f..33659e714f 100644
--- a/src/plugins/load.js
+++ b/src/plugins/load.js
@@ -225,7 +225,7 @@ module.exports = function (Plugins) {
pluginData.modules.forEach(function (file) {
if (strip) {
- modules[file.replace(new RegExp('\.?(\/[^\/]+){' + strip + '}\/'), '')] = path.join('./node_modules/', pluginData.id, file);
+ modules[file.replace(new RegExp('.?(/[^/]+){' + strip + '}/'), '')] = path.join('./node_modules/', pluginData.id, file);
} else {
modules[path.basename(file)] = path.join('./node_modules/', pluginData.id, file);
}
@@ -268,11 +268,10 @@ module.exports = function (Plugins) {
if (!atRootLevel && relPath) {
winston.verbose('[plugins/load] File not found: ' + fullPath + ' (Ascending)');
return resolveModulePath(path.join(__dirname, '../..', relPath));
- } else {
- // Already at root level, file was simply not found
- winston.warn('[plugins/load] File not found: ' + fullPath + ' (Ignoring)');
- return null;
}
+ // Already at root level, file was simply not found
+ winston.warn('[plugins/load] File not found: ' + fullPath + ' (Ignoring)');
+ return null;
}
}
diff --git a/src/socket.io/index.js b/src/socket.io/index.js
index 21edc91453..d6907538fd 100644
--- a/src/socket.io/index.js
+++ b/src/socket.io/index.js
@@ -83,9 +83,8 @@ function onMessage(socket, payload) {
var methodToCall = parts.reduce(function (prev, cur) {
if (prev !== null && prev[cur]) {
return prev[cur];
- } else {
- return null;
}
+ return null;
}, Namespaces);
if (!methodToCall) {
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index bd4c7febc2..cdcc01f08b 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -93,9 +93,9 @@ function rateLimitExceeded(socket) {
var delay = meta.config.hasOwnProperty('chatMessageDelay') ? parseInt(meta.config.chatMessageDelay, 10) : 200;
if (now - socket.lastChatMessageTime < delay) {
return true;
- } else {
- socket.lastChatMessageTime = now;
}
+ socket.lastChatMessageTime = now;
+
return false;
}
diff --git a/src/topics/recent.js b/src/topics/recent.js
index eb2aebec66..dbecd035b4 100644
--- a/src/topics/recent.js
+++ b/src/topics/recent.js
@@ -79,9 +79,8 @@ module.exports = function (Topics) {
tids = results.topicData.filter(function (topic) {
if (topic) {
return results.ignoredCids.indexOf(topic.cid.toString()) === -1;
- } else {
- return false;
}
+ return false;
}).map(function (topic) {
return topic.tid;
});
diff --git a/test/utils.js b/test/utils.js
index e5d0666931..2beedcf3eb 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -38,7 +38,7 @@ describe('Utility Methods', function () {
describe('cleanUpTag', function () {
it('should cleanUp a tag', function (done) {
- var cleanedTag = utils.cleanUpTag(',\/#!$%\^\*;TaG1:{}=_`<>\'"~()?\|');
+ var cleanedTag = utils.cleanUpTag(',/#!$%^*;TaG1:{}=_`<>\'"~()?|');
assert.equal(cleanedTag, 'tag1');
done();
});