Skip to content

Commit

Permalink
eslint: Enable more temporarily-disabled rules (#39057)
Browse files Browse the repository at this point in the history
* `no-undef-init`: One instance fixed.
* `no-prototype-builtins`: Swapped a lot of calls to `.hasOwnProperty()`
  with `Object.hasOwn()`, since all browsers we support appear to
  support that.
* `no-unused-expressions`: Enabled, but with `allowShortCircuit` and
  `allowTernary` set true since we have a lot of that. Fixing these
  caught bugs in two different places!

Also, moved the `eqeqeq` override ignoring `== null` from "temporarily
disabled" to "maybe we want to keep this override permanently", to be
decided later. Put the `no-unused-expressions` allowing of short-circuit
and ternarys there too.
  • Loading branch information
anomiex authored Aug 27, 2024
1 parent cffea69 commit 62905b8
Show file tree
Hide file tree
Showing 68 changed files with 180 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function hasProgressLabel( octokit, owner, repo, number ) {
async function getMilestoneDates( plugin, nextMilestone ) {
let releaseDate = 'none scheduled';
let codeFreezeDate;
if ( nextMilestone && nextMilestone.hasOwnProperty( 'due_on' ) && nextMilestone.due_on ) {
if ( nextMilestone && Object.hasOwn( nextMilestone, 'due_on' ) && nextMilestone.due_on ) {
releaseDate = moment( nextMilestone.due_on ).format( 'LL' );

// Look for a code freeze date in the milestone description.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = ( babel, opts ) => {
callee = callee.expressions[ callee.expressions.length - 1 ];
}
const funcName = t.isMemberExpression( callee ) ? callee.property.name : callee.name;
if ( ! functions.hasOwnProperty( funcName ) ) {
if ( ! Object.hasOwn( functions, funcName ) ) {
return;
}
const idx = functions[ funcName ];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


3 changes: 2 additions & 1 deletion projects/js-packages/boost-score-api/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe( 'getScoreMovementPercentage', () => {

expect( getScoreMovementPercentage( changedMockData.scores ) ).toBe( 0 );

( changedMockData.scores.noBoost.desktop = 80 ), ( changedMockData.scores.noBoost.mobile = 70 );
changedMockData.scores.noBoost.desktop = 80;
changedMockData.scores.noBoost.mobile = 70;

expect( getScoreMovementPercentage( changedMockData.scores ) ).toBe( 13 );
} );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


2 changes: 1 addition & 1 deletion projects/js-packages/components/tools/jp-redirect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function getRedirectUrl( source: string, args: GetRedirectUrlArgs
if (
! Object.keys( queryVars ).includes( 'site' ) &&
typeof jetpack_redirects !== 'undefined' &&
jetpack_redirects.hasOwnProperty( 'currentSiteRawUrl' )
Object.hasOwn( jetpack_redirects, 'currentSiteRawUrl' )
) {
queryVars.site = jetpack_redirects.currentBlogID ?? jetpack_redirects.currentSiteRawUrl;
}
Expand Down
5 changes: 5 additions & 0 deletions projects/js-packages/config/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


2 changes: 1 addition & 1 deletion projects/js-packages/config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ try {
}

const jetpackConfigHas = key => {
return jetpackConfig.hasOwnProperty( key );
return Object.hasOwn( jetpackConfig, key );
};

const jetpackConfigGet = key => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class GettextExtractor {
return;
}

if ( ! this.#functions.hasOwnProperty( callee.name ) ) {
if ( ! Object.hasOwn( this.#functions, callee.name ) ) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions projects/js-packages/idc/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


4 changes: 2 additions & 2 deletions projects/js-packages/idc/components/idc-screen/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const IDCScreen = props => {
initializeAnalytics( tracksEventData, tracksUserData );

if ( tracksEventData ) {
if ( tracksEventData.hasOwnProperty( 'isAdmin' ) && tracksEventData.isAdmin ) {
if ( Object.hasOwn( tracksEventData, 'isAdmin' ) && tracksEventData.isAdmin ) {
trackAndBumpMCStats( 'notice_view' );
} else {
trackAndBumpMCStats( 'non_admin_notice_view', {
page: tracksEventData.hasOwnProperty( 'currentScreen' )
page: Object.hasOwn( tracksEventData, 'currentScreen' )
? tracksEventData.currentScreen
: false,
} );
Expand Down
8 changes: 4 additions & 4 deletions projects/js-packages/idc/tools/tracking.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import analytics from '@automattic/jetpack-analytics';
export function initializeAnalytics( tracksEventData, tracksUserData ) {
if (
tracksUserData &&
tracksUserData.hasOwnProperty( 'userid' ) &&
tracksUserData.hasOwnProperty( 'username' )
Object.hasOwn( tracksUserData, 'userid' ) &&
Object.hasOwn( tracksUserData, 'username' )
) {
analytics.initialize( tracksUserData.userid, tracksUserData.username );
}

if ( tracksEventData ) {
if ( tracksEventData.hasOwnProperty( 'blogID' ) ) {
if ( Object.hasOwn( tracksEventData, 'blogID' ) ) {
analytics.assignSuperProps( { blog_id: tracksEventData.blogID } );
}

if ( tracksEventData.hasOwnProperty( 'platform' ) ) {
if ( Object.hasOwn( tracksEventData, 'platform' ) ) {
analytics.assignSuperProps( { platform: tracksEventData.platform } );
}
}
Expand Down
5 changes: 5 additions & 0 deletions projects/packages/connection/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './style.scss';
* The initial renderer function.
*/
function render() {
if ( ! window.hasOwnProperty( 'JP_IDENTITY_CRISIS__INITIAL_STATE' ) ) {
if ( ! Object.hasOwn( window, 'JP_IDENTITY_CRISIS__INITIAL_STATE' ) ) {
return;
}

Expand Down Expand Up @@ -47,10 +47,10 @@ function render() {
tracksUserData={ tracksUserData || {} }
tracksEventData={ tracksEventData }
customContent={
consumerData.hasOwnProperty( 'customContent' ) ? consumerData.customContent : {}
Object.hasOwn( consumerData, 'customContent' ) ? consumerData.customContent : {}
}
isAdmin={ isAdmin }
logo={ consumerData.hasOwnProperty( 'logo' ) ? consumerData.logo : undefined }
logo={ Object.hasOwn( consumerData, 'logo' ) ? consumerData.logo : undefined }
possibleDynamicSiteUrlDetected={ possibleDynamicSiteUrlDetected }
isDevelopmentSite={ isDevelopmentSite }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ const useKeydownHandler = ( { onEscape, onArrowRight, onArrowLeft }: Props ): vo

switch ( event.key ) {
case 'Escape':
onEscape && ( onEscape(), ( handled = true ) );
if ( onEscape ) {
onEscape();
handled = true;
}
break;
case 'ArrowRight':
onArrowRight && ( onArrowRight(), ( handled = true ) );
if ( onArrowRight ) {
onArrowRight();
handled = true;
}
break;
case 'ArrowLeft':
onArrowLeft && ( onArrowLeft(), ( handled = true ) );
if ( onArrowLeft ) {
onArrowLeft();
handled = true;
}
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// Sass or Less require Partial -- so ajax call to get it from PHP.
// We can explicitly override for specific providers by testing if `'sass' === preprocessor`
if ( jpCustomizerCssPreview.preprocessors.hasOwnProperty( preprocessor ) ) {
if ( Object.hasOwn( jpCustomizerCssPreview.preprocessors, preprocessor ) ) {
return api.selectiveRefresh.Partial.prototype.refresh.call( partial );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function IDCModal() {
return null;
}

if ( ! window.hasOwnProperty( 'JP_IDENTITY_CRISIS__INITIAL_STATE' ) ) {
if ( ! Object.hasOwn( window, 'JP_IDENTITY_CRISIS__INITIAL_STATE' ) ) {
return null;
}

Expand Down
5 changes: 5 additions & 0 deletions projects/packages/my-jetpack/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


5 changes: 5 additions & 0 deletions projects/packages/search/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SearchFilters extends Component {
);

renderStaticFilterComponent = configuration => {
if ( configuration.hasOwnProperty( 'visible' ) && ! configuration.visible ) {
if ( Object.hasOwn( configuration, 'visible' ) && ! configuration.visible ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TabbedSearchFilters extends Component {
};

renderStaticFilterComponent = configuration => {
if ( configuration.hasOwnProperty( 'visible' ) && ! configuration.visible ) {
if ( Object.hasOwn( configuration, 'visible' ) && ! configuration.visible ) {
return null;
}

Expand Down
5 changes: 5 additions & 0 deletions projects/packages/videopress/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function Player( {
const { atTime, previewOnHover, previewAtTime, previewLoopDuration, type } =
attributes.posterData;

let timeToSetPlayerPosition = undefined;
let timeToSetPlayerPosition;
if ( type === 'video-frame' ) {
if ( previewOnHover ) {
timeToSetPlayerPosition = previewAtTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function mapObjectKeysToCamel( originalObject, deleteOriginalProp = false
const object = Object.assign( {}, originalObject );

for ( const key in object ) {
if ( object.hasOwnProperty( key ) && isSnake( key ) ) {
if ( Object.hasOwn( object, key ) && isSnake( key ) ) {
object[ snakeToCamel( key ) ] = object[ key ];

if ( deleteOriginalProp ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function fetchWeightUsingProxy( url: string ): Promise< Response > {
// If the JSON data contains the content length, create a new response object with the JSON headers and the original response body.
const headers = new Headers();
for ( const key in json.data ) {
if ( json.data.hasOwnProperty( key ) ) {
if ( Object.hasOwn( json.data, key ) ) {
headers.set( key, json.data[ key ] );
}
}
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/boost/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix new eslint sniffs. Should be no change in functionality.


Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test.describe( 'Cache module', () => {
}

expect(
response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache'.toLowerCase() ),
Object.hasOwn( response.headers(), 'X-Jetpack-Boost-Cache'.toLowerCase() ),
'Page Cache header should not be present'
).toBeFalsy();
} );
Expand Down Expand Up @@ -122,13 +122,13 @@ test.describe( 'Cache module', () => {
// First visit should always be a miss.
if ( totalVisits === 1 ) {
expect(
responseHeaders.hasOwnProperty( cacheHeaderName ) &&
Object.hasOwn( responseHeaders, cacheHeaderName ) &&
responseHeaders[ cacheHeaderName ] === 'miss',
'Page Cache header should be set to miss on first visit.'
).toBeTruthy();
} else {
expect(
responseHeaders.hasOwnProperty( cacheHeaderName ) &&
Object.hasOwn( responseHeaders, cacheHeaderName ) &&
responseHeaders[ cacheHeaderName ] === 'hit',
'Page Cache header should be set to hit on second visit.'
).toBeTruthy();
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/debug-helper/changelog/add-more-eslint-rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Actually call `e.preventDefault()` in various 'click' handlers.
12 changes: 6 additions & 6 deletions projects/plugins/debug-helper/modules/inc/js/broken-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class BrokenToken {
);

this.setCustomBlogTokenButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.displayEditBlogToken();
} );

this.cancelEditBlogTokenButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.cancelEditBlogToken();
} );

Expand All @@ -26,12 +26,12 @@ class BrokenToken {
);

this.setCustomUserTokenButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.displayEditUserToken();
} );

this.cancelEditUserTokenButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.cancelEditUserToken();
} );

Expand All @@ -41,12 +41,12 @@ class BrokenToken {
this.cancelEditBlogIDButton = document.getElementById( 'broken-token-cancel-edit-blog-id' );

this.setCustomBlogIDButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.displayEditBlogID();
} );

this.cancelEditBlogIDButton.addEventListener( 'click', e => {
e.preventDefault;
e.preventDefault();
this.cancelEditBlogID();
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class JetpackNotices extends React.Component {
this.state = {
isMasterbarNoticeDismissed:
cookieParsed &&
cookieParsed.hasOwnProperty(
Object.hasOwn(
cookieParsed,
'jetpack_deprecate_dismissed[jetpack-masterbar-admin-removal-notice]'
) &&
'1' ===
Expand All @@ -209,7 +210,7 @@ class JetpackNotices extends React.Component {

render() {
const siteDataErrors = this.props.siteDataErrors.filter( error =>
error.hasOwnProperty( 'action' )
Object.hasOwn( error, 'action' )
);

const isUserConnectScreen = this.props.location.pathname.startsWith( '/connect-user' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class JetpackConnectionErrors extends React.Component {
error.action,
error.message,
error.code,
error.hasOwnProperty( 'data' ) ? error.data : {},
Object.hasOwn( error, 'data' ) ? error.data : {},
supportURl
);

Expand All @@ -67,10 +67,10 @@ export default class JetpackConnectionErrors extends React.Component {

render() {
const errorsToDisplay = {};
const errors = this.props.errors.filter( error => error.hasOwnProperty( 'action' ) );
const errors = this.props.errors.filter( error => Object.hasOwn( error, 'action' ) );

for ( const error of errors ) {
if ( ! errorsToDisplay.hasOwnProperty( error.action ) ) {
if ( ! Object.hasOwn( errorsToDisplay, error.action ) ) {
errorsToDisplay[ error.action ] = error;
}
}
Expand Down
Loading

0 comments on commit 62905b8

Please sign in to comment.