Skip to content

Commit

Permalink
Refactor no-async-in-computed-properties (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 3, 2021
1 parent 6b62278 commit 3fb52a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
53 changes: 26 additions & 27 deletions lib/rules/no-async-in-computed-properties.js
Expand Up @@ -11,33 +11,29 @@ const utils = require('../utils')
* @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
*/

const PROMISE_FUNCTIONS = ['then', 'catch', 'finally']
const PROMISE_FUNCTIONS = new Set(['then', 'catch', 'finally'])

const PROMISE_METHODS = ['all', 'race', 'reject', 'resolve']
const PROMISE_METHODS = new Set(['all', 'race', 'reject', 'resolve'])

const TIMED_FUNCTIONS = [
const TIMED_FUNCTIONS = new Set([
'setTimeout',
'setInterval',
'setImmediate',
'requestAnimationFrame'
]
])

/**
* @param {CallExpression} node
*/
function isTimedFunction(node) {
const callee = utils.skipChainExpression(node.callee)
return (
((node.type === 'CallExpression' &&
callee.type === 'Identifier' &&
TIMED_FUNCTIONS.indexOf(callee.name) !== -1) ||
(node.type === 'CallExpression' &&
callee.type === 'MemberExpression' &&
((callee.type === 'Identifier' && TIMED_FUNCTIONS.has(callee.name)) ||
(callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'window' &&
callee.property.type === 'Identifier' &&
TIMED_FUNCTIONS.indexOf(callee.property.name) !== -1)) &&
node.arguments.length
TIMED_FUNCTIONS.has(utils.getStaticPropertyName(callee) || ''))) &&
node.arguments.length > 0
)
}

Expand All @@ -46,15 +42,16 @@ function isTimedFunction(node) {
*/
function isPromise(node) {
const callee = utils.skipChainExpression(node.callee)
if (node.type === 'CallExpression' && callee.type === 'MemberExpression') {
if (callee.type === 'MemberExpression') {
const name = utils.getStaticPropertyName(callee)
return (
name &&
// hello.PROMISE_FUNCTION()
(callee.property.type === 'Identifier' &&
PROMISE_FUNCTIONS.indexOf(callee.property.name) !== -1) || // Promise.PROMISE_METHOD()
(callee.object.type === 'Identifier' &&
callee.object.name === 'Promise' &&
callee.property.type === 'Identifier' &&
PROMISE_METHODS.indexOf(callee.property.name) !== -1)
(PROMISE_FUNCTIONS.has(name) ||
// Promise.PROMISE_METHOD()
(callee.object.type === 'Identifier' &&
callee.object.name === 'Promise' &&
PROMISE_METHODS.has(name)))
)
}
return false
Expand All @@ -79,7 +76,7 @@ module.exports = {
create(context) {
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */
const computedPropertiesMap = new Map()
/** @type {Array<FunctionExpression | ArrowFunctionExpression>} */
/** @type {(FunctionExpression | ArrowFunctionExpression)[]} */
const computedFunctionNodes = []

/**
Expand Down Expand Up @@ -124,7 +121,7 @@ module.exports = {
* @param {ComponentComputedProperty[]} computedProperties
*/
function verify(node, targetBody, type, computedProperties = []) {
computedProperties.forEach((cp) => {
for (const cp of computedProperties) {
if (
cp.value &&
node.loc.start.line >= cp.value.loc.start.line &&
Expand All @@ -140,14 +137,15 @@ module.exports = {
propertyName: cp.key || 'unknown'
}
})
return
}
})
}

computedFunctionNodes.forEach((c) => {
for (const cf of computedFunctionNodes) {
if (
node.loc.start.line >= c.loc.start.line &&
node.loc.end.line <= c.loc.end.line &&
targetBody === c.body
node.loc.start.line >= cf.body.loc.start.line &&
node.loc.end.line <= cf.body.loc.end.line &&
targetBody === cf.body
) {
context.report({
node,
Expand All @@ -156,8 +154,9 @@ module.exports = {
expressionName: expressionTypes[type]
}
})
return
}
})
}
}
return Object.assign(
{
Expand Down
33 changes: 11 additions & 22 deletions lib/utils/index.js
Expand Up @@ -842,17 +842,11 @@ module.exports = {
if (propValue.type === 'FunctionExpression') {
value = propValue.body
} else if (propValue.type === 'ObjectExpression') {
const get = propValue.properties.find(
/**
* @param {ESNode} p
* @returns { p is (Property & { value: FunctionExpression }) }
*/
(p) =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'get' &&
p.value.type === 'FunctionExpression'
)
const get = /** @type {(Property & { value: FunctionExpression }) | null} */ (findProperty(
propValue,
'get',
(p) => p.value.type === 'FunctionExpression'
))
value = get ? get.value.body : null
}

Expand Down Expand Up @@ -880,18 +874,13 @@ module.exports = {
}

if (arg.type === 'ObjectExpression') {
const getProperty = arg.properties.find(
/**
* @param {ESNode} p
* @returns { p is (Property & { value: FunctionExpression | ArrowFunctionExpression }) }
*/
const getProperty = /** @type {(Property & { value: FunctionExpression | ArrowFunctionExpression }) | null} */ (findProperty(
arg,
'get',
(p) =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'get' &&
(p.value.type === 'FunctionExpression' ||
p.value.type === 'ArrowFunctionExpression')
)
p.value.type === 'FunctionExpression' ||
p.value.type === 'ArrowFunctionExpression'
))
return getProperty ? getProperty.value : null
}

Expand Down

0 comments on commit 3fb52a9

Please sign in to comment.