English
vue-perfectionist/no-watcher-cleanup-after-await
Disallow calling
onWatcherCleanup()afterawaitin inline Vue watcher callbacks.
At a glance
| Concern | Behavior |
|---|---|
| Scope | Vue scripts and standalone JS/TS files |
| Defaults | Report cleanup registration when every path reaching it has awaited. |
| Required options | None; enabling the rule uses its defaults. |
| Automatic fixes | — |
| Type information | No TypeScript type services required. |
Usage
Enable this rule alongside your existing parser configuration. Vue SFCs need vue-eslint-parser, and TypeScript scripts need @typescript-eslint/parser.
js
import { defineConfig } from 'eslint/config'
import vuePerfectionist from 'eslint-plugin-vue-perfectionist'
export default defineConfig([
// Your existing Vue and TypeScript parser configurations go here.
{
files: ['**/*.{vue,js,ts}'],
plugins: { 'vue-perfectionist': vuePerfectionist },
rules: {
'vue-perfectionist/no-watcher-cleanup-after-await': 'error',
},
},
])📖 Rule Details
Vue's onWatcherCleanup() needs the active watcher to register cleanup. Once the callback suspends at an await, Vue has restored the previous active watcher. Register cleanup before suspension so Vue can cancel stale work when the watcher reruns. The callback parameter onCleanup is bound to the watcher and can be called after an await. See Vue's watcher cleanup guide.
vue
<script setup lang="ts">
import { onWatcherCleanup, ref, watch } from 'vue'
const userId = ref(1)
watch(userId, async id => {
const controller = new AbortController()
onWatcherCleanup(() => controller.abort())
await fetch(`/api/users/${id}`, { signal: controller.signal })
})
</script>correct
vue
<script setup lang="ts">
import { onWatcherCleanup, ref, watch } from 'vue'
const userId = ref(1)
watch(userId, async id => {
const controller = new AbortController()
await fetch(`/api/users/${id}`, { signal: controller.signal })
onWatcherCleanup(() => controller.abort())
})
</script>incorrect
Each report points to the cleanup callee. The call must follow an await on every reachable path. For example, both branches here suspend before cleanup.
js
import { onWatcherCleanup, watchEffect } from 'vue'
watchEffect(async () => {
if (useCache()) {
await readCache()
} else {
await loadData()
}
onWatcherCleanup(dispose)
})incorrect
A branch that skips the await makes the following call outside the rule's reporting threshold. If cleanup must be registered after asynchronous work, use the bound parameter.
js
import { onWatcherCleanup, watchEffect } from 'vue'
watchEffect(async () => {
if (useCache()) {
await readCache()
}
onWatcherCleanup(dispose)
})
watchEffect(async onCleanup => {
const dispose = await openResource()
onCleanup(dispose)
})correct
🔧 Options
This rule has no options. It reports diagnostics without offering automatic fixes or suggestions.
Scope
The rule checks inline async arrow functions and function expressions passed to watch, watchEffect, watchPostEffect, or watchSyncEffect in ordinary SFC scripts, <script setup>, and standalone JavaScript and TypeScript. It recognizes named Vue imports, import aliases, and non-computed namespace calls from vue. The shared auto-import setting enables unbound Vue API names. Local shadows take precedence.
Function references, watch sources, template expressions, optional API calls, and spread-ambiguous callback positions are outside the check. Nested functions have separate control flow. A nested inline Vue watcher is checked on its own. Class static blocks and static field initializers run after the computed class keys and use the enclosing callback's suspension state. Instance field initializers run when an instance is created.
The analysis uses ESLint's control flow and explicit await expressions. Branch reachability and exceptions are approximated from syntax. For destructuring declarations and assignments, it evaluates the value before the pattern. It skips awaits inside the pattern and implicit suspensions in for await...of.
The analysis accounts for break and continue passing through intervening finally blocks. Possible exceptions are tracked at the operations where they can occur and pass through intervening finally blocks before reaching an outer catch. A synchronous exception while evaluating an await operand can reach catch or finally before suspension. A rejected awaited promise reaches its handler after suspension. A call after finally is checked along the paths that continue to it.