Skip to content

Commit 3fb52a9

Browse files
authored
Refactor no-async-in-computed-properties (#1405)
1 parent 6b62278 commit 3fb52a9

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

lib/rules/no-async-in-computed-properties.js

+26-27
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,29 @@ const utils = require('../utils')
1111
* @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
1212
*/
1313

14-
const PROMISE_FUNCTIONS = ['then', 'catch', 'finally']
14+
const PROMISE_FUNCTIONS = new Set(['then', 'catch', 'finally'])
1515

16-
const PROMISE_METHODS = ['all', 'race', 'reject', 'resolve']
16+
const PROMISE_METHODS = new Set(['all', 'race', 'reject', 'resolve'])
1717

18-
const TIMED_FUNCTIONS = [
18+
const TIMED_FUNCTIONS = new Set([
1919
'setTimeout',
2020
'setInterval',
2121
'setImmediate',
2222
'requestAnimationFrame'
23-
]
23+
])
2424

2525
/**
2626
* @param {CallExpression} node
2727
*/
2828
function isTimedFunction(node) {
2929
const callee = utils.skipChainExpression(node.callee)
3030
return (
31-
((node.type === 'CallExpression' &&
32-
callee.type === 'Identifier' &&
33-
TIMED_FUNCTIONS.indexOf(callee.name) !== -1) ||
34-
(node.type === 'CallExpression' &&
35-
callee.type === 'MemberExpression' &&
31+
((callee.type === 'Identifier' && TIMED_FUNCTIONS.has(callee.name)) ||
32+
(callee.type === 'MemberExpression' &&
3633
callee.object.type === 'Identifier' &&
3734
callee.object.name === 'window' &&
38-
callee.property.type === 'Identifier' &&
39-
TIMED_FUNCTIONS.indexOf(callee.property.name) !== -1)) &&
40-
node.arguments.length
35+
TIMED_FUNCTIONS.has(utils.getStaticPropertyName(callee) || ''))) &&
36+
node.arguments.length > 0
4137
)
4238
}
4339

@@ -46,15 +42,16 @@ function isTimedFunction(node) {
4642
*/
4743
function isPromise(node) {
4844
const callee = utils.skipChainExpression(node.callee)
49-
if (node.type === 'CallExpression' && callee.type === 'MemberExpression') {
45+
if (callee.type === 'MemberExpression') {
46+
const name = utils.getStaticPropertyName(callee)
5047
return (
48+
name &&
5149
// hello.PROMISE_FUNCTION()
52-
(callee.property.type === 'Identifier' &&
53-
PROMISE_FUNCTIONS.indexOf(callee.property.name) !== -1) || // Promise.PROMISE_METHOD()
54-
(callee.object.type === 'Identifier' &&
55-
callee.object.name === 'Promise' &&
56-
callee.property.type === 'Identifier' &&
57-
PROMISE_METHODS.indexOf(callee.property.name) !== -1)
50+
(PROMISE_FUNCTIONS.has(name) ||
51+
// Promise.PROMISE_METHOD()
52+
(callee.object.type === 'Identifier' &&
53+
callee.object.name === 'Promise' &&
54+
PROMISE_METHODS.has(name)))
5855
)
5956
}
6057
return false
@@ -79,7 +76,7 @@ module.exports = {
7976
create(context) {
8077
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */
8178
const computedPropertiesMap = new Map()
82-
/** @type {Array<FunctionExpression | ArrowFunctionExpression>} */
79+
/** @type {(FunctionExpression | ArrowFunctionExpression)[]} */
8380
const computedFunctionNodes = []
8481

8582
/**
@@ -124,7 +121,7 @@ module.exports = {
124121
* @param {ComponentComputedProperty[]} computedProperties
125122
*/
126123
function verify(node, targetBody, type, computedProperties = []) {
127-
computedProperties.forEach((cp) => {
124+
for (const cp of computedProperties) {
128125
if (
129126
cp.value &&
130127
node.loc.start.line >= cp.value.loc.start.line &&
@@ -140,14 +137,15 @@ module.exports = {
140137
propertyName: cp.key || 'unknown'
141138
}
142139
})
140+
return
143141
}
144-
})
142+
}
145143

146-
computedFunctionNodes.forEach((c) => {
144+
for (const cf of computedFunctionNodes) {
147145
if (
148-
node.loc.start.line >= c.loc.start.line &&
149-
node.loc.end.line <= c.loc.end.line &&
150-
targetBody === c.body
146+
node.loc.start.line >= cf.body.loc.start.line &&
147+
node.loc.end.line <= cf.body.loc.end.line &&
148+
targetBody === cf.body
151149
) {
152150
context.report({
153151
node,
@@ -156,8 +154,9 @@ module.exports = {
156154
expressionName: expressionTypes[type]
157155
}
158156
})
157+
return
159158
}
160-
})
159+
}
161160
}
162161
return Object.assign(
163162
{

lib/utils/index.js

+11-22
Original file line numberDiff line numberDiff line change
@@ -842,17 +842,11 @@ module.exports = {
842842
if (propValue.type === 'FunctionExpression') {
843843
value = propValue.body
844844
} else if (propValue.type === 'ObjectExpression') {
845-
const get = propValue.properties.find(
846-
/**
847-
* @param {ESNode} p
848-
* @returns { p is (Property & { value: FunctionExpression }) }
849-
*/
850-
(p) =>
851-
p.type === 'Property' &&
852-
p.key.type === 'Identifier' &&
853-
p.key.name === 'get' &&
854-
p.value.type === 'FunctionExpression'
855-
)
845+
const get = /** @type {(Property & { value: FunctionExpression }) | null} */ (findProperty(
846+
propValue,
847+
'get',
848+
(p) => p.value.type === 'FunctionExpression'
849+
))
856850
value = get ? get.value.body : null
857851
}
858852

@@ -880,18 +874,13 @@ module.exports = {
880874
}
881875

882876
if (arg.type === 'ObjectExpression') {
883-
const getProperty = arg.properties.find(
884-
/**
885-
* @param {ESNode} p
886-
* @returns { p is (Property & { value: FunctionExpression | ArrowFunctionExpression }) }
887-
*/
877+
const getProperty = /** @type {(Property & { value: FunctionExpression | ArrowFunctionExpression }) | null} */ (findProperty(
878+
arg,
879+
'get',
888880
(p) =>
889-
p.type === 'Property' &&
890-
p.key.type === 'Identifier' &&
891-
p.key.name === 'get' &&
892-
(p.value.type === 'FunctionExpression' ||
893-
p.value.type === 'ArrowFunctionExpression')
894-
)
881+
p.value.type === 'FunctionExpression' ||
882+
p.value.type === 'ArrowFunctionExpression'
883+
))
895884
return getProperty ? getProperty.value : null
896885
}
897886

0 commit comments

Comments
 (0)