Use Object.hasOwn instead of Object.prototype.hasOwnProperty

Reference:
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn
This commit is contained in:
Raymond Hill 2025-03-24 08:15:09 -04:00
parent 0c8de6b550
commit 0fce659bb0
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
21 changed files with 52 additions and 84 deletions

View file

@ -43,9 +43,6 @@ if ( vAPI.canWASM === false ) {
vAPI.supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
/******************************************************************************/
vAPI.app = {
@ -190,7 +187,7 @@ vAPI.browserSettings = (( ) => {
set: function(details) {
for ( const setting in details ) {
if ( hasOwnProperty(details, setting) === false ) { continue; }
if ( Object.hasOwn(details, setting) === false ) { continue; }
switch ( setting ) {
case 'prefetching': {
const enabled = !!details[setting];
@ -1220,7 +1217,7 @@ vAPI.Net = class {
{
const wrrt = browser.webRequest.ResourceType;
for ( const typeKey in wrrt ) {
if ( hasOwnProperty(wrrt, typeKey) ) {
if ( Object.hasOwn(wrrt, typeKey) ) {
this.validTypes.add(wrrt[typeKey]);
}
}

View file

@ -30,10 +30,6 @@ const obsoleteTemplateString = i18n$('3pExternalListObsolete');
const reValidExternalList = /^[a-z-]+:\/\/(?:\S+\/\S*|\/\S+)/m;
const recentlyUpdated = 1 * 60 * 60 * 1000; // 1 hour
// https://eslint.org/docs/latest/rules/no-prototype-builtins
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
let listsetDetails = {};
/******************************************************************************/
@ -246,7 +242,7 @@ const renderFilterLists = ( ) => {
}
for ( const [ listkey, listDetails ] of Object.entries(response.available) ) {
let groupkey = listDetails.group2 || listDetails.group;
if ( hasOwnProperty(listTree, groupkey) === false ) {
if ( Object.hasOwn(listTree, groupkey) === false ) {
groupkey = 'unknown';
}
const groupDetails = listTree[groupkey];
@ -603,7 +599,7 @@ const selectFilterLists = async ( ) => {
const toRemove = [];
for ( const liEntry of qsa$('#lists .listEntry[data-role="leaf"]') ) {
const listkey = liEntry.dataset.key;
if ( hasOwnProperty(listsetDetails.available, listkey) === false ) {
if ( Object.hasOwn(listsetDetails.available, listkey) === false ) {
continue;
}
const listDetails = listsetDetails.available[listkey];

View file

@ -88,7 +88,7 @@ const hashFromAdvancedSettings = function(raw) {
const arrayFromObject = function(o) {
const out = [];
for ( const k in o ) {
if ( o.hasOwnProperty(k) === false ) { continue; }
if ( Object.hasOwn(o, k) === false ) { continue; }
out.push([ k, `${o[k]}` ]);
}
return out;

View file

@ -47,9 +47,6 @@ let remoteServerFriendly = false;
/******************************************************************************/
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
const stringIsNotEmpty = s => typeof s === 'string' && s !== '';
const parseExpires = s => {
@ -735,7 +732,7 @@ async function assetCacheRead(assetKey, updateReadTime = false) {
}
if ( bin instanceof Object === false ) { return reportBack(''); }
if ( hasOwnProperty(bin, internalKey) === false ) { return reportBack(''); }
if ( Object.hasOwn(bin, internalKey) === false ) { return reportBack(''); }
const entry = assetCacheRegistry[assetKey];
if ( entry === undefined ) { return reportBack(''); }

View file

@ -44,10 +44,6 @@ const keysFromGetArg = arg => {
let fastCache = 'indexedDB';
// https://eslint.org/docs/latest/rules/no-prototype-builtins
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
/*******************************************************************************
*
* Extension storage
@ -76,7 +72,7 @@ const cacheStorage = (( ) => {
if ( found.length === wanted.length ) { return; }
const missing = [];
for ( const key of wanted ) {
if ( hasOwnProperty(outbin, key) ) { continue; }
if ( Object.hasOwn(outbin, key) ) { continue; }
missing.push(key);
}
return missing;
@ -118,7 +114,7 @@ const cacheStorage = (( ) => {
if ( argbin instanceof Object === false ) { return; }
if ( Array.isArray(argbin) ) { return; }
for ( const key of wanted ) {
if ( hasOwnProperty(argbin, key) === false ) { continue; }
if ( Object.hasOwn(argbin, key) === false ) { continue; }
outbin[key] = argbin[key];
}
}).then(( ) => {
@ -187,7 +183,7 @@ const cacheStorage = (( ) => {
},
select(api) {
if ( hasOwnProperty(cacheAPIs, api) === false ) { return fastCache; }
if ( Object.hasOwn(cacheAPIs, api) === false ) { return fastCache; }
fastCache = api;
for ( const k of Object.keys(cacheAPIs) ) {
if ( k === api ) { continue; }
@ -672,7 +668,7 @@ const idbStorage = (( ) => {
}
if ( argbin instanceof Object && Array.isArray(argbin) === false ) {
for ( const key of keys ) {
if ( hasOwnProperty(outbin, key) ) { continue; }
if ( Object.hasOwn(outbin, key) ) { continue; }
outbin[key] = argbin[key];
}
}

View file

@ -45,7 +45,7 @@ let details = {};
let lists;
for ( const rawFilter in response ) {
if ( Object.prototype.hasOwnProperty.call(response, rawFilter) ) {
if ( Object.hasOwn(response, rawFilter) ) {
lists = response[rawFilter];
break;
}

View file

@ -177,7 +177,7 @@ function rulesToDoc(clearHistory) {
edit.startOperation();
for ( const key in thePanes ) {
if ( thePanes.hasOwnProperty(key) === false ) { continue; }
if ( Object.hasOwn(thePanes, key) === false ) { continue; }
const doc = thePanes[key].doc;
const rules = filterRules(key);
if (

View file

@ -30,7 +30,7 @@ import punycode from '../lib/punycode.js';
// Object.create(null) is used below to eliminate worries about unexpected
// property names in prototype chain -- and this way we don't have to use
// hasOwnProperty() to avoid this.
// Object.hasOwn() to avoid this.
const supportedDynamicTypes = Object.create(null);
Object.assign(supportedDynamicTypes, {

View file

@ -29,7 +29,7 @@ const decomposedSource = [];
// Object.create(null) is used below to eliminate worries about unexpected
// property names in prototype chain -- and this way we don't have to use
// hasOwnProperty() to avoid this.
// Object.hasOwn() to avoid this.
const switchBitOffsets = Object.create(null);
Object.assign(switchBitOffsets, {

View file

@ -173,7 +173,7 @@ if ( isBackgroundProcess !== true ) {
}
textout += textin.slice(0, match.index);
let prop = match[0].slice(2, -2);
if ( Object.prototype.hasOwnProperty.call(dict, prop) ) {
if ( Object.hasOwn(dict, prop) ) {
textout += dict[prop].replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
} else {

View file

@ -70,9 +70,6 @@ const tabIdFromAttribute = function(elem) {
return isNaN(tabId) ? 0 : tabId;
};
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
const dispatchTabidChange = vAPI.defer.create(( ) => {
document.dispatchEvent(new Event('tabIdChanged'));
});
@ -253,7 +250,7 @@ class LogEntry {
this.voided = false;
if ( details instanceof Object === false ) { return; }
for ( const prop in this ) {
if ( hasOwnProperty(details, prop) === false ) { continue; }
if ( Object.hasOwn(details, prop) === false ) { continue; }
this[prop] = details[prop];
}
if ( details.aliasURL !== undefined ) {
@ -1224,7 +1221,7 @@ dom.on(document, 'keydown', ev => {
const onColorsReady = function(response) {
dom.cl.toggle(dom.body, 'dirty', response.dirty);
for ( const url in response.colors ) {
if ( hasOwnProperty(response.colors, url) === false ) { continue; }
if ( Object.hasOwn(response.colors, url) === false ) { continue; }
const colorEntry = response.colors[url];
const node = qs$(dialog, `.dynamic .entry .action[data-url="${url}"]`);
if ( node === null ) { continue; }
@ -1291,7 +1288,7 @@ dom.on(document, 'keydown', ev => {
dom.cl.toggle(
qs$(dialog, '#createStaticFilter'),
'disabled',
hasOwnProperty(createdStaticFilters, value) || value === ''
Object.hasOwn(createdStaticFilters, value) || value === ''
);
};
@ -1332,7 +1329,7 @@ dom.on(document, 'keydown', ev => {
const value = staticFilterNode().value
.replace(/^((?:@@)?\/.+\/)(\$|$)/, '$1*$2');
// Avoid duplicates
if ( hasOwnProperty(createdStaticFilters, value) ) { return; }
if ( Object.hasOwn(createdStaticFilters, value) ) { return; }
createdStaticFilters[value] = true;
// https://github.com/uBlockOrigin/uBlock-issues/issues/1281#issuecomment-704217175
// TODO:
@ -2835,7 +2832,7 @@ const loggerStats = (( ) => {
};
const setRadioButton = function(group, value) {
if ( hasOwnProperty(options, group) === false ) { return; }
if ( Object.hasOwn(options, group) === false ) { return; }
const groupEl = qs$(dialog, `[data-radio="${group}"]`);
const buttonEls = qsa$(groupEl, '[data-radio-item]');
for ( const buttonEl of buttonEls ) {

View file

@ -58,9 +58,6 @@ import µb from './background.js';
/******************************************************************************/
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
// https://github.com/uBlockOrigin/uBlock-issues/issues/710
// Listeners have a name and a "privileged" status.
// The nameless default handler is always deemed "privileged".
@ -1095,7 +1092,7 @@ const restoreUserData = async function(request) {
// Discard unknown setting or setting with default value.
for ( const key in hiddenSettings ) {
if (
hasOwnProperty(µb.hiddenSettingsDefault, key) === false ||
Object.hasOwn(µb.hiddenSettingsDefault, key) === false ||
hiddenSettings[key] === µb.hiddenSettingsDefault[key]
) {
delete hiddenSettings[key];
@ -1147,7 +1144,7 @@ const resetUserData = async function() {
// Filter lists
const prepListEntries = function(entries) {
for ( const k in entries ) {
if ( hasOwnProperty(entries, k) === false ) { continue; }
if ( Object.hasOwn(entries, k) === false ) { continue; }
const entry = entries[k];
if ( typeof entry.supportURL === 'string' && entry.supportURL !== '' ) {
entry.supportName = hostnameFromURI(entry.supportURL);
@ -1335,7 +1332,7 @@ const getSupportData = async function() {
let addedListset = {};
let removedListset = {};
for ( const listKey in lists ) {
if ( hasOwnProperty(lists, listKey) === false ) { continue; }
if ( Object.hasOwn(lists, listKey) === false ) { continue; }
const list = lists[listKey];
if ( list.content !== 'filters' ) { continue; }
const used = µb.selectedFilterLists.includes(listKey);

View file

@ -68,9 +68,6 @@ let cachedPopupHash = '';
const reCyrillicNonAmbiguous = /[\u0400-\u042b\u042d-\u042f\u0431\u0432\u0434\u0436-\u043d\u0442\u0444\u0446-\u0449\u044b-\u0454\u0457\u0459-\u0460\u0462-\u0474\u0476-\u04ba\u04bc\u04be-\u04ce\u04d0-\u0500\u0502-\u051a\u051c\u051e-\u052f]/;
const reCyrillicAmbiguous = /[\u042c\u0430\u0433\u0435\u043e\u043f\u0440\u0441\u0443\u0445\u044a\u0455\u0456\u0458\u0461\u0475\u04bb\u04bd\u04cf\u0501\u051b\u051d]/;
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
/******************************************************************************/
const cachePopupData = function(data) {
@ -89,7 +86,7 @@ const cachePopupData = function(data) {
return popupData;
}
for ( const hostname in hostnameDict ) {
if ( hasOwnProperty(hostnameDict, hostname) === false ) { continue; }
if ( Object.hasOwn(hostnameDict, hostname) === false ) { continue; }
let domain = hostnameDict[hostname].domain;
let prefix = hostname.slice(0, 0 - domain.length - 1);
// Prefix with space char for 1st-party hostnames: this ensure these
@ -161,7 +158,7 @@ const formatNumber = function(count) {
});
if (
intl.resolvedOptions instanceof Function &&
hasOwnProperty(intl.resolvedOptions(), 'notation')
Object.hasOwn(intl.resolvedOptions(), 'notation')
) {
intlNumberFormat = intl;
}
@ -546,7 +543,7 @@ const renderPrivacyExposure = function() {
if ( des === '*' || desHostnameDone.has(des) ) { continue; }
const hnDetails = hostnameDict[des];
const { domain, counts } = hnDetails;
if ( hasOwnProperty(allDomains, domain) === false ) {
if ( Object.hasOwn(allDomains, domain) === false ) {
allDomains[domain] = false;
allDomainCount += 1;
}

View file

@ -53,7 +53,7 @@ export function runAt(fn, when) {
const tokens = Array.isArray(state) ? state : [ state ];
for ( const token of tokens ) {
const prop = `${token}`;
if ( targets.hasOwnProperty(prop) === false ) { continue; }
if ( Object.hasOwn(targets, prop) === false ) { continue; }
return targets[prop];
}
return 0;

View file

@ -93,7 +93,7 @@ const initWorker = function() {
};
for ( const listKey in µb.availableFilterLists ) {
if ( Object.prototype.hasOwnProperty.call(µb.availableFilterLists, listKey) === false ) {
if ( Object.hasOwn(µb.availableFilterLists, listKey) === false ) {
continue;
}
const entry = µb.availableFilterLists[listKey];

View file

@ -306,9 +306,6 @@ const shouldCompress = (s, options) =>
options.compressThreshold <= s.length
);
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
/*******************************************************************************
*
* A large Uint is always a positive integer (can be zero), assumed to be
@ -1155,7 +1152,7 @@ export const getConfig = ( ) => Object.assign({}, currentConfig);
export const setConfig = config => {
for ( const key in Object.keys(config) ) {
if ( hasOwnProperty(defaultConfig, key) === false ) { continue; }
if ( Object.hasOwn(defaultConfig, key) === false ) { continue; }
const val = config[key];
if ( typeof val !== typeof defaultConfig[key] ) { continue; }
if ( (validateConfig[key])(val) === false ) { continue; }

View file

@ -357,15 +357,15 @@ const onFirstFetchReady = (fetched, adminExtra) => {
const toFetch = (from, fetched) => {
for ( const k in from ) {
if ( from.hasOwnProperty(k) === false ) { continue; }
if ( Object.hasOwn(from, k) === false ) { continue; }
fetched[k] = from[k];
}
};
const fromFetch = (to, fetched) => {
for ( const k in to ) {
if ( to.hasOwnProperty(k) === false ) { continue; }
if ( fetched.hasOwnProperty(k) === false ) { continue; }
if ( Object.hasOwn(to, k) === false ) { continue; }
if ( Object.hasOwn(fetched, k) === false ) { continue; }
to[k] = fetched[k];
}
};

View file

@ -48,12 +48,6 @@ import µb from './background.js';
/******************************************************************************/
// https://eslint.org/docs/latest/rules/no-prototype-builtins
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);
/******************************************************************************/
µb.getBytesInUse = async function() {
const promises = [];
let bytesInUse;
@ -186,7 +180,7 @@ const hasOwnProperty = (o, p) =>
for ( const entry of adminSettings ) {
if ( entry.length < 1 ) { continue; }
const name = entry[0];
if ( hasOwnProperty(usDefault, name) === false ) { continue; }
if ( Object.hasOwn(usDefault, name) === false ) { continue; }
const value = entry.length < 2
? usDefault[name]
: this.settingValueFromString(usDefault, name, entry[1]);
@ -215,8 +209,8 @@ const hasOwnProperty = (o, p) =>
const toRemove = [];
for ( const key in this.userSettings ) {
if ( hasOwnProperty(this.userSettings, key) === false ) { continue; }
if ( hasOwnProperty(toSave, key) ) { continue; }
if ( Object.hasOwn(this.userSettings, key) === false ) { continue; }
if ( Object.hasOwn(toSave, key) ) { continue; }
toRemove.push(key);
}
if ( toRemove.length !== 0 ) {
@ -253,7 +247,7 @@ const hasOwnProperty = (o, p) =>
for ( const entry of advancedSettings ) {
if ( entry.length < 1 ) { continue; }
const name = entry[0];
if ( hasOwnProperty(hsDefault, name) === false ) { continue; }
if ( Object.hasOwn(hsDefault, name) === false ) { continue; }
const value = entry.length < 2
? hsDefault[name]
: this.hiddenSettingValueFromString(name, entry[1]);
@ -287,8 +281,8 @@ const hasOwnProperty = (o, p) =>
}
for ( const key in hsDefault ) {
if ( hasOwnProperty(hsDefault, key) === false ) { continue; }
if ( hasOwnProperty(hsAdmin, name) ) { continue; }
if ( Object.hasOwn(hsDefault, key) === false ) { continue; }
if ( Object.hasOwn(hsAdmin, name) ) { continue; }
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
this.hiddenSettings[key] = hs[key];
}
@ -334,8 +328,8 @@ onBroadcast(msg => {
const matches = /^\s*(\S+)\s+(.+)$/.exec(line);
if ( matches === null || matches.length !== 3 ) { continue; }
const name = matches[1];
if ( hasOwnProperty(out, name) === false ) { continue; }
if ( hasOwnProperty(this.hiddenSettingsAdmin, name) ) { continue; }
if ( Object.hasOwn(out, name) === false ) { continue; }
if ( Object.hasOwn(this.hiddenSettingsAdmin, name) ) { continue; }
const value = this.hiddenSettingValueFromString(name, matches[2]);
if ( value !== undefined ) {
out[name] = value;
@ -347,7 +341,7 @@ onBroadcast(msg => {
µb.hiddenSettingValueFromString = function(name, value) {
if ( typeof name !== 'string' || typeof value !== 'string' ) { return; }
const hsDefault = this.hiddenSettingsDefault;
if ( hasOwnProperty(hsDefault, name) === false ) { return; }
if ( Object.hasOwn(hsDefault, name) === false ) { return; }
let r;
switch ( typeof hsDefault[name] ) {
case 'boolean':
@ -688,7 +682,7 @@ onBroadcast(msg => {
µb.autoSelectRegionalFilterLists = function(lists) {
const selectedListKeys = [ this.userFiltersPath ];
for ( const key in lists ) {
if ( hasOwnProperty(lists, key) === false ) { continue; }
if ( Object.hasOwn(lists, key) === false ) { continue; }
const list = lists[key];
if ( list.content !== 'filters' ) { continue; }
if ( list.off !== true ) {
@ -941,7 +935,7 @@ onBroadcast(msg => {
let acceptedCount = snfe.acceptedCount + sxfe.acceptedCount;
let discardedCount = snfe.discardedCount + sxfe.discardedCount;
µb.applyCompiledFilters(compiled, assetKey === µb.userFiltersPath);
if ( hasOwnProperty(µb.availableFilterLists, assetKey) ) {
if ( Object.hasOwn(µb.availableFilterLists, assetKey) ) {
const entry = µb.availableFilterLists[assetKey];
entry.entryCount = snfe.acceptedCount + sxfe.acceptedCount -
acceptedCount;
@ -977,7 +971,7 @@ onBroadcast(msg => {
// content.
const toLoad = [];
for ( const assetKey in lists ) {
if ( hasOwnProperty(lists, assetKey) === false ) { continue; }
if ( Object.hasOwn(lists, assetKey) === false ) { continue; }
if ( lists[assetKey].off ) { continue; }
toLoad.push(
µb.getCompiledFilterList(assetKey).then(details => {
@ -1428,8 +1422,8 @@ onBroadcast(msg => {
const µbus = this.userSettings;
const adminus = data.userSettings;
for ( const name in µbus ) {
if ( hasOwnProperty(µbus, name) === false ) { continue; }
if ( hasOwnProperty(adminus, name) === false ) { continue; }
if ( Object.hasOwn(µbus, name) === false ) { continue; }
if ( Object.hasOwn(adminus, name) === false ) { continue; }
bin[name] = adminus[name];
binNotEmpty = true;
}
@ -1600,7 +1594,7 @@ onBroadcast(msg => {
if ( topic === 'before-asset-updated' ) {
if ( details.type === 'filters' ) {
if (
hasOwnProperty(this.availableFilterLists, details.assetKey) === false ||
Object.hasOwn(this.availableFilterLists, details.assetKey) === false ||
this.selectedFilterLists.indexOf(details.assetKey) === -1 ||
this.badLists.get(details.assetKey)
) {
@ -1615,7 +1609,7 @@ onBroadcast(msg => {
// Skip selfie-related content.
if ( details.assetKey.startsWith('selfie/') ) { return; }
const cached = typeof details.content === 'string' && details.content !== '';
if ( hasOwnProperty(this.availableFilterLists, details.assetKey) ) {
if ( Object.hasOwn(this.availableFilterLists, details.assetKey) ) {
if ( cached ) {
if ( this.selectedFilterLists.indexOf(details.assetKey) !== -1 ) {
this.extractFilterListMetadata(

View file

@ -251,7 +251,7 @@ const textEncode = (( ) => {
return {
encode: function(charset, buf) {
return encoders.hasOwnProperty(charset) ?
return Object.hasOwn(encoders, charset) ?
encoders[charset](buf) :
buf;
},

View file

@ -333,7 +333,7 @@ const matchBucket = function(url, hostname, bucket, start) {
}
// Change -- but only if the user setting actually exists.
const mustSave = us.hasOwnProperty(name) && value !== us[name];
const mustSave = Object.hasOwn(us, name) && value !== us[name];
if ( mustSave ) {
us[name] = value;
}

View file

@ -93,7 +93,7 @@ import µb from './background.js';
µb.getModifiedSettings = function(edit, orig = {}) {
const out = {};
for ( const prop in edit ) {
if ( orig.hasOwnProperty(prop) && edit[prop] !== orig[prop] ) {
if ( Object.hasOwn(orig, prop) && edit[prop] !== orig[prop] ) {
out[prop] = edit[prop];
}
}
@ -102,7 +102,7 @@ import µb from './background.js';
µb.settingValueFromString = function(orig, name, s) {
if ( typeof name !== 'string' || typeof s !== 'string' ) { return; }
if ( orig.hasOwnProperty(name) === false ) { return; }
if ( Object.hasOwn(orig, name) === false ) { return; }
let r;
switch ( typeof orig[name] ) {
case 'boolean':