Skip to content

Commit

Permalink
fix(provide/inject): do not mutate original provide options during merge
Browse files Browse the repository at this point in the history
fix #12854
  • Loading branch information
yyx990803 committed Nov 9, 2022
1 parent 5e3d4e9 commit d1899ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/core/util/options.ts
Expand Up @@ -51,7 +51,8 @@ if (__DEV__) {
*/
function mergeData(
to: Record<string | symbol, any>,
from: Record<string | symbol, any> | null
from: Record<string | symbol, any> | null,
recursive = true
): Record<PropertyKey, any> {
if (!from) return to
let key, toVal, fromVal
Expand All @@ -66,7 +67,7 @@ function mergeData(
if (key === '__ob__') continue
toVal = to[key]
fromVal = from[key]
if (!hasOwn(to, key)) {
if (!recursive || !hasOwn(to, key)) {
set(to, key, fromVal)
} else if (
toVal !== fromVal &&
Expand Down Expand Up @@ -262,7 +263,22 @@ strats.props =
if (childVal) extend(ret, childVal)
return ret
}
strats.provide = mergeDataOrFn

strats.provide = function (parentVal: Object | null, childVal: Object | null) {
if (!parentVal) return childVal
return function () {
const ret = Object.create(null)
mergeData(ret, isFunction(parentVal) ? parentVal.call(this) : parentVal)
if (childVal) {
mergeData(
ret,
isFunction(childVal) ? childVal.call(this) : childVal,
false // non-recursive
)
}
return ret
}
}

/**
* Default strategy.
Expand Down
8 changes: 8 additions & 0 deletions test/unit/features/options/inject.spec.ts
Expand Up @@ -712,4 +712,12 @@ describe('Options provide/inject', () => {
await nextTick()
expect(spy).toHaveBeenCalledWith(2)
})

// #12854
test('should not mutate original provide options', () => {
const hairMixin = { provide: { hair: 'red' } }
const eyesMixin = { provide: { eyes: 'brown' } }
new Vue({ mixins: [hairMixin, eyesMixin], render() {} }).$mount()
expect(eyesMixin.provide).toStrictEqual({ eyes: 'brown' })
})
})

0 comments on commit d1899ca

Please sign in to comment.