Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
})
)
}
}