]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
revert: "dx(computed): warn incorrect use of getCurrentInstance inside computed"
authorEvan You <yyx990803@gmail.com>
Tue, 9 Jan 2024 08:37:50 +0000 (16:37 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 9 Jan 2024 08:37:50 +0000 (16:37 +0800)
This reverts commit 324e817ef8d00c091e5d1537789542241c876d66.

packages/runtime-core/__tests__/apiComputed.spec.ts [deleted file]
packages/runtime-core/src/apiComputed.ts
packages/runtime-core/src/component.ts

diff --git a/packages/runtime-core/__tests__/apiComputed.spec.ts b/packages/runtime-core/__tests__/apiComputed.spec.ts
deleted file mode 100644 (file)
index 4646692..0000000
+++ /dev/null
@@ -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()
-  })
-})
index d634196764f30c966e5f79f4612e73a24aa0b952..97db0da453cd63bcb48979c49e81f6067c9ad864 100644 (file)
@@ -1,42 +1,10 @@
-import {
-  type ComputedGetter,
-  type WritableComputedOptions,
-  computed as _computed,
-} from '@vue/reactivity'
+import { computed as _computed } from '@vue/reactivity'
 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 = (
-  getterOrOptions: ComputedGetter<unknown> | WritableComputedOptions<unknown>,
+  getterOrOptions: any,
   debugOptions?: any,
 ) => {
-  if (__DEV__) {
-    if (isFunction(getterOrOptions)) {
-      getterOrOptions = wrapComputedGetter(getterOrOptions)
-    } else {
-      getterOrOptions.get = wrapComputedGetter(getterOrOptions.get)
-    }
-  }
-
-  // @ts-expect-error the 3rd argument is hidden from public types
+  // @ts-expect-error
   return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
 }
index a8edbb9e22866c74a946dc8d9b368a12dd4b807d..1508627e579faa65c44c05c1ee2b89f7552c4037 100644 (file)
@@ -85,7 +85,6 @@ import {
 } from './compat/compatConfig'
 import type { SchedulerJob } from './scheduler'
 import type { LifecycleHooks } from './enums'
-import { isInComputedGetter } from './apiComputed'
 
 export type Data = Record<string, unknown>
 
@@ -632,18 +631,8 @@ export function createComponentInstance(
 
 export let currentInstance: ComponentInternalInstance | null = null
 
-export const getCurrentInstance: () => ComponentInternalInstance | null =
-  () => {
-    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
-  }
+export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
+  currentInstance || currentRenderingInstance
 
 let internalSetCurrentInstance: (
   instance: ComponentInternalInstance | null,