Skip to content

Commit 57eccd6

Browse files
committed
fix: use modern Object.hasOwn
1 parent 1e98501 commit 57eccd6

5 files changed

Lines changed: 23 additions & 24 deletions

File tree

‎packages/docs/core-concepts/plugins.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ import { toRef, ref } from 'vue'
124124
pinia.use(({ store }) => {
125125
// to correctly handle SSR, we need to make sure we are not overriding an
126126
// existing value
127-
if (!store.$state.hasOwnProperty('hasError')) {
127+
if (!Object.hasOwn(store.$state, 'hasError')) {
128128
// hasError is defined within the plugin, so each store has their individual
129129
// state property
130130
const hasError = ref(false)
@@ -154,7 +154,7 @@ import { toRef, ref } from 'vue'
154154

155155
pinia.use(({ store }) => {
156156
// this is the same code as above for reference
157-
if (!store.$state.hasOwnProperty('hasError')) {
157+
if (!Object.hasOwn(store.$state, 'hasError')) {
158158
const hasError = ref(false)
159159
store.$state.hasError = hasError
160160
}

‎packages/docs/zh/core-concepts/plugins.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ import { toRef, ref } from 'vue'
124124
pinia.use(({ store }) => {
125125
// 为了正确地处理 SSR,我们需要确保我们没有重写任何一个
126126
// 现有的值
127-
if (!store.$state.hasOwnProperty('hasError')) {
127+
if (!Object.hasOwn(store.$state, 'hasError')) {
128128
// 在插件中定义 hasError,因此每个 store 都有各自的
129129
// hasError 状态
130130
const hasError = ref(false)
@@ -151,7 +151,7 @@ pinia.use(({ store }) => {
151151
```js
152152
import { set, toRef } from '@vue/composition-api'
153153
pinia.use(({ store }) => {
154-
if (!store.$state.hasOwnProperty('secret')) {
154+
if (!Object.hasOwn(store.$state, 'secret')) {
155155
const secretRef = ref('secret')
156156
// 如果这些数据是要在 SSR 过程中使用的
157157
// 你应该将其设置在 `$state' 属性上
@@ -176,7 +176,7 @@ import { toRef, ref } from 'vue'
176176

177177
pinia.use(({ store }) => {
178178
// 和上面的代码一样,只是为了参考
179-
if (!store.$state.hasOwnProperty('hasError')) {
179+
if (!Object.hasOwn(store.$state, 'hasError')) {
180180
const hasError = ref(false)
181181
store.$state.hasError = hasError
182182
}

‎packages/pinia/__tests__/storePlugins.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('store plugins', () => {
4242

4343
// must call use after installing the plugin
4444
pinia.use(({ app, store }) => {
45-
if (!store.$state.hasOwnProperty('pluginN')) {
45+
if (!Object.hasOwn(store.$state, 'pluginN')) {
4646
// @ts-expect-error: cannot be a ref yet
4747
store.$state.pluginN = ref(20)
4848
}
@@ -72,7 +72,7 @@ describe('store plugins', () => {
7272
mount({ template: 'none' }, { global: { plugins: [pinia] } })
7373

7474
pinia.use(({ app, store }) => {
75-
if (!store.$state.hasOwnProperty('pluginN')) {
75+
if (!Object.hasOwn(store.$state, 'pluginN')) {
7676
// @ts-expect-error: cannot be a ref yet
7777
store.$state.pluginN = ref(20)
7878
}
@@ -163,7 +163,7 @@ describe('store plugins', () => {
163163

164164
// must call use after installing the plugin
165165
pinia.use(({ app, store }) => {
166-
if (!store.$state.hasOwnProperty('shared')) {
166+
if (!Object.hasOwn(store.$state, 'shared')) {
167167
// @ts-expect-error: cannot be a ref yet
168168
store.$state.shared = ref(20)
169169
}

‎packages/pinia/src/store.ts‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ function mergeReactiveObjects<
8888

8989
// no need to go through symbols because they cannot be serialized anyway
9090
for (const key in patchToApply) {
91-
if (!patchToApply.hasOwnProperty(key)) continue
91+
if (!Object.hasOwn(patchToApply, key)) continue
9292
const subPatch = patchToApply[key]
9393
const targetValue = target[key]
9494
if (
9595
isPlainObject(targetValue) &&
9696
isPlainObject(subPatch) &&
97-
target.hasOwnProperty(key) &&
97+
Object.hasOwn(target, key) &&
9898
!isRef(subPatch) &&
9999
!isReactive(subPatch)
100100
) {
@@ -133,10 +133,7 @@ export function skipHydrate<T = any>(obj: T): T {
133133
* @returns true if `obj` should be hydrated
134134
*/
135135
export function shouldHydrate(obj: any) {
136-
return (
137-
!isPlainObject(obj) ||
138-
!Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol)
139-
)
136+
return !isPlainObject(obj) || !Object.hasOwn(obj, skipHydrateSymbol)
140137
}
141138

142139
const { assign } = Object
@@ -283,7 +280,7 @@ function createSetupStore<
283280

284281
// avoid triggering too many listeners
285282
// https://github.com/vuejs/pinia/issues/1129
286-
let activeListener: Symbol | undefined
283+
let activeListener: symbol | undefined
287284
function $patch(stateMutation: (state: UnwrapRef<S>) => void): void
288285
function $patch(partialState: _DeepPartial<UnwrapRef<S>>): void
289286
function $patch(
@@ -938,10 +935,12 @@ export function defineStore(
938935
* - `SS` is the return type of the setup function
939936
* @see {@link StoreDefinition}
940937
*/
941-
export interface SetupStoreDefinition<Id extends string, SS>
942-
extends StoreDefinition<
943-
Id,
944-
_ExtractStateFromSetupStore<SS>,
945-
_ExtractGettersFromSetupStore<SS>,
946-
_ExtractActionsFromSetupStore<SS>
947-
> {}
938+
export interface SetupStoreDefinition<
939+
Id extends string,
940+
SS,
941+
> extends StoreDefinition<
942+
Id,
943+
_ExtractStateFromSetupStore<SS>,
944+
_ExtractGettersFromSetupStore<SS>,
945+
_ExtractActionsFromSetupStore<SS>
946+
> {}

‎packages/testing/src/testing.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ function mergeReactiveObjects<T extends StateTree>(
185185
): T {
186186
// no need to go through symbols because they cannot be serialized anyway
187187
for (const key in patchToApply) {
188-
if (!patchToApply.hasOwnProperty(key)) continue
188+
if (!Object.hasOwn(patchToApply, key)) continue
189189
const subPatch = patchToApply[key]
190190
const targetValue = target[key]
191191
if (
192192
isPlainObject(targetValue) &&
193193
isPlainObject(subPatch) &&
194-
target.hasOwnProperty(key) &&
194+
Object.hasOwn(target, key) &&
195195
!isRef(subPatch) &&
196196
!isReactive(subPatch)
197197
) {

0 commit comments

Comments
 (0)