-
-
Notifications
You must be signed in to change notification settings - Fork 696
feat(no-ref-as-operand): support ref detection from composable functions #2954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Implement comprehensive support for detecting ref objects returned from composable functions
in the no-ref-as-operand rule. This enhancement includes:
Type Information Foundation:
- Add utility functions to detect functions that return Ref objects
- checkFunctionReturnsRef(): Analyzes function bodies for ref returns
- isRefCall(): Recursively checks for ref() calls in various patterns
- Support for multiple return patterns:
* Direct ref() calls: return ref(0)
* Object properties: return { data: ref(0) }
* Array elements: return [ref(0)]
Composable Function Detection:
- Implement processComposableRefCall() method for handling composable function calls
- Build map of ref-returning functions via body analysis (not JSDoc)
- Detect variable assignments from composable function calls
- Properly handle all scope contexts, not just global scope
- Generate appropriate error messages showing composable function names
Testing:
- Add 6 new test cases covering composable function ref detection
- Test valid usage with proper .value access
- Test invalid usage without .value access
- All 55 tests pass (49 existing + 6 new)
This approach is more robust than JSDoc detection as it analyzes actual code structure
and works reliably in test environments.
🦋 Changeset detectedLatest commit: 2507cc9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
FloEdelmann
commented
Dec 12, 2025
This only works for composables defined inside the component itself, right? So not for imported ones? If so, it's probably not that useful and will likely result in exactly that issue raised right after release 😅
As I wrote in #2519 (comment), we could use variable types (Ref<...>) when using typescript-eslint. Would you be willing to look into that?
Closes #2519
Summary
Extends the
vue/no-ref-as-operandrule to detect and report when ref objectsreturned from composable functions are used without accessing the
.valueproperty.Previously, the rule only detected direct ref declarations like
ref(0).Now it also detects ref objects returned from composable functions:
Motivation
Composable functions are a common pattern in Vue 3 for code reuse. When they return
Ref objects, developers should use the .value property to access the wrapped value.
However, the rule previously had no way to detect when these composable-returned refs
were used incorrectly.
This enhancement provides early feedback to developers, helping them follow Vue 3
best practices and preventing subtle bugs.
Implementation Details
Scope and Limitations
The detection works for composable functions defined in the same file. Due to
ESLint's single-file analysis model, composable functions imported from external
modules cannot be analyzed. This is a fundamental ESLint framework constraint.
Testing