Skip to content

Commit 27384cb

Browse files
authored
feat(lint): fix various eslint rules
Modernize code using eslint advisory introduced in GH-2596. This PR fixes several smaller rules not worth a separate PRs.
1 parent e684c62 commit 27384cb

File tree

24 files changed

+167
-145
lines changed

24 files changed

+167
-145
lines changed

.eslintrc.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ module.exports = {
8080
'unicorn/no-null': 'off',
8181
'unicorn/no-this-assignment': 'off',
8282
'unicorn/numeric-separators-style': 'off',
83+
'unicorn/prefer-array-find': 'off',
8384
'unicorn/prefer-array-some': 'off', // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2007
8485
'unicorn/prefer-module': 'off',
8586
'unicorn/prevent-abbreviations': 'off',
@@ -118,32 +119,10 @@ module.exports = {
118119
'unicorn/no-nested-ternary': 'off', // about 80 errors (except 14 nested ternary operators autofixable)
119120

120121
// TODO
121-
'no-bitwise': 'off',
122-
'no-cond-assign': 'off',
123-
'no-empty': 'off',
124-
'no-labels': 'off',
125-
'no-loop-func': 'off',
126-
'no-new-func': 'off',
127-
'no-path-concat': 'off',
128-
'no-prototype-builtins': 'off',
129122
'no-return-assign': 'off',
130-
'no-self-assign': 'off',
131123
'no-shadow-restricted-names': 'off',
132-
'no-unused-expressions': 'off',
133124
'no-use-before-define': 'off',
134-
'unicorn/empty-brace-spaces': 'off',
135-
'unicorn/escape-case': 'off',
136-
'unicorn/new-for-builtins': 'off',
137-
'unicorn/no-hex-escape': 'off',
138-
'unicorn/no-instanceof-array': 'off',
139-
'unicorn/no-process-exit': 'off',
140-
'unicorn/no-useless-switch-case': 'off',
141-
'unicorn/no-useless-undefined': 'off',
142-
'unicorn/prefer-array-find': 'off',
143-
'unicorn/prefer-default-parameters': 'off',
144-
'unicorn/prefer-native-coercion-functions': 'off',
145125
'unicorn/prefer-negative-index': 'off',
146-
'unicorn/prefer-switch': 'off',
147126
},
148127
reportUnusedDisableDirectives: true,
149128
globals: {

scripts/nightly-version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const getNightlyVersion = async function () {
5959
actions.setOutput('shouldPublish', false);
6060

6161
console.log('No new commits since last publish. Exiting.');
62-
process.exit(0);
62+
process.exit(0); // eslint-disable-line unicorn/no-process-exit
6363

6464
return;
6565
}

src/definitions/behaviors/api.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@
461461
? floatValue
462462
: (el.value === 'false' ? false : el.value)),
463463
nameKeys = el.name.match(settings.regExp.key) || [],
464-
k,
465464
pushKey = el.name.replace(/\[]$/, '')
466465
;
467466
if (!(pushKey in pushes)) {
@@ -476,7 +475,9 @@
476475
value = pushValues[pushKey];
477476
}
478477

479-
while ((k = nameKeys.pop()) !== undefined) {
478+
while (nameKeys.length > 0) {
479+
var k = nameKeys.pop();
480+
480481
if (k == '' && !Array.isArray(value)) { // foo[]
481482
value = build([], pushes[pushKey]++, value);
482483
} else if (settings.regExp.fixed.test(k)) { // foo[n]
@@ -820,9 +821,7 @@
820821
if (!isWindow(element)) {
821822
if (module.is.input()) {
822823
data.value = $module.val();
823-
} else if (module.is.form()) {
824-
825-
} else {
824+
} else if (!module.is.form()) {
826825
data.text = $module.text();
827826
}
828827
}

src/definitions/behaviors/form.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@
136136
},
137137

138138
attachEvents: function (selector, action) {
139-
action = action || 'submit';
139+
if (!action) {
140+
action = 'submit';
141+
}
142+
140143
$(selector).on('click' + eventNamespace, function (event) {
141144
module[action]();
142145
event.preventDefault();
@@ -283,7 +286,7 @@
283286

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

286-
formIsDirty |= isDirty;
289+
formIsDirty = formIsDirty || isDirty;
287290
});
288291

289292
if (formIsDirty) {
@@ -618,16 +621,20 @@
618621
module.verbose('Finding field with identifier', identifier);
619622
identifier = module.escape.string(identifier);
620623
var t;
621-
if ((t = $field.filter('#' + identifier)).length > 0) {
624+
t = $field.filter('#' + identifier);
625+
if (t.length > 0) {
622626
return t;
623627
}
624-
if ((t = $field.filter('[name="' + identifier + '"]')).length > 0) {
628+
t = $field.filter('[name="' + identifier + '"]');
629+
if (t.length > 0) {
625630
return t;
626631
}
627-
if ((t = $field.filter('[name="' + identifier + '[]"]')).length > 0) {
632+
t = $field.filter('[name="' + identifier + '[]"]');
633+
if (t.length > 0) {
628634
return t;
629635
}
630-
if ((t = $field.filter('[data-' + metadata.validate + '="' + identifier + '"]')).length > 0) {
636+
t = $field.filter('[data-' + metadata.validate + '="' + identifier + '"]');
637+
if (t.length > 0) {
631638
return t;
632639
}
633640
module.error(error.noField.replace('{identifier}', identifier));
@@ -1866,14 +1873,24 @@
18661873
matchingValue,
18671874
matchingElement
18681875
;
1869-
if ((matchingElement = $module.find('[data-validate="' + identifier + '"]')).length > 0) {
1870-
matchingValue = matchingElement.val();
1871-
} else if ((matchingElement = $module.find('#' + identifier)).length > 0) {
1876+
matchingElement = $module.find('[data-validate="' + identifier + '"]');
1877+
if (matchingElement.length > 0) {
18721878
matchingValue = matchingElement.val();
1873-
} else if ((matchingElement = $module.find('[name="' + identifier + '"]')).length > 0) {
1874-
matchingValue = matchingElement.val();
1875-
} else if ((matchingElement = $module.find('[name="' + identifier + '[]"]')).length > 0) {
1876-
matchingValue = matchingElement;
1879+
} else {
1880+
matchingElement = $module.find('#' + identifier);
1881+
if (matchingElement.length > 0) {
1882+
matchingValue = matchingElement.val();
1883+
} else {
1884+
matchingElement = $module.find('[name="' + identifier + '"]');
1885+
if (matchingElement.length > 0) {
1886+
matchingValue = matchingElement.val();
1887+
} else {
1888+
matchingElement = $module.find('[name="' + identifier + '[]"]');
1889+
if (matchingElement.length > 0) {
1890+
matchingValue = matchingElement;
1891+
}
1892+
}
1893+
}
18771894
}
18781895

18791896
return (matchingValue !== undefined)
@@ -1888,14 +1905,24 @@
18881905
matchingValue,
18891906
matchingElement
18901907
;
1891-
if ((matchingElement = $module.find('[data-validate="' + identifier + '"]')).length > 0) {
1892-
matchingValue = matchingElement.val();
1893-
} else if ((matchingElement = $module.find('#' + identifier)).length > 0) {
1894-
matchingValue = matchingElement.val();
1895-
} else if ((matchingElement = $module.find('[name="' + identifier + '"]')).length > 0) {
1908+
matchingElement = $module.find('[data-validate="' + identifier + '"]');
1909+
if (matchingElement.length > 0) {
18961910
matchingValue = matchingElement.val();
1897-
} else if ((matchingElement = $module.find('[name="' + identifier + '[]"]')).length > 0) {
1898-
matchingValue = matchingElement;
1911+
} else {
1912+
matchingElement = $module.find('#' + identifier);
1913+
if (matchingElement.length > 0) {
1914+
matchingValue = matchingElement.val();
1915+
} else {
1916+
matchingElement = $module.find('[name="' + identifier + '"]');
1917+
if (matchingElement.length > 0) {
1918+
matchingValue = matchingElement.val();
1919+
} else {
1920+
matchingElement = $module.find('[name="' + identifier + '[]"]');
1921+
if (matchingElement.length > 0) {
1922+
matchingValue = matchingElement;
1923+
}
1924+
}
1925+
}
18991926
}
19001927

19011928
return (matchingValue !== undefined)
@@ -2005,7 +2032,7 @@
20052032
;
20062033
while (length--) {
20072034
sum += producedValue[multiple][parseInt(cardNumber.charAt(length), 10)];
2008-
multiple ^= 1;
2035+
multiple ^= 1; // eslint-disable-line no-bitwise
20092036
}
20102037

20112038
return (sum % 10 === 0 && sum > 0);

src/definitions/behaviors/visibility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
},
228228

229229
precache: function (images, callback) {
230-
if (!(images instanceof Array)) {
230+
if (!(Array.isArray(images))) {
231231
images = [images];
232232
}
233233
var

src/definitions/modules/calendar.js

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -643,38 +643,51 @@
643643

644644
if (module.popup('is visible')) {
645645
var mode = module.get.mode();
646-
if (keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40) {
646+
switch (keyCode) {
647647
// arrow keys
648-
var bigIncrement = mode === 'day' ? 7 : mode === 'hour' ? 4 : mode === 'minute' ? timeGap.column : 3;
649-
var increment = keyCode === 37 ? -1 : keyCode === 38 ? -bigIncrement : keyCode == 39 ? 1 : bigIncrement;
650-
increment *= mode === 'minute' ? settings.minTimeGap : 1;
651-
var focusDate = module.get.focusDate() || module.get.date() || new Date();
652-
var year = focusDate.getFullYear() + (mode === 'year' ? increment : 0);
653-
var month = focusDate.getMonth() + (mode === 'month' ? increment : 0);
654-
var day = focusDate.getDate() + (mode === 'day' ? increment : 0);
655-
var hour = focusDate.getHours() + (mode === 'hour' ? increment : 0);
656-
var minute = focusDate.getMinutes() + (mode === 'minute' ? increment : 0);
657-
var newFocusDate = new Date(year, month, day, hour, minute);
658-
if (settings.type === 'time') {
659-
newFocusDate = module.helper.mergeDateTime(focusDate, newFocusDate);
660-
}
661-
if (module.helper.isDateInRange(newFocusDate, mode)) {
662-
module.set.focusDate(newFocusDate);
648+
case 37:
649+
case 38:
650+
case 39:
651+
case 40: {
652+
var bigIncrement = mode === 'day' ? 7 : mode === 'hour' ? 4 : mode === 'minute' ? timeGap.column : 3;
653+
var increment = keyCode === 37 ? -1 : keyCode === 38 ? -bigIncrement : keyCode == 39 ? 1 : bigIncrement;
654+
increment *= mode === 'minute' ? settings.minTimeGap : 1;
655+
var focusDate = module.get.focusDate() || module.get.date() || new Date();
656+
var year = focusDate.getFullYear() + (mode === 'year' ? increment : 0);
657+
var month = focusDate.getMonth() + (mode === 'month' ? increment : 0);
658+
var day = focusDate.getDate() + (mode === 'day' ? increment : 0);
659+
var hour = focusDate.getHours() + (mode === 'hour' ? increment : 0);
660+
var minute = focusDate.getMinutes() + (mode === 'minute' ? increment : 0);
661+
var newFocusDate = new Date(year, month, day, hour, minute);
662+
if (settings.type === 'time') {
663+
newFocusDate = module.helper.mergeDateTime(focusDate, newFocusDate);
664+
}
665+
if (module.helper.isDateInRange(newFocusDate, mode)) {
666+
module.set.focusDate(newFocusDate);
667+
}
668+
669+
break;
663670
}
664-
} else if (keyCode === 13) {
665-
// enter
666-
var date = module.get.focusDate();
667-
if (date && !settings.isDisabled(date, mode) && !module.helper.isDisabled(date, mode) && module.helper.isEnabled(date, mode)) {
668-
if (settings.onSelect.call(element, date, module.get.mode()) !== false) {
669-
module.selectDate(date);
671+
// enter key
672+
case 13: {
673+
var date = module.get.focusDate();
674+
if (date && !settings.isDisabled(date, mode) && !module.helper.isDisabled(date, mode) && module.helper.isEnabled(date, mode)) {
675+
if (settings.onSelect.call(element, date, module.get.mode()) !== false) {
676+
module.selectDate(date);
677+
}
670678
}
679+
// disable form submission:
680+
event.preventDefault();
681+
event.stopPropagation();
682+
683+
break;
671684
}
672-
// disable form submission:
673-
event.preventDefault();
674-
event.stopPropagation();
675-
} else if (keyCode === 27) {
676-
module.popup('hide');
677-
event.stopPropagation();
685+
// escape key
686+
case 27:
687+
module.popup('hide');
688+
event.stopPropagation();
689+
690+
break;
678691
}
679692
}
680693

@@ -1320,7 +1333,10 @@
13201333
return date;
13211334
},
13221335
dateDiff: function (date1, date2, mode) {
1323-
mode = mode || 'day';
1336+
if (!mode) {
1337+
mode = 'day';
1338+
}
1339+
13241340
var isTimeOnly = settings.type === 'time';
13251341
var isYear = mode === 'year';
13261342
var isYearOrMonth = isYear || mode === 'month';
@@ -1643,8 +1659,7 @@
16431659
today: function (settings) {
16441660
return settings.type === 'date' ? settings.text.today : settings.text.now;
16451661
},
1646-
cell: function (cell, date, cellOptions) {
1647-
},
1662+
cell: function (cell, date, cellOptions) {},
16481663
},
16491664

16501665
parser: {
@@ -1684,7 +1699,7 @@
16841699
month = -1,
16851700
year = -1
16861701
;
1687-
var isAm = undefined;
1702+
var isAm;
16881703

16891704
var isTimeOnly = settings.type === 'time';
16901705
var isDateOnly = settings.type.indexOf('time') < 0;
@@ -1897,28 +1912,22 @@
18971912
},
18981913

18991914
// callback when date changes
1900-
onChange: function (date, text, mode) {
1901-
},
1915+
onChange: function (date, text, mode) {},
19021916

19031917
// callback before show animation, return false to prevent show
1904-
onShow: function () {
1905-
},
1918+
onShow: function () {},
19061919

19071920
// callback after show animation
1908-
onVisible: function () {
1909-
},
1921+
onVisible: function () {},
19101922

19111923
// callback before hide animation, return false to prevent hide
1912-
onHide: function () {
1913-
},
1924+
onHide: function () {},
19141925

19151926
// callback after hide animation
1916-
onHidden: function () {
1917-
},
1927+
onHidden: function () {},
19181928

19191929
// callback before item is selected, return false to prevent selection
1920-
onSelect: function (date, mode) {
1921-
},
1930+
onSelect: function (date, mode) {},
19221931

19231932
// is the given date disabled?
19241933
isDisabled: function (date, mode) {

src/definitions/modules/dropdown.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -967,17 +967,22 @@
967967
if (queryLength === termLength) {
968968
return (query === term);
969969
}
970-
search: for (var characterIndex = 0, nextCharacterIndex = 0; characterIndex < queryLength; characterIndex++) {
970+
for (var characterIndex = 0, nextCharacterIndex = 0; characterIndex < queryLength; characterIndex++) {
971971
var
972+
continueSearch = false,
972973
queryCharacter = query.charCodeAt(characterIndex)
973974
;
974975
while (nextCharacterIndex < termLength) {
975976
if (term.charCodeAt(nextCharacterIndex++) === queryCharacter) {
976-
continue search;
977+
continueSearch = true;
978+
979+
break;
977980
}
978981
}
979982

980-
return false;
983+
if (!continueSearch) {
984+
return false;
985+
}
981986
}
982987

983988
return true;
@@ -2122,9 +2127,7 @@
21222127
return;
21232128
}
21242129
if (isMultiple) {
2125-
if ($.inArray(module.escape.htmlEntities(String(optionValue)), value.map(function (v) {
2126-
return String(v);
2127-
})) !== -1) {
2130+
if ($.inArray(module.escape.htmlEntities(String(optionValue)), value.map(String)) !== -1) {
21282131
$selectedItem = ($selectedItem)
21292132
? $selectedItem.add($choice)
21302133
: $choice;
@@ -3206,7 +3209,7 @@
32063209
}
32073210
},
32083211
diacritics: function (text) {
3209-
return settings.ignoreDiacritics ? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : text;
3212+
return settings.ignoreDiacritics ? text.normalize('NFD').replace(/[\u0300-\u036F]/g, '') : text;
32103213
},
32113214
},
32123215

0 commit comments

Comments
 (0)