Skip to content

Commit e684c62

Browse files
authored
feat(lint): fix else with return, unify ternary CS
Modernize code using eslint advisory introduced in GH-2596. This PR focuses on fixing rules like no-else-return and other ternary operator related.
1 parent e658be3 commit e684c62

File tree

31 files changed

+376
-386
lines changed

31 files changed

+376
-386
lines changed

.eslintrc.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,11 @@ module.exports = {
118118
'unicorn/no-nested-ternary': 'off', // about 80 errors (except 14 nested ternary operators autofixable)
119119

120120
// TODO
121-
'import/extensions': 'off',
122-
'import/no-dynamic-require': 'off',
123121
'no-bitwise': 'off',
124122
'no-cond-assign': 'off',
125-
'no-else-return': 'off',
126123
'no-empty': 'off',
127124
'no-labels': 'off',
128125
'no-loop-func': 'off',
129-
'no-mixed-operators': 'off',
130-
'no-multi-assign': 'off',
131126
'no-new-func': 'off',
132127
'no-path-concat': 'off',
133128
'no-prototype-builtins': 'off',
@@ -136,7 +131,6 @@ module.exports = {
136131
'no-shadow-restricted-names': 'off',
137132
'no-unused-expressions': 'off',
138133
'no-use-before-define': 'off',
139-
'unicorn/consistent-function-scoping': 'off',
140134
'unicorn/empty-brace-spaces': 'off',
141135
'unicorn/escape-case': 'off',
142136
'unicorn/new-for-builtins': 'off',
@@ -147,12 +141,9 @@ module.exports = {
147141
'unicorn/no-useless-undefined': 'off',
148142
'unicorn/prefer-array-find': 'off',
149143
'unicorn/prefer-default-parameters': 'off',
150-
'unicorn/prefer-logical-operator-over-ternary': 'off',
151144
'unicorn/prefer-native-coercion-functions': 'off',
152145
'unicorn/prefer-negative-index': 'off',
153-
'unicorn/prefer-regexp-test': 'off',
154146
'unicorn/prefer-switch': 'off',
155-
'unicorn/prefer-ternary': 'off',
156147
},
157148
reportUnusedDisableDirectives: true,
158149
globals: {

src/definitions/behaviors/api.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
? window
2424
: globalThis;
2525

26-
$.api = $.fn.api = function (parameters) {
26+
$.fn.api = function (parameters) {
2727
var
2828
// use window context if none specified
2929
$allModules = isFunction(this)
@@ -207,10 +207,10 @@
207207
module.error(error.beforeSend);
208208

209209
return;
210-
} else {
211-
module.cancelled = false;
212210
}
213211

212+
module.cancelled = false;
213+
214214
// get url
215215
url = module.get.templatedURL();
216216

@@ -304,11 +304,11 @@
304304
module.verbose('XHR request determined to be aborted');
305305

306306
return true;
307-
} else {
308-
module.verbose('XHR request was not aborted');
309-
310-
return false;
311307
}
308+
309+
module.verbose('XHR request was not aborted');
310+
311+
return false;
312312
},
313313
validResponse: function (response) {
314314
if ((!module.is.expectingJSON()) || !isFunction(settings.successTest)) {
@@ -321,11 +321,11 @@
321321
module.debug('Response passed success test', response);
322322

323323
return true;
324-
} else {
325-
module.debug('Response failed success test', response);
326-
327-
return false;
328324
}
325+
326+
module.debug('Response failed success test', response);
327+
328+
return false;
329329
},
330330
},
331331

@@ -376,13 +376,13 @@
376376
url = false;
377377

378378
return false;
379-
} else {
380-
module.verbose('Found required variable', variable, value);
381-
value = (settings.encodeParameters)
382-
? module.get.urlEncodedValue(value)
383-
: value;
384-
url = url.replace(templatedString, value);
385379
}
380+
381+
module.verbose('Found required variable', variable, value);
382+
value = (settings.encodeParameters)
383+
? module.get.urlEncodedValue(value)
384+
: value;
385+
url = url.replace(templatedString, value);
386386
});
387387
}
388388
if (optionalVariables) {
@@ -408,11 +408,9 @@
408408
} else {
409409
module.verbose('Optional variable not found', variable);
410410
// remove preceding slash if set
411-
if (url.indexOf('/' + templatedString) !== -1) {
412-
url = url.replace('/' + templatedString, '');
413-
} else {
414-
url = url.replace(templatedString, '');
415-
}
411+
url = url.indexOf('/' + templatedString) !== -1
412+
? url.replace('/' + templatedString, '')
413+
: url.replace(templatedString, '');
416414
}
417415
});
418416
}
@@ -620,7 +618,8 @@
620618
settings.onAbort.call(context, status, $module, xhr);
621619

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

837836
return false;
838-
} else if (settings.on == 'auto') {
837+
}
838+
if (settings.on == 'auto') {
839839
if ($module.is('input')) {
840840
return (element.oninput !== undefined)
841841
? 'input'
842842
: (element.onpropertychange !== undefined)
843843
? 'propertychange'
844844
: 'keyup';
845-
} else if ($module.is('form')) {
845+
}
846+
if ($module.is('form')) {
846847
return 'submit';
847-
} else {
848-
return 'click';
849848
}
850-
} else {
851-
return settings.on;
849+
850+
return 'click';
852851
}
852+
853+
return settings.on;
853854
},
854855
templatedURL: function (action) {
855856
action = action || $module.data(metadata.action) || settings.action || false;
@@ -1061,6 +1062,7 @@
10611062
? returnedValue
10621063
: this;
10631064
};
1065+
$.api = $.fn.api;
10641066

10651067
$.api.settings = {
10661068

src/definitions/behaviors/form.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,9 @@
277277
isDirty
278278
;
279279

280-
if (isCheckbox) {
281-
isDirty = module.is.checkboxDirty($el);
282-
} else {
283-
isDirty = module.is.fieldDirty($el);
284-
}
280+
isDirty = isCheckbox
281+
? module.is.checkboxDirty($el)
282+
: module.is.fieldDirty($el);
285283

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

@@ -307,11 +305,12 @@
307305
empty: function ($field) {
308306
if (!$field || $field.length === 0) {
309307
return true;
310-
} else if ($field.is(selector.checkbox)) {
308+
}
309+
if ($field.is(selector.checkbox)) {
311310
return !$field.is(':checked');
312-
} else {
313-
return module.is.blank($field);
314311
}
312+
313+
return module.is.blank($field);
315314
},
316315
blank: function ($field) {
317316
return String($field.val()).trim() === '';
@@ -324,16 +323,16 @@
324323
module.verbose('Checking if field is valid', field);
325324

326325
return module.validate.field(validation[field], field, !!showErrors);
327-
} else {
328-
module.verbose('Checking if form is valid');
329-
$.each(validation, function (fieldName, field) {
330-
if (!module.is.valid(fieldName, showErrors)) {
331-
allValid = false;
332-
}
333-
});
334-
335-
return allValid;
336326
}
327+
328+
module.verbose('Checking if form is valid');
329+
$.each(validation, function (fieldName, field) {
330+
if (!module.is.valid(fieldName, showErrors)) {
331+
allValid = false;
332+
}
333+
});
334+
335+
return allValid;
337336
},
338337
dirty: function () {
339338
return dirty;
@@ -488,9 +487,9 @@
488487
changeEvent: function (type, $input) {
489488
if (type == 'checkbox' || type == 'radio' || type == 'hidden' || $input.is('select')) {
490489
return 'change';
491-
} else {
492-
return module.get.inputEvent();
493490
}
491+
492+
return module.get.inputEvent();
494493
},
495494
inputEvent: function () {
496495
return (document.createElement('input').oninput !== undefined)
@@ -721,11 +720,7 @@
721720
: false;
722721
}
723722
} else if (isCheckbox) {
724-
if (isChecked) {
725-
values[name] = value || true;
726-
} else {
727-
values[name] = false;
728-
}
723+
values[name] = isChecked ? value || true : false;
729724
} else if (isCalendar) {
730725
var date = $calendar.calendar('get date');
731726

@@ -1682,7 +1677,7 @@
16821677

16831678
// is not empty or blank string
16841679
empty: function (value) {
1685-
return !(value === undefined || value === '' || Array.isArray(value) && value.length === 0);
1680+
return !(value === undefined || value === '' || (Array.isArray(value) && value.length === 0));
16861681
},
16871682

16881683
// checkbox checked
@@ -1744,10 +1739,12 @@
17441739
parts
17451740
;
17461741
if (!range || ['', '..'].indexOf(range) !== -1) {
1742+
17471743
// do nothing
17481744
} else if (range.indexOf('..') == -1) {
17491745
if (regExp.test(range)) {
1750-
min = max = range - 0;
1746+
min = range - 0;
1747+
max = min;
17511748
}
17521749
} else {
17531750
parts = range.split('..', 2);

src/definitions/globals/site.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
? window
2020
: globalThis;
2121

22-
$.site = $.fn.site = function (parameters) {
22+
$.fn.site = function (parameters) {
2323
var
2424
time = Date.now(),
2525
performance = [],
@@ -417,6 +417,7 @@
417417
? returnedValue
418418
: this;
419419
};
420+
$.site = $.fn.site;
420421

421422
$.site.settings = {
422423

0 commit comments

Comments
 (0)