-
-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/no-watch-after-await
rule (#1068)
* New: Add `vue/no-watch-after-await` rule * Add check for watchEffect. * Update vue/no-watch-after-await
- Loading branch information
Showing
6 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-watch-after-await | ||
description: disallow asynchronously registered `watch` | ||
--- | ||
# vue/no-watch-after-await | ||
> disallow asynchronously registered `watch` | ||
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/vue3-strongly-recommended"` and `"plugin:vue/vue3-recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports the `watch()` after `await` expression. | ||
In `setup()` function, `watch()` should be registered synchronously. | ||
|
||
<eslint-code-block :rules="{'vue/no-watch-after-await': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
import { watch } from 'vue' | ||
export default { | ||
async setup() { | ||
/* ✓ GOOD */ | ||
watch(watchSource, () => { /* ... */ }) | ||
await doSomething() | ||
/* ✗ BAD */ | ||
watch(watchSource, () => { /* ... */ }) | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
This rule is not reported when using the stop handle. | ||
|
||
<eslint-code-block :rules="{'vue/no-watch-after-await': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
import { watch } from 'vue' | ||
export default { | ||
async setup() { | ||
await doSomething() | ||
/* ✓ GOOD */ | ||
const stopHandle = watch(watchSource, () => { /* ... */ }) | ||
// later | ||
stopHandle() | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further reading | ||
|
||
- [Vue RFCs - 0013-composition-api](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0013-composition-api.md) | ||
- [Vue Composition API - API Reference - Stopping the Watcher](https://composition-api.vuejs.org/api.html#stopping-the-watcher) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-watch-after-await.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-watch-after-await.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
const { ReferenceTracker } = require('eslint-utils') | ||
const utils = require('../utils') | ||
|
||
function isMaybeUsedStopHandle (node) { | ||
const parent = node.parent | ||
if (parent) { | ||
if (parent.type === 'VariableDeclarator') { | ||
// var foo = watch() | ||
return true | ||
} | ||
if (parent.type === 'AssignmentExpression') { | ||
// foo = watch() | ||
return true | ||
} | ||
if (parent.type === 'CallExpression') { | ||
// foo(watch()) | ||
return true | ||
} | ||
if (parent.type === 'Property') { | ||
// {foo: watch()} | ||
return true | ||
} | ||
if (parent.type === 'ArrayExpression') { | ||
// [watch()] | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow asynchronously registered `watch`', | ||
categories: ['vue3-essential'], | ||
url: 'https://eslint.vuejs.org/rules/no-watch-after-await.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
forbidden: 'The `watch` after `await` expression are forbidden.' | ||
} | ||
}, | ||
create (context) { | ||
const watchCallNodes = new Set() | ||
const setupFunctions = new Map() | ||
const forbiddenNodes = new Map() | ||
|
||
function addForbiddenNode (property, node) { | ||
let list = forbiddenNodes.get(property) | ||
if (!list) { | ||
list = [] | ||
forbiddenNodes.set(property, list) | ||
} | ||
list.push(node) | ||
} | ||
|
||
let scopeStack = { upper: null, functionNode: null } | ||
|
||
return Object.assign( | ||
{ | ||
'Program' () { | ||
const tracker = new ReferenceTracker(context.getScope()) | ||
const traceMap = { | ||
vue: { | ||
[ReferenceTracker.ESM]: true, | ||
watch: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
watchEffect: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
} | ||
} | ||
|
||
for (const { node } of tracker.iterateEsmReferences(traceMap)) { | ||
watchCallNodes.add(node) | ||
} | ||
}, | ||
'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) { | ||
if (utils.getStaticPropertyName(node) !== 'setup') { | ||
return | ||
} | ||
|
||
setupFunctions.set(node.value, { | ||
setupProperty: node, | ||
afterAwait: false | ||
}) | ||
}, | ||
':function' (node) { | ||
scopeStack = { upper: scopeStack, functionNode: node } | ||
}, | ||
'AwaitExpression' () { | ||
const setupFunctionData = setupFunctions.get(scopeStack.functionNode) | ||
if (!setupFunctionData) { | ||
return | ||
} | ||
setupFunctionData.afterAwait = true | ||
}, | ||
'CallExpression' (node) { | ||
const setupFunctionData = setupFunctions.get(scopeStack.functionNode) | ||
if (!setupFunctionData || !setupFunctionData.afterAwait) { | ||
return | ||
} | ||
|
||
if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) { | ||
addForbiddenNode(setupFunctionData.setupProperty, node) | ||
} | ||
}, | ||
':function:exit' (node) { | ||
scopeStack = scopeStack.upper | ||
|
||
setupFunctions.delete(node) | ||
} | ||
}, | ||
utils.executeOnVue(context, obj => { | ||
const reportsList = obj.properties | ||
.map(item => forbiddenNodes.get(item)) | ||
.filter(reports => !!reports) | ||
for (const reports of reportsList) { | ||
for (const node of reports) { | ||
context.report({ | ||
node, | ||
messageId: 'forbidden' | ||
}) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.