Skip to content

Commit

Permalink
feat(lint): fix else with return, unify ternary CS
Browse files Browse the repository at this point in the history
Modernize code using eslint advisory introduced in GH-2596. This PR focuses on fixing rules like no-else-return and other ternary operator related.
  • Loading branch information
mvorisek authored Dec 12, 2022
1 parent e658be3 commit e684c62
Show file tree
Hide file tree
Showing 31 changed files with 376 additions and 386 deletions.
9 changes: 0 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,11 @@ module.exports = {
'unicorn/no-nested-ternary': 'off', // about 80 errors (except 14 nested ternary operators autofixable)

// TODO
'import/extensions': 'off',
'import/no-dynamic-require': 'off',
'no-bitwise': 'off',
'no-cond-assign': 'off',
'no-else-return': 'off',
'no-empty': 'off',
'no-labels': 'off',
'no-loop-func': 'off',
'no-mixed-operators': 'off',
'no-multi-assign': 'off',
'no-new-func': 'off',
'no-path-concat': 'off',
'no-prototype-builtins': 'off',
Expand All @@ -136,7 +131,6 @@ module.exports = {
'no-shadow-restricted-names': 'off',
'no-unused-expressions': 'off',
'no-use-before-define': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/empty-brace-spaces': 'off',
'unicorn/escape-case': 'off',
'unicorn/new-for-builtins': 'off',
Expand All @@ -147,12 +141,9 @@ module.exports = {
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-array-find': 'off',
'unicorn/prefer-default-parameters': 'off',
'unicorn/prefer-logical-operator-over-ternary': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
'unicorn/prefer-negative-index': 'off',
'unicorn/prefer-regexp-test': 'off',
'unicorn/prefer-switch': 'off',
'unicorn/prefer-ternary': 'off',
},
reportUnusedDisableDirectives: true,
globals: {
Expand Down
60 changes: 31 additions & 29 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
? window
: globalThis;

$.api = $.fn.api = function (parameters) {
$.fn.api = function (parameters) {
var
// use window context if none specified
$allModules = isFunction(this)
Expand Down Expand Up @@ -207,10 +207,10 @@
module.error(error.beforeSend);

return;
} else {
module.cancelled = false;
}

module.cancelled = false;

// get url
url = module.get.templatedURL();

Expand Down Expand Up @@ -304,11 +304,11 @@
module.verbose('XHR request determined to be aborted');

return true;
} else {
module.verbose('XHR request was not aborted');

return false;
}

module.verbose('XHR request was not aborted');

return false;
},
validResponse: function (response) {
if ((!module.is.expectingJSON()) || !isFunction(settings.successTest)) {
Expand All @@ -321,11 +321,11 @@
module.debug('Response passed success test', response);

return true;
} else {
module.debug('Response failed success test', response);

return false;
}

module.debug('Response failed success test', response);

return false;
},
},

Expand Down Expand Up @@ -376,13 +376,13 @@
url = false;

return false;
} else {
module.verbose('Found required variable', variable, value);
value = (settings.encodeParameters)
? module.get.urlEncodedValue(value)
: value;
url = url.replace(templatedString, value);
}

module.verbose('Found required variable', variable, value);
value = (settings.encodeParameters)
? module.get.urlEncodedValue(value)
: value;
url = url.replace(templatedString, value);
});
}
if (optionalVariables) {
Expand All @@ -408,11 +408,9 @@
} else {
module.verbose('Optional variable not found', variable);
// remove preceding slash if set
if (url.indexOf('/' + templatedString) !== -1) {
url = url.replace('/' + templatedString, '');
} else {
url = url.replace(templatedString, '');
}
url = url.indexOf('/' + templatedString) !== -1
? url.replace('/' + templatedString, '')
: url.replace(templatedString, '');
}
});
}
Expand Down Expand Up @@ -620,7 +618,8 @@
settings.onAbort.call(context, status, $module, xhr);

return true;
} else if (status == 'invalid') {
}
if (status == 'invalid') {
module.debug('JSON did not pass success test. A server-side error has most likely occurred', response);
} else if (status == 'error') {
if (xhr !== undefined) {
Expand Down Expand Up @@ -835,21 +834,23 @@
module.debug('API called without element, no events attached');

return false;
} else if (settings.on == 'auto') {
}
if (settings.on == 'auto') {
if ($module.is('input')) {
return (element.oninput !== undefined)
? 'input'
: (element.onpropertychange !== undefined)
? 'propertychange'
: 'keyup';
} else if ($module.is('form')) {
}
if ($module.is('form')) {
return 'submit';
} else {
return 'click';
}
} else {
return settings.on;

return 'click';
}

return settings.on;
},
templatedURL: function (action) {
action = action || $module.data(metadata.action) || settings.action || false;
Expand Down Expand Up @@ -1061,6 +1062,7 @@
? returnedValue
: this;
};
$.api = $.fn.api;

$.api.settings = {

Expand Down
49 changes: 23 additions & 26 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,9 @@
isDirty
;

if (isCheckbox) {
isDirty = module.is.checkboxDirty($el);
} else {
isDirty = module.is.fieldDirty($el);
}
isDirty = isCheckbox
? module.is.checkboxDirty($el)
: module.is.fieldDirty($el);

$el.data(settings.metadata.isDirty, isDirty);

Expand All @@ -307,11 +305,12 @@
empty: function ($field) {
if (!$field || $field.length === 0) {
return true;
} else if ($field.is(selector.checkbox)) {
}
if ($field.is(selector.checkbox)) {
return !$field.is(':checked');
} else {
return module.is.blank($field);
}

return module.is.blank($field);
},
blank: function ($field) {
return String($field.val()).trim() === '';
Expand All @@ -324,16 +323,16 @@
module.verbose('Checking if field is valid', field);

return module.validate.field(validation[field], field, !!showErrors);
} else {
module.verbose('Checking if form is valid');
$.each(validation, function (fieldName, field) {
if (!module.is.valid(fieldName, showErrors)) {
allValid = false;
}
});

return allValid;
}

module.verbose('Checking if form is valid');
$.each(validation, function (fieldName, field) {
if (!module.is.valid(fieldName, showErrors)) {
allValid = false;
}
});

return allValid;
},
dirty: function () {
return dirty;
Expand Down Expand Up @@ -488,9 +487,9 @@
changeEvent: function (type, $input) {
if (type == 'checkbox' || type == 'radio' || type == 'hidden' || $input.is('select')) {
return 'change';
} else {
return module.get.inputEvent();
}

return module.get.inputEvent();
},
inputEvent: function () {
return (document.createElement('input').oninput !== undefined)
Expand Down Expand Up @@ -721,11 +720,7 @@
: false;
}
} else if (isCheckbox) {
if (isChecked) {
values[name] = value || true;
} else {
values[name] = false;
}
values[name] = isChecked ? value || true : false;
} else if (isCalendar) {
var date = $calendar.calendar('get date');

Expand Down Expand Up @@ -1682,7 +1677,7 @@

// is not empty or blank string
empty: function (value) {
return !(value === undefined || value === '' || Array.isArray(value) && value.length === 0);
return !(value === undefined || value === '' || (Array.isArray(value) && value.length === 0));
},

// checkbox checked
Expand Down Expand Up @@ -1744,10 +1739,12 @@
parts
;
if (!range || ['', '..'].indexOf(range) !== -1) {

// do nothing
} else if (range.indexOf('..') == -1) {
if (regExp.test(range)) {
min = max = range - 0;
min = range - 0;
max = min;
}
} else {
parts = range.split('..', 2);
Expand Down
3 changes: 2 additions & 1 deletion src/definitions/globals/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
? window
: globalThis;

$.site = $.fn.site = function (parameters) {
$.fn.site = function (parameters) {
var
time = Date.now(),
performance = [],
Expand Down Expand Up @@ -417,6 +417,7 @@
? returnedValue
: this;
};
$.site = $.fn.site;

$.site.settings = {

Expand Down
Loading

0 comments on commit e684c62

Please sign in to comment.