-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dx(computed): warn incorrect use of getCurrentInstance inside computed
- Loading branch information
Showing
3 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
import { computed as _computed } from '@vue/reactivity' | ||
import { | ||
type ComputedGetter, | ||
type WritableComputedOptions, | ||
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: any, | ||
getterOrOptions: ComputedGetter<unknown> | WritableComputedOptions<unknown>, | ||
debugOptions?: any, | ||
) => { | ||
// @ts-expect-error | ||
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 | ||
return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters