Skip to content

Commit f2513c4

Browse files
authored
Merge pull request #15875 from Automattic/refactor/use-object-has-own-8
refactor: use Object.hasOwn instead of Object#hasOwnProperty
2 parents 0275815 + e027e4f commit f2513c4

30 files changed

Lines changed: 110 additions & 120 deletions

lib/aggregate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Aggregate.prototype._optionsForExec = function() {
103103
const options = this.options || {};
104104

105105
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
106-
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
106+
if (!Object.hasOwn(options, 'session') && asyncLocalStorage?.session != null) {
107107
options.session = asyncLocalStorage.session;
108108
}
109109

lib/cast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module.exports = function cast(schema, obj, options, context) {
107107
val = cast(schema, val, options, context);
108108
} else if (path === '$text') {
109109
val = castTextSearch(val, path);
110-
} else if (path === '$comment' && !schema.paths.hasOwnProperty('$comment')) {
110+
} else if (path === '$comment' && !Object.hasOwn(schema.paths, '$comment')) {
111111
val = castString(val, path);
112112
obj[path] = val;
113113
} else {

lib/connection.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
164164
*/
165165

166166
Connection.prototype.get = function getOption(key) {
167-
if (this.config.hasOwnProperty(key)) {
167+
if (Object.hasOwn(this.config, key)) {
168168
return this.config[key];
169169
}
170170

@@ -192,7 +192,7 @@ Connection.prototype.get = function getOption(key) {
192192
*/
193193

194194
Connection.prototype.set = function setOption(key, val) {
195-
if (this.config.hasOwnProperty(key)) {
195+
if (Object.hasOwn(this.config, key)) {
196196
this.config[key] = val;
197197
return val;
198198
}
@@ -459,7 +459,7 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
459459

460460
const ordered = options.ordered == null ? true : options.ordered;
461461
const asyncLocalStorage = this.base.transactionAsyncLocalStorage?.getStore();
462-
if ((!options || !options.hasOwnProperty('session')) && asyncLocalStorage?.session != null) {
462+
if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
463463
options = { ...options, session: asyncLocalStorage.session };
464464
}
465465

@@ -477,7 +477,7 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
477477
if (op.name == null) {
478478
throw new MongooseError('Must specify operation name in Connection.prototype.bulkWrite()');
479479
}
480-
if (!castBulkWrite.cast.hasOwnProperty(op.name)) {
480+
if (!Object.hasOwn(castBulkWrite.cast, op.name)) {
481481
throw new MongooseError(`Unrecognized bulkWrite() operation name ${op.name}`);
482482
}
483483

@@ -513,7 +513,7 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
513513
results[i] = error;
514514
continue;
515515
}
516-
if (!castBulkWrite.cast.hasOwnProperty(op.name)) {
516+
if (!Object.hasOwn(castBulkWrite.cast, op.name)) {
517517
const error = new MongooseError(`Unrecognized bulkWrite() operation name ${op.name}`);
518518
validationErrors.push({ index: i, error: error });
519519
results[i] = error;
@@ -772,10 +772,10 @@ async function _wrapUserTransaction(fn, session, mongoose) {
772772
function _resetSessionDocuments(session) {
773773
for (const doc of session[sessionNewDocuments].keys()) {
774774
const state = session[sessionNewDocuments].get(doc);
775-
if (state.hasOwnProperty('isNew')) {
775+
if (Object.hasOwn(state, 'isNew')) {
776776
doc.$isNew = state.isNew;
777777
}
778-
if (state.hasOwnProperty('versionKey')) {
778+
if (Object.hasOwn(state, 'versionKey')) {
779779
doc.set(doc.schema.options.versionKey, state.versionKey);
780780
}
781781

@@ -1013,7 +1013,7 @@ Connection.prototype.onOpen = function() {
10131013
// avoid having the collection subscribe to our event emitter
10141014
// to prevent 0.3 warning
10151015
for (const i in this.collections) {
1016-
if (utils.object.hasOwnProperty(this.collections, i)) {
1016+
if (Object.hasOwn(this.collections, i)) {
10171017
this.collections[i].onOpen();
10181018
}
10191019
}
@@ -1321,7 +1321,7 @@ Connection.prototype.onClose = function onClose(force) {
13211321
// avoid having the collection subscribe to our event emitter
13221322
// to prevent 0.3 warning
13231323
for (const i in this.collections) {
1324-
if (utils.object.hasOwnProperty(this.collections, i)) {
1324+
if (Object.hasOwn(this.collections, i)) {
13251325
this.collections[i].onClose(force);
13261326
}
13271327
}

lib/document.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ function init(self, obj, doc, opts, prefix) {
780780
}
781781
} else {
782782
// Retain order when overwriting defaults
783-
if (doc.hasOwnProperty(i) && value !== void 0 && !opts.hydratedPopulatedDocs) {
783+
if (Object.hasOwn(doc, i) && value !== void 0 && !opts.hydratedPopulatedDocs) {
784784
delete doc[i];
785785
}
786786
if (value === null) {
@@ -1156,7 +1156,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
11561156
const orderedKeys = Object.keys(this.$__schema.tree);
11571157
for (let i = 0, len = orderedKeys.length; i < len; ++i) {
11581158
(key = orderedKeys[i]) &&
1159-
(this._doc.hasOwnProperty(key)) &&
1159+
(Object.hasOwn(this._doc, key)) &&
11601160
(orderedDoc[key] = undefined);
11611161
}
11621162
this._doc = Object.assign(orderedDoc, this._doc);
@@ -1206,8 +1206,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
12061206
return this;
12071207
}
12081208
const wasModified = this.$isModified(path);
1209-
const hasInitialVal = this.$__.savedState != null && this.$__.savedState.hasOwnProperty(path);
1210-
if (this.$__.savedState != null && !this.$isNew && !this.$__.savedState.hasOwnProperty(path)) {
1209+
const hasInitialVal = this.$__.savedState != null && Object.hasOwn(this.$__.savedState, path);
1210+
if (this.$__.savedState != null && !this.$isNew && !Object.hasOwn(this.$__.savedState, path)) {
12111211
const initialVal = this.$__getValue(path);
12121212
this.$__.savedState[path] = initialVal;
12131213

@@ -1512,7 +1512,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
15121512
this.$__.session[sessionNewDocuments].get(this).modifiedPaths &&
15131513
!this.$__.session[sessionNewDocuments].get(this).modifiedPaths.has(savedStatePath);
15141514
if (savedState != null &&
1515-
savedState.hasOwnProperty(savedStatePath) &&
1515+
Object.hasOwn(savedState, savedStatePath) &&
15161516
(!isInTransaction || isModifiedWithinTransaction) &&
15171517
utils.deepEqual(val, savedState[savedStatePath])) {
15181518
this.unmarkModified(path);
@@ -1994,7 +1994,7 @@ Document.prototype.$get = Document.prototype.get;
19941994

19951995
Document.prototype.$__path = function(path) {
19961996
const adhocs = this.$__.adhocPaths;
1997-
const adhocType = adhocs && adhocs.hasOwnProperty(path) ? adhocs[path] : null;
1997+
const adhocType = adhocs && Object.hasOwn(adhocs, path) ? adhocs[path] : null;
19981998

19991999
if (adhocType) {
20002000
return adhocType;
@@ -2038,7 +2038,7 @@ Document.prototype.$__saveInitialState = function $__saveInitialState(path) {
20382038
if (savedState != null) {
20392039
const firstDot = savedStatePath.indexOf('.');
20402040
const topLevelPath = firstDot === -1 ? savedStatePath : savedStatePath.slice(0, firstDot);
2041-
if (!savedState.hasOwnProperty(topLevelPath)) {
2041+
if (!Object.hasOwn(savedState, topLevelPath)) {
20422042
savedState[topLevelPath] = clone(this.$__getValue(topLevelPath));
20432043
}
20442044
}
@@ -2349,15 +2349,15 @@ Document.prototype.$isDefault = function(path) {
23492349
}
23502350

23512351
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2352-
return this.$__.activePaths.getStatePaths('default').hasOwnProperty(path);
2352+
return Object.hasOwn(this.$__.activePaths.getStatePaths('default'), path);
23532353
}
23542354

23552355
let paths = path;
23562356
if (!Array.isArray(paths)) {
23572357
paths = paths.split(' ');
23582358
}
23592359

2360-
return paths.some(path => this.$__.activePaths.getStatePaths('default').hasOwnProperty(path));
2360+
return paths.some(path => Object.hasOwn(this.$__.activePaths.getStatePaths('default'), path));
23612361
};
23622362

23632363
/**
@@ -2411,7 +2411,7 @@ Document.prototype.isDirectModified = function(path) {
24112411
}
24122412

24132413
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2414-
const res = this.$__.activePaths.getStatePaths('modify').hasOwnProperty(path);
2414+
const res = Object.hasOwn(this.$__.activePaths.getStatePaths('modify'), path);
24152415
if (res || path.indexOf('.') === -1) {
24162416
return res;
24172417
}
@@ -2450,15 +2450,15 @@ Document.prototype.isInit = function(path) {
24502450
}
24512451

24522452
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2453-
return this.$__.activePaths.getStatePaths('init').hasOwnProperty(path);
2453+
return Object.hasOwn(this.$__.activePaths.getStatePaths('init'), path);
24542454
}
24552455

24562456
let paths = path;
24572457
if (!Array.isArray(paths)) {
24582458
paths = paths.split(' ');
24592459
}
24602460

2461-
return paths.some(path => this.$__.activePaths.getStatePaths('init').hasOwnProperty(path));
2461+
return paths.some(path => Object.hasOwn(this.$__.activePaths.getStatePaths('init'), path));
24622462
};
24632463

24642464
/**
@@ -2596,7 +2596,7 @@ Document.prototype.isDirectSelected = function isDirectSelected(path) {
25962596
return true;
25972597
}
25982598

2599-
if (this.$__.selected.hasOwnProperty(path)) {
2599+
if (Object.hasOwn(this.$__.selected, path)) {
26002600
return inclusive;
26012601
}
26022602

@@ -2772,7 +2772,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
27722772
if (doc.$isModified(fullPathToSubdoc, null, modifiedPaths) &&
27732773
// Avoid using isDirectModified() here because that does additional checks on whether the parent path
27742774
// is direct modified, which can cause performance issues re: gh-14897
2775-
!subdocParent.$__.activePaths.getStatePaths('modify').hasOwnProperty(fullPathToSubdoc) &&
2775+
!Object.hasOwn(subdocParent.$__.activePaths.getStatePaths('modify'), fullPathToSubdoc) &&
27762776
!subdocParent.$isDefault(fullPathToSubdoc)) {
27772777
paths.add(fullPathToSubdoc);
27782778

@@ -2843,7 +2843,12 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
28432843
// Single nested paths (paths embedded under single nested subdocs) will
28442844
// be validated on their own when we call `validate()` on the subdoc itself.
28452845
// Re: gh-8468
2846-
Object.keys(flat).filter(path => !doc.$__schema.singleNestedPaths.hasOwnProperty(path)).forEach(addToPaths);
2846+
const singleNestedPaths = doc.$__schema.singleNestedPaths;
2847+
for (const path of Object.keys(flat)) {
2848+
if (!Object.hasOwn(singleNestedPaths, path)) {
2849+
addToPaths(path);
2850+
}
2851+
}
28472852
}
28482853
}
28492854

@@ -4184,7 +4189,7 @@ function applyVirtuals(self, json, options, toObjectOptions) {
41844189
}
41854190

41864191
// Allow skipping aliases with `toObject({ virtuals: true, aliases: false })`
4187-
if (!aliases && schema.aliases.hasOwnProperty(path)) {
4192+
if (!aliases && Object.hasOwn(schema.aliases, path)) {
41884193
continue;
41894194
}
41904195

@@ -5097,7 +5102,7 @@ function checkDivergentArray(doc, path, array) {
50975102
// would be similarly destructive as we never received all
50985103
// elements of the array and potentially would overwrite data.
50995104
const check = pop.options.match ||
5100-
pop.options.options && utils.object.hasOwnProperty(pop.options.options, 'limit') || // 0 is not permitted
5105+
pop.options.options && Object.hasOwn(pop.options.options, 'limit') || // 0 is not permitted
51015106
pop.options.options && pop.options.options.skip || // 0 is permitted
51025107
pop.options.select && // deselected _id?
51035108
(pop.options.select._id === 0 ||

lib/drivers/node-mongodb-native/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ NativeConnection.prototype.useDb = function(name, options) {
108108
function wireup() {
109109
newConn.client = _this.client;
110110
const _opts = {};
111-
if (options.hasOwnProperty('noListener')) {
111+
if (Object.hasOwn(options, 'noListener')) {
112112
_opts.noListener = options.noListener;
113113
}
114114
newConn.db = _this.client.db(name, _opts);
@@ -515,7 +515,7 @@ function _setClient(conn, client, options, dbName) {
515515
conn.onOpen();
516516

517517
for (const i in conn.collections) {
518-
if (utils.object.hasOwnProperty(conn.collections, i)) {
518+
if (Object.hasOwn(conn.collections, i)) {
519519
conn.collections[i].onOpen();
520520
}
521521
}

lib/helpers/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function flatten(update, path, options, schema) {
5555
if (isNested) {
5656
const paths = Object.keys(schema.paths);
5757
for (const p of paths) {
58-
if (p.startsWith(path + key + '.') && !result.hasOwnProperty(p)) {
58+
if (p.startsWith(path + key + '.') && !Object.hasOwn(result, p)) {
5959
result[p] = void 0;
6060
}
6161
}

lib/helpers/indexes/applySchemaCollation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function applySchemaCollation(indexKeys, indexOptions, schemaOp
77
return;
88
}
99

10-
if (schemaOptions.hasOwnProperty('collation') && !indexOptions.hasOwnProperty('collation')) {
10+
if (Object.hasOwn(schemaOptions, 'collation') && !Object.hasOwn(indexOptions, 'collation')) {
1111
indexOptions.collation = schemaOptions.collation;
1212
}
1313
};

lib/helpers/indexes/isDefaultIdIndex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ module.exports = function isDefaultIdIndex(index) {
1414
}
1515

1616
const key = get(index, 'key', {});
17-
return Object.keys(key).length === 1 && key.hasOwnProperty('_id');
17+
return Object.keys(key).length === 1 && Object.hasOwn(key, '_id');
1818
};

lib/helpers/model/applyMethods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function applyMethods(model, schema) {
2828
}
2929
for (const method of Object.keys(schema.methods)) {
3030
const fn = schema.methods[method];
31-
if (schema.tree.hasOwnProperty(method)) {
31+
if (Object.hasOwn(schema.tree, method)) {
3232
throw new Error('You have a method and a property in your schema both ' +
3333
'named "' + method + '"');
3434
}

lib/helpers/model/castBulkWrite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function _addDiscriminatorToObject(schema, obj) {
297297

298298
function decideModelByObject(model, object) {
299299
const discriminatorKey = model.schema.options.discriminatorKey;
300-
if (object != null && object.hasOwnProperty(discriminatorKey)) {
300+
if (object != null && Object.hasOwn(object, discriminatorKey)) {
301301
model = getDiscriminatorByValue(model.discriminators, object[discriminatorKey]) || model;
302302
}
303303
return model;

0 commit comments

Comments
 (0)