diff --git a/lib/rules/no-use-computed-property-like-method.js b/lib/rules/no-use-computed-property-like-method.js index 5d5bd8b4a..6e89ad263 100644 --- a/lib/rules/no-use-computed-property-like-method.js +++ b/lib/rules/no-use-computed-property-like-method.js @@ -68,26 +68,39 @@ function resolvedExpressions(context, node) { * @returns {Iterable} */ function* extractResolvedExpressions(node) { - if (node.type === 'Identifier') { - const variable = utils.findVariableByIdentifier(context, node) - if (variable) { - for (const ref of variable.references) { - const id = ref.identifier - if (id.parent.type === 'VariableDeclarator') { - if (id.parent.id === id && id.parent.init) { - yield* resolvedExpressionsInternal(id.parent.init) + switch (node.type) { + case 'Identifier': { + const variable = utils.findVariableByIdentifier(context, node) + if (variable) { + for (const ref of variable.references) { + const id = ref.identifier + if (id.parent.type === 'VariableDeclarator') { + if (id.parent.id === id && id.parent.init) { + yield* resolvedExpressionsInternal(id.parent.init) + } + } else if ( + id.parent.type === 'AssignmentExpression' && + id.parent.left === id + ) { + yield* resolvedExpressionsInternal(id.parent.right) } - } else if ( - id.parent.type === 'AssignmentExpression' && - id.parent.left === id - ) { - yield* resolvedExpressionsInternal(id.parent.right) } } + + break + } + case 'ConditionalExpression': { + yield* resolvedExpressionsInternal(node.consequent) + yield* resolvedExpressionsInternal(node.alternate) + + break + } + case 'LogicalExpression': { + yield* resolvedExpressionsInternal(node.left) + yield* resolvedExpressionsInternal(node.right) + + break } - } else if (node.type === 'ConditionalExpression') { - yield* resolvedExpressionsInternal(node.consequent) - yield* resolvedExpressionsInternal(node.alternate) } } } @@ -103,7 +116,7 @@ function resolvedExpressions(context, node) { * props: { * propA: String, // => String * propB: { - * type: Number // => String + * type: Number // => Number * }, * } */ @@ -189,7 +202,6 @@ function maybeFunction(context, node) { expr.type === 'Literal' || expr.type === 'TemplateLiteral' || expr.type === 'BinaryExpression' || - expr.type === 'LogicalExpression' || expr.type === 'UnaryExpression' || expr.type === 'UpdateExpression' ) { diff --git a/tests/lib/rules/no-use-computed-property-like-method.js b/tests/lib/rules/no-use-computed-property-like-method.js index 63ddc0dd2..4a5e25ebe 100644 --- a/tests/lib/rules/no-use-computed-property-like-method.js +++ b/tests/lib/rules/no-use-computed-property-like-method.js @@ -564,6 +564,78 @@ tester.run('no-use-computed-property-like-method', rule, { } ` + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + ` + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + ` } ], invalid: [ @@ -1096,6 +1168,42 @@ tester.run('no-use-computed-property-like-method', rule, { `, errors: ['Use x instead of x().'] + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + `, + errors: [ + 'Use this.computedReturnNotFunction1 instead of this.computedReturnNotFunction1().', + 'Use this.computedReturnNotFunction2 instead of this.computedReturnNotFunction2().' + ] } ] })