diff --git a/lib/rules/no-async-in-computed-properties.js b/lib/rules/no-async-in-computed-properties.js index 6d853f52a..b12fa3407 100644 --- a/lib/rules/no-async-in-computed-properties.js +++ b/lib/rules/no-async-in-computed-properties.js @@ -11,16 +11,16 @@ 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 @@ -28,16 +28,12 @@ const TIMED_FUNCTIONS = [ 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 ) } @@ -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 @@ -79,7 +76,7 @@ module.exports = { create(context) { /** @type {Map} */ const computedPropertiesMap = new Map() - /** @type {Array} */ + /** @type {(FunctionExpression | ArrowFunctionExpression)[]} */ const computedFunctionNodes = [] /** @@ -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 && @@ -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, @@ -156,8 +154,9 @@ module.exports = { expressionName: expressionTypes[type] } }) + return } - }) + } } return Object.assign( { diff --git a/lib/utils/index.js b/lib/utils/index.js index 6d38bb97a..94d9994d2 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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 } @@ -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 }