ESLint: Update syntax to ES6 and autofix

Change-Id: I613cc64a9bf3f375a40f75a1441b83f181a2ab42
This commit is contained in:
Ed Sanders 2024-06-14 12:53:32 +01:00
parent 6958b99f2e
commit 1b101f09ee
8 changed files with 121 additions and 123 deletions

View file

@ -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": [
{

View file

@ -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 &&

View file

@ -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();
}

View file

@ -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( {

View file

@ -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 ) );

View file

@ -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 = $( '<textarea>' );
const $textarea = $( '<textarea>' );
$textarea.val( wikitext );
$( '#qunit-fixture' ).append( $textarea );
@ -167,24 +167,22 @@
$( '#qunit-fixture' ).find( '.CodeMirror' ).remove();
}
testCases.forEach( function ( testCase ) {
QUnit.test( 'Syntax highlighting: ' + testCase.title, function ( assert ) {
return mw.loader.using( config.pluginModules ).then( function () {
setup( testCase.input, function () {
var html = $( '.CodeMirror-code' ).html()
// CodeMirror does this only on WebKit browsers. Strip it to ensure tests
// pass across all browsers.
.replace( / style="padding-right: 0\.1px;"/g, '' )
// Reduce noise we don't really care about to make tests more readable.
.replace( / role="presentation"/g, '' )
.replace( / class="\s*CodeMirror-line\s*"/g, '' );
assert.strictEqual(
html,
testCase.output,
'Textarea contents'
);
} );
testCases.forEach( ( testCase ) => {
QUnit.test( 'Syntax highlighting: ' + testCase.title, ( assert ) => mw.loader.using( config.pluginModules ).then( () => {
setup( testCase.input, () => {
const html = $( '.CodeMirror-code' ).html()
// CodeMirror does this only on WebKit browsers. Strip it to ensure tests
// pass across all browsers.
.replace( / style="padding-right: 0\.1px;"/g, '' )
// Reduce noise we don't really care about to make tests more readable.
.replace( / role="presentation"/g, '' )
.replace( / class="\s*CodeMirror-line\s*"/g, '' );
assert.strictEqual(
html,
testCase.output,
'Textarea contents'
);
} );
} );
} ) );
} );
}() );

View file

@ -39,7 +39,7 @@ ve.ui.CodeMirrorAction.static.isLineNumbering = function () {
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;
@ -51,7 +51,7 @@ ve.ui.CodeMirrorAction.static.isLineNumbering = function () {
* @return {boolean} Action was executed
*/
ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
var action = this,
const action = this,
surface = this.surface,
surfaceView = surface.getView(),
doc = surface.getModel().getDocument();
@ -62,20 +62,20 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
'ext.CodeMirror.lib',
'ext.CodeMirror.mode.mediawiki',
'jquery.client'
] ).then( function () {
var config = mw.config.get( 'extCodeMirrorConfig' );
] ).then( () => {
const config = mw.config.get( 'extCodeMirrorConfig' );
if ( !surface.mirror ) {
// Action was toggled to false since promise started
return;
}
mw.loader.using( config.pluginModules, function () {
mw.loader.using( config.pluginModules, () => {
if ( !surface.mirror ) {
// Action was toggled to false since promise started
return;
}
var tabSizeValue = surfaceView.documentView.documentNode.$element.css( 'tab-size' );
var cmOptions = {
const tabSizeValue = surfaceView.documentView.documentNode.$element.css( 'tab-size' );
const cmOptions = {
value: surface.getDom(),
mwConfig: config,
readOnly: 'nocursor',
@ -120,8 +120,8 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
surfaceView.$element.addClass( 'cm-mw-colorblind-colors' );
}
var profile = $.client.profile();
var supportsTransparentText = 'WebkitTextFillColor' in document.body.style &&
const profile = $.client.profile();
const supportsTransparentText = 'WebkitTextFillColor' in document.body.style &&
// Disable on Firefox+OSX (T175223)
!( profile.layout === 'gecko' && profile.platform === 'mac' );
@ -133,7 +133,7 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
if ( cmOptions.lineNumbers ) {
// Transfer gutter width to VE overlay.
var updateGutter = function ( cmDisplay ) {
const updateGutter = function ( cmDisplay ) {
surfaceView.$documentNode.css( 'margin-left', cmDisplay.gutters.offsetWidth );
};
CodeMirror.on( surface.mirror.display, 'updateGutter', updateGutter );
@ -154,7 +154,7 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
action.onLangChange();
ve.init.target.once( 'surfaceReady', function () {
ve.init.target.once( 'surfaceReady', () => {
if ( surface.mirror ) {
surface.mirror.refresh();
}
@ -177,7 +177,7 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
// Reset gutter.
surfaceView.$documentNode.css( 'margin-left', '' );
var mirrorElement = surface.mirror.getWrapperElement();
const mirrorElement = surface.mirror.getWrapperElement();
mirrorElement.parentNode.removeChild( mirrorElement );
}
@ -193,7 +193,7 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
* @param {ve.dm.Selection} selection
*/
ve.ui.CodeMirrorAction.prototype.onSelect = function ( selection ) {
var range = selection.getCoveringRange();
const range = selection.getCoveringRange();
// Do not re-trigger bracket matching as long as something is selected
if ( !range || !range.isCollapsed() ) {
@ -207,7 +207,7 @@ ve.ui.CodeMirrorAction.prototype.onSelect = function ( selection ) {
* Handle langChange events from the document view
*/
ve.ui.CodeMirrorAction.prototype.onLangChange = function () {
var surface = this.surface,
const surface = this.surface,
doc = surface.getView().getDocument(),
dir = doc.getDir(), lang = doc.getLang();
@ -226,13 +226,13 @@ ve.ui.CodeMirrorAction.prototype.onLangChange = function () {
* @param {ve.dm.Transaction} tx
*/
ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
var offset = 0,
let offset = 0,
replacements = [],
action = this,
store = this.surface.getModel().getDocument().getStore(),
mirror = this.surface.mirror;
tx.operations.forEach( function ( op ) {
tx.operations.forEach( ( op ) => {
if ( op.type === 'retain' ) {
offset += op.length;
} else if ( op.type === 'replace' ) {
@ -247,7 +247,7 @@ ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
} );
// Apply replacements in reverse to avoid having to shift offsets
for ( var i = replacements.length - 1; i >= 0; i-- ) {
for ( let i = replacements.length - 1; i >= 0; i-- ) {
mirror.replaceRange(
replacements[ i ].data,
replacements[ i ].start,
@ -277,11 +277,13 @@ ve.ui.CodeMirrorAction.prototype.getPosFromOffset = function ( veOffset ) {
/* Registration */
// eslint-disable-next-line no-jquery/no-global-selector
var contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
// New pages will use wgPageContentLanguage which is set on the html element.
document.documentElement.dir;
{
// eslint-disable-next-line no-jquery/no-global-selector
const contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
// New pages will use wgPageContentLanguage which is set on the html element.
document.documentElement.dir;
if ( contentDir === 'ltr' ) {
ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );
if ( contentDir === 'ltr' ) {
ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );
}
}

View file

@ -39,7 +39,7 @@ ve.ui.CodeMirrorTool.prototype.onSelect = function () {
// Parent method
ve.ui.CodeMirrorTool.super.prototype.onSelect.apply( this, arguments );
var useCodeMirror = !!this.toolbar.surface.mirror;
const useCodeMirror = !!this.toolbar.surface.mirror;
this.setActive( useCodeMirror );
new mw.Api().saveOption( 'usecodemirror', useCodeMirror ? 1 : 0 );
@ -58,13 +58,13 @@ ve.ui.CodeMirrorTool.prototype.onSelect = function () {
* @inheritdoc
*/
ve.ui.CodeMirrorTool.prototype.onSurfaceChange = function ( oldSurface, newSurface ) {
var isDisabled = newSurface.getMode() !== 'source';
const isDisabled = newSurface.getMode() !== 'source';
this.setDisabled( isDisabled );
if ( !isDisabled ) {
var command = this.getCommand();
var surface = this.toolbar.getSurface();
var useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
const command = this.getCommand();
const surface = this.toolbar.getSurface();
const useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
command.execute( surface, [ useCodeMirror ] );
this.setActive( useCodeMirror );
@ -82,19 +82,21 @@ ve.ui.CodeMirrorTool.prototype.onSurfaceChange = function ( oldSurface, newSurfa
ve.ui.CodeMirrorTool.prototype.onUpdateState = function () {};
// eslint-disable-next-line no-jquery/no-global-selector
var contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
// New pages will use wgPageContentLanguage which is set on the html element.
document.documentElement.dir;
{
// eslint-disable-next-line no-jquery/no-global-selector
const contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
// New pages will use wgPageContentLanguage which is set on the html element.
document.documentElement.dir;
if ( contentDir === 'ltr' ) {
/* Registration */
ve.ui.toolFactory.register( ve.ui.CodeMirrorTool );
if ( contentDir === 'ltr' ) {
/* Registration */
ve.ui.toolFactory.register( ve.ui.CodeMirrorTool );
/* Command */
ve.ui.commandRegistry.register(
new ve.ui.Command(
'codeMirror', 'codeMirror', 'toggle'
)
);
/* Command */
ve.ui.commandRegistry.register(
new ve.ui.Command(
'codeMirror', 'codeMirror', 'toggle'
)
);
}
}