Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/no-expose-after-await
rule (#1712)
- Loading branch information
Showing
5 changed files
with
407 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,51 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-expose-after-await | ||
description: disallow asynchronously registered `expose` | ||
--- | ||
# vue/no-expose-after-await | ||
|
||
> disallow asynchronously registered `expose` | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports the `expose()` after `await` expression. | ||
In `setup()` function, `expose()` should be registered synchronously. | ||
|
||
<eslint-code-block :rules="{'vue/no-expose-after-await': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
import { watch } from 'vue' | ||
export default { | ||
async setup(props, { expose }) { | ||
/* ✓ GOOD */ | ||
expose({/* ... */}) | ||
await doSomething() | ||
/* ✗ BAD */ | ||
expose({/* ... */}) | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [Vue RFCs - 0042-expose-api](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0042-expose-api.md) | ||
- [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-expose-after-await.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-expose-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,202 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const { findVariable } = require('eslint-utils') | ||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Get the callee member node from the given CallExpression | ||
* @param {CallExpression} node CallExpression | ||
*/ | ||
function getCalleeMemberNode(node) { | ||
const callee = utils.skipChainExpression(node.callee) | ||
|
||
if (callee.type === 'MemberExpression') { | ||
const name = utils.getStaticPropertyName(callee) | ||
if (name) { | ||
return { name, member: callee } | ||
} | ||
} | ||
return null | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow asynchronously registered `expose`', | ||
categories: undefined, | ||
// categories: ['vue3-essential'], TODO Change with the major version | ||
url: 'https://eslint.vuejs.org/rules/no-expose-after-await.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
forbidden: 'The `expose` after `await` expression are forbidden.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** | ||
* @typedef {object} SetupScopeData | ||
* @property {boolean} afterAwait | ||
* @property {[number,number]} range | ||
* @property {Set<Identifier>} exposeReferenceIds | ||
* @property {Set<Identifier>} contextReferenceIds | ||
*/ | ||
/** | ||
* @typedef {object} ScopeStack | ||
* @property {ScopeStack | null} upper | ||
* @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} scopeNode | ||
*/ | ||
/** @type {Map<FunctionDeclaration | FunctionExpression | ArrowFunctionExpression, SetupScopeData>} */ | ||
const setupScopes = new Map() | ||
|
||
/** @type {ScopeStack | null} */ | ||
let scopeStack = null | ||
|
||
return utils.defineVueVisitor(context, { | ||
onSetupFunctionEnter(node) { | ||
const contextParam = node.params[1] | ||
if (!contextParam) { | ||
// no arguments | ||
return | ||
} | ||
if (contextParam.type === 'RestElement') { | ||
// cannot check | ||
return | ||
} | ||
if (contextParam.type === 'ArrayPattern') { | ||
// cannot check | ||
return | ||
} | ||
/** @type {Set<Identifier>} */ | ||
const contextReferenceIds = new Set() | ||
/** @type {Set<Identifier>} */ | ||
const exposeReferenceIds = new Set() | ||
if (contextParam.type === 'ObjectPattern') { | ||
const exposeProperty = utils.findAssignmentProperty( | ||
contextParam, | ||
'expose' | ||
) | ||
if (!exposeProperty) { | ||
return | ||
} | ||
const exposeParam = exposeProperty.value | ||
// `setup(props, {emit})` | ||
const variable = | ||
exposeParam.type === 'Identifier' | ||
? findVariable(context.getScope(), exposeParam) | ||
: null | ||
if (!variable) { | ||
return | ||
} | ||
for (const reference of variable.references) { | ||
if (!reference.isRead()) { | ||
continue | ||
} | ||
exposeReferenceIds.add(reference.identifier) | ||
} | ||
} else if (contextParam.type === 'Identifier') { | ||
// `setup(props, context)` | ||
const variable = findVariable(context.getScope(), contextParam) | ||
if (!variable) { | ||
return | ||
} | ||
for (const reference of variable.references) { | ||
if (!reference.isRead()) { | ||
continue | ||
} | ||
contextReferenceIds.add(reference.identifier) | ||
} | ||
} | ||
setupScopes.set(node, { | ||
afterAwait: false, | ||
range: node.range, | ||
exposeReferenceIds, | ||
contextReferenceIds | ||
}) | ||
}, | ||
/** | ||
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node | ||
*/ | ||
':function'(node) { | ||
scopeStack = { | ||
upper: scopeStack, | ||
scopeNode: node | ||
} | ||
}, | ||
':function:exit'() { | ||
scopeStack = scopeStack && scopeStack.upper | ||
}, | ||
/** @param {AwaitExpression} node */ | ||
AwaitExpression(node) { | ||
if (!scopeStack) { | ||
return | ||
} | ||
const setupScope = setupScopes.get(scopeStack.scopeNode) | ||
if (!setupScope || !utils.inRange(setupScope.range, node)) { | ||
return | ||
} | ||
setupScope.afterAwait = true | ||
}, | ||
/** @param {CallExpression} node */ | ||
CallExpression(node) { | ||
if (!scopeStack) { | ||
return | ||
} | ||
const setupScope = setupScopes.get(scopeStack.scopeNode) | ||
if ( | ||
!setupScope || | ||
!setupScope.afterAwait || | ||
!utils.inRange(setupScope.range, node) | ||
) { | ||
return | ||
} | ||
const { contextReferenceIds, exposeReferenceIds } = setupScope | ||
if ( | ||
node.callee.type === 'Identifier' && | ||
exposeReferenceIds.has(node.callee) | ||
) { | ||
// setup(props,{expose}) {expose()} | ||
context.report({ | ||
node, | ||
messageId: 'forbidden' | ||
}) | ||
} else { | ||
const expose = getCalleeMemberNode(node) | ||
if ( | ||
expose && | ||
expose.name === 'expose' && | ||
expose.member.object.type === 'Identifier' && | ||
contextReferenceIds.has(expose.member.object) | ||
) { | ||
// setup(props,context) {context.emit()} | ||
context.report({ | ||
node, | ||
messageId: 'forbidden' | ||
}) | ||
} | ||
} | ||
}, | ||
onSetupFunctionExit(node) { | ||
setupScopes.delete(node) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.