revert: "dx(computed): warn incorrect use of getCurrentInstance inside computed"

This reverts commit 324e817ef8.
This commit is contained in:
Evan You 2024-01-09 16:37:50 +08:00
parent 3135fcb0e4
commit 2fd3905738
3 changed files with 5 additions and 92 deletions

View File

@ -1,44 +0,0 @@
import {
computed,
getCurrentInstance,
h,
nodeOps,
render,
} from '@vue/runtime-test'
describe('api: computed', () => {
test('should warn if getCurrentInstance is called inside computed getter', () => {
const Comp = {
setup() {
const c = computed(() => {
getCurrentInstance()
return 1
})
return () => c.value
},
}
render(h(Comp), nodeOps.createElement('div'))
expect(
'getCurrentInstance() called inside a computed getter',
).toHaveBeenWarned()
})
test('should warn if getCurrentInstance is called inside computed getter (object syntax)', () => {
const Comp = {
setup() {
const c = computed({
get: () => {
getCurrentInstance()
return 1
},
set: () => {},
})
return () => c.value
},
}
render(h(Comp), nodeOps.createElement('div'))
expect(
'getCurrentInstance() called inside a computed getter',
).toHaveBeenWarned()
})
})

View File

@ -1,42 +1,10 @@
import { import { computed as _computed } from '@vue/reactivity'
type ComputedGetter,
type WritableComputedOptions,
computed as _computed,
} from '@vue/reactivity'
import { isInSSRComponentSetup } from './component' import { isInSSRComponentSetup } from './component'
import { isFunction } from '@vue/shared'
/**
* For dev warning only.
* Context: https://github.com/vuejs/core/discussions/9974
*/
export let isInComputedGetter = false
function wrapComputedGetter(
getter: ComputedGetter<unknown>,
): ComputedGetter<unknown> {
return () => {
isInComputedGetter = true
try {
return getter()
} finally {
isInComputedGetter = false
}
}
}
export const computed: typeof _computed = ( export const computed: typeof _computed = (
getterOrOptions: ComputedGetter<unknown> | WritableComputedOptions<unknown>, getterOrOptions: any,
debugOptions?: any, debugOptions?: any,
) => { ) => {
if (__DEV__) { // @ts-expect-error
if (isFunction(getterOrOptions)) {
getterOrOptions = wrapComputedGetter(getterOrOptions)
} else {
getterOrOptions.get = wrapComputedGetter(getterOrOptions.get)
}
}
// @ts-expect-error the 3rd argument is hidden from public types
return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup) return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
} }

View File

@ -85,7 +85,6 @@ import {
} from './compat/compatConfig' } from './compat/compatConfig'
import type { SchedulerJob } from './scheduler' import type { SchedulerJob } from './scheduler'
import type { LifecycleHooks } from './enums' import type { LifecycleHooks } from './enums'
import { isInComputedGetter } from './apiComputed'
export type Data = Record<string, unknown> export type Data = Record<string, unknown>
@ -632,18 +631,8 @@ export function createComponentInstance(
export let currentInstance: ComponentInternalInstance | null = null export let currentInstance: ComponentInternalInstance | null = null
export const getCurrentInstance: () => ComponentInternalInstance | null = export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
() => { currentInstance || currentRenderingInstance
if (__DEV__ && isInComputedGetter) {
warn(
`getCurrentInstance() called inside a computed getter. ` +
`This is incorrect usage as computed getters are not guaranteed ` +
`to be executed with an active component instance. If you are using ` +
`a composable inside a computed getter, move it ouside to the setup scope.`,
)
}
return currentInstance || currentRenderingInstance
}
let internalSetCurrentInstance: ( let internalSetCurrentInstance: (
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,