-
-
Notifications
You must be signed in to change notification settings - Fork 4k
refactor: use Object.hasOwn instead of Object#hasOwnProperty #15875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the codebase to use the modern Object.hasOwn() API instead of the legacy Object.prototype.hasOwnProperty.call() and .hasOwnProperty() patterns. This is a port of #15866 to the 8.x branch, bringing consistency and modernization to property existence checks across the codebase.
Key changes:
- Replaced all instances of
.hasOwnProperty()andObject.prototype.hasOwnProperty.call()withObject.hasOwn() - Removed the custom utility function
exports.object.hasOwnPropertyfromlib/utils.js - Applied optional chaining (
?.) in some locations to simplify null checks
Reviewed changes
Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/virtualType.js | Updated property check to use Object.hasOwn() |
| lib/utils.js | Removed deprecated hasOwnProperty utility function and updated hasUserDefinedProperty |
| lib/types/objectid.js | Updated prototype property check |
| lib/types/documentArray/index.js | Converted proxy handler property checks and added optional chaining |
| lib/types/array/index.js | Converted proxy handler property checks and added optional chaining |
| lib/schemaType.js | Updated constructor and instance property checks |
| lib/schema/array.js | Updated type checking logic |
| lib/schema.js | Converted numerous property existence checks across schema operations |
| lib/query.js | Updated option and discriminator key checks |
| lib/mongoose.js | Updated model caching checks and added optional chaining |
| lib/model.js | Updated session option checks and hook context checks |
| lib/helpers/update/decorateUpdateWithVersionKey.js | Simplified hasKey helper function |
| lib/helpers/update/applyTimestampsToUpdate.js | Updated timestamp update checks |
| lib/helpers/timestamps/setupTimestamps.js | Updated currentTime option check |
| lib/helpers/setDefaultsOnInsert.js | Updated path existence checks |
| lib/helpers/query/getEmbeddedDiscriminatorPath.js | Updated array filter checks |
| lib/helpers/projection/applyProjection.js | Updated projection property checks |
| lib/helpers/populate/*.js | Updated various populate-related property checks |
| lib/helpers/model/castBulkWrite.js | Updated discriminator key check |
| lib/helpers/model/applyMethods.js | Updated method conflict check |
| lib/helpers/indexes/*.js | Updated index-related property checks |
| lib/helpers/common.js | Updated nested path check |
| lib/drivers/node-mongodb-native/connection.js | Updated option and collection property checks |
| lib/document.js | Extensive updates to property checks and refactored filter chain to for loop |
| lib/connection.js | Updated config, session, and collection property checks |
| lib/cast.js | Updated schema path check |
| lib/aggregate.js | Updated session option check |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vkarpov15
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. Just for completeness, Object.hasOwn() has been in Node.js since 16.9.0, and Mongoose 8 requires >=16.20.1 so this is safe.
Port #15866 to 8.x