Skip to content

Commit

Permalink
Update vue/no-async-in-computed-properties rule to support `<script…
Browse files Browse the repository at this point in the history
… setup>` (#1533)

* Update `vue/no-async-in-computed-properties` rule to support `<script setup>`

* update
  • Loading branch information
ota-meshi committed Jul 2, 2021
1 parent 11b2077 commit 2d4c49c
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 64 deletions.
152 changes: 88 additions & 64 deletions lib/rules/no-async-in-computed-properties.js
Expand Up @@ -8,6 +8,7 @@ const utils = require('../utils')

/**
* @typedef {import('../utils').VueObjectData} VueObjectData
* @typedef {import('../utils').VueVisitor} VueVisitor
* @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
*/

Expand Down Expand Up @@ -97,11 +98,16 @@ module.exports = {

/**
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node
* @param {VueObjectData} data
* @param {VueObjectData|undefined} [info]
*/
function onFunctionEnter(node, { node: vueNode }) {
function onFunctionEnter(node, info) {
if (node.async) {
verify(node, node.body, 'async', computedPropertiesMap.get(vueNode))
verify(
node,
node.body,
'async',
info ? computedPropertiesMap.get(info.node) : null
)
}

scopeStack = {
Expand All @@ -118,10 +124,10 @@ module.exports = {
* @param {ESNode} node
* @param {BlockStatement | Expression} targetBody
* @param {keyof expressionTypes} type
* @param {ComponentComputedProperty[]} computedProperties
* @param {ComponentComputedProperty[]|undefined|null} computedProperties
*/
function verify(node, targetBody, type, computedProperties = []) {
for (const cp of computedProperties) {
function verify(node, targetBody, type, computedProperties) {
for (const cp of computedProperties || []) {
if (
cp.value &&
node.loc.start.line >= cp.value.loc.start.line &&
Expand Down Expand Up @@ -158,7 +164,74 @@ module.exports = {
}
}
}
return Object.assign(
const nodeVisitor = {
':function': onFunctionEnter,
':function:exit': onFunctionExit,

/**
* @param {NewExpression} node
* @param {VueObjectData|undefined} [info]
*/
NewExpression(node, info) {
if (!scopeStack) {
return
}
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'Promise'
) {
verify(
node,
scopeStack.body,
'new',
info ? computedPropertiesMap.get(info.node) : null
)
}
},

/**
* @param {CallExpression} node
* @param {VueObjectData|undefined} [info]
*/
CallExpression(node, info) {
if (!scopeStack) {
return
}
if (isPromise(node)) {
verify(
node,
scopeStack.body,
'promise',
info ? computedPropertiesMap.get(info.node) : null
)
} else if (isTimedFunction(node)) {
verify(
node,
scopeStack.body,
'timed',
info ? computedPropertiesMap.get(info.node) : null
)
}
},

/**
* @param {AwaitExpression} node
* @param {VueObjectData|undefined} [info]
*/
AwaitExpression(node, info) {
if (!scopeStack) {
return
}
verify(
node,
scopeStack.body,
'await',
info ? computedPropertiesMap.get(info.node) : null
)
}
}

return utils.compositingVisitors(
{
Program() {
const tracker = new ReferenceTracker(context.getScope())
Expand All @@ -181,63 +254,14 @@ module.exports = {
}
}
},
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
computedPropertiesMap.set(node, utils.getComputedProperties(node))
},
':function': onFunctionEnter,
':function:exit': onFunctionExit,

NewExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'Promise'
) {
verify(
node,
scopeStack.body,
'new',
computedPropertiesMap.get(vueNode)
)
}
},

CallExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
if (isPromise(node)) {
verify(
node,
scopeStack.body,
'promise',
computedPropertiesMap.get(vueNode)
)
} else if (isTimedFunction(node)) {
verify(
node,
scopeStack.body,
'timed',
computedPropertiesMap.get(vueNode)
)
}
},

AwaitExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
verify(
node,
scopeStack.body,
'await',
computedPropertiesMap.get(vueNode)
)
}
})
utils.isScriptSetup(context)
? utils.defineScriptSetupVisitor(context, nodeVisitor)
: utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
computedPropertiesMap.set(node, utils.getComputedProperties(node))
},
...nodeVisitor
})
)
}
}

0 comments on commit 2d4c49c

Please sign in to comment.