Skip to content

Commit 2d4c49c

Browse files
authored
Update vue/no-async-in-computed-properties rule to support <script setup> (#1533)
* Update `vue/no-async-in-computed-properties` rule to support `<script setup>` * update
1 parent 11b2077 commit 2d4c49c

File tree

2 files changed

+290
-64
lines changed

2 files changed

+290
-64
lines changed

Diff for: lib/rules/no-async-in-computed-properties.js

+88-64
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const utils = require('../utils')
88

99
/**
1010
* @typedef {import('../utils').VueObjectData} VueObjectData
11+
* @typedef {import('../utils').VueVisitor} VueVisitor
1112
* @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
1213
*/
1314

@@ -97,11 +98,16 @@ module.exports = {
9798

9899
/**
99100
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node
100-
* @param {VueObjectData} data
101+
* @param {VueObjectData|undefined} [info]
101102
*/
102-
function onFunctionEnter(node, { node: vueNode }) {
103+
function onFunctionEnter(node, info) {
103104
if (node.async) {
104-
verify(node, node.body, 'async', computedPropertiesMap.get(vueNode))
105+
verify(
106+
node,
107+
node.body,
108+
'async',
109+
info ? computedPropertiesMap.get(info.node) : null
110+
)
105111
}
106112

107113
scopeStack = {
@@ -118,10 +124,10 @@ module.exports = {
118124
* @param {ESNode} node
119125
* @param {BlockStatement | Expression} targetBody
120126
* @param {keyof expressionTypes} type
121-
* @param {ComponentComputedProperty[]} computedProperties
127+
* @param {ComponentComputedProperty[]|undefined|null} computedProperties
122128
*/
123-
function verify(node, targetBody, type, computedProperties = []) {
124-
for (const cp of computedProperties) {
129+
function verify(node, targetBody, type, computedProperties) {
130+
for (const cp of computedProperties || []) {
125131
if (
126132
cp.value &&
127133
node.loc.start.line >= cp.value.loc.start.line &&
@@ -158,7 +164,74 @@ module.exports = {
158164
}
159165
}
160166
}
161-
return Object.assign(
167+
const nodeVisitor = {
168+
':function': onFunctionEnter,
169+
':function:exit': onFunctionExit,
170+
171+
/**
172+
* @param {NewExpression} node
173+
* @param {VueObjectData|undefined} [info]
174+
*/
175+
NewExpression(node, info) {
176+
if (!scopeStack) {
177+
return
178+
}
179+
if (
180+
node.callee.type === 'Identifier' &&
181+
node.callee.name === 'Promise'
182+
) {
183+
verify(
184+
node,
185+
scopeStack.body,
186+
'new',
187+
info ? computedPropertiesMap.get(info.node) : null
188+
)
189+
}
190+
},
191+
192+
/**
193+
* @param {CallExpression} node
194+
* @param {VueObjectData|undefined} [info]
195+
*/
196+
CallExpression(node, info) {
197+
if (!scopeStack) {
198+
return
199+
}
200+
if (isPromise(node)) {
201+
verify(
202+
node,
203+
scopeStack.body,
204+
'promise',
205+
info ? computedPropertiesMap.get(info.node) : null
206+
)
207+
} else if (isTimedFunction(node)) {
208+
verify(
209+
node,
210+
scopeStack.body,
211+
'timed',
212+
info ? computedPropertiesMap.get(info.node) : null
213+
)
214+
}
215+
},
216+
217+
/**
218+
* @param {AwaitExpression} node
219+
* @param {VueObjectData|undefined} [info]
220+
*/
221+
AwaitExpression(node, info) {
222+
if (!scopeStack) {
223+
return
224+
}
225+
verify(
226+
node,
227+
scopeStack.body,
228+
'await',
229+
info ? computedPropertiesMap.get(info.node) : null
230+
)
231+
}
232+
}
233+
234+
return utils.compositingVisitors(
162235
{
163236
Program() {
164237
const tracker = new ReferenceTracker(context.getScope())
@@ -181,63 +254,14 @@ module.exports = {
181254
}
182255
}
183256
},
184-
utils.defineVueVisitor(context, {
185-
onVueObjectEnter(node) {
186-
computedPropertiesMap.set(node, utils.getComputedProperties(node))
187-
},
188-
':function': onFunctionEnter,
189-
':function:exit': onFunctionExit,
190-
191-
NewExpression(node, { node: vueNode }) {
192-
if (!scopeStack) {
193-
return
194-
}
195-
if (
196-
node.callee.type === 'Identifier' &&
197-
node.callee.name === 'Promise'
198-
) {
199-
verify(
200-
node,
201-
scopeStack.body,
202-
'new',
203-
computedPropertiesMap.get(vueNode)
204-
)
205-
}
206-
},
207-
208-
CallExpression(node, { node: vueNode }) {
209-
if (!scopeStack) {
210-
return
211-
}
212-
if (isPromise(node)) {
213-
verify(
214-
node,
215-
scopeStack.body,
216-
'promise',
217-
computedPropertiesMap.get(vueNode)
218-
)
219-
} else if (isTimedFunction(node)) {
220-
verify(
221-
node,
222-
scopeStack.body,
223-
'timed',
224-
computedPropertiesMap.get(vueNode)
225-
)
226-
}
227-
},
228-
229-
AwaitExpression(node, { node: vueNode }) {
230-
if (!scopeStack) {
231-
return
232-
}
233-
verify(
234-
node,
235-
scopeStack.body,
236-
'await',
237-
computedPropertiesMap.get(vueNode)
238-
)
239-
}
240-
})
257+
utils.isScriptSetup(context)
258+
? utils.defineScriptSetupVisitor(context, nodeVisitor)
259+
: utils.defineVueVisitor(context, {
260+
onVueObjectEnter(node) {
261+
computedPropertiesMap.set(node, utils.getComputedProperties(node))
262+
},
263+
...nodeVisitor
264+
})
241265
)
242266
}
243267
}

0 commit comments

Comments
 (0)