vize:croquis/cf/module-scope-reactive
Reactive state is created at module scope and shared by every caller.
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.
Module-scope reactive state is legal for intentional application stores. This example assumes component/request isolation; the published contract currently has no producer.
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 Counter from './Counter.vue';
</script>
<template>
<Counter /><Counter />
</template>
Counter.vue
<script setup lang="ts">
import { createCounter } from './counter';
const { count } = createCounter();
</script>
<template>
<button @click="count++">{{ count }}</button>
</template>
Bad
The module initializes count once, and both Counter instances receive the same ref. Clicking one changes both counters even though this example intends independent instance state.
counter.ts
import { ref } from 'vue';
const count = ref(0);
export function createCounter() { return { count }; }
Good
Creating the ref inside createCounter gives each synchronous setup call a separate state object, so each button owns its counter.
counter.ts
import { ref } from 'vue';
export function createCounter() {
const count = ref(0);
return { count };
}
The Good files demonstrate the change described above; other diagnostics can still apply to the complete project.