Skip to content

Commit 7525ad9

Browse files
authored
Merge pull request #15866 from Automattic/refactor/use-object-has-own
refactor: use Object.hasOwn instead of Object.prototype.hasOwnProperty(...)
2 parents 9d094b3 + f0ee1d3 commit 7525ad9

30 files changed

+109
-119
lines changed

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
@@ -784,7 +784,7 @@ function init(self, obj, doc, opts, prefix) {
784784
}
785785
} else {
786786
// Retain order when overwriting defaults
787-
if (doc.hasOwnProperty(i) && value !== void 0 && !opts.hydratedPopulatedDocs) {
787+
if (Object.hasOwn(doc, i) && value !== void 0 && !opts.hydratedPopulatedDocs) {
788788
delete doc[i];
789789
}
790790
if (value === null) {
@@ -1167,7 +1167,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
11671167
const orderedKeys = Object.keys(this.$__schema.tree);
11681168
for (let i = 0, len = orderedKeys.length; i < len; ++i) {
11691169
(key = orderedKeys[i]) &&
1170-
(this._doc.hasOwnProperty(key)) &&
1170+
(Object.hasOwn(this._doc, key)) &&
11711171
(orderedDoc[key] = undefined);
11721172
}
11731173
this._doc = Object.assign(orderedDoc, this._doc);
@@ -1217,8 +1217,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
12171217
return this;
12181218
}
12191219
const wasModified = this.$isModified(path);
1220-
const hasInitialVal = this.$__.savedState != null && this.$__.savedState.hasOwnProperty(path);
1221-
if (this.$__.savedState != null && !this.$isNew && !this.$__.savedState.hasOwnProperty(path)) {
1220+
const hasInitialVal = this.$__.savedState != null && Object.hasOwn(this.$__.savedState, path);
1221+
if (this.$__.savedState != null && !this.$isNew && !Object.hasOwn(this.$__.savedState, path)) {
12221222
const initialVal = this.$__getValue(path);
12231223
this.$__.savedState[path] = initialVal;
12241224

@@ -1523,7 +1523,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
15231523
this.$__.session[sessionNewDocuments].get(this).modifiedPaths &&
15241524
!this.$__.session[sessionNewDocuments].get(this).modifiedPaths.has(savedStatePath);
15251525
if (savedState != null &&
1526-
savedState.hasOwnProperty(savedStatePath) &&
1526+
Object.hasOwn(savedState, savedStatePath) &&
15271527
(!isInTransaction || isModifiedWithinTransaction) &&
15281528
utils.deepEqual(val, savedState[savedStatePath])) {
15291529
this.unmarkModified(path);
@@ -2005,7 +2005,7 @@ Document.prototype.$get = Document.prototype.get;
20052005

20062006
Document.prototype.$__path = function(path) {
20072007
const adhocs = this.$__.adhocPaths;
2008-
const adhocType = adhocs && adhocs.hasOwnProperty(path) ? adhocs[path] : null;
2008+
const adhocType = adhocs && Object.hasOwn(adhocs, path) ? adhocs[path] : null;
20092009

20102010
if (adhocType) {
20112011
return adhocType;
@@ -2049,7 +2049,7 @@ Document.prototype.$__saveInitialState = function $__saveInitialState(path) {
20492049
if (savedState != null) {
20502050
const firstDot = savedStatePath.indexOf('.');
20512051
const topLevelPath = firstDot === -1 ? savedStatePath : savedStatePath.slice(0, firstDot);
2052-
if (!savedState.hasOwnProperty(topLevelPath)) {
2052+
if (!Object.hasOwn(savedState, topLevelPath)) {
20532053
savedState[topLevelPath] = clone(this.$__getValue(topLevelPath));
20542054
}
20552055
}
@@ -2360,15 +2360,15 @@ Document.prototype.$isDefault = function(path) {
23602360
}
23612361

23622362
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2363-
return this.$__.activePaths.getStatePaths('default').hasOwnProperty(path);
2363+
return Object.hasOwn(this.$__.activePaths.getStatePaths('default'), path);
23642364
}
23652365

23662366
let paths = path;
23672367
if (!Array.isArray(paths)) {
23682368
paths = paths.split(' ');
23692369
}
23702370

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

23742374
/**
@@ -2422,7 +2422,7 @@ Document.prototype.isDirectModified = function(path) {
24222422
}
24232423

24242424
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2425-
const res = this.$__.activePaths.getStatePaths('modify').hasOwnProperty(path);
2425+
const res = Object.hasOwn(this.$__.activePaths.getStatePaths('modify'), path);
24262426
if (res || path.indexOf('.') === -1) {
24272427
return res;
24282428
}
@@ -2461,15 +2461,15 @@ Document.prototype.isInit = function(path) {
24612461
}
24622462

24632463
if (typeof path === 'string' && path.indexOf(' ') === -1) {
2464-
return this.$__.activePaths.getStatePaths('init').hasOwnProperty(path);
2464+
return Object.hasOwn(this.$__.activePaths.getStatePaths('init'), path);
24652465
}
24662466

24672467
let paths = path;
24682468
if (!Array.isArray(paths)) {
24692469
paths = paths.split(' ');
24702470
}
24712471

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

24752475
/**
@@ -2607,7 +2607,7 @@ Document.prototype.isDirectSelected = function isDirectSelected(path) {
26072607
return true;
26082608
}
26092609

2610-
if (this.$__.selected.hasOwnProperty(path)) {
2610+
if (Object.hasOwn(this.$__.selected, path)) {
26112611
return inclusive;
26122612
}
26132613

@@ -2779,7 +2779,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
27792779
if (doc.$isModified(fullPathToSubdoc, null, modifiedPaths) &&
27802780
// Avoid using isDirectModified() here because that does additional checks on whether the parent path
27812781
// is direct modified, which can cause performance issues re: gh-14897
2782-
!subdocParent.$__.activePaths.getStatePaths('modify').hasOwnProperty(fullPathToSubdoc) &&
2782+
!Object.hasOwn(subdocParent.$__.activePaths.getStatePaths('modify'), fullPathToSubdoc) &&
27832783
!subdocParent.$isDefault(fullPathToSubdoc)) {
27842784
paths.add(fullPathToSubdoc);
27852785

@@ -2850,7 +2850,12 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
28502850
// Single nested paths (paths embedded under single nested subdocs) will
28512851
// be validated on their own when we call `validate()` on the subdoc itself.
28522852
// Re: gh-8468
2853-
Object.keys(flat).filter(path => !doc.$__schema.singleNestedPaths.hasOwnProperty(path)).forEach(addToPaths);
2853+
const singleNestedPaths = doc.$__schema.singleNestedPaths;
2854+
for (const path of Object.keys(flat)) {
2855+
if (!Object.hasOwn(singleNestedPaths, path)) {
2856+
addToPaths(path);
2857+
}
2858+
}
28542859
}
28552860
}
28562861

@@ -4188,7 +4193,7 @@ function applyVirtuals(self, json, options, toObjectOptions) {
41884193
}
41894194

41904195
// Allow skipping aliases with `toObject({ virtuals: true, aliases: false })`
4191-
if (!aliases && schema.aliases.hasOwnProperty(path)) {
4196+
if (!aliases && Object.hasOwn(schema.aliases, path)) {
41924197
continue;
41934198
}
41944199

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ function _setClient(conn, client, options, dbName) {
508508
conn.onOpen();
509509

510510
for (const i in conn.collections) {
511-
if (utils.object.hasOwnProperty(conn.collections, i)) {
511+
if (Object.hasOwn(conn.collections, i)) {
512512
conn.collections[i].onOpen();
513513
}
514514
}

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)