Skip to content

Commit 7d22e76

Browse files
committed
Update vue/no-watch-after-await
1 parent a503ea1 commit 7d22e76

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

docs/rules/no-watch-after-await.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,35 @@ import { watch } from 'vue'
2222
export default {
2323
async setup() {
2424
/* ✓ GOOD */
25-
watch(() => { /* ... */ })
25+
watch(watchSource, () => { /* ... */ })
2626
2727
await doSomething()
2828
2929
/* ✗ BAD */
30-
watch(() => { /* ... */ })
30+
watch(watchSource, () => { /* ... */ })
31+
}
32+
}
33+
</script>
34+
```
35+
36+
</eslint-code-block>
37+
38+
This rule is not reported when using the stop handle.
39+
40+
<eslint-code-block :rules="{'vue/no-watch-after-await': ['error']}">
41+
42+
```vue
43+
<script>
44+
import { watch } from 'vue'
45+
export default {
46+
async setup() {
47+
await doSomething()
48+
49+
/* ✓ GOOD */
50+
const stopHandle = watch(watchSource, () => { /* ... */ })
51+
52+
// later
53+
stopHandle()
3154
}
3255
}
3356
</script>
@@ -42,6 +65,7 @@ Nothing.
4265
## :books: Further reading
4366

4467
- [Vue RFCs - 0013-composition-api](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0013-composition-api.md)
68+
- [Vue Composition API - API Reference - Stopping the Watcher](https://composition-api.vuejs.org/api.html#stopping-the-watcher)
4569

4670
## :mag: Implementation
4771

lib/rules/no-watch-after-await.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
const { ReferenceTracker } = require('eslint-utils')
77
const utils = require('../utils')
88

9+
function isMaybeUsedStopHandle (node) {
10+
const parent = node.parent
11+
if (parent) {
12+
if (parent.type === 'VariableDeclarator') {
13+
// var foo = watch()
14+
return true
15+
}
16+
if (parent.type === 'AssignmentExpression') {
17+
// foo = watch()
18+
return true
19+
}
20+
if (parent.type === 'CallExpression') {
21+
// foo(watch())
22+
return true
23+
}
24+
if (parent.type === 'Property') {
25+
// {foo: watch()}
26+
return true
27+
}
28+
if (parent.type === 'ArrayExpression') {
29+
// [watch()]
30+
return true
31+
}
32+
}
33+
return false
34+
}
35+
936
module.exports = {
1037
meta: {
1138
type: 'suggestion',
@@ -82,7 +109,7 @@ module.exports = {
82109
return
83110
}
84111

85-
if (watchCallNodes.has(node)) {
112+
if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) {
86113
addForbiddenNode(setupFunctionData.setupProperty, node)
87114
}
88115
},

tests/lib/rules/no-watch-after-await.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tester.run('no-watch-after-await', rule, {
2020
import {watch} from 'vue'
2121
export default {
2222
async setup() {
23-
watch(() => { /* ... */ }) // ok
23+
watch(foo, () => { /* ... */ }) // ok
2424
2525
await doSomething()
2626
}
@@ -35,7 +35,7 @@ tester.run('no-watch-after-await', rule, {
3535
import {watch} from 'vue'
3636
export default {
3737
async setup() {
38-
watch(() => { /* ... */ })
38+
watch(foo, () => { /* ... */ })
3939
}
4040
}
4141
</script>
@@ -49,7 +49,7 @@ tester.run('no-watch-after-await', rule, {
4949
export default {
5050
async setup() {
5151
watchEffect(() => { /* ... */ })
52-
watch(() => { /* ... */ })
52+
watch(foo, () => { /* ... */ })
5353
await doSomething()
5454
}
5555
}
@@ -70,6 +70,27 @@ tester.run('no-watch-after-await', rule, {
7070
}
7171
</script>
7272
`
73+
},
74+
{
75+
filename: 'test.vue',
76+
code: `
77+
<script>
78+
import {watch, watchEffect} from 'vue'
79+
export default {
80+
async setup() {
81+
await doSomething()
82+
const a = watchEffect(() => { /* ... */ })
83+
const b = watch(foo, () => { /* ... */ })
84+
c = watch()
85+
d(watch())
86+
e = {
87+
foo: watch()
88+
}
89+
f = [watch()]
90+
}
91+
}
92+
</script>
93+
`
7394
}
7495
],
7596
invalid: [
@@ -82,7 +103,7 @@ tester.run('no-watch-after-await', rule, {
82103
async setup() {
83104
await doSomething()
84105
85-
watch(() => { /* ... */ }) // error
106+
watch(foo, () => { /* ... */ }) // error
86107
}
87108
}
88109
</script>
@@ -93,7 +114,7 @@ tester.run('no-watch-after-await', rule, {
93114
line: 8,
94115
column: 11,
95116
endLine: 8,
96-
endColumn: 37
117+
endColumn: 42
97118
}
98119
]
99120
},
@@ -107,7 +128,7 @@ tester.run('no-watch-after-await', rule, {
107128
await doSomething()
108129
109130
watchEffect(() => { /* ... */ })
110-
watch(() => { /* ... */ })
131+
watch(foo, () => { /* ... */ })
111132
}
112133
}
113134
</script>
@@ -132,11 +153,11 @@ tester.run('no-watch-after-await', rule, {
132153
async setup() {
133154
await doSomething()
134155
135-
watch(() => { /* ... */ })
156+
watch(foo, () => { /* ... */ })
136157
137158
await doSomething()
138159
139-
watch(() => { /* ... */ })
160+
watch(foo, () => { /* ... */ })
140161
}
141162
}
142163
</script>

0 commit comments

Comments
 (0)