From 1b101f09eeafacba5babe715a5b32724cb0741dc Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Fri, 14 Jun 2024 12:53:32 +0100 Subject: [PATCH] ESLint: Update syntax to ES6 and autofix Change-Id: I613cc64a9bf3f375a40f75a1441b83f181a2ab42 --- resources/.eslintrc.json | 4 +- resources/addon/edit/matchbrackets-wmde.js | 10 ++-- resources/ext.CodeMirror.WikiEditor.js | 48 ++++++++-------- resources/ext.CodeMirror.js | 2 +- resources/mode/mediawiki/mediawiki.js | 56 +++++++++---------- .../tests/qunit/CodeMirror.mediawiki.test.js | 38 ++++++------- .../modules/ve-cm/ve.ui.CodeMirrorAction.js | 48 ++++++++-------- .../modules/ve-cm/ve.ui.CodeMirrorTool.js | 38 +++++++------ 8 files changed, 121 insertions(+), 123 deletions(-) diff --git a/resources/.eslintrc.json b/resources/.eslintrc.json index 67b06a90..51c09995 100644 --- a/resources/.eslintrc.json +++ b/resources/.eslintrc.json @@ -1,7 +1,7 @@ { "root": true, "extends": [ - "wikimedia/client-es5", + "wikimedia/client", "wikimedia/jquery", "wikimedia/mediawiki" ], @@ -13,7 +13,7 @@ }, "rules": { "max-len": "off", - "es-x/no-object-assign": "warn" + "prefer-const": "warn" }, "overrides": [ { diff --git a/resources/addon/edit/matchbrackets-wmde.js b/resources/addon/edit/matchbrackets-wmde.js index a50c03da..0497d01d 100644 --- a/resources/addon/edit/matchbrackets-wmde.js +++ b/resources/addon/edit/matchbrackets-wmde.js @@ -37,7 +37,7 @@ } /* eslint-enable */ - var surroundingBrackets = { + const surroundingBrackets = { '(': ')', ')': false, '[': ']', @@ -47,7 +47,7 @@ }; function findSurroundingBrackets( cm, where, config ) { - var from, to, ch, + let from, to, ch, nestedBracketsToSkip = 0, lineNo = where.line, line = cm.getLine( lineNo ), @@ -140,13 +140,13 @@ } function stillTheSameMarks( marks, config ) { - var same = config.currentMarks && + const same = config.currentMarks && // No need to compare the details if it's not even the same amount config.currentMarks.length === marks.length && // We need the flexibility because the order can be closing → opening bracket as well - marks.every( function ( mark ) { + marks.every( ( mark ) => { // These are typically only 2 elements for the opening and closing bracket - for ( var i = 0; i < config.currentMarks.length; i++ ) { + for ( let i = 0; i < config.currentMarks.length; i++ ) { // Ordered from "most likely to change" to "least likely" for performance if ( config.currentMarks[i].from.ch === mark.from.ch && config.currentMarks[i].from.line === mark.from.line && diff --git a/resources/ext.CodeMirror.WikiEditor.js b/resources/ext.CodeMirror.WikiEditor.js index 2ea45820..980cfefd 100644 --- a/resources/ext.CodeMirror.WikiEditor.js +++ b/resources/ext.CodeMirror.WikiEditor.js @@ -1,10 +1,10 @@ function init() { - var extCodeMirror = require( 'ext.CodeMirror' ); - var codeMirror, $textbox1, realtimePreviewHandler; + const extCodeMirror = require( 'ext.CodeMirror' ); + let codeMirror, $textbox1, realtimePreviewHandler; - var useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0; + let useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0; - var originHooksTextarea = $.valHooks.textarea; + const originHooksTextarea = $.valHooks.textarea; // define jQuery hook for searching and replacing text using JS if CodeMirror is enabled, see Bug: T108711 $.valHooks.textarea = { get: function ( elem ) { @@ -27,7 +27,7 @@ function init() { // jQuery.textSelection overrides for CodeMirror. // See jQuery.textSelection.js for method documentation - var cmTextSelection = { + const cmTextSelection = { getContents: function () { return codeMirror.doc.getValue(); }, @@ -51,7 +51,7 @@ function init() { return this; }, getCaretPosition: function ( options ) { - var caretPos = codeMirror.doc.indexFromPos( codeMirror.doc.getCursor( true ) ), + const caretPos = codeMirror.doc.indexFromPos( codeMirror.doc.getCursor( true ) ), endPos = codeMirror.doc.indexFromPos( codeMirror.doc.getCursor( false ) ); if ( options.startAndEnd ) { return [ caretPos, endPos ]; @@ -83,14 +83,14 @@ function init() { return false; } - var namespaces = mw.config.get( 'wgCodeMirrorLineNumberingNamespaces' ); + const namespaces = mw.config.get( 'wgCodeMirrorLineNumberingNamespaces' ); // Set to [] to disable everywhere, or null to enable everywhere return !namespaces || namespaces.indexOf( mw.config.get( 'wgNamespaceNumber' ) ) !== -1; } // Keep these modules in sync with MediaWiki\Extension\CodeMirror\Hooks.php - var codeMirrorCoreModules = [ + const codeMirrorCoreModules = [ 'ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki' ]; @@ -100,15 +100,15 @@ function init() { * and react to changes coming from WikiEditor (including Realtime Preview if its enabled). */ function setupSizing() { - var $codeMirror = $( codeMirror.getWrapperElement() ); + const $codeMirror = $( codeMirror.getWrapperElement() ); // Only add resizing corner if realtime preview is enabled, // because that feature provides height resizing (even when preview isn't used). if ( mw.loader.getState( 'ext.wikiEditor.realtimepreview' ) === 'ready' ) { codeMirror.setSize( '100%', $textbox1.parent().height() ); } - var $resizableHandle = $codeMirror.find( '.ui-resizable-handle' ); - mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( function ( realtimePreview ) { + const $resizableHandle = $codeMirror.find( '.ui-resizable-handle' ); + mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( ( realtimePreview ) => { // CodeMirror may have been turned on and then off again before realtimepreview is enabled, in which case it will be null. if ( !codeMirror ) { return; @@ -121,7 +121,7 @@ function init() { // Fix the width and height of the CodeMirror area. codeMirror.setSize( '100%', realtimePreview.twoPaneLayout.$element.height() ); } ); - mw.hook( 'ext.WikiEditor.realtimepreview.resize' ).add( function ( resizingBar ) { + mw.hook( 'ext.WikiEditor.realtimepreview.resize' ).add( ( resizingBar ) => { // CodeMirror may have been turned off after realtimepreview was opened, in which case it will be null. if ( !codeMirror ) { return; @@ -129,7 +129,7 @@ function init() { // Keep in sync with the height of the pane. codeMirror.setSize( '100%', resizingBar.getResizedPane().height() ); } ); - mw.hook( 'ext.WikiEditor.realtimepreview.disable' ).add( function () { + mw.hook( 'ext.WikiEditor.realtimepreview.disable' ).add( () => { // Re-show the corner resize handle. $resizableHandle.show(); // CodeMirror may have been turned off after realtimepreview was opened, in which case it will be null. @@ -145,10 +145,10 @@ function init() { * Replaces the default textarea with CodeMirror */ function enableCodeMirror() { - var config = mw.config.get( 'extCodeMirrorConfig' ); + const config = mw.config.get( 'extCodeMirrorConfig' ); - mw.loader.using( codeMirrorCoreModules.concat( config.pluginModules ), function () { - var $codeMirror, cmOptions, + mw.loader.using( codeMirrorCoreModules.concat( config.pluginModules ), () => { + let $codeMirror, cmOptions, selectionStart = $textbox1.prop( 'selectionStart' ), selectionEnd = $textbox1.prop( 'selectionEnd' ), scrollTop = $textbox1.scrollTop(), @@ -192,13 +192,13 @@ function init() { codeMirror = CodeMirror.fromTextArea( $textbox1[ 0 ], cmOptions ); $codeMirror = $( codeMirror.getWrapperElement() ); - codeMirror.on( 'focus', function () { + codeMirror.on( 'focus', () => { $textbox1.triggerHandler( 'focus' ); } ); - codeMirror.on( 'blur', function () { + codeMirror.on( 'blur', () => { $textbox1.triggerHandler( 'blur' ); } ); - mw.hook( 'editRecovery.loadEnd' ).add( function ( data ) { + mw.hook( 'editRecovery.loadEnd' ).add( ( data ) => { codeMirror.on( 'change', data.fieldChangeHandler ); } ); @@ -250,7 +250,7 @@ function init() { */ function updateToolbarButton() { // eslint-disable-next-line no-jquery/no-global-selector - var $button = $( '#mw-editbutton-codemirror' ); + const $button = $( '#mw-editbutton-codemirror' ); $button.toggleClass( 'mw-editbutton-codemirror-active', !!useCodeMirror ); @@ -264,7 +264,7 @@ function init() { * Enables or disables CodeMirror */ function switchCodeMirror() { - var selectionObj, selectionStart, selectionEnd, scrollTop, hasFocus, $codeMirror; + let selectionObj, selectionStart, selectionEnd, scrollTop, hasFocus, $codeMirror; if ( codeMirror ) { scrollTop = codeMirror.getScrollInfo().top; @@ -305,7 +305,7 @@ function init() { * Adds the CodeMirror button to WikiEditor */ function addCodeMirrorToWikiEditor() { - var $codeMirrorButton, + let $codeMirrorButton, context = $textbox1.data( 'wikiEditor-context' ), toolbar = context && context.modules && context.modules.toolbar; @@ -357,13 +357,13 @@ function init() { } // Add CodeMirror button to the enhanced editing toolbar. - mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) { + mw.hook( 'wikiEditor.toolbarReady' ).add( ( $textarea ) => { $textbox1 = $textarea; addCodeMirrorToWikiEditor(); } ); // Synchronize textarea with CodeMirror before leaving - window.addEventListener( 'beforeunload', function () { + window.addEventListener( 'beforeunload', () => { if ( codeMirror ) { codeMirror.save(); } diff --git a/resources/ext.CodeMirror.js b/resources/ext.CodeMirror.js index 68bfcd7e..1218e2bf 100644 --- a/resources/ext.CodeMirror.js +++ b/resources/ext.CodeMirror.js @@ -6,7 +6,7 @@ require( './ext.CodeMirror.data.js' ); * @param {Object} data */ function logUsage( data ) { - var event, editCountBucket; + let event, editCountBucket; /* eslint-disable camelcase */ event = Object.assign( { diff --git a/resources/mode/mediawiki/mediawiki.js b/resources/mode/mediawiki/mediawiki.js index 7cf95f5f..c7c06f23 100644 --- a/resources/mode/mediawiki/mediawiki.js +++ b/resources/mode/mediawiki/mediawiki.js @@ -2,7 +2,7 @@ 'use strict'; function eatMnemonic( stream, style, mnemonicStyle ) { - var ok; + let ok; if ( stream.eat( '#' ) ) { if ( stream.eat( 'x' ) ) { ok = stream.eatWhile( /[a-fA-F\d]/ ) && stream.eat( ';' ); @@ -19,9 +19,9 @@ return style; } - CodeMirror.defineMode( 'mediawiki', function ( config /* , parserConfig */ ) { + CodeMirror.defineMode( 'mediawiki', ( config /* , parserConfig */ ) => { - var mwConfig = config.mwConfig, + let mwConfig = config.mwConfig, urlProtocols = new RegExp( '^(?:' + mwConfig.urlProtocols + ')', 'i' ), permittedHtmlTags = { b: true, bdi: true, del: true, i: true, ins: true, @@ -49,7 +49,7 @@ } function makeLocalStyle( style, state, endGround ) { - var ground = ''; + let ground = ''; switch ( state.nTemplate ) { case 0: break; @@ -363,9 +363,9 @@ } function eatLinkText() { - var linkIsBold, linkIsItalic; + let linkIsBold, linkIsItalic; return function ( stream, state ) { - var tmpstyle; + let tmpstyle; if ( stream.match( ']]' ) ) { state.tokenize = state.stack.pop(); return makeLocalStyle( 'mw-link-bracket', state, 'nLink' ); @@ -394,7 +394,7 @@ function eatTagName( chars, isCloseTag, isHtmlTag ) { return function ( stream, state ) { - var name = ''; + let name = ''; while ( chars > 0 ) { chars--; name = name + stream.next(); @@ -464,7 +464,7 @@ function eatExtTagArea( name ) { return function ( stream, state ) { - var origString = false, + let origString = false, from = stream.pos, to, @@ -503,7 +503,7 @@ function eatExtTokens( origString ) { return function ( stream, state ) { - var ret; + let ret; if ( state.extMode === false ) { ret = ( origString === false && stream.sol() ? 'line-cm-mw-exttag' : 'mw-exttag' ); stream.skipToEnd(); @@ -634,7 +634,7 @@ function eatWikiText( style, mnemonicStyle ) { return function ( stream, state ) { - var ch, tmp, mt, name, isCloseTag, tagname, + let ch, tmp, mt, name, isCloseTag, tagname, sol = stream.sol(); function chain( parser ) { @@ -870,7 +870,7 @@ // firstsingleletterword has maximum priority // firstmultiletterword has medium priority // firstspace has low priority - var end = stream.pos, + const end = stream.pos, str = stream.string.slice( 0, end - 3 ), x1 = str.slice( -1, -1 + 1 ), x2 = str.slice( -2, -2 + 1 ); @@ -911,7 +911,7 @@ }; }, token: function ( stream, state ) { - var style, p, t, f, + let style, p, t, f, readyTokens = [], tmpTokens = []; @@ -975,7 +975,7 @@ return t.style; }, blankLine: function ( state ) { - var ret; + let ret; if ( state.extName ) { if ( state.extMode ) { ret = ''; @@ -994,7 +994,7 @@ function eatNowiki( style, lineStyle ) { return function ( stream, state, ownLine ) { - var s; + let s; if ( ownLine && stream.sol() ) { state.ownLine = true; } else if ( ownLine === false && state.ownLine ) { @@ -1009,22 +1009,18 @@ }; } - CodeMirror.defineMode( 'mw-tag-pre', function ( /* config, parserConfig */ ) { - return { - startState: function () { - return {}; - }, - token: eatNowiki( 'mw-tag-pre', 'line-cm-mw-tag-pre' ) - }; - } ); + CodeMirror.defineMode( 'mw-tag-pre', ( /* config, parserConfig */ ) => ( { + startState: function () { + return {}; + }, + token: eatNowiki( 'mw-tag-pre', 'line-cm-mw-tag-pre' ) + } ) ); - CodeMirror.defineMode( 'mw-tag-nowiki', function ( /* config, parserConfig */ ) { - return { - startState: function () { - return {}; - }, - token: eatNowiki( 'mw-tag-nowiki', 'line-cm-mw-tag-nowiki' ) - }; - } ); + CodeMirror.defineMode( 'mw-tag-nowiki', ( /* config, parserConfig */ ) => ( { + startState: function () { + return {}; + }, + token: eatNowiki( 'mw-tag-nowiki', 'line-cm-mw-tag-nowiki' ) + } ) ); }( CodeMirror ) ); diff --git a/resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js b/resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js index cc035540..c45f7468 100644 --- a/resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js +++ b/resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js @@ -8,7 +8,7 @@ * * @type {Object} */ - var config = mw.config.get( 'extCodeMirrorConfig' ), + const config = mw.config.get( 'extCodeMirrorConfig' ), extCiteLoaded = config.tagModes.ref, testCases = [ { @@ -146,7 +146,7 @@ * @param {Function} callback Ran after CodeMirror has been initialized. */ function setup( wikitext, callback ) { - var $textarea = $( '