分析 Vue 3 Composition API 使用情况——审计响应式模式、可组合函数设计、ref 与 reactive 的选择、计算属性、watcher 使用及组件……
Deep analysis of Vue 3 Composition API usage. Detects anti-patterns in reactivity, composable design, watcher usage, lifecycle hooks, and component structure. Produces actionable findings with severity levels.
Use when: reviewing Vue 3 quality, migrating from Options API, auditing composable architecture, or enforcing Composition API conventions.
bash
cat package.json 2>/dev/null | jq '{vue: .dependencies.vue, nuxt: .dependencies.nuxt, pinia: .dependencies.pinia}'
find . -name "*.vue" -not -path '*/node_modules/*' | wc -l
# Options API components
grep -rl "export default {" --include="*.vue" . 2>/dev/null | while read f; do
grep -l "data()\|methods:\|computed:" "$f" 2>/dev/null
done | sort -u | head -20
# Script setup (preferred) vs setup()
grep -rl "<script setup" --include="*.vue" . 2>/dev/null | wc -l
grep -rl "setup()" --include="*.vue" . 2>/dev/null | wc -l
Classify each component as Options API, setup(), or <script setup>. Flag mixed-style codebases.
bash
grep -rn "ref(\|reactive(\|shallowRef\|shallowReactive" --include="*.vue" --include="*.ts" . 2>/dev/null | head -30
# Reactivity loss: destructuring reactive without toRefs
grep -rn "const {.*} = .*reactive\|let {.*} = .*reactive" --include="*.vue" --include="*.ts" . 2>/dev/null | head -15
# toRefs usage
grep -rn "toRefs\|toRef(" --include="*.vue" --include="*.ts" . 2>/dev/null | head -15
Check for:
reactive() without toRefs() silently breaks reactivityref for primitives and replaceable objects, reactive for stable complex objectsreactive({count: 0}) instead of ref(0)ref(reactive({...})) — always a bugbash
grep -rn "computed(" --include="*.vue" --include="*.ts" . 2>/dev/null | head -20
# Side effects in computed
grep -A5 "computed(" --include="*.vue" --include="*.ts" . 2>/dev/null \
| grep -i "console\.\|fetch(\|emit(\|\.value =" | head -15
# Watchers
grep -rn "watch(\|watchEffect(" --include="*.vue" --include="*.ts" . 2>/dev/null | head -20
# Immediate watchers (often replaceable with watchEffect)
grep -A10 "watch(" --include="*.vue" --include="*.ts" . 2>/dev/null | grep "immediate: true" | head -10
# Deep watchers on large objects
grep -A10 "watch(" --include="*.vue" --include="*.ts" . 2>/dev/null | grep "deep: true" | head -10
Flag:
localStorage/ window won't trigger re-computationwatchEffect()bash
find . -name "use*.ts" -o -name "use*.js" -not -path '*/node_modules/*' 2>/dev/null | head -20
find . -path "*/composables/*" -not -name "use*" -not -path '*/node_modules/*' -name "*.ts" 2>/dev/null | head -10
# Composables using component-specific APIs
grep -rn "defineProps\|defineEmits\|useSlots" --include="*.ts" . 2>/dev/null | grep -i "composable\|use" | head -10
Evaluate:
use prefixonUnmountedbash
# onMounted without matching onUnmounted
for f in $(grep -rl "onMounted" --include="*.vue" . 2>/dev/null); do
if ! grep -q "onUnmounted\|onBeforeUnmount" "$f"; then
echo "MISSING_CLEANUP: $f"
fi
done | head -15
# addEventListener without removeEventListener
grep -l "addEventListener" --include="*.vue" --include="*.ts" . 2>/dev/null | while read f; do
if ! grep -q "removeEventListener" "$f"; then echo "LISTENER_LEAK: $f"; fi
done | head -15
# setInterval without clearInterval
grep -l "setInterval" --include="*.vue" . 2>/dev/null | while read f; do
if ! grep -q "clearInterval" "$f"; then echo "INTERVAL_LEAK: $f"; fi
done | head -10
bash
grep -rn "defineProps" --include="*.vue" . 2>/dev/null | head -15
grep -A5 "defineProps(\[" --include="*.vue" . 2>/dev/null | head -15
grep -rn "provide(\|inject(" --include="*.vue" --include="*.ts" . 2>/dev/null | head -15
Flag:
defineProps(['foo']) has no type safety; use object or TypeScript generic syntaxInjectionKey<T> for type safetymarkdown
# Vue Composition API Analysis — [Project Name]
## Summary
- Components: N | Script setup: N% | Options API remaining: N
- Critical: N | Warnings: N
## API Style Migration
| Style | Count | Files |
|-------|-------|-------|
| `<script setup>` | N | — |
| `setup()` function | N | file1.vue |
| Options API | N | legacy1.vue |
## Critical Findings
### [C1] Reactivity Loss — destructuring reactive without toRefs
- **File**: src/components/UserForm.vue:24
- **Code**: `const { name, email } = userState`
- **Fix**: `const { name, email } = toRefs(userState)`
### [C2] Side Effect in Computed
- **File**: src/composables/useCart.ts:45
- **Fix**: Move localStorage write to a watcher
## Warnings
### [W1] Missing Cleanup — addEventListener in onMounted, no onUnmounted
### [W2] Deep Watcher on 1000+ item array — watch specific properties instead
## Composable Health
| Composable | Issues |
|-----------|--------|
| useAuth | Returns raw values instead of refs |
| useCart | No cleanup for event subscription |
## Recommendations
1. Convert N Options API components to `<script setup>`
2. Add `InjectionKey<T>` types to provide/inject pairs
3. Replace `watch(..., { immediate: true })` with `watchEffect()` in N places
4. Add `onUnmounted` cleanup to N components with resource subscriptions
Anti-PatternSeverityDescriptionDestructure reactiveCriticalBreaks reactivity silentlySide effects in computedCriticalNon-deterministic, SSR-incompatibleref(reactive(...))CriticalDouble-wrapping, confusing unwrapMissing watcher cleanupHighMemory leaks, race conditionsDeep watch large objectsMediumPerformance degradationwatch + immediateLowPrefer watchEffectArray-style definePropsLowNo type safety or defaults
npx vue-tsc --noEmit to type-check all SFC files including template expressionseffectScope() in composables that create many watchers/effects for batch cleanupuseAsyncData/ useFetch for proper key usage and deduplication