From cf2fcc9b823f490e6b1e18df4ae1c520df202e31 Mon Sep 17 00:00:00 2001 From: "David R. Myers" Date: Thu, 27 Jul 2023 16:02:21 -0400 Subject: [PATCH] Report on standalone function calls too --- lib/rules/no-setup-props-destructure.js | 67 ++++++++++++------- tests/lib/rules/no-setup-props-destructure.js | 17 +++++ 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/rules/no-setup-props-destructure.js b/lib/rules/no-setup-props-destructure.js index 1170267e7..653a0b449 100644 --- a/lib/rules/no-setup-props-destructure.js +++ b/lib/rules/no-setup-props-destructure.js @@ -35,9 +35,8 @@ module.exports = { */ /** @type {Map} */ const setupScopePropsReferenceIds = new Map() - const outerExpressionTypes = new Set([ + const wrapperExpressionTypes = new Set([ 'ArrayExpression', - 'CallExpression', 'ObjectExpression' ]) @@ -68,27 +67,11 @@ module.exports = { const rightNode = utils.skipChainExpression(right) - if (outerExpressionTypes.has(rightNode.type)) { - const propRefs = [...propsReferences.refs.values()] - const isPropsMemberAccessed = propRefs.some((props) => { - const isPropsInCallExpression = utils.inRange(rightNode.range, props) - - if (isPropsInCallExpression) { - const isPropMemberExpression = - props.parent.type === 'MemberExpression' && - props.parent.object === props - - if (isPropMemberExpression) { - return true - } - } - - return false - }) - - if (isPropsMemberAccessed) { - return report(left, 'getProperty', propsReferences.scopeName) - } + if ( + wrapperExpressionTypes.has(rightNode.type) && + isPropsMemberAccessed(rightNode, propsReferences) + ) { + return report(rightNode, 'getProperty', propsReferences.scopeName) } if ( @@ -108,6 +91,24 @@ module.exports = { report(left, 'getProperty', propsReferences.scopeName) } } + + /** + * @param {Expression} node + * @param {ScopePropsReferences} propsReferences + */ + function isPropsMemberAccessed(node, propsReferences) { + const propRefs = [...propsReferences.refs.values()] + + return propRefs.some((props) => { + const isPropsInExpressionRange = utils.inRange(node.range, props) + const isPropsMemberExpression = + props.parent.type === 'MemberExpression' && + props.parent.object === props + + return isPropsInExpressionRange && isPropsMemberExpression + }) + } + /** * @typedef {object} ScopeStack * @property {ScopeStack | null} upper @@ -182,6 +183,26 @@ module.exports = { setupScopePropsReferenceIds.delete(node) }, + /** + * @param {CallExpression} node + */ + CallExpression(node) { + if (!scopeStack) { + return + } + + const propsReferenceIds = setupScopePropsReferenceIds.get( + scopeStack.scopeNode + ) + + if (!propsReferenceIds) { + return + } + + if (isPropsMemberAccessed(node, propsReferenceIds)) { + report(node, 'getProperty', propsReferenceIds.scopeName) + } + }, /** * @param {VariableDeclarator} node */ diff --git a/tests/lib/rules/no-setup-props-destructure.js b/tests/lib/rules/no-setup-props-destructure.js index de2048350..5d9a704d1 100644 --- a/tests/lib/rules/no-setup-props-destructure.js +++ b/tests/lib/rules/no-setup-props-destructure.js @@ -660,6 +660,23 @@ tester.run('no-setup-props-destructure', rule, { line: 4 } ] + }, + { + filename: 'test.vue', + code: ` + + `, + errors: [ + { + messageId: 'getProperty', + line: 6 + } + ] } ] })