vize:croquis/cf/watch-can-be-computed
A watcher only copies a value into state and can be a computed.
Default severity: Not emitted
Applies to: Analyzed component graph and the supported facts described below
Automatic fix: None; review related files and apply the repair
Options: No per-code options; supported CLI findings accept severity overrides
This is a published diagnostic contract without a current producer. The Bad/Good scenario below explains the risk and repair; no flag currently makes this code trigger.
This illustrates the published preference for purely derived state. Watchers remain appropriate for external effects or independently writable state; no current producer emits this contract.
Shared project files
Use these unchanged files in both Bad and Good. Install the imported packages in the project: Vue, plus vue-router or Pinia where shown. Follow any version-specific support note. The entry root makes the component relationship explicit.
main.ts
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app');
index.html
<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><title>Contract scenario</title></head>
<body><div id="app"></div><script type="module" src="/main.ts"></script></body></html>
App.vue
<script setup lang="ts">
import { useDouble } from './use-double';
const { count, doubled } = useDouble();
</script>
<template>
<button @click="count++">{{ count }}</button><p>{{ doubled }}</p>
</template>
Bad
The watcher performs no external effect; it only keeps a second writable ref synchronized with twice count. This example has no independent writes to that derived value.
use-double.ts
import { ref, watch } from 'vue';
export function useDouble() {
const count = ref(0);
const doubled = ref(0);
watch(count, next => { doubled.value = next * 2; }, { immediate: true });
return { count, doubled };
}
Good
A computed getter expresses the same derivation directly and removes the manual synchronization and extra writable state.
use-double.ts
import { computed, ref } from 'vue';
export function useDouble() {
const count = ref(0);
const doubled = computed(() => count.value * 2);
return { count, doubled };
}
The Good files demonstrate the change described above; other diagnostics can still apply to the complete project.