From 846f29ef6d8ee4ccca8b9d928c754b5c5964124a Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 29 Dec 2017 23:28:11 +0800 Subject: [PATCH] style(js): stricter eslint rules (#108) * style(js): stricter eslint rules * build: ignore .eslintignore --- .eslintignore | 4 ++ .eslintrc.js | 6 ++- gulpfile.js | 1 + resources/assets/src/js/admin/customize.js | 4 +- resources/assets/src/js/admin/players.js | 20 ++++---- resources/assets/src/js/admin/plugins.js | 6 +-- resources/assets/src/js/admin/tables.js | 18 ++++---- resources/assets/src/js/admin/update.js | 4 +- resources/assets/src/js/admin/users.js | 36 +++++++-------- resources/assets/src/js/auth/captcha.js | 2 +- resources/assets/src/js/auth/forgot.js | 8 ++-- resources/assets/src/js/auth/login.js | 14 +++--- resources/assets/src/js/auth/register.js | 16 +++---- resources/assets/src/js/auth/reset.js | 10 ++-- resources/assets/src/js/common/i18n.js | 8 ++-- resources/assets/src/js/common/layout.js | 2 +- resources/assets/src/js/common/notify.js | 12 ++--- resources/assets/src/js/common/polyfill.js | 4 +- .../assets/src/js/common/texture-preview.js | 12 ++--- resources/assets/src/js/common/utils.js | 24 +++++----- resources/assets/src/js/skinlib/index.js | 30 ++++++------ resources/assets/src/js/skinlib/operations.js | 20 ++++---- resources/assets/src/js/skinlib/upload.js | 20 ++++---- resources/assets/src/js/user/closet.js | 46 +++++++++---------- resources/assets/src/js/user/player.js | 28 +++++------ resources/assets/src/js/user/profile.js | 32 ++++++------- resources/assets/src/js/user/sign.js | 2 +- 27 files changed, 199 insertions(+), 190 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..12d18299 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +node_modules/ +resources/assets/dist/ +resources/assets/src/vendor/ +gulpfile.js diff --git a/.eslintrc.js b/.eslintrc.js index 98df0bbf..b5f5022f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,10 @@ module.exports = { "object-curly-spacing": ["error", "always"], "no-unused-vars": "warn", "no-console": "off", - "comma-style": ["warn", "last"] + "comma-style": ["warn", "last"], + "prefer-const": "warn", + "no-var": "error", + "eqeqeq": "error", }, "globals": { "url": false, @@ -29,6 +32,7 @@ module.exports = { "parserOptions": { "ecmaVersion": 2017 }, + "root": true, "env":{ "node": true, "es6": true, diff --git a/gulpfile.js b/gulpfile.js index 4f850954..c424e6f4 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -181,6 +181,7 @@ gulp.task('zip', () => { 'LICENSE', '!.babelrc', '!.eslintrc.js', + '!.eslintignore', '!.travis.yml', '!{.env,.env.testing}', '!{.git,.git/**}', diff --git a/resources/assets/src/js/admin/customize.js b/resources/assets/src/js/admin/customize.js index 711f82df..53e49823 100644 --- a/resources/assets/src/js/admin/customize.js +++ b/resources/assets/src/js/admin/customize.js @@ -4,7 +4,7 @@ $('#layout-skins-list [data-skin]').click(function (e) { e.preventDefault(); - let skin_name = $(this).data('skin'); + const skin_name = $(this).data('skin'); $('body').removeClass(current_skin).addClass(skin_name); current_skin = skin_name; }); @@ -17,7 +17,7 @@ async function submitColor() { dataType: 'json', data: { color_scheme: current_skin } }); - errno == 0 ? toastr.success(msg) : toastr.warning(msg); + errno === 0 ? toastr.success(msg) : toastr.warning(msg); } catch (error) { showAjaxError(error); } diff --git a/resources/assets/src/js/admin/players.js b/resources/assets/src/js/admin/players.js index ba3fec8b..439363cb 100644 --- a/resources/assets/src/js/admin/players.js +++ b/resources/assets/src/js/admin/players.js @@ -11,14 +11,14 @@ async function changePreference() { preference: $(this).val() } }); - errno == 0 ? toastr.success(msg) : toastr.warning(msg); + errno === 0 ? toastr.success(msg) : toastr.warning(msg); } catch (error) { showAjaxError(error); } } function changeTexture(pid, playerName) { - let dom = ` + const dom = `
- - + + `; } }, @@ -169,7 +169,7 @@ function initPlayersTable() { searchable: false, orderable: false, render: (data, type, row) => { - let html = { steve: '', alex: '', cape: '' }; + const html = { steve: '', alex: '', cape: '' }; ['steve', 'alex', 'cape'].forEach(textureType => { if (row['tid_' + textureType] === 0) { html[textureType] = ``; @@ -247,7 +247,7 @@ function initPluginsTable() { searchable: false, orderable: false, render: (data, type, row) => { - let switchEnableButton, configViewButton, deletePluginButton; + let switchEnableButton, configViewButton; if (data.enabled) { switchEnableButton = ` ${trans('admin.disablePlugin')}`; @@ -262,7 +262,7 @@ function initPluginsTable() { configViewButton = ` ${trans('admin.configurePlugin')}`; } - deletePluginButton = ` + const deletePluginButton = ` ${trans('admin.deletePlugin')}`; return switchEnableButton + configViewButton + deletePluginButton; } diff --git a/resources/assets/src/js/admin/update.js b/resources/assets/src/js/admin/update.js index 3d885f43..e24837d0 100644 --- a/resources/assets/src/js/admin/update.js +++ b/resources/assets/src/js/admin/update.js @@ -91,8 +91,8 @@ function progressPolling(fileSize) { async function checkForUpdates() { try { const data = await fetch({ url: url('admin/update/check') }); - if (data.available == true) { - let dom = `v${data.latest}`; + if (data.available === true) { + const dom = `v${data.latest}`; $(`[href="${url('admin/update')}"]`).append(dom); } diff --git a/resources/assets/src/js/admin/users.js b/resources/assets/src/js/admin/users.js index 31fea525..4074823f 100644 --- a/resources/assets/src/js/admin/users.js +++ b/resources/assets/src/js/admin/users.js @@ -1,8 +1,8 @@ 'use strict'; async function changeUserEmail(uid) { - let dom = $(`tr#user-${uid} > td:nth-child(2)`), - newUserEmail = ''; + const dom = $(`tr#user-${uid} > td:nth-child(2)`); + let newUserEmail = ''; try { newUserEmail = await swal({ @@ -26,7 +26,7 @@ async function changeUserEmail(uid) { data: { uid: uid, email: newUserEmail } }); - if (errno == 0) { + if (errno === 0) { dom.text(newUserEmail); toastr.success(msg); @@ -39,8 +39,8 @@ async function changeUserEmail(uid) { } async function changeUserNickName(uid) { - let dom = $(`tr#user-${uid} > td:nth-child(3)`), - newNickName = ''; + const dom = $(`tr#user-${uid} > td:nth-child(3)`); + let newNickName = ''; try { newNickName = await swal({ @@ -63,7 +63,7 @@ async function changeUserNickName(uid) { dataType: 'json', data: { uid: uid, nickname: newNickName } }); - if (errno == 0) { + if (errno === 0) { dom.text(newNickName); toastr.success(msg); @@ -94,7 +94,7 @@ async function changeUserPwd(uid) { dataType: 'json', data: { uid: uid, password: password } }); - errno == 0 ? toastr.success(msg) : toastr.warning(msg); + errno === 0 ? toastr.success(msg) : toastr.warning(msg); } catch (error) { showAjaxError(error); } @@ -109,7 +109,7 @@ async function changeUserScore(uid, score) { // Handle id formatted as '#user-1234' data: { uid: uid.slice(5), score: score } }); - errno == 0 ? toastr.success(msg) : toastr.warning(msg); + errno === 0 ? toastr.success(msg) : toastr.warning(msg); } catch (error) { showAjaxError(error); } @@ -124,17 +124,17 @@ async function changeBanStatus(uid) { data: { uid: uid } }); - if (errno == 0) { - let dom = $(`#ban-${uid}`); + if (errno === 0) { + const dom = $(`#ban-${uid}`); - if (dom.attr('data') == 'banned') { + if (dom.attr('data') === 'banned') { dom.text(trans('admin.ban')).attr('data', 'normal'); } else { dom.text(trans('admin.unban')).attr('data', 'banned'); } $(`#user-${uid} > td.status`).text( - permission == -1 ? trans('admin.banned') : trans('admin.normal') + permission === -1 ? trans('admin.banned') : trans('admin.normal') ); toastr.success(msg); @@ -155,17 +155,17 @@ async function changeAdminStatus(uid) { data: { uid: uid } }); - if (errno == 0) { - let dom = $(`#admin-${uid}`); + if (errno === 0) { + const dom = $(`#admin-${uid}`); - if (dom.attr('data') == 'admin') { + if (dom.attr('data') === 'admin') { dom.text(trans('admin.setAdmin')).attr('data', 'normal'); } else { dom.text(trans('admin.unsetAdmin')).attr('data', 'admin'); } $(`#user-${uid} > td.status`).text( - (permission == 1) ? trans('admin.admin') : trans('admin.normal') + (permission === 1) ? trans('admin.admin') : trans('admin.normal') ); toastr.success(msg); @@ -196,7 +196,7 @@ async function deleteUserAccount(uid) { data: { uid: uid } }); - if (errno == 0) { + if (errno === 0) { $('tr#user-' + uid).remove(); toastr.success(msg); } else { @@ -209,7 +209,7 @@ async function deleteUserAccount(uid) { $('body').on('keypress', '.score', function(event){ // Change score when Enter key is pressed - if (event.which == 13) { + if (event.which === 13) { $(this).blur(); changeUserScore($(this).parent().parent().attr('id'), $(this).val()); } diff --git a/resources/assets/src/js/auth/captcha.js b/resources/assets/src/js/auth/captcha.js index c501668d..77a14728 100644 --- a/resources/assets/src/js/auth/captcha.js +++ b/resources/assets/src/js/auth/captcha.js @@ -1,7 +1,7 @@ 'use strict'; function refreshCaptcha() { - let timestamp = new Date().getTime(); + const timestamp = new Date().getTime(); // Refresh Captcha Image $('.captcha').attr('src', url(`auth/captcha?${timestamp}`)); // Clear input diff --git a/resources/assets/src/js/auth/forgot.js b/resources/assets/src/js/auth/forgot.js index b240af0e..cf391d77 100644 --- a/resources/assets/src/js/auth/forgot.js +++ b/resources/assets/src/js/auth/forgot.js @@ -5,18 +5,18 @@ $('#forgot-button').click(e => { e.preventDefault(); - let data = { + const data = { email: $('#email').val(), captcha: $('#captcha').val() }; (function validate({ email, captcha }, callback) { - if (email == '') { + if (email === '') { showMsg(trans('auth.emptyEmail')); $('#email').focus(); } else if (!/\S+@\S+\.\S+/.test(email)) { showMsg(trans('auth.invalidEmail'), 'warning'); - } else if (captcha == '') { + } else if (captcha === '') { showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); } else { @@ -35,7 +35,7 @@ $('#forgot-button').click(e => { ).prop('disabled', 'disabled'); } }); - if (errno == 0) { + if (errno === 0) { showMsg(msg, 'success'); $('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled'); } else { diff --git a/resources/assets/src/js/auth/login.js b/resources/assets/src/js/auth/login.js index c402b200..4219b4cd 100644 --- a/resources/assets/src/js/auth/login.js +++ b/resources/assets/src/js/auth/login.js @@ -5,24 +5,24 @@ $('#login-button').click(async e => { e.preventDefault(); - let data = { + const data = { identification: $('#identification').val(), password: $('#password').val(), keep: $('#keep').prop('checked') ? true : false }; - if (data.identification == '') { + if (data.identification === '') { showMsg(trans('auth.emptyIdentification')); $('#identification').focus(); - } else if (data.password == '') { + } else if (data.password === '') { showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else { // Verify it when captcha form is shown - if ($('#captcha-form').css('display') == 'block') { + if ($('#captcha-form').css('display') === 'block') { data.captcha = $('#captcha').val(); - if (data.captcha == '') { + if (data.captcha === '') { showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); return false; @@ -41,7 +41,7 @@ $('#login-button').click(async e => { ).prop('disabled', 'disabled'); } }); - if (errno == 0) { + if (errno === 0) { swal({ type: 'success', html: msg }); setTimeout(() => { @@ -49,7 +49,7 @@ $('#login-button').click(async e => { }, 1000); } else { if (login_fails > 3) { - if ($('#captcha-form').css('display') == 'none') { + if ($('#captcha-form').css('display') === 'none') { swal({ type: 'error', html: trans('auth.tooManyFails') }); $('#captcha-form').show(); diff --git a/resources/assets/src/js/auth/register.js b/resources/assets/src/js/auth/register.js index ec6463f3..4178715b 100644 --- a/resources/assets/src/js/auth/register.js +++ b/resources/assets/src/js/auth/register.js @@ -5,7 +5,7 @@ $('#register-button').click(e => { e.preventDefault(); - let data = { + const data = { email: $('#email').val(), password: $('#password').val(), nickname: $('#nickname').val(), @@ -15,27 +15,27 @@ $('#register-button').click(e => { (function validate({ email, password, nickname, captcha }, callback) { // Massive form validation - if (email == '') { + if (email === '') { showMsg(trans('auth.emptyEmail')); $('#email').focus(); } else if (!/\S+@\S+\.\S+/.test(email)) { showMsg(trans('auth.invalidEmail'), 'warning'); - } else if (password == '') { + } else if (password === '') { showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else if (password.length < 8 || password.length > 16) { showMsg(trans('auth.invalidPassword'), 'warning'); $('#password').focus(); - } else if ($('#confirm-pwd').val() == '') { + } else if ($('#confirm-pwd').val() === '') { showMsg(trans('auth.emptyConfirmPwd')); $('#confirm-pwd').focus(); - } else if (password != $('#confirm-pwd').val()) { + } else if (password !== $('#confirm-pwd').val()) { showMsg(trans('auth.invalidConfirmPwd'), 'warning'); $('#confirm-pwd').focus(); - } else if (nickname == '') { + } else if (nickname === '') { showMsg(trans('auth.emptyNickname')); $('#nickname').focus(); - } else if (captcha == '') { + } else if (captcha === '') { showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); } else { @@ -56,7 +56,7 @@ $('#register-button').click(e => { ).prop('disabled', 'disabled'); } }); - if (errno == 0) { + if (errno === 0) { swal({ type: 'success', html: msg }) .then(() => window.location = url('user')); diff --git a/resources/assets/src/js/auth/reset.js b/resources/assets/src/js/auth/reset.js index 28bfe00e..49728d13 100644 --- a/resources/assets/src/js/auth/reset.js +++ b/resources/assets/src/js/auth/reset.js @@ -3,23 +3,23 @@ $('#reset-button').click(e => { e.preventDefault(); - let data = { + const data = { uid: $('#uid').val(), password: $('#password').val(), token: getQueryString('token') }; (function validate({ password }, callback) { - if (password == '') { + if (password === '') { showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else if (password.length < 8 || password.length > 16) { showMsg(trans('auth.invalidPassword'), 'warning'); $('#password').focus(); - } else if ($('#confirm-pwd').val() == '') { + } else if ($('#confirm-pwd').val() === '') { showMsg(trans('auth.emptyConfirmPwd')); $('#confirm-pwd').focus(); - } else if (password != $('#confirm-pwd').val()) { + } else if (password !== $('#confirm-pwd').val()) { showMsg(trans('auth.invalidConfirmPwd'), 'warning'); $('#confirm-pwd').focus(); } else { @@ -38,7 +38,7 @@ $('#reset-button').click(e => { ).prop('disabled', 'disabled'); } }); - if (errno == 0) { + if (errno === 0) { swal({ type: 'success', html: msg diff --git a/resources/assets/src/js/common/i18n.js b/resources/assets/src/js/common/i18n.js index 6afc4554..6671fde9 100644 --- a/resources/assets/src/js/common/i18n.js +++ b/resources/assets/src/js/common/i18n.js @@ -9,7 +9,7 @@ $.currentLocale = Object.create(null); * @return void */ function loadLocales() { - for (let lang in $.locales) { + for (const lang in $.locales) { if (!isEmpty($.locales[lang])) { $.currentLocale = $.locales[lang] || Object.create(null); } @@ -28,10 +28,10 @@ function trans(key, parameters = {}) { loadLocales(); } - let segments = key.split('.'); + const segments = key.split('.'); let temp = $.currentLocale || {}; - for (let i in segments) { + for (const i in segments) { if (isEmpty(temp[segments[i]])) { return key; } else { @@ -39,7 +39,7 @@ function trans(key, parameters = {}) { } } - for (let i in parameters) { + for (const i in parameters) { if (!isEmpty(parameters[i])) { temp = temp.replace(':'+i, parameters[i]); } diff --git a/resources/assets/src/js/common/layout.js b/resources/assets/src/js/common/layout.js index 2633d22e..b6050b50 100644 --- a/resources/assets/src/js/common/layout.js +++ b/resources/assets/src/js/common/layout.js @@ -30,7 +30,7 @@ $(document).ready(() => { }); function activateLayout() { - if (location.pathname == '/' || location.pathname.includes('auth')) + if (location.pathname === '/' || location.pathname.includes('auth')) return; $.AdminLTE.layout.activate(); diff --git a/resources/assets/src/js/common/notify.js b/resources/assets/src/js/common/notify.js index 4018361a..02abae7a 100644 --- a/resources/assets/src/js/common/notify.js +++ b/resources/assets/src/js/common/notify.js @@ -18,7 +18,7 @@ function showMsg(msg, type = 'info') { * @return {void} */ function showAjaxError(json) { - if (typeof json == 'string') { + if (typeof json === 'string') { return console.warn(json); } @@ -39,12 +39,12 @@ function showAjaxError(json) { * @return {void} */ function showModal(msg, title = 'Message', type = 'default', options = {}) { - let btnType = (type != 'default') ? 'btn-outline' : 'btn-primary'; - let btnText = options.btnText || 'OK'; - let onClick = (options.callback === undefined) ? 'data-dismiss="modal"' : `onclick="${options.callback}"`; - let destroyOnClose = (options.destroyOnClose === false) ? false : true; + const btnType = (type !== 'default') ? 'btn-outline' : 'btn-primary'; + const btnText = options.btnText || 'OK'; + const onClick = (options.callback === undefined) ? 'data-dismiss="modal"' : `onclick="${options.callback}"`; + const destroyOnClose = (options.destroyOnClose === false) ? false : true; - let dom = ` + const dom = `