Skip to content

Commit

Permalink
eslint: replace es-x/no-array-prototype-includes with unicorn/prefer-…
Browse files Browse the repository at this point in the history
…includes (wikimedia-gadgets#2125)

* eslint: turn off es-x/no-array-prototype-includes

after discussion in wikimedia-gadgets#2101

* turn on unicorn/prefer-includes

* more autofixes

* fix linter error
  • Loading branch information
NovemLinguae authored Dec 11, 2024
1 parent a6afebb commit ad012f7
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"rules": {
"array-bracket-spacing": "off",
"computed-property-spacing": "off",
"es-x/no-array-prototype-includes": "off",
"indent": "off",
"jsdoc/require-param": "off",
"jsdoc/require-returns": "off",
Expand All @@ -38,9 +39,9 @@
"selector": "MemberExpression > Identifier[name=\"fail\"]"
}
],
"unicorn/prefer-includes": "error",

"camelcase": ["warn", {"properties": "never"}],
"es-x/no-array-prototype-includes": "warn",
"es-x/no-object-values": "warn",
"mediawiki/class-doc": "warn",
"new-cap": "warn",
Expand Down
2 changes: 1 addition & 1 deletion modules/twinklebatchdelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Twinkle.batchdelete.callback.toggleSubpages = function twDbatchToggleSubpages(e)
const pageTitle = mw.Title.newFromText(pageName);

// No need to look for subpages in main/file/mediawiki space
if ([0, 6, 8].indexOf(pageTitle.namespace) > -1) {
if ([0, 6, 8].includes(pageTitle.namespace)) {
subpageLister.workerSuccess();
return;
}
Expand Down
6 changes: 3 additions & 3 deletions modules/twinkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Twinkle.block.processUserInfo = function twinkleblockProcessUserInfo(data, fn) {

Twinkle.block.isRegistered = !!userinfo.userid;
if (Twinkle.block.isRegistered) {
Twinkle.block.userIsBot = !!userinfo.groupmemberships && userinfo.groupmemberships.map((e) => e.group).indexOf('bot') !== -1;
Twinkle.block.userIsBot = !!userinfo.groupmemberships && userinfo.groupmemberships.map((e) => e.group).includes('bot');
} else {
Twinkle.block.userIsBot = false;
}
Expand Down Expand Up @@ -1548,7 +1548,7 @@ Twinkle.block.callback.toggle_see_alsos = function twinkleblockCallbackToggleSee

if (!Twinkle.block.seeAlsos.length) {
this.form.reason.value = reason;
} else if (reason.indexOf('{{') !== -1) {
} else if (reason.includes('{{')) {
this.form.reason.value = reason + ' <!-- see also ' + seeAlsoMessage + ' -->';
} else {
this.form.reason.value = reason + '; see also ' + seeAlsoMessage;
Expand Down Expand Up @@ -1760,7 +1760,7 @@ Twinkle.block.callback.evaluate = function twinkleblockCallbackEvaluate(e) {

if (toBlock) {
if (blockoptions.partial) {
if (blockoptions.disabletalk && blockoptions.namespacerestrictions.indexOf('3') === -1) {
if (blockoptions.disabletalk && !blockoptions.namespacerestrictions.includes('3')) {
return alert('Partial blocks cannot prevent talk page access unless also restricting them from editing User talk space!');
}
if (!blockoptions.namespacerestrictions && !blockoptions.pagerestrictions) {
Expand Down
8 changes: 4 additions & 4 deletions modules/twinkleconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,12 +1171,12 @@ Twinkle.config.init = function twinkleconfigInit() {
check.setAttribute('type', 'checkbox');
check.setAttribute('id', pref.name + '_' + itemkey);
check.setAttribute('name', pref.name + '_' + itemkey);
if (gotPref && gotPref.indexOf(itemkey) !== -1) {
if (gotPref && gotPref.includes(itemkey)) {
check.setAttribute('checked', 'checked');
}
// cater for legacy integer array values for unlinkNamespaces (this can be removed a few years down the track...)
if (pref.name === 'unlinkNamespaces') {
if (gotPref && gotPref.indexOf(parseInt(itemkey, 10)) !== -1) {
if (gotPref && gotPref.includes(parseInt(itemkey, 10))) {
check.setAttribute('checked', 'checked');
}
}
Expand Down Expand Up @@ -1314,7 +1314,7 @@ Twinkle.config.init = function twinkleconfigInit() {
box.appendChild(document.createTextNode(', or by editing this page.'));
$(box).insertAfter($('#contentSub'));

} else if (['monobook', 'vector', 'vector-2022', 'cologneblue', 'modern', 'timeless', 'minerva', 'common'].indexOf(scriptPageName) !== -1) {
} else if (['monobook', 'vector', 'vector-2022', 'cologneblue', 'modern', 'timeless', 'minerva', 'common'].includes(scriptPageName)) {
// place "Looking for Twinkle options?" notice
box.setAttribute('class', 'config-userskin-box');

Expand Down Expand Up @@ -1537,7 +1537,7 @@ Twinkle.config.resetPref = function twinkleconfigResetPref(pref) {
case 'set':
$.each(pref.setValues, (itemkey) => {
if (document.getElementById(pref.name + '_' + itemkey)) {
document.getElementById(pref.name + '_' + itemkey).checked = Twinkle.defaultConfig[pref.name].indexOf(itemkey) !== -1;
document.getElementById(pref.name + '_' + itemkey).checked = Twinkle.defaultConfig[pref.name].includes(itemkey);
}
});
break;
Expand Down
2 changes: 1 addition & 1 deletion modules/twinkleimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Twinkle.image.callback.evaluate = function twinkleimageCallbackEvaluate(event) {
throw new Error('Twinkle.image.callback.evaluate: unknown criterion');
}

const lognomination = Twinkle.getPref('logSpeedyNominations') && Twinkle.getPref('noLogOnSpeedyNomination').indexOf(csdcrit.toLowerCase()) === -1;
const lognomination = Twinkle.getPref('logSpeedyNominations') && !Twinkle.getPref('noLogOnSpeedyNomination').includes(csdcrit.toLowerCase());
const templatename = input.derivative ? 'dw ' + input.type : input.type;

const params = $.extend({
Expand Down
2 changes: 1 addition & 1 deletion modules/twinkleprod.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

Twinkle.prod = function twinkleprod() {
if (([0, 6].indexOf(mw.config.get('wgNamespaceNumber')) === -1) ||
if ((![0, 6].includes(mw.config.get('wgNamespaceNumber'))) ||
!mw.config.get('wgCurRevisionId') ||
Morebits.isPageRedirect()) {
return;
Expand Down
6 changes: 3 additions & 3 deletions modules/twinkleprotect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ Twinkle.protect.callback.evaluate = function twinkleprotectCallbackEvaluate(e) {
break;
case 'unprotect':
var admins = $.map(Twinkle.protect.currentProtectionLevels, (pl) => {
if (!pl.admin || Twinkle.protect.trustedBots.indexOf(pl.admin) !== -1) {
if (!pl.admin || Twinkle.protect.trustedBots.includes(pl.admin)) {
return null;
}
return 'User:' + pl.admin;
Expand Down Expand Up @@ -1401,7 +1401,7 @@ Twinkle.protect.callback.annotateProtectReason = function twinkleprotectCallback
$(form.protectReason_notes_rfppRevid).parent().hide();
}
} else if (this.name === 'protectReason_notes_rfppRevid') {
Twinkle.protect.protectReasonAnnotations = Twinkle.protect.protectReasonAnnotations.filter((el) => el.indexOf('[[Special:Permalink') === -1);
Twinkle.protect.protectReasonAnnotations = Twinkle.protect.protectReasonAnnotations.filter((el) => !el.includes('[[Special:Permalink'));
if (e.target.value.length) {
const permalink = '[[Special:Permalink/' + e.target.value + '#' + Morebits.pageNameNorm + ']]';
Twinkle.protect.protectReasonAnnotations.push(permalink);
Expand Down Expand Up @@ -1459,7 +1459,7 @@ Twinkle.protect.callbacks = {
return;
}
} else {
const needsTagToBeCommentedOut = ['javascript', 'css', 'sanitized-css'].indexOf(protectedPage.getContentModel()) !== -1;
const needsTagToBeCommentedOut = ['javascript', 'css', 'sanitized-css'].includes(protectedPage.getContentModel());
if (needsTagToBeCommentedOut) {
if (params.noinclude) {
tag = '/* <noinclude>{{' + tag + '}}</noinclude> */';
Expand Down
28 changes: 14 additions & 14 deletions modules/twinklerollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ Twinkle.rollback.addLinks = {
if (mw.config.exists('wgRelevantUserName') || isRange) {
// Get the username these contributions are for
let username = mw.config.get('wgRelevantUserName');
if (Twinkle.getPref('showRollbackLinks').indexOf('contribs') !== -1 ||
(mw.config.get('wgUserName') !== username && Twinkle.getPref('showRollbackLinks').indexOf('others') !== -1) ||
(mw.config.get('wgUserName') === username && Twinkle.getPref('showRollbackLinks').indexOf('mine') !== -1)) {
if (Twinkle.getPref('showRollbackLinks').includes('contribs') ||
(mw.config.get('wgUserName') !== username && Twinkle.getPref('showRollbackLinks').includes('others')) ||
(mw.config.get('wgUserName') === username && Twinkle.getPref('showRollbackLinks').includes('mine'))) {
const $list = $('#mw-content-text').find('ul li:has(span.mw-uctop):has(.mw-changeslist-diff)');

$list.each((key, current) => {
Expand All @@ -218,7 +218,7 @@ Twinkle.rollback.addLinks = {
},

recentchanges: function() {
if (Twinkle.getPref('showRollbackLinks').indexOf('recent') !== -1) {
if (Twinkle.getPref('showRollbackLinks').includes('recent')) {
// Latest and revertable (not page creations, logs, categorizations, etc.)
let $list = $('.mw-changeslist .mw-changeslist-last.mw-changeslist-src-mw-edit');
// Exclude top-level header if "group changes" preference is used
Expand All @@ -238,7 +238,7 @@ Twinkle.rollback.addLinks = {
},

history: function() {
if (Twinkle.getPref('showRollbackLinks').indexOf('history') !== -1) {
if (Twinkle.getPref('showRollbackLinks').includes('history')) {
// All revs
const histList = $('#pagehistory li').toArray();

Expand Down Expand Up @@ -286,7 +286,7 @@ Twinkle.rollback.addLinks = {
extraParams += xtitle === 'otitle' ? mw.config.get('wgDiffOldId') : mw.config.get('wgDiffNewId');

const href = $talkLink.attr('href');
if (href.indexOf('?') === -1) {
if (!href.includes('?')) {
$talkLink.attr('href', href + '?' + extraParams);
} else {
$talkLink.attr('href', href + '&' + extraParams);
Expand All @@ -311,7 +311,7 @@ Twinkle.rollback.addLinks = {
// Not latest revision, add [restore this revision] link to newer revision
const newTitle = document.getElementById('mw-diff-ntitle1').parentNode;
newTitle.insertBefore(Twinkle.rollback.linkBuilder.restoreThisRevisionLink('wgDiffNewId'), newTitle.firstChild);
} else if (Twinkle.getPref('showRollbackLinks').indexOf('diff') !== -1 && mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId') || document.getElementById('differences-prevlink'))) {
} else if (Twinkle.getPref('showRollbackLinks').includes('diff') && mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId') || document.getElementById('differences-prevlink'))) {
// Normally .mw-userlink is a link, but if the
// username is hidden, it will be a span with
// .history-deleted as well. When a sysop views the
Expand Down Expand Up @@ -459,11 +459,11 @@ Twinkle.rollback.callbacks = {
undoafter: revertToRevID,
basetimestamp: touched,
starttimestamp: loadtimestamp,
minor: Twinkle.getPref('markRevertedPagesAsMinor').indexOf('torev') !== -1 ? true : undefined,
minor: Twinkle.getPref('markRevertedPagesAsMinor').includes('torev') ? true : undefined,
format: 'json'
};
// Handle watching, possible expiry
if (Twinkle.getPref('watchRevertedPages').indexOf('torev') !== -1) {
if (Twinkle.getPref('watchRevertedPages').includes('torev')) {
const watchOrExpiry = Twinkle.getPref('watchRevertedExpiry');

if (!watchOrExpiry || watchOrExpiry === 'no') {
Expand Down Expand Up @@ -541,7 +541,7 @@ Twinkle.rollback.callbacks = {
} else if (params.type === 'vand' &&
// Okay to test on user since it will either fail or sysop will correctly access it
// Besides, none of the trusted bots are going to be revdel'd
Twinkle.rollback.trustedBots.indexOf(top.user) !== -1 && revs.length > 1 &&
Twinkle.rollback.trustedBots.includes(top.user) && revs.length > 1 &&
revs[1].revid === params.revid) {
Morebits.Status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', lastuser), ', a trusted bot, and the revision before was made by our vandal, so we will proceed with the revert.' ]);
index = 2;
Expand All @@ -557,7 +557,7 @@ Twinkle.rollback.callbacks = {
userNorm = params.user || Twinkle.rollback.hiddenName;
}

if (Twinkle.rollback.trustedBots.indexOf(params.user) !== -1) {
if (Twinkle.rollback.trustedBots.includes(params.user)) {
switch (params.type) {
case 'vand':
Morebits.Status.info('Info', [ 'Vandalism revert was chosen on ', Morebits.htmlNode('strong', userNorm), '. As this is a trusted bot, we assume you wanted to revert vandalism made by the previous user instead.' ]);
Expand Down Expand Up @@ -685,7 +685,7 @@ Twinkle.rollback.callbacks = {
}

// Decide whether to notify the user on success
if (!Twinkle.rollback.skipTalk && Twinkle.getPref('openTalkPage').indexOf(params.type) !== -1 &&
if (!Twinkle.rollback.skipTalk && Twinkle.getPref('openTalkPage').includes(params.type) &&
!params.userHidden && mw.config.get('wgUserName') !== params.user) {
params.notifyUser = true;
// Pass along to the warn module
Expand All @@ -712,11 +712,11 @@ Twinkle.rollback.callbacks = {
undoafter: params.goodid,
basetimestamp: touched,
starttimestamp: loadtimestamp,
minor: Twinkle.getPref('markRevertedPagesAsMinor').indexOf(params.type) !== -1 ? true : undefined,
minor: Twinkle.getPref('markRevertedPagesAsMinor').includes(params.type) ? true : undefined,
format: 'json'
};
// Handle watching, possible expiry
if (Twinkle.getPref('watchRevertedPages').indexOf(params.type) !== -1) {
if (Twinkle.getPref('watchRevertedPages').includes(params.type)) {
const watchOrExpiry = Twinkle.getPref('watchRevertedExpiry');

if (!watchOrExpiry || watchOrExpiry === 'no') {
Expand Down
38 changes: 19 additions & 19 deletions modules/twinklespeedy.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ Twinkle.speedy.generateCsdList = function twinklespeedyGenerateCsdList(list, mod
return null;
}

if (criterion.showInNamespaces && criterion.showInNamespaces.indexOf(pageNamespace) < 0) {
if (criterion.showInNamespaces && !criterion.showInNamespaces.includes(pageNamespace)) {
return null;
}
if (criterion.hideInNamespaces && criterion.hideInNamespaces.indexOf(pageNamespace) > -1) {
if (criterion.hideInNamespaces && criterion.hideInNamespaces.includes(pageNamespace)) {
return null;
}

Expand Down Expand Up @@ -713,7 +713,7 @@ Twinkle.speedy.userList = [
label: 'U1: User request',
value: 'userreq',
tooltip: 'Personal subpages, upon request by their user. In some rare cases there may be administrative need to retain the page. Also, sometimes, main user pages may be deleted as well. See Wikipedia:User page for full instructions and guidelines',
subgroup: mw.config.get('wgNamespaceNumber') === 3 && mw.config.get('wgTitle').indexOf('/') === -1 ? {
subgroup: mw.config.get('wgNamespaceNumber') === 3 && !mw.config.get('wgTitle').includes('/') ? {
name: 'userreq_rationale',
type: 'input',
label: 'A mandatory rationale to explain why this user talk page should be deleted:',
Expand Down Expand Up @@ -1169,7 +1169,7 @@ Twinkle.speedy.callbacks = {
notifytext += (params.welcomeuser ? '' : '|nowelcome=yes') + '}} ~~~~';

editsummary = 'Notification: speedy deletion' + (params.warnUser ? '' : ' nomination');
if (params.normalizeds.indexOf('g10') === -1) { // no article name in summary for G10 taggings
if (!params.normalizeds.includes('g10')) { // no article name in summary for G10 taggings
editsummary += ' of [[:' + Morebits.pageNameNorm + ']].';
} else {
editsummary += ' of an attack page.';
Expand Down Expand Up @@ -1371,12 +1371,12 @@ Twinkle.speedy.callbacks = {
params.utparams = buildData[1];

// Set the correct value for |ts= parameter in {{db-g13}}
if (params.normalizeds.indexOf('g13') !== -1) {
if (params.normalizeds.includes('g13')) {
code = code.replace('$TIMESTAMP', pageobj.getLastEditTime());
}

// Tag if possible, post on talk if not
if (pageobj.canEdit() && ['wikitext', 'Scribunto', 'javascript', 'css', 'sanitized-css'].indexOf(pageobj.getContentModel()) !== -1 && mw.config.get('wgNamespaceNumber') !== 710 /* TimedText */) {
if (pageobj.canEdit() && ['wikitext', 'Scribunto', 'javascript', 'css', 'sanitized-css'].includes(pageobj.getContentModel()) && mw.config.get('wgNamespaceNumber') !== 710 /* TimedText */) {
let text = pageobj.getPageText();

statelem.status('Checking for tags on the page...');
Expand Down Expand Up @@ -1412,7 +1412,7 @@ Twinkle.speedy.callbacks = {
}

if (params.requestsalt) {
if (params.normalizeds.indexOf('g10') === -1) {
if (!params.normalizeds.includes('g10')) {
code += '\n{{salt}}';
} else {
code = '{{salt}}\n' + code;
Expand All @@ -1422,11 +1422,11 @@ Twinkle.speedy.callbacks = {
if (mw.config.get('wgPageContentModel') === 'Scribunto') {
// Scribunto isn't parsed like wikitext, so CSD templates on modules need special handling to work
let equals = '';
while (code.indexOf(']' + equals + ']') !== -1) {
while (code.includes(']' + equals + ']')) {
equals += '=';
}
code = "require('Module:Module wikitext')._addText([" + equals + '[' + code + ']' + equals + ']);';
} else if (['javascript', 'css', 'sanitized-css'].indexOf(mw.config.get('wgPageContentModel')) !== -1) {
} else if (['javascript', 'css', 'sanitized-css'].includes(mw.config.get('wgPageContentModel'))) {
// Likewise for JS/CSS pages
code = '/* ' + code + ' */';
}
Expand All @@ -1447,7 +1447,7 @@ Twinkle.speedy.callbacks = {
}

// Blank attack pages
if (params.normalizeds.indexOf('g10') !== -1) {
if (params.normalizeds.includes('g10')) {
text = code;
} else {
// Insert tag after short description or any hatnotes
Expand Down Expand Up @@ -1532,7 +1532,7 @@ Twinkle.speedy.callbacks = {
let editsummary = 'Logging speedy deletion nomination';
let appendText = '# [[:' + Morebits.pageNameNorm;

if (params.normalizeds.indexOf('g10') === -1) { // no article name in log for G10 taggings
if (!params.normalizeds.includes('g10')) { // no article name in log for G10 taggings
appendText += ']]' + fileLogLink + ': ';
editsummary += ' of [[:' + Morebits.pageNameNorm + ']].';
} else {
Expand Down Expand Up @@ -1900,18 +1900,18 @@ Twinkle.speedy.callback.evaluateSysop = function twinklespeedyCallbackEvaluateSy
// analyse each criterion to determine whether to watch the page, prompt for summary, or notify the creator
let watchPage, promptForSummary;
normalizeds.forEach((norm) => {
if (Twinkle.getPref('watchSpeedyPages').indexOf(norm) !== -1) {
if (Twinkle.getPref('watchSpeedyPages').includes(norm)) {
watchPage = Twinkle.getPref('watchSpeedyExpiry');
}
if (Twinkle.getPref('promptForSpeedyDeletionSummary').indexOf(norm) !== -1) {
if (Twinkle.getPref('promptForSpeedyDeletionSummary').includes(norm)) {
promptForSummary = true;
}
});

const warnusertalk = form.warnusertalk.checked && normalizeds.some((norm, index) => Twinkle.getPref('warnUserOnSpeedyDelete').indexOf(norm) !== -1 &&
const warnusertalk = form.warnusertalk.checked && normalizeds.some((norm, index) => Twinkle.getPref('warnUserOnSpeedyDelete').includes(norm) &&
!(norm === 'g6' && values[index] !== 'copypaste'));

const welcomeuser = warnusertalk && normalizeds.some((norm) => Twinkle.getPref('welcomeUserOnSpeedyDeletionNotification').indexOf(norm) !== -1);
const welcomeuser = warnusertalk && normalizeds.some((norm) => Twinkle.getPref('welcomeUserOnSpeedyDeletionNotification').includes(norm));

const params = {
values: values,
Expand Down Expand Up @@ -1953,12 +1953,12 @@ Twinkle.speedy.callback.evaluateUser = function twinklespeedyCallbackEvaluateUse
const normalizeds = values.map((value) => Twinkle.speedy.normalizeHash[value]);

// analyse each criterion to determine whether to watch the page/notify the creator
const watchPage = normalizeds.some((csdCriteria) => Twinkle.getPref('watchSpeedyPages').indexOf(csdCriteria) !== -1) && Twinkle.getPref('watchSpeedyExpiry');
const watchPage = normalizeds.some((csdCriteria) => Twinkle.getPref('watchSpeedyPages').includes(csdCriteria)) && Twinkle.getPref('watchSpeedyExpiry');

const notifyuser = form.notify.checked && normalizeds.some((norm, index) => Twinkle.getPref('notifyUserOnSpeedyDeletionNomination').indexOf(norm) !== -1 &&
const notifyuser = form.notify.checked && normalizeds.some((norm, index) => Twinkle.getPref('notifyUserOnSpeedyDeletionNomination').includes(norm) &&
!(norm === 'g6' && values[index] !== 'copypaste'));
const welcomeuser = notifyuser && normalizeds.some((norm) => Twinkle.getPref('welcomeUserOnSpeedyDeletionNotification').indexOf(norm) !== -1);
const csdlog = Twinkle.getPref('logSpeedyNominations') && normalizeds.some((norm) => Twinkle.getPref('noLogOnSpeedyNomination').indexOf(norm) === -1);
const welcomeuser = notifyuser && normalizeds.some((norm) => Twinkle.getPref('welcomeUserOnSpeedyDeletionNotification').includes(norm));
const csdlog = Twinkle.getPref('logSpeedyNominations') && normalizeds.some((norm) => !Twinkle.getPref('noLogOnSpeedyNomination').includes(norm));

const params = {
values: values,
Expand Down
Loading

0 comments on commit ad012f7

Please sign in to comment.