Skip to content

Commit

Permalink
ci: 👷 lint code to MediaWiki standards
Browse files Browse the repository at this point in the history
Check commit and GitHub actions for more details
  • Loading branch information
github-actions committed Jun 7, 2024
1 parent d0e6842 commit 2607651
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 61 deletions.
3 changes: 1 addition & 2 deletions resources/skins.citizen.preferences/clientPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ function createRow( className ) {
* @param {string} featureName
* @return {MwMessage}
*/
const getFeatureLabelMsg = ( featureName ) =>
getMessage( `${ featureName }-name` );
const getFeatureLabelMsg = ( featureName ) => getMessage( `${ featureName }-name` );

/**
* adds a toggle button
Expand Down
8 changes: 3 additions & 5 deletions resources/skins.citizen.preferences/clientPrefs.polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ function isValidFeatureValue( value ) {
function saveClientPrefs( feature, value ) {
const existingStorage = mw.storage.get( CLIENTPREF_STORAGE_NAME ) || '';
const data = {};
existingStorage.split( CLIENTPREF_DELIMITER ).forEach( function ( keyValuePair ) {
existingStorage.split( CLIENTPREF_DELIMITER ).forEach( ( keyValuePair ) => {
const m = keyValuePair.match( /^([\w-]+)-clientpref-(\w+)$/ );
if ( m ) {
data[ m[ 1 ] ] = m[ 2 ];
}
} );
data[ feature ] = value;

const newStorage = Object.keys( data ).map( function ( key ) {
return key + CLIENTPREF_SUFFIX + data[ key ];
} ).join( CLIENTPREF_DELIMITER );
const newStorage = Object.keys( data ).map( ( key ) => key + CLIENTPREF_SUFFIX + data[ key ] ).join( CLIENTPREF_DELIMITER );
mw.storage.set( CLIENTPREF_STORAGE_NAME, newStorage );
}

Expand Down Expand Up @@ -100,7 +98,7 @@ function clientPrefs() {
get: function ( feature ) {
const featurePrefix = feature + CLIENTPREF_SUFFIX;
const docClass = document.documentElement.className;
// eslint-disable-next-line security/detect-non-literal-regexp

const featureRegEx = new RegExp(
'(^| )' + mw.util.escapeRegExp( featurePrefix ) + '([a-zA-Z0-9]+)( |$)'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ function handleClientPreferences() {
'skin-citizen-light',
'skin-citizen-dark'
];
const legacyThemeKey = Object.keys( CLIENTPREFS_THEME_MAP ).find( ( key ) => {
return CLIENTPREFS_THEME_MAP[ key ] === clientPrefs.get( 'skin-theme' );
} );
const legacyThemeKey = Object.keys( CLIENTPREFS_THEME_MAP ).find( ( key ) => CLIENTPREFS_THEME_MAP[ key ] === clientPrefs.get( 'skin-theme' ) );
document.documentElement.classList.remove( ...LEGACY_THEME_CLASSES );
document.documentElement.classList.add( `skin-citizen-${ legacyThemeKey }` );
};
Expand Down
6 changes: 3 additions & 3 deletions resources/skins.citizen.scripts/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ window.clientPrefs = () => {
const storage = localStorage.getItem( 'mwclientpreferences' );
if ( storage ) {
// TODO: Just use array for localStorage
storage.split( ',' ).forEach( function ( pref ) {
storage.split( ',' ).forEach( ( pref ) => {
className = className.replace(
// eslint-disable-next-line security/detect-non-literal-regexp

new RegExp( '(^| )' + pref.replace( /-clientpref-\w+$|[^\w-]+/g, '' ) + '-clientpref-\\w+( |$)' ),
'$1' + pref + '$2'
);
Expand All @@ -33,7 +33,7 @@ window.clientPrefs = () => {
// eslint-disable-next-line max-len, es-x/no-object-values
const classesToRemove = Object.values( CLIENTPREFS_THEME_MAP ).map( ( theme ) => LEGACY_PREFIX + theme );
className = className.replace(
// eslint-disable-next-line security/detect-non-literal-regexp

new RegExp( classesToRemove.join( '|' ), 'g' ),
''
);
Expand Down
2 changes: 1 addition & 1 deletion resources/skins.citizen.scripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function setLoadingIndicatorListeners( element, attach, eventCallback ) {
/** @type { "addEventListener" | "removeEventListener" } */
const addOrRemoveListener = ( attach ? 'addEventListener' : 'removeEventListener' );

[ 'input', 'focusin', 'focusout' ].forEach( function ( eventType ) {
[ 'input', 'focusin', 'focusout' ].forEach( ( eventType ) => {
element[ addOrRemoveListener ]( eventType, eventCallback );
} );

Expand Down
26 changes: 11 additions & 15 deletions resources/skins.citizen.scripts/skin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ function enableCssAnimations( document ) {
function initStickyHeader( document ) {
const { initDirectionObserver, initIntersectionObserver } = require( './scrollObserver.js' );

const toggleScrollClass = ( removeClass, addClass ) => {
return () => {
window.requestAnimationFrame( () => {
document.body.classList.remove( removeClass );
document.body.classList.add( addClass );
} );
};
const toggleScrollClass = ( removeClass, addClass ) => () => {
window.requestAnimationFrame( () => {
document.body.classList.remove( removeClass );
document.body.classList.add( addClass );
} );
};

const addScrollDownClass = toggleScrollClass( SCROLL_UP_CLASS, SCROLL_DOWN_CLASS );
const addScrollUpClass = toggleScrollClass( SCROLL_DOWN_CLASS, SCROLL_UP_CLASS );

const toggleStickyClass = ( state ) => {
return () => {
window.requestAnimationFrame( () => {
document.body.classList.toggle( STICKY_CLASS, state );
} );
};
const toggleStickyClass = ( state ) => () => {
window.requestAnimationFrame( () => {
document.body.classList.toggle( STICKY_CLASS, state );
} );
};

initDirectionObserver( addScrollDownClass, addScrollUpClass, 10 );
Expand Down Expand Up @@ -118,7 +114,7 @@ function main( window ) {
checkbox.bind();
dropdown.init();

mw.hook( 'wikipage.content' ).add( function ( content ) {
mw.hook( 'wikipage.content' ).add( ( content ) => {
// content is a jQuery object
// note that this refers to .mw-body-content, not #bodyContent
initBodyContent( content[ 0 ] );
Expand Down Expand Up @@ -148,7 +144,7 @@ function main( window ) {
if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
main( window );
} else {
document.addEventListener( 'DOMContentLoaded', function () {
document.addEventListener( 'DOMContentLoaded', () => {
main( window );
} );
}
16 changes: 5 additions & 11 deletions resources/skins.citizen.scripts/tableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,19 @@ function init( bodyContent ) {
return;
}

const extractIds = () => {
return Array.from( toc.querySelectorAll( '.citizen-toc__listItem' ) )
.map( ( tocListEl ) => tocListEl.id.slice( 4 ) );
};
const extractIds = () => Array.from( toc.querySelectorAll( '.citizen-toc__listItem' ) )
.map( ( tocListEl ) => tocListEl.id.slice( 4 ) );

const queryElements = ( ids ) => {
return ids.map( ( id ) => bodyContent.querySelector( '#' + CSS.escape( id ) ) )
.filter( ( element ) => element !== null && element !== undefined );
};
const queryElements = ( ids ) => ids.map( ( id ) => bodyContent.querySelector( '#' + CSS.escape( id ) ) )
.filter( ( element ) => element !== null && element !== undefined );

const headlines = queryElements( extractIds() );

const computedStyle = window.getComputedStyle( document.documentElement );
const scrollPaddingTop = computedStyle.getPropertyValue( 'scroll-padding-top' );
const topMargin = Number( scrollPaddingTop.slice( 0, -2 ) ) + 20;

const getTopMargin = () => {
return topMargin;
};
const getTopMargin = () => topMargin;

const initSectionObserver = require( './sectionObserver.js' ).init;

Expand Down
8 changes: 2 additions & 6 deletions resources/skins.citizen.search/searchClients/mwActionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ function adaptApiResponse( config, query, response, showDescription ) {

// Sometimes there can be multiple redirect object for the same page, only take the one with lower index
if ( response.pages.length !== pageCount ) {
response.pages = response.pages.filter( ( obj ) => {
return Object.prototype.hasOwnProperty.call( obj, 'title' );
} );
response.pages = response.pages.filter( ( obj ) => Object.prototype.hasOwnProperty.call( obj, 'title' ) );
}
}

Expand Down Expand Up @@ -195,9 +193,7 @@ function mwActionApiSearchClient( config ) {
}
} );
const searchResponsePromise = result.fetch
.then( ( /** @type {ActionResponse} */ res ) => {
return adaptApiResponse( config, q, res, showDescription );
} );
.then( ( /** @type {ActionResponse} */ res ) => adaptApiResponse( config, q, res, showDescription ) );
return {
abort: result.abort,
fetch: searchResponsePromise
Expand Down
4 changes: 1 addition & 3 deletions resources/skins.citizen.search/searchClients/mwRestApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ function mwRestApiSearchClient( config ) {
}
} );
const searchResponsePromise = result.fetch
.then( ( /** @type {RestResponse} */ res ) => {
return adaptApiResponse( config, q, res, showDescription );
} );
.then( ( /** @type {RestResponse} */ res ) => adaptApiResponse( config, q, res, showDescription ) );
return {
abort: result.abort,
fetch: searchResponsePromise
Expand Down
16 changes: 4 additions & 12 deletions resources/skins.citizen.search/searchClients/smwAskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ function getFirstString( s ) {
*/
function adaptApiResponse( config, query, response, showDescription ) {
const urlGeneratorInstance = urlGenerator( config );
const getDescription = ( page ) => {
return getFirstString( page?.printouts?.Description ).slice( 0, 60 );
};
const getDescription = ( page ) => getFirstString( page?.printouts?.Description ).slice( 0, 60 );

return {
query,
Expand Down Expand Up @@ -113,12 +111,8 @@ function smwAskApiSearchClient( config ) {
const searchApiUrl = config.wgScriptPath + '/api.php';

const getConditions = () => {
const separateConditions = ( s ) => {
return s.replace( /\]\]\s*\[\[/g, '|' );
};
const removeSquareBrackets = ( s ) => {
return s.replace( /\[|\]/g, '' );
};
const separateConditions = ( s ) => s.replace( /\]\]\s*\[\[/g, '|' );
const removeSquareBrackets = ( s ) => s.replace( /\[|\]/g, '' );
const conditions = removeSquareBrackets( separateConditions( q ) );
return conditions;
};
Expand Down Expand Up @@ -159,9 +153,7 @@ function smwAskApiSearchClient( config ) {
}
} );
const searchResponsePromise = result.fetch
.then( ( /** @type {SMWAskArgResponse} */ res ) => {
return adaptApiResponse( config, q, res, showDescription );
} );
.then( ( /** @type {SMWAskArgResponse} */ res ) => adaptApiResponse( config, q, res, showDescription ) );
return {
abort: result.abort,
fetch: searchResponsePromise
Expand Down

0 comments on commit 2607651

Please sign in to comment.