Skip to content

Commit

Permalink
chore: use Object.hasOwn() (#749)
Browse files Browse the repository at this point in the history
This change should have no user impact.

`Object.hasOwn()` replaces `Object.prototype.hasOwnProperty()` in modern
JavaScript. Its advantages can be seen in the following code snippet:

```
// Still works even if you overwrite `hasOwnProperty`:
const overwritten = { hasOwnProperty: () => true }
Object.hasOwn(overwritten, "foo")
// => false
overwritten.hasOwnProperty("foo")
// => true (incorrect)

// Still works even if the object has a null prototype:
const nullPrototype = Object.create(null)
nullPrototype.foo = "bar"
Object.hasOwn(nullPrototype, "foo")
// => true
nullPrototype.hasOwnProperty("foo")
// TypeError: nullPrototype.hasOwnProperty is not a function
```

This replaces all uses of `Object.prototype.hasOwnProperty()` with
`Object.hasOwn()`.

I think this is a useful change on its own, but will also help in a
future change.
  • Loading branch information
EvanHahn authored Aug 15, 2024
1 parent 265a5ec commit 1fc0795
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/blob-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ function proxyProps(target, props) {
// @ts-ignore - too much time to learn how to teach this to Typescript
return new Proxy(target, {
get(target, prop, receiver) {
// eslint-disable-next-line no-prototype-builtins
if (props.hasOwnProperty(prop)) {
if (Object.hasOwn(props, prop)) {
return Reflect.get(props, prop, receiver)
} else {
return Reflect.get(target, prop, receiver)
Expand Down
12 changes: 2 additions & 10 deletions src/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export async function readConfig(configPath) {
schemaName: 'field',
}
for (const key of Object.keys(valueSchemas.field.properties)) {
if (hasOwn(field, key)) {
if (Object.hasOwn(field, key)) {
fieldValue[key] = field[key]
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ export async function readConfig(configPath) {
terms: [],
}
for (const key of Object.keys(valueSchemas.preset.properties)) {
if (hasOwn(preset, key)) {
if (Object.hasOwn(preset, key)) {
presetValue[key] = preset[key]
}
}
Expand Down Expand Up @@ -553,14 +553,6 @@ function isRecord(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}

/**
* @param {Record<string | symbol, unknown>} obj
* @param {string | symbol} prop
*/
function hasOwn(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}

/**
* @param {Object} obj
* @param {string | undefined} [obj.fileVersion]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2019",
"lib": ["es2020"],
"lib": ["es2022"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
Expand Down

0 comments on commit 1fc0795

Please sign in to comment.