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-lifecycle-after-await
rule (#1067)
- Loading branch information
Showing
5 changed files
with
340 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,47 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-lifecycle-after-await | ||
description: disallow asynchronously registered lifecycle hooks | ||
--- | ||
# vue/no-lifecycle-after-await | ||
> disallow asynchronously registered lifecycle hooks | ||
## :book: Rule Details | ||
|
||
This rule reports the lifecycle hooks after `await` expression. | ||
In `setup()` function, `onXXX` lifecycle hooks should be registered synchronously. | ||
|
||
<eslint-code-block :rules="{'vue/no-lifecycle-after-await': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
import { onMounted } from 'vue' | ||
export default { | ||
async setup() { | ||
/* ✓ GOOD */ | ||
onMounted(() => { /* ... */ }) | ||
await doSomething() | ||
/* ✗ BAD */ | ||
onMounted(() => { /* ... */ }) | ||
} | ||
} | ||
</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) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-lifecycle-after-await.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-lifecycle-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
const { ReferenceTracker } = require('eslint-utils') | ||
const utils = require('../utils') | ||
|
||
const LIFECYCLE_HOOKS = ['onBeforeMount', 'onBeforeUnmount', 'onBeforeUpdate', 'onErrorCaptured', 'onMounted', 'onRenderTracked', 'onRenderTriggered', 'onUnmounted', 'onUpdated', 'onActivated', 'onDeactivated'] | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow asynchronously registered lifecycle hooks', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-lifecycle-after-await.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
forbidden: 'The lifecycle hooks after `await` expression are forbidden.' | ||
} | ||
}, | ||
create (context) { | ||
const lifecycleHookCallNodes = 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 | ||
} | ||
} | ||
for (const lifecycleHook of LIFECYCLE_HOOKS) { | ||
traceMap.vue[lifecycleHook] = { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
} | ||
|
||
for (const { node } of tracker.iterateEsmReferences(traceMap)) { | ||
lifecycleHookCallNodes.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 (lifecycleHookCallNodes.has(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' | ||
}) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
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,180 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-lifecycle-after-await') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2019, sourceType: 'module' } | ||
}) | ||
|
||
tester.run('no-lifecycle-after-await', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onMounted} from 'vue' | ||
export default { | ||
async setup() { | ||
onMounted(() => { /* ... */ }) // ok | ||
await doSomething() | ||
} | ||
} | ||
</script> | ||
` | ||
}, { | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onMounted} from 'vue' | ||
export default { | ||
async setup() { | ||
onMounted(() => { /* ... */ }) | ||
} | ||
} | ||
</script> | ||
` | ||
}, { | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, onActivated, onDeactivated} from 'vue' | ||
export default { | ||
async setup() { | ||
onBeforeMount(() => { /* ... */ }) | ||
onBeforeUnmount(() => { /* ... */ }) | ||
onBeforeUpdate(() => { /* ... */ }) | ||
onErrorCaptured(() => { /* ... */ }) | ||
onMounted(() => { /* ... */ }) | ||
onRenderTracked(() => { /* ... */ }) | ||
onRenderTriggered(() => { /* ... */ }) | ||
onUnmounted(() => { /* ... */ }) | ||
onUpdated(() => { /* ... */ }) | ||
onActivated(() => { /* ... */ }) | ||
onDeactivated(() => { /* ... */ }) | ||
await doSomething() | ||
} | ||
} | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onMounted} from 'vue' | ||
export default { | ||
async _setup() { | ||
await doSomething() | ||
onMounted(() => { /* ... */ }) // error | ||
} | ||
} | ||
</script> | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onMounted} from 'vue' | ||
export default { | ||
async setup() { | ||
await doSomething() | ||
onMounted(() => { /* ... */ }) // error | ||
} | ||
} | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
message: 'The lifecycle hooks after `await` expression are forbidden.', | ||
line: 8, | ||
column: 11, | ||
endLine: 8, | ||
endColumn: 41 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, onActivated, onDeactivated} from 'vue' | ||
export default { | ||
async setup() { | ||
await doSomething() | ||
onBeforeMount(() => { /* ... */ }) | ||
onBeforeUnmount(() => { /* ... */ }) | ||
onBeforeUpdate(() => { /* ... */ }) | ||
onErrorCaptured(() => { /* ... */ }) | ||
onMounted(() => { /* ... */ }) | ||
onRenderTracked(() => { /* ... */ }) | ||
onRenderTriggered(() => { /* ... */ }) | ||
onUnmounted(() => { /* ... */ }) | ||
onUpdated(() => { /* ... */ }) | ||
onActivated(() => { /* ... */ }) | ||
onDeactivated(() => { /* ... */ }) | ||
} | ||
} | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'forbidden', | ||
line: 8 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 9 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 10 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 11 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 12 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 13 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 14 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 15 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 16 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 17 | ||
}, | ||
{ | ||
messageId: 'forbidden', | ||
line: 18 | ||
} | ||
] | ||
} | ||
] | ||
}) |