Hygiene: Discourage use of $.each
authorjdlrobson <jdlrobson@gmail.com>
Fri, 10 Aug 2018 18:02:22 +0000 (11:02 -0700)
committerjdlrobson <jdlrobson@gmail.com>
Thu, 6 Sep 2018 00:21:29 +0000 (17:21 -0700)
Even though Array.prototype.forEach only works on arrays, and
$.each is more generic, I think it makes sense to begin discouraging
the usage of $.each now. This can be overriden by ignore lines or
by Array.prototype.forEach compatible lines. This doesn't seem too
much of an ask of engineers and helps future migrations

Bug: T200877
Change-Id: I339cff311a830699c8e32f07cec338a39870c53f

26 files changed:
.eslintrc.json
resources/src/jquery.tablesorter/jquery.tablesorter.js
resources/src/mediawiki.Uri/Uri.js
resources/src/mediawiki.action/mediawiki.action.edit.preview.js
resources/src/mediawiki.api/index.js
resources/src/mediawiki.api/upload.js
resources/src/mediawiki.inspect.js
resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js
resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FilterGroup.js
resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FilterItem.js
resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js
resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.SavedQueriesModel.js
resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.GroupWidget.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.MenuSelectWidget.js
resources/src/mediawiki.special.apisandbox/apisandbox.js
resources/src/mediawiki.template.mustache.js
resources/src/mediawiki.widgets.datetime/DateTimeInputWidget.js
resources/src/mediawiki.widgets/mw.widgets.CategoryMultiselectWidget.js
resources/src/mediawiki.widgets/mw.widgets.CategoryTagItemWidget.js
resources/src/mediawiki.widgets/mw.widgets.CheckMatrixWidget.js
resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
tests/qunit/data/testrunner.js
tests/qunit/suites/resources/mediawiki/mediawiki.cldr.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.language.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index f088617..4630e07 100644 (file)
                                "property": "inArray",
                                "message": "Please use Array.prototype.indexOf instead"
                        },
+                       {
+                               "object": "$",
+                               "property": "each",
+                               "message": "Please consider different approaches to $.each, especially when using Array's. You can override this warning if necessary with eslint-disable-next-line."
+                       },
                        {
                                "object": "$",
                                "property": "isArray",
index 6287126..cbdc2fa 100644 (file)
 
                        // Loop through all the dom cells of the thead
                        $tableRows.each( function ( rowIndex, row ) {
+                               // eslint-disable-next-line no-restricted-properties
                                $.each( row.cells, function ( columnIndex, cell ) {
                                        var matrixRowIndex,
                                                matrixColumnIndex;
        function convertSortList( sortObjects ) {
                var sortList = [];
                sortObjects.forEach( function ( sortObject ) {
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( sortObject, function ( columnIndex, order ) {
                                var orderIndex = ( order === 'desc' ) ? 1 : 0;
                                sortList.push( [ parseInt( columnIndex, 10 ), orderIndex ] );
index eb7cff0..bfdfc88 100644 (file)
                         */
                        getQueryString: function () {
                                var args = [];
+                               // eslint-disable-next-line no-restricted-properties
                                $.each( this.query, function ( key, val ) {
                                        var k = Uri.encode( key ),
                                                vals = Array.isArray( val ) ? val : [ val ];
index b87fba7..dc1f33b 100644 (file)
                                }
 
                                newList = [];
+                               // eslint-disable-next-line no-restricted-properties
                                $.each( response.parse.indicators, function ( name, indicator ) {
                                        newList.push(
                                                $( '<div>' )
index 51f359c..8c9b309 100644 (file)
@@ -53,6 +53,7 @@
        // Pre-populate with fake ajax promises to save http requests for tokens
        // we already have on the page via the user.tokens module (T36733).
        promises[ defaultOptions.ajax.url ] = {};
+       // eslint-disable-next-line no-restricted-properties
        $.each( mw.user.tokens.get(), function ( key, value ) {
                // This requires #getToken to use the same key as user.tokens.
                // Format: token-type + "Token" (eg. csrfToken, patrolToken, watchToken).
index f343321..dc82fb5 100644 (file)
 
                        file.name = 'file';
 
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( data, function ( key, val ) {
                                $form.append( getHiddenInput( key, val ) );
                        } );
index f4545de..3a649a9 100644 (file)
 
                style.textContent = css;
                document.body.appendChild( style );
+               // eslint-disable-next-line no-restricted-properties
                $.each( style.sheet.cssRules, function ( index, rule ) {
                        selectors.total++;
                        // document.querySelector() on prefixed pseudo-elements can throw exceptions
index b7fd6fd..b3154e1 100644 (file)
        mw.jqueryMsg.HtmlEmitter = function ( language, magic ) {
                var jmsg = this;
                this.language = language;
+               // eslint-disable-next-line no-restricted-properties
                $.each( magic, function ( key, val ) {
                        jmsg[ key.toLowerCase() ] = function () {
                                return val;
                 */
                concat: function ( nodes ) {
                        var $span = $( '<span>' ).addClass( 'mediaWiki_htmlEmitter' );
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( nodes, function ( i, node ) {
                                // Let jQuery append nodes, arrays of nodes and jQuery objects
                                // other things (strings, numbers, ..) are appended as text nodes (not as HTML strings)
index a62acc5..7aceed6 100644 (file)
@@ -1,3 +1,4 @@
+/* eslint-disable no-restricted-properties */
 ( function ( mw ) {
        /**
         * View model for a filter group
index d7f7b02..7e35b33 100644 (file)
@@ -94,6 +94,7 @@
 
                key = key || 'contextDescription';
 
+               // eslint-disable-next-line no-restricted-properties
                $.each( conflicts, function ( filterName, conflict ) {
                        if ( !conflict.item.isSelected() ) {
                                return;
index cdf1f63..45d54a0 100644 (file)
@@ -1,3 +1,4 @@
+/* eslint-disable no-restricted-properties */
 ( function ( mw, $ ) {
        /**
         * View model for the filters selection and display
index 77d5961..e7cb6b6 100644 (file)
@@ -1,3 +1,4 @@
+/* eslint-disable no-restricted-properties */
 ( function ( mw, $ ) {
        /**
         * View model for saved queries
index 5af001e..1664e86 100644 (file)
@@ -1,3 +1,4 @@
+/* eslint-disable no-restricted-properties */
 ( function ( mw, $ ) {
 
        var byteLength = require( 'mediawiki.String' ).byteLength;
index 167df09..7a95e3d 100644 (file)
@@ -22,6 +22,7 @@
 
                if ( config.events ) {
                        // Aggregate events
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( config.events, function ( eventName, eventEmit ) {
                                aggregate[ eventName ] = eventEmit;
                        } );
index 40b2fcf..d4f0aea 100644 (file)
                this.$overlay.append( this.highlightPopup.$element );
 
                // Count groups per view
+               // eslint-disable-next-line no-restricted-properties
                $.each( groups, function ( groupName, groupModel ) {
                        if ( !groupModel.isHidden() ) {
                                viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
                        }
                } );
 
+               // eslint-disable-next-line no-restricted-properties
                $.each( groups, function ( groupName, groupModel ) {
                        var currentItems = [],
                                view = groupModel.getView();
index beac624..ad5fb06 100644 (file)
                }
 
                toRemove = {};
+               // eslint-disable-next-line no-restricted-properties
                $.each( this.templatedItemsCache, function ( k, el ) {
                        if ( el.widget.isElementAttached() ) {
                                toRemove[ k ] = el;
index 9f5e5c4..c3139a2 100644 (file)
@@ -21,6 +21,7 @@
                                render: function ( data, partialTemplates ) {
                                        var partials = {};
                                        if ( partialTemplates ) {
+                                               /* eslint-disable-next-line no-restricted-properties */
                                                $.each( partialTemplates, function ( name, template ) {
                                                        partials[ name ] = template.getSource();
                                                } );
index 17f1fb4..77271f7 100644 (file)
                                } else {
                                        maxlength = spec.size;
                                        if ( spec.intercalarySize ) {
+                                               // eslint-disable-next-line no-restricted-properties
                                                $.each( spec.intercalarySize, reduceFunc );
                                        }
                                        $field = $( '<input>' ).attr( 'type', 'text' )
index c506379..a71b3d1 100644 (file)
                                } ).done( function ( res ) {
                                        var categories = [];
 
-                                       $.each( res.query.pages, function ( index, page ) {
+                                       res.query.pages.forEach( function ( page ) {
                                                if ( !page.missing ) {
                                                        categories.push( page.title );
                                                }
                                } ).done( function ( res ) {
                                        var categories = [];
 
-                                       $.each( res.query.pages, function ( index, page ) {
+                                       res.query.pages.forEach( function ( page ) {
                                                if ( !page.missing && Array.isArray( page.categories ) ) {
                                                        categories.push.apply( categories, page.categories.map( function ( category ) {
                                                                return category.title;
index f0b2d44..77c2c35 100644 (file)
                        var
                                normalized = {},
                                pages = {};
-                       $.each( response.query.normalized || [], function ( index, data ) {
+                       ( response.query.normalized || [] ).forEach( function ( data ) {
                                normalized[ data.fromencoded ? decodeURIComponent( data.from ) : data.from ] = data.to;
                        } );
-                       $.each( response.query.pages, function ( index, page ) {
+                       response.query.pages.forEach( function ( page ) {
                                pages[ page.title ] = !page.missing;
                        } );
                        titles.forEach( function ( title ) {
index 21b4f10..64b7592 100644 (file)
                $headRow.append( $( '<td>' ).text( '\u00A0' ) );
 
                // Iterate over the columns object (ignore the value)
+               // eslint-disable-next-line no-restricted-properties
                $.each( this.columns, function ( columnLabel ) {
                        $headRow.append( $( '<td>' ).html( columnLabel ) );
                } );
                $table.append( $headRow );
 
                // Build table
+               // eslint-disable-next-line no-restricted-properties
                $.each( this.rows, function ( rowLabel, rowTag ) {
                        var $row = $( '<tr>' ),
                                labelField = new OO.ui.FieldLayout(
@@ -61,6 +63,7 @@
                        $row.append( $( '<td>' ).append( labelField.$element ) );
 
                        // Columns
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( widget.columns, function ( columnLabel, columnTag ) {
                                var thisTag = columnTag + '-' + rowTag,
                                        checkbox = new OO.ui.CheckboxInputWidget( {
                // setDisabled sometimes gets called before the widget is ready
                if ( this.checkboxes && Object.keys( this.checkboxes ).length > 0 ) {
                        // Propagate to all checkboxes and update their disabled state
+                       // eslint-disable-next-line no-restricted-properties
                        $.each( this.checkboxes, function ( name, checkbox ) {
                                checkbox.setDisabled( widget.isTagDisabled( name ) );
                        } );
index c25ab45..75bf891 100644 (file)
                        urls = data.data[ 3 ],
                        self = this;
 
+               // eslint-disable-next-line no-restricted-properties
                $.each( titles, function ( i, result ) {
                        items.push( new mw.widgets.TitleOptionWidget(
                                self.getOptionWidgetData(
index d8972ab..981043b 100644 (file)
                                // Check for incomplete animations/requests/etc and throw if there are any.
                                if ( $.timers && $.timers.length !== 0 ) {
                                        timers = $.timers.length;
+                                       // eslint-disable-next-line no-restricted-properties
                                        $.each( $.timers, function ( i, timer ) {
                                                var node = timer.elem;
                                                mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
        QUnit.whenPromisesComplete = function () {
                var altPromises = [];
 
+               // When we have ES6 support we'll be able to use Array.from here
+               // eslint-disable-next-line no-restricted-properties
                $.each( arguments, function ( i, arg ) {
                        var alt = $.Deferred();
                        altPromises.push( alt );
index 17eaa7e..b5824d3 100644 (file)
@@ -74,6 +74,7 @@
                } );
        }
 
+       // eslint-disable-next-line no-restricted-properties
        $.each( pluralTestcases, function ( langCode, tests ) {
                if ( langCode === mw.config.get( 'wgUserLanguage' ) ) {
                        pluralTest( langCode, tests );
index 9ea3c11..d460c0b 100644 (file)
                ]
        };
 
+       // eslint-disable-next-line no-restricted-properties
        $.each( grammarTests, function ( langCode, test ) {
                if ( langCode === mw.config.get( 'wgUserLanguage' ) ) {
                        grammarTest( langCode, test );
index 272b147..0a65788 100644 (file)
        QUnit.test( 'wikiUrlencode', function ( assert ) {
                assert.strictEqual( util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
                // See also wfUrlencodeTest.php#provideURLS
+               // eslint-disable-next-line no-restricted-properties
                $.each( {
                        '+': '%2B',
                        '&': '%26',