English
vue-perfectionist/no-reactive-composable-return
Disallow returning Vue reactive proxies directly from composables.
At a glance
| Concern | Behavior |
|---|---|
| Scope | Vue scripts and standalone JS/TS files |
| Defaults | Report direct returns of reactive() and shallowReactive() results in named useXxx composables. |
| Required options | None; enabling the rule uses its defaults. |
| Automatic fixes | — |
| Type information | No TypeScript type services required. |
Destructuring a reactive proxy reads its properties once. A destructured primitive such as count no longer tracks updates to state.count. Return a plain object containing refs so consumers can destructure it and keep tracking state changes. See Vue's composable return guidance.
Usage
With the plugin and parsers configured, enable the rule in your ESLint flat config. See Configuration.
js
{
rules: {
'vue-perfectionist/no-reactive-composable-return': 'error',
},
}📖 Rule Details
The rule reports direct returns of Vue's reactive() or shallowReactive(). It also reports returned local variables initialized with one of those calls when their bindings are never reassigned. Updating a property of the proxy does not reassign the binding.
ts
import { reactive, shallowReactive } from 'vue'
export const usePage = () => reactive({ current: 1 })
export function useCounter() {
const state = shallowReactive({ count: 0 })
return state
}incorrect
Returning an individual ref or a plain object is allowed. A plain object can also expose the whole proxy as a named field. Callers can destructure that field and continue accessing properties on the proxy.
ts
import { reactive, ref } from 'vue'
export function useCounter() {
const count = ref(0)
return { count }
}
export function usePage() {
const state = reactive({ current: 1 })
return { state }
}correct
🔧 Options
| Option | Type | Default | Meaning |
|---|---|---|---|
functionPattern | string | '^use[A-Z]' | Regular expression matching function binding names. An empty string matches every named definition. |
ignoreNames | string[] | [] | Exact binding names to exclude, taking precedence over the pattern. |
Use ignoreNames for composables that intentionally expose a proxy as their public API.
Scope
The rule checks standalone JS/TS and both Vue script blocks using syntax and lexical scope. Matching function declarations and arrow or function expressions used as variable initializers are checked, including async functions and concise arrows. Each explicit return is checked in its own function. Anonymous callbacks, object and class methods, generators, and template expressions are outside scope.
Named imports from vue, including aliases, and dot calls on a Vue namespace import are recognized. Enable shared auto-import recognition to check unbound reactive and shallowReactive calls. TypeScript expression wrappers such as as and satisfies are transparent. Computed and optional calls are outside scope.
The rule follows a returned identifier to a single local initializer in the same composable. For var declarations, the initializer must precede the return in an enclosing block. Module bindings, parameters, reassigned variables, aliases of other variables, and results hidden inside conditional expressions or wrapper calls are outside scope.
Diagnostics and fixes
The diagnostic points to the returned call or identifier. Changing a proxy return to a plain object of refs can change how callers read and write state, so the rule provides no automatic fixes or suggestions.
Use require-composable-return-bindings to check how fields of returned object literals are declared.