Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Apr 23, 2024
1 parent c63ddb1 commit d838b58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"es-x/no-regexp-s-flag": "off",
"es-x/no-rest-spread-properties": "off",
"es-x/no-string-prototype-matchall": "off",
"es-x/no-string-prototype-replaceall": "off",
"jsdoc/require-param": "off",
"jsdoc/require-param-type": "off",
"jsdoc/require-returns": "off",
Expand Down
84 changes: 42 additions & 42 deletions UserRightsDiff/UserRightsDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,89 +16,89 @@ This script works in Special:UserRights, in watchlists, and when clicking "right

// Don't bother converting this user script to a class. There's a nasty bug involving calling this.functionName2 inside of $().each( this.functionName ). "this" is an HTML element, not the class. Not sure how to fix.

$(function() {
$( function () {
/** Don't delete "(none)". Delete all other parentheses and tags. */
function deleteParenthesesAndTags(text) {
function deleteParenthesesAndTags( text ) {
// delete unicode character U+200E. this whitespace character shows up on watchlists, and causes an extra space if not deleted.
text = text.replace("\u200E", '');
text = text.replace( '\u200E', '' );
// get rid of 2 layers of nested parentheses. will help with some edge cases.
text = text.replace(/\([^()]*\([^()]*\)[^()]*\)/gs, '');
text = text.replace( /\([^()]*\([^()]*\)[^()]*\)/gs, '' );
// get rid of 1 layer of nested parentheses, except for (none)
text = text.replace(/(?!\(none\))\(.*?\){1,}/gs, '');
text = text.replace( /(?!\(none\))\(.*?\){1,}/gs, '' );
// remove Tag: and anything after it
text = text.replace(/ Tags?:.*?$/, '');
text = text.replace( / Tags?:.*?$/, '' );
// cleanup so it's easier to write unit tests (output doesn't have extra spaces in it)
text = text.replace(/ {2,}/gs, ' ');
text = text.replace(/(\S) ,/gs, '$1,');
text = text.replace( / {2,}/gs, ' ' );
text = text.replace( /(\S) ,/gs, '$1,' );
text = text.trim();
return text;
}

/** Fixes a bug where the username contains the word "from", which is searched for by a RegEx later. */
function deleteBeginningOfLogEntry(text) {
text = text.replace(/^.*changed group membership for .* from /, ' from ');
text = text.replace(/^.*was automatically updated from /gs, ' from ');
function deleteBeginningOfLogEntry( text ) {
text = text.replace( /^.*changed group membership for .* from /, ' from ' );
text = text.replace( /^.*was automatically updated from /gs, ' from ' );
return text;
}

function permStringToArray(string) {
string = string.replace(/^(.*) and (.*?$)/, '$1, $2');
if ( string === '(none)') {
function permStringToArray( string ) {
string = string.replace( /^(.*) and (.*?$)/, '$1, $2' );
if ( string === '(none)' ) {
return [];
}
let array = string.split(', ').map(function(str) {
const array = string.split( ', ' ).map( function ( str ) {
str = str.trim();
str = str.replace(/[\s.,/#!$%^&*;:{}=\-_`~()]{2,}/g, ''); // remove fragments of punctuation. can result when trying to delete nested parentheses. will delete fragments such as " .)"
str = str.replace( /[\s.,/#!$%^&*;:{}=\-_`~()]{2,}/g, '' ); // remove fragments of punctuation. can result when trying to delete nested parentheses. will delete fragments such as " .)"
return str;
});
} );
return array;
}

function permArrayToString(array) {
array = array.join(', ');
function permArrayToString( array ) {
array = array.join( ', ' );
return array;
}

function checkLine() {
let text = $(this).text();
let text = $( this ).text();
let from, to;
try {
text = deleteParenthesesAndTags(text);
text = deleteBeginningOfLogEntry(text);
let matches = / from (.*?) to (.*?)(?: \(.*)?$/.exec(text);
from = permStringToArray(matches[1]);
to = permStringToArray(matches[2]);
} catch (err) {
throw new Error("UserRightsDiff.js error. Error was: " + err + ". Input text was: " + $(this).text());
text = deleteParenthesesAndTags( text );
text = deleteBeginningOfLogEntry( text );
const matches = / from (.*?) to (.*?)(?: \(.*)?$/.exec( text );
from = permStringToArray( matches[ 1 ] );
to = permStringToArray( matches[ 2 ] );
} catch ( err ) {
throw new Error( 'UserRightsDiff.js error. Error was: ' + err + '. Input text was: ' + $( this ).text() );
}
let added = to.filter(x => !from.includes(x));
let removed = from.filter(x => !to.includes(x));
let added = to.filter( ( x ) => !from.includes( x ) );
let removed = from.filter( ( x ) => !to.includes( x ) );
added = added.length > 0 ?
'<span class="user-rights-diff" style="background-color:lawngreen">[ADDED: ' + permArrayToString(added) + ']</span>' :
'<span class="user-rights-diff" style="background-color:lawngreen">[ADDED: ' + permArrayToString( added ) + ']</span>' :
'';
removed = removed.length > 0 ?
'<span class="user-rights-diff" style="background-color:yellow">[REMOVED: ' + permArrayToString(removed) + ']</span>' :
'<span class="user-rights-diff" style="background-color:yellow">[REMOVED: ' + permArrayToString( removed ) + ']</span>' :
'';
let noChange = added.length === 0 && removed.length === 0 ?
const noChange = added.length === 0 && removed.length === 0 ?
'<span class="user-rights-diff" style="background-color:lightgray">[NO CHANGE]</span>' :
'';
$(this).append(`<br />${added} ${removed} ${noChange}`);
$( this ).append( `<br />${ added } ${ removed } ${ noChange }` );
}

function checkLog() {
$('body').off('DOMNodeInserted'); // prevent infinite loop
if ( $('.user-rights-diff').length === 0 ) { // don't run twice on the same page
$('.mw-logevent-loglines .mw-logline-rights').each( checkLine ); // Special:UserRights, BradV SuperLinks
$('.mw-changeslist-log-rights .mw-changeslist-log-entry').each( checkLine ); // watchlist
$( 'body' ).off( 'DOMNodeInserted' ); // prevent infinite loop
if ( $( '.user-rights-diff' ).length === 0 ) { // don't run twice on the same page
$( '.mw-logevent-loglines .mw-logline-rights' ).each( checkLine ); // Special:UserRights, BradV SuperLinks
$( '.mw-changeslist-log-rights .mw-changeslist-log-entry' ).each( checkLine ); // watchlist
}
$('body').on('DOMNodeInserted', '.mw-logevent-loglines', checkLog);
$( 'body' ).on( 'DOMNodeInserted', '.mw-logevent-loglines', checkLog );
}

// User:BradV/Scripts/SuperLinks.js
$('body').on('DOMNodeInserted', '.mw-logevent-loglines', checkLog);
$( 'body' ).on( 'DOMNodeInserted', '.mw-logevent-loglines', checkLog );

// Special:UserRights
checkLog();
});
} );

// </nowiki>
// </nowiki>

0 comments on commit d838b58

Please sign in to comment.