Skip to content
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

feat(computed): warn computed self recursion #12225

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
triggerRef,
} from '../src'
import { EffectFlags, pauseTracking, resetTracking } from '../src/effect'
import type { ComputedRef, ComputedRefImpl } from '../src/computed'
import {
COMPUTED_SELF_RECURSIVE_WARN,
type ComputedRef,
type ComputedRefImpl,
} from '../src/computed'

describe('reactivity/computed', () => {
it('should return updated value', () => {
Expand Down Expand Up @@ -482,7 +486,6 @@ describe('reactivity/computed', () => {
const c3 = computed(() => c2.value)

c3.value
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('should work when chained(ref+computed)', () => {
Expand All @@ -496,7 +499,6 @@ describe('reactivity/computed', () => {
const c2 = computed(() => v.value + c1.value)
expect(c2.value).toBe('0foo')
expect(c2.value).toBe('1foo')
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('should trigger effect even computed already dirty', () => {
Expand All @@ -520,7 +522,7 @@ describe('reactivity/computed', () => {
expect(fnSpy).toBeCalledTimes(2)
expect(fnSpy.mock.calls).toMatchObject([['0foo'], ['2foo']])
expect(v.value).toBe(2)
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

// #10185
Expand Down Expand Up @@ -551,7 +553,6 @@ describe('reactivity/computed', () => {
v1.value.v.value = 999

expect(c3.value).toBe('yes')
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('should be not dirty after deps mutate (mutate deps in computed)', async () => {
Expand All @@ -573,7 +574,7 @@ describe('reactivity/computed', () => {
await nextTick()
await nextTick()
expect(serializeInner(root)).toBe(`2`)
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

it('should not trigger effect scheduler by recursive computed effect', async () => {
Expand All @@ -596,7 +597,7 @@ describe('reactivity/computed', () => {
v.value += ' World'
await nextTick()
expect(serializeInner(root)).toBe('Hello World World World World')
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

test('should not trigger if value did not change', () => {
Expand Down Expand Up @@ -895,6 +896,7 @@ describe('reactivity/computed', () => {
expect(serializeInner(root)).toBe(
'Hello World World World World | Hello World World World World',
)
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

it('should keep dirty level when side effect computed value changed', () => {
Expand All @@ -920,6 +922,7 @@ describe('reactivity/computed', () => {

expect(d.value.d).toBe(1)
expect(serializeInner(root)).toBe('11')
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

it('should be recomputed without being affected by side effects', () => {
Expand All @@ -935,7 +938,6 @@ describe('reactivity/computed', () => {
expect(c2.value).toBe('0,0')
v.value = 1
expect(c2.value).toBe('1,0')
// expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('debug: onTrigger (ref)', () => {
Expand Down Expand Up @@ -1004,6 +1006,7 @@ describe('reactivity/computed', () => {
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
expect(COMPUTED_SELF_RECURSIVE_WARN).toHaveBeenWarned()
})

test('manual trigger computed', () => {
Expand Down
14 changes: 12 additions & 2 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface WritableComputedOptions<T, S = T> {
set: ComputedSetter<S>
}

export const COMPUTED_SELF_RECURSIVE_WARN: string =
`Computed has a self-recursive issue,` +
` likely because a computed is mutating its own dependency in its getter.` +
` State mutations in computed getters should be avoided. ` +
` Check the documentation for more details: https://vuejs.org/guide/essentials/computed.html#getters-should-be-side-effect-free`

/**
* @private exported by @vue/reactivity for Vue core use, but not exported from
* the main vue package
Expand Down Expand Up @@ -123,8 +129,12 @@ export class ComputedRefImpl<T = any> implements Subscriber {
) {
batch(this, true)
return true
} else if (__DEV__) {
// TODO warn
} else if (
__DEV__ &&
activeSub === this &&
(this._warnRecursive || __TEST__)
) {
warn(COMPUTED_SELF_RECURSIVE_WARN, `\n\ngetter: `, this.fn)
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export interface AppConfig {
isCustomElement?: (tag: string) => boolean

/**
* TODO document for 3.5
* Enable warnings for computed getters that recursively trigger itself.
*/
warnRecursiveComputed?: boolean
Expand Down