From: Evan You Date: Sun, 25 Feb 2024 12:38:33 +0000 (+0800) Subject: dx(reactivity): disable recursive computed warning by default X-Git-Tag: v3.4.20~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b31dd7468b06eb4cb1bdf070584df5bc613946fa;p=thirdparty%2Fvuejs%2Fcore.git dx(reactivity): disable recursive computed warning by default Now can be enabled with app.config.warnRecursiveComputed option. close #10341 --- diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index c3f9c915b1..da63fe8475 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -42,6 +42,11 @@ export class ComputedRefImpl { public _cacheable: boolean + /** + * Dev only + */ + _warnRecursive?: boolean + constructor( private getter: ComputedGetter, private readonly _setter: ComputedSetter, @@ -74,7 +79,9 @@ export class ComputedRefImpl { } trackRefValue(self) if (self.effect._dirtyLevel >= DirtyLevels.MaybeDirty_ComputedSideEffect) { - __DEV__ && warn(COMPUTED_SIDE_EFFECT_WARN, `\n\ngetter: `, this.getter) + if (__DEV__ && (__TEST__ || this._warnRecursive)) { + warn(COMPUTED_SIDE_EFFECT_WARN, `\n\ngetter: `, this.getter) + } triggerRefValue(self, DirtyLevels.MaybeDirty_ComputedSideEffect) } return self._value diff --git a/packages/reactivity/src/index.ts b/packages/reactivity/src/index.ts index 1c80fbc752..505ec9e203 100644 --- a/packages/reactivity/src/index.ts +++ b/packages/reactivity/src/index.ts @@ -43,6 +43,7 @@ export { type WritableComputedOptions, type ComputedGetter, type ComputedSetter, + type ComputedRefImpl, } from './computed' export { deferredComputed } from './deferredComputed' export { diff --git a/packages/runtime-core/src/apiComputed.ts b/packages/runtime-core/src/apiComputed.ts index 97db0da453..a7d959dfaa 100644 --- a/packages/runtime-core/src/apiComputed.ts +++ b/packages/runtime-core/src/apiComputed.ts @@ -1,10 +1,17 @@ -import { computed as _computed } from '@vue/reactivity' -import { isInSSRComponentSetup } from './component' +import { type ComputedRefImpl, computed as _computed } from '@vue/reactivity' +import { getCurrentInstance, isInSSRComponentSetup } from './component' export const computed: typeof _computed = ( getterOrOptions: any, debugOptions?: any, ) => { // @ts-expect-error - return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup) + const c = _computed(getterOrOptions, debugOptions, isInSSRComponentSetup) + if (__DEV__) { + const i = getCurrentInstance() + if (i && i.appContext.config.warnRecursiveComputed) { + ;(c as unknown as ComputedRefImpl)._warnRecursive = true + } + } + return c } diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index c476cdbe70..a1bf09fd21 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -110,6 +110,12 @@ export interface AppConfig { * @deprecated use config.compilerOptions.isCustomElement */ isCustomElement?: (tag: string) => boolean + + /** + * TODO document for 3.5 + * Enable warnings for computed getters that recursively trigger itself. + */ + warnRecursiveComputed?: boolean } export interface AppContext {